Files
deploy/util/helm/client_test.go
41536738c8 refactor(cd): hard-fork - rename CRD API group argoproj.io -> deploy.hanzo.ai (#1)
* feat(api): hard-fork CRD API group argoproj.io -> deploy.hanzo.ai

Hanzo CD hard-fork, phase 1.1. Single source of truth is
pkg/apis/application/register.go's Group constant (Application/AppProject/
ApplicationSet all derive their FullName from it); every other Go call-site
that hardcoded the literal group string is updated to match: the 3 generated
informer GVRs, the app-of-apps nested-Application detector in types.go, the
resource-tree Application-kind cache populator, the 3 NotFound error
constructors in server/application, and the applicationset-controller's
leader-election Lease name/ID (kept in lockstep across Go source + RBAC
resourceNames, or leader election would get RBAC-denied on startup).

CRD YAMLs (application/appproject/applicationset) and every RBAC apiGroups
rule that actually governs our 3 CRDs are repointed to deploy.hanzo.ai, across
both manifests/base/* (namespaced Roles) and manifests/cluster-rbac/* (cluster
scope). Two adjacent things that share the "argoproj.io" string but are NOT
ours are deliberately left untouched: gitops-engine's health-check dispatch
for Argo Workflows' `Workflow` kind, and argocd-server-clusterrole's second
apiGroups block granting read on Argo Workflows' `workflows` resource — both
belong to a separate, unrelated Argoproj-family project we are not forking.

Also untouched, per explicit scope: annotation/label keys (argocd.argoproj.io/*)
and finalizers (*.argoproj.io) that track already-applied live resources, and
argocd-cm.yaml's resource.customizations.ignoreResourceUpdates keys (a narrow
app-of-apps diffing nicety, not build- or RBAC-relevant).

CGO_ENABLED=0 go build ./... verified green.

* ci: wire canonical hanzo.yml + cicd.yml (Hanzo CD image build)

Phase 1.3. Follows the same one-way pattern every hanzo/lux/zoo repo uses:
hanzo.yml declares the image (root Dockerfile -> ghcr.io/hanzoai/deploy) and a
fast go-build sanity test (the multi-stage Dockerfile build is the slow, real
gate); cicd.yml is the ~10-line trigger shim importing hanzoai/ci's reusable
build workflow on the arc pool, secrets from KMS only.

No `deploy:` block yet, matching the hanzoai/cms build-only-first-cutover
pattern: this repo's own live hanzo-cd namespace runs the whole Hanzo fleet's
GitOps, so the prod image/CRD swap is a deliberate separate action (see the
[HELD] universe PR), never an automatic rollout on merge. Every PR/push still
builds and pushes an immutable ghcr.io/hanzoai/deploy:sha-<7>-amd64 image.

* feat(ui): rebrand Argo CD -> Hanzo CD (title, favicon, logo, sidebar)

Phase 1.2. User-facing "Argo CD"/"ArgoCD" text replaced with "Hanzo CD"
across the login page, help modal, settings screens, version-info panel,
sync-options warning, fullscreen-logs title, app.tsx page titles, and
index.html's <title>/noscript text (dropped the now-wrong upstream CLI
doc link rather than invent a hanzo.ai URL that doesn't exist).

Logo/favicon assets replaced with the canonical Hanzo mark (never
hand-drawn, copied from ~/work/hanzo/logo/dist): the two real usages of
Argo's logo collapse onto one Hanzo glyph (mono + white variants) at the
same 5 call sites (login icon + footer, login.scss $logoPath/
$logoNegativePath, sidebar text-logo), preserving the existing CSS
invert() filters so both the light login panel and dark sidebar still
render correctly. Old argologo.svg/argo_o.svg/argo.png deleted (no
longer referenced). favicon-{16x16,32x32}.png added at
ui/src/assets/favicon/ (index.html's links to that path were previously
dead - the directory didn't exist in this checkout). package.json name
argo-cd-ui -> hanzo-cd-ui.

Left alone, deliberately: the two Kubernetes resource-kind icon sets
under ui/src/assets/images/resources/ (unrelated to branding), and
upstream doc/project links (argoproj.io, argo-cd.readthedocs.io) since
we have no equivalent hanzo.ai docs to point to yet - except the login
footer logo's outbound link, which now points to hanzo.ai instead of
argoproj.io (a Hanzo-branded mark linking to the upstream project it
was forked from read as an obvious leftover).

* feat(module): rename Go module github.com/argoproj/argo-cd/v3 -> github.com/hanzoai/deploy

Phase 2. No version suffix on the new path (org policy: never publish our
own Go modules above v1.x.x / with a v2+ suffix) — this fork starts a fresh
v0/v1 module identity rather than carrying the v3 the upstream fork point
had reached.

Mechanical, symmetric rewrite: 704 .go files (import statements only; two
disjoint substitutions applied specific-first so the nested gitops-engine
module path never double-matches), plus go.mod (module line, the
gitops-engine require+replace pair, version reset to v0.0.0-... since a
suffix-less path can't carry a v3 semver), gitops-engine/go.mod, Makefile
(ldflags PACKAGE + 2 TEST_MODULE filters), .golangci.yaml, .goreleaser.yaml,
.mockery.yaml (+ gitops-engine's), Procfile, the codegen/proto generation
scripts (hack/generate-proto.sh, update-codegen.sh, update-openapi.sh), and
20 .proto files' go_package options. Also one OpenTelemetry tracer name
string in gitops-engine/pkg/sync/sync_context.go (a cosmestic label, not an
import, but worth keeping consistent).

CGO_ENABLED=0 go build ./... verified green for both the root module and
the nested gitops-engine module; go vet clean on both; go mod tidy run
(network available) to reconcile go.sum, dependency set unchanged.

Left alone, deliberately: doc-only mentions (docs/snyk/*.html scan reports,
gitops-engine/README.md), a comment-only reference in
.github/workflows/release.yaml, and one Renovate bot config line — none are
build- or codegen-load-bearing. Also left alone: comments citing upstream
GitHub issue URLs (github.com/argoproj/argo-cd/issues/NNNNN) and a handful
of test fixtures using "https://github.com/argoproj/argo-cd.git" as a
representative example repo URL — both are accurate historical/test-data
references, not stale self-references.

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-29 18:27:58 -07:00

902 lines
27 KiB
Go

package helm
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/json"
"fmt"
"math"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
utilio "github.com/hanzoai/deploy/util/io"
"github.com/hanzoai/deploy/util/workloadidentity"
"github.com/hanzoai/deploy/util/workloadidentity/mocks"
)
type fakeIndexCache struct {
data []byte
}
type fakeTagsList struct {
Tags []string `json:"tags"`
}
func (f *fakeIndexCache) SetHelmIndex(_ string, indexData []byte) error {
f.data = indexData
return nil
}
func (f *fakeIndexCache) GetHelmIndex(_ string, indexData *[]byte) error {
*indexData = f.data
return nil
}
func TestIndex(t *testing.T) {
t.Run("Invalid", func(t *testing.T) {
client := NewClient("", HelmCreds{}, false, "", "")
_, err := client.GetIndex(t.Context(), false, 10000)
require.Error(t, err)
})
t.Run("Stable", func(t *testing.T) {
client := NewClient("https://argoproj.github.io/argo-helm", HelmCreds{}, false, "", "")
index, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
assert.NotNil(t, index)
})
t.Run("BasicAuth", func(t *testing.T) {
client := NewClient("https://argoproj.github.io/argo-helm", HelmCreds{
Username: "my-password",
Password: "my-username",
}, false, "", "")
index, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
assert.NotNil(t, index)
})
t.Run("Cached", func(t *testing.T) {
fakeIndex := Index{Entries: map[string]Entries{"fake": {}}}
data := bytes.Buffer{}
err := yaml.NewEncoder(&data).Encode(fakeIndex)
require.NoError(t, err)
client := NewClient("https://argoproj.github.io/argo-helm", HelmCreds{}, false, "", "", WithIndexCache(&fakeIndexCache{data: data.Bytes()}))
index, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
assert.Equal(t, fakeIndex, *index)
})
t.Run("Limited", func(t *testing.T) {
client := NewClient("https://argoproj.github.io/argo-helm", HelmCreds{}, false, "", "")
_, err := client.GetIndex(t.Context(), false, 100)
assert.ErrorContains(t, err, "unexpected end of stream")
})
}
func Test_nativeHelmChart_ExtractChart(t *testing.T) {
client := NewClient("https://argoproj.github.io/argo-helm", HelmCreds{}, false, "", "")
path, closer, err := client.ExtractChart(t.Context(), "argo-cd", "0.7.1", false, math.MaxInt64, true)
require.NoError(t, err)
defer utilio.Close(closer)
info, err := os.Stat(path)
require.NoError(t, err)
assert.True(t, info.IsDir())
}
func Test_nativeHelmChart_ExtractChartWithLimiter(t *testing.T) {
client := NewClient("https://argoproj.github.io/argo-helm", HelmCreds{}, false, "", "")
_, _, err := client.ExtractChart(t.Context(), "argo-cd", "0.7.1", false, 100, false)
require.Error(t, err, "error while iterating on tar reader: unexpected EOF")
}
func Test_nativeHelmChart_ExtractChart_insecure(t *testing.T) {
client := NewClient("https://argoproj.github.io/argo-helm", HelmCreds{InsecureSkipVerify: true}, false, "", "")
path, closer, err := client.ExtractChart(t.Context(), "argo-cd", "0.7.1", false, math.MaxInt64, true)
require.NoError(t, err)
defer utilio.Close(closer)
info, err := os.Stat(path)
require.NoError(t, err)
assert.True(t, info.IsDir())
}
func Test_normalizeChartName(t *testing.T) {
t.Run("Test non-slashed name", func(t *testing.T) {
n := normalizeChartName("mychart")
assert.Equal(t, "mychart", n)
})
t.Run("Test single-slashed name", func(t *testing.T) {
n := normalizeChartName("myorg/mychart")
assert.Equal(t, "mychart", n)
})
t.Run("Test chart name with suborg", func(t *testing.T) {
n := normalizeChartName("myorg/mysuborg/mychart")
assert.Equal(t, "mychart", n)
})
t.Run("Test double-slashed name", func(t *testing.T) {
n := normalizeChartName("myorg//mychart")
assert.Equal(t, "mychart", n)
})
t.Run("Test invalid chart name - ends with slash", func(t *testing.T) {
n := normalizeChartName("myorg/")
assert.Equal(t, "myorg/", n)
})
t.Run("Test invalid chart name - is dot", func(t *testing.T) {
n := normalizeChartName("myorg/.")
assert.Equal(t, "myorg/.", n)
})
t.Run("Test invalid chart name - is two dots", func(t *testing.T) {
n := normalizeChartName("myorg/..")
assert.Equal(t, "myorg/..", n)
})
}
func TestIsHelmOciRepo(t *testing.T) {
assert.True(t, IsHelmOciRepo("demo.goharbor.io"))
assert.True(t, IsHelmOciRepo("demo.goharbor.io:8080"))
assert.False(t, IsHelmOciRepo("https://demo.goharbor.io"))
assert.False(t, IsHelmOciRepo("https://demo.goharbor.io:8080"))
}
func TestGetIndexURL(t *testing.T) {
urlTemplate := `https://gitlab.com/projects/%s/packages/helm/stable`
t.Run("URL without escaped characters", func(t *testing.T) {
rawURL := fmt.Sprintf(urlTemplate, "232323982")
want := rawURL + "/index.yaml"
got, err := getIndexURL(rawURL)
assert.Equal(t, want, got)
require.NoError(t, err)
})
t.Run("URL with escaped characters", func(t *testing.T) {
rawURL := fmt.Sprintf(urlTemplate, "mygroup%2Fmyproject")
want := rawURL + "/index.yaml"
got, err := getIndexURL(rawURL)
assert.Equal(t, want, got)
require.NoError(t, err)
})
t.Run("URL with invalid escaped characters", func(t *testing.T) {
rawURL := fmt.Sprintf(urlTemplate, "mygroup%**myproject")
got, err := getIndexURL(rawURL)
assert.Empty(t, got)
require.Error(t, err)
})
}
func TestGetTagsFromUrl(t *testing.T) {
t.Run("should return tags correctly while following the link header", func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("called %s", r.URL.Path)
var responseTags fakeTagsList
w.Header().Set("Content-Type", "application/json")
if !strings.Contains(r.URL.String(), "token") {
w.Header().Set("Link", fmt.Sprintf("<https://%s%s?token=next-token>; rel=next", r.Host, r.URL.Path))
responseTags = fakeTagsList{
Tags: []string{"first"},
}
} else {
responseTags = fakeTagsList{
Tags: []string{
"second",
"2.8.0",
"2.8.0-prerelease",
"2.8.0_build",
"2.8.0-prerelease_build",
"2.8.0-prerelease.1_build.1234",
},
}
}
w.WriteHeader(http.StatusOK)
require.NoError(t, json.NewEncoder(w).Encode(responseTags))
}))
client := NewClient(server.URL, HelmCreds{InsecureSkipVerify: true}, true, "", "")
tags, err := client.GetTags(t.Context(), "mychart", true)
require.NoError(t, err)
assert.ElementsMatch(t, tags, []string{
"first",
"second",
"2.8.0",
"2.8.0-prerelease",
"2.8.0+build",
"2.8.0-prerelease+build",
"2.8.0-prerelease.1+build.1234",
})
})
t.Run("should return an error not when oci is not enabled", func(t *testing.T) {
client := NewClient("example.com", HelmCreds{}, false, "", "")
_, err := client.GetTags(t.Context(), "my-chart", true)
assert.ErrorIs(t, ErrOCINotEnabled, err)
})
}
func TestGetTagsFromURLPrivateRepoAuthentication(t *testing.T) {
username := "my-username"
password := "my-password"
expectedAuthorization := "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=" // base64(user:password)
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("called %s", r.URL.Path)
authorization := r.Header.Get("Authorization")
if authorization == "" {
w.Header().Set("WWW-Authenticate", `Basic realm="helm repo to get tags"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
assert.Equal(t, expectedAuthorization, authorization)
responseTags := fakeTagsList{
Tags: []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0_build",
"2.8.0-prerelease_build",
"2.8.0-prerelease.1_build.1234",
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
require.NoError(t, json.NewEncoder(w).Encode(responseTags))
}))
t.Cleanup(server.Close)
serverURL, err := url.Parse(server.URL)
require.NoError(t, err)
testCases := []struct {
name string
repoURL string
}{
{
name: "should login correctly when the repo path is in the server root with http scheme",
repoURL: server.URL,
},
{
name: "should login correctly when the repo path is not in the server root with http scheme",
repoURL: server.URL + "/my-repo",
},
{
name: "should login correctly when the repo path is in the server root without http scheme",
repoURL: serverURL.Host,
},
{
name: "should login correctly when the repo path is not in the server root without http scheme",
repoURL: serverURL.Host + "/my-repo",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
client := NewClient(testCase.repoURL, HelmCreds{
InsecureSkipVerify: true,
Username: username,
Password: password,
}, true, "", "")
tags, err := client.GetTags(t.Context(), "mychart", true)
require.NoError(t, err)
assert.ElementsMatch(t, tags, []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0+build",
"2.8.0-prerelease+build",
"2.8.0-prerelease.1+build.1234",
})
})
}
}
func TestGetTagsFromURLPrivateRepoWithAzureWorkloadIdentityAuthentication(t *testing.T) {
expectedAuthorization := "Basic MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwOmFjY2Vzc1Rva2Vu" // base64(00000000-0000-0000-0000-000000000000:accessToken)
mockServerURL := ""
mockedServerURL := func() string {
return mockServerURL
}
workloadIdentityMock := &mocks.TokenProvider{}
workloadIdentityMock.EXPECT().GetToken("https://management.core.windows.net/.default").Return(&workloadidentity.Token{AccessToken: "accessToken"}, nil)
mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("called %s", r.URL.Path)
switch r.URL.Path {
case "/v2/":
w.Header().Set("Www-Authenticate", fmt.Sprintf(`Bearer realm=%q,service=%q`, mockedServerURL(), mockedServerURL()[8:]))
w.WriteHeader(http.StatusUnauthorized)
case "/oauth2/exchange":
response := `{"refresh_token":"accessToken"}`
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(response))
require.NoError(t, err)
default:
authorization := r.Header.Get("Authorization")
if authorization == "" {
w.Header().Set("WWW-Authenticate", `Basic realm="helm repo to get tags"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
assert.Equal(t, expectedAuthorization, authorization)
responseTags := fakeTagsList{
Tags: []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0_build",
"2.8.0-prerelease_build",
"2.8.0-prerelease.1_build.1234",
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
require.NoError(t, json.NewEncoder(w).Encode(responseTags))
}
}))
mockServerURL = mockServer.URL
t.Cleanup(mockServer.Close)
serverURL, err := url.Parse(mockServer.URL)
require.NoError(t, err)
testCases := []struct {
name string
repoURL string
}{
{
name: "should login correctly when the repo path is in the server root with http scheme",
repoURL: mockServer.URL,
},
{
name: "should login correctly when the repo path is not in the server root with http scheme",
repoURL: mockServer.URL + "/my-repo",
},
{
name: "should login correctly when the repo path is in the server root without http scheme",
repoURL: serverURL.Host,
},
{
name: "should login correctly when the repo path is not in the server root without http scheme",
repoURL: serverURL.Host + "/my-repo",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
client := NewClient(testCase.repoURL, AzureWorkloadIdentityCreds{
repoURL: mockServer.URL[8:],
InsecureSkipVerify: true,
tokenProvider: workloadIdentityMock,
}, true, "", "")
tags, err := client.GetTags(t.Context(), "mychart", true)
require.NoError(t, err)
assert.ElementsMatch(t, tags, []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0+build",
"2.8.0-prerelease+build",
"2.8.0-prerelease.1+build.1234",
})
})
}
}
func TestGetTagsFromURLEnvironmentAuthentication(t *testing.T) {
bearerToken := "Zm9vOmJhcg=="
expectedAuthorization := "Basic " + bearerToken
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("called %s", r.URL.Path)
authorization := r.Header.Get("Authorization")
if authorization == "" {
w.Header().Set("WWW-Authenticate", `Basic realm="helm repo to get tags"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
assert.Equal(t, expectedAuthorization, authorization)
responseTags := fakeTagsList{
Tags: []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0_build",
"2.8.0-prerelease_build",
"2.8.0-prerelease.1_build.1234",
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
require.NoError(t, json.NewEncoder(w).Encode(responseTags))
}))
t.Cleanup(server.Close)
serverURL, err := url.Parse(server.URL)
require.NoError(t, err)
tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "config.json")
t.Setenv("DOCKER_CONFIG", tempDir)
config := fmt.Sprintf(`{"auths":{%q:{"auth":%q}}}`, server.URL, bearerToken)
require.NoError(t, os.WriteFile(configPath, []byte(config), 0o666))
testCases := []struct {
name string
repoURL string
}{
{
name: "should login correctly when the repo path is in the server root with http scheme",
repoURL: server.URL,
},
{
name: "should login correctly when the repo path is not in the server root with http scheme",
repoURL: server.URL + "/my-repo",
},
{
name: "should login correctly when the repo path is in the server root without http scheme",
repoURL: serverURL.Host,
},
{
name: "should login correctly when the repo path is not in the server root without http scheme",
repoURL: serverURL.Host + "/my-repo",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
client := NewClient(testCase.repoURL, HelmCreds{
InsecureSkipVerify: true,
}, true, "", "")
tags, err := client.GetTags(t.Context(), "mychart", true)
require.NoError(t, err)
assert.ElementsMatch(t, tags, []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0+build",
"2.8.0-prerelease+build",
"2.8.0-prerelease.1+build.1234",
})
})
}
}
func TestGetTagsCaching(t *testing.T) {
requestCount := 0
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestCount++
t.Logf("request %d called %s", requestCount, r.URL.Path)
responseTags := fakeTagsList{
Tags: []string{
"1.0.0",
"1.1.0",
"2.0.0_beta",
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
require.NoError(t, json.NewEncoder(w).Encode(responseTags))
}))
t.Cleanup(server.Close)
serverURL, err := url.Parse(server.URL)
require.NoError(t, err)
t.Run("should cache tags correctly", func(t *testing.T) {
cache := &fakeIndexCache{}
client := NewClient(serverURL.Host, HelmCreds{
InsecureSkipVerify: true,
}, true, "", "", WithIndexCache(cache))
tags1, err := client.GetTags(t.Context(), "mychart", false)
require.NoError(t, err)
assert.ElementsMatch(t, tags1, []string{
"1.0.0",
"1.1.0",
"2.0.0+beta",
})
assert.Equal(t, 1, requestCount)
requestCount = 0
tags2, err := client.GetTags(t.Context(), "mychart", false)
require.NoError(t, err)
assert.ElementsMatch(t, tags2, []string{
"1.0.0",
"1.1.0",
"2.0.0+beta",
})
assert.Equal(t, 0, requestCount)
assert.NotEmpty(t, cache.data)
type entriesStruct struct {
Tags []string
}
var entries entriesStruct
err = json.Unmarshal(cache.data, &entries)
require.NoError(t, err)
assert.ElementsMatch(t, entries.Tags, []string{
"1.0.0",
"1.1.0",
"2.0.0+beta",
})
})
t.Run("should bypass cache when noCache is true", func(t *testing.T) {
cache := &fakeIndexCache{}
client := NewClient(serverURL.Host, HelmCreds{
InsecureSkipVerify: true,
}, true, "", "", WithIndexCache(cache))
requestCount = 0
tags1, err := client.GetTags(t.Context(), "mychart", true)
require.NoError(t, err)
assert.ElementsMatch(t, tags1, []string{
"1.0.0",
"1.1.0",
"2.0.0+beta",
})
assert.Equal(t, 1, requestCount)
tags2, err := client.GetTags(t.Context(), "mychart", true)
require.NoError(t, err)
assert.ElementsMatch(t, tags2, []string{
"1.0.0",
"1.1.0",
"2.0.0+beta",
})
assert.Equal(t, 2, requestCount)
})
}
func TestGetTagsUsesHTTP2(t *testing.T) {
t.Run("should negotiate HTTP/2 when TLS is configured", func(t *testing.T) {
var requestProtos []string
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestProtos = append(requestProtos, r.Proto)
t.Logf("called %s with proto %s", r.URL.Path, r.Proto)
responseTags := fakeTagsList{
Tags: []string{"1.0.0"},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
require.NoError(t, json.NewEncoder(w).Encode(responseTags))
}))
// httptest.NewTLSServer only advertises http/1.1 in ALPN, so we must
// configure the server to also offer h2 for HTTP/2 negotiation to work.
server.TLS = &tls.Config{NextProtos: []string{"h2", "http/1.1"}}
server.StartTLS()
t.Cleanup(server.Close)
client := NewClient(server.URL, HelmCreds{InsecureSkipVerify: true}, true, "", "")
tags, err := client.GetTags(t.Context(), "mychart", true)
require.NoError(t, err)
assert.Equal(t, []string{"1.0.0"}, tags)
// Verify that at least one request used HTTP/2. When ForceAttemptHTTP2 is
// not set on the Transport, Go's TLS stack won't negotiate h2 even though
// the server supports it, because a custom TLSClientConfig disables the
// automatic HTTP/2 setup.
require.NotEmpty(t, requestProtos, "expected at least one request to the server")
hasHTTP2 := slices.Contains(requestProtos, "HTTP/2.0")
assert.True(t, hasHTTP2, "expected at least one HTTP/2 request, but got protocols: %v", requestProtos)
})
}
func TestLoadRepoIndexUsesHTTP2(t *testing.T) {
t.Run("should negotiate HTTP/2 when fetching index", func(t *testing.T) {
var requestProto string
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestProto = r.Proto
t.Logf("called %s with proto %s", r.URL.Path, r.Proto)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`apiVersion: v1
entries: {}
`))
}))
server.TLS = &tls.Config{NextProtos: []string{"h2", "http/1.1"}}
server.StartTLS()
t.Cleanup(server.Close)
client := NewClient(server.URL, HelmCreds{InsecureSkipVerify: true}, false, "", "")
_, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
assert.Equal(t, "HTTP/2.0", requestProto, "expected HTTP/2 request for index fetch, but got %s", requestProto)
})
}
func TestUserAgentIsSet(t *testing.T) {
t.Run("Default User-Agent for traditional Helm repo", func(t *testing.T) {
// Create a test server that captures the User-Agent header
receivedUserAgent := ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedUserAgent = r.Header.Get("User-Agent")
// Return a valid minimal index.yaml
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`apiVersion: v1
entries: {}
`))
}))
defer ts.Close()
// Create client and make a request
client := NewClient(ts.URL, HelmCreds{}, false, "", "")
_, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
// Verify User-Agent was set and contains expected components
assert.NotEmpty(t, receivedUserAgent, "User-Agent header should be set")
assert.Contains(t, receivedUserAgent, "argocd-repo-server", "User-Agent should contain 'argocd-repo-server'")
t.Logf("User-Agent sent: %s", receivedUserAgent)
})
t.Run("Custom User-Agent via WithUserAgent option", func(t *testing.T) {
// Create a test server that captures the User-Agent header
receivedUserAgent := ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedUserAgent = r.Header.Get("User-Agent")
// Return a valid minimal index.yaml
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`apiVersion: v1
entries: {}
`))
}))
defer ts.Close()
// Create client with custom User-Agent
customUA := "my-custom-app/1.2.3 ([email protected])"
client := NewClient(ts.URL, HelmCreds{}, false, "", "", WithUserAgent(customUA))
_, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
// Verify custom User-Agent was used
assert.Equal(t, customUA, receivedUserAgent, "Custom User-Agent should be used")
t.Logf("Custom User-Agent sent: %s", receivedUserAgent)
})
}
func TestUserAgentRequiredByServer(t *testing.T) {
t.Run("Server rejects requests without User-Agent", func(t *testing.T) {
// Create a test server that mimics Wikimedia's behavior
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userAgent := r.Header.Get("User-Agent")
t.Logf("Server received User-Agent: '%s'", userAgent)
if userAgent == "" {
// Mimic Wikimedia's rejection of empty User-Agent
w.WriteHeader(http.StatusForbidden)
_, _ = w.Write([]byte(`authorization failed: Please set a user-agent and respect our robot policy`))
return
}
// Accept request with User-Agent
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`apiVersion: v1
entries: {}
`))
}))
defer ts.Close()
// Create client (should automatically set User-Agent)
client := NewClient(ts.URL, HelmCreds{}, false, "", "")
_, err := client.GetIndex(t.Context(), false, 10000)
// Should succeed because our implementation sets User-Agent
require.NoError(t, err, "Request should succeed with User-Agent set")
t.Log("Success! Server accepted request with User-Agent")
})
}
func TestUserAgentPriority(t *testing.T) {
t.Run("Custom User-Agent set via WithUserAgent option", func(t *testing.T) {
// Create a test server that captures the User-Agent header
receivedUserAgent := ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedUserAgent = r.Header.Get("User-Agent")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`apiVersion: v1
entries: {}
`))
}))
defer ts.Close()
// Set custom User-Agent using WithUserAgent option
customUA := "CustomAgent/1.0 (test)"
client := NewClient(ts.URL, HelmCreds{}, false, "", "", WithUserAgent(customUA))
_, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
// Verify custom User-Agent was used
assert.Equal(t, customUA, receivedUserAgent, "Custom User-Agent should be used when set")
t.Logf("Custom User-Agent sent: %s", receivedUserAgent)
})
t.Run("Default User-Agent used when no custom value provided", func(t *testing.T) {
// Create a test server that captures the User-Agent header
receivedUserAgent := ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedUserAgent = r.Header.Get("User-Agent")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`apiVersion: v1
entries: {}
`))
}))
defer ts.Close()
// Create client without custom User-Agent
client := NewClient(ts.URL, HelmCreds{}, false, "", "")
_, err := client.GetIndex(t.Context(), false, 10000)
require.NoError(t, err)
// Verify default User-Agent was used
assert.Contains(t, receivedUserAgent, "argocd-repo-server", "Should use default User-Agent format")
t.Logf("Default User-Agent sent: %s", receivedUserAgent)
})
}
func TestWithPlainHTTP(t *testing.T) {
t.Run("not set by default", func(t *testing.T) {
c := NewClient("my.registry.com", HelmCreds{}, true, "", "")
assert.False(t, c.(*nativeHelmChart).plainHTTP)
})
t.Run("set via WithPlainHTTP option", func(t *testing.T) {
c := NewClient("my.registry.com", HelmCreds{}, true, "", "", WithPlainHTTP())
assert.True(t, c.(*nativeHelmChart).plainHTTP)
})
}
func Test_nativeHelmChart_ExtractChartRespectsChartCacheExpiration(t *testing.T) {
const (
chartName = "test-chart"
chartVersion = "1.0.0"
)
var (
mu sync.Mutex
chartRequests int
chartArchive = testHelmChartArchive(t, chartName, chartVersion, "foo: first\n")
server *httptest.Server
)
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/index.yaml":
w.Header().Set("Content-Type", "application/x-yaml")
_, _ = fmt.Fprintf(w, `apiVersion: v1
entries:
%[1]s:
- apiVersion: v2
name: %[1]s
version: %[2]s
urls:
- %[3]s/%[1]s-%[2]s.tgz
`, chartName, chartVersion, server.URL)
case "/" + chartName + "-" + chartVersion + ".tgz":
mu.Lock()
chartRequests++
body := append([]byte(nil), chartArchive...)
mu.Unlock()
w.Header().Set("Content-Type", "application/gzip")
_, _ = w.Write(body)
default:
http.NotFound(w, r)
}
}))
t.Cleanup(server.Close)
client := NewClient(server.URL, HelmCreds{}, false, "", "",
WithChartPaths(utilio.NewRandomizedTempPaths(t.TempDir())),
WithHelmChartCacheExpiration(time.Hour),
)
nativeClient, ok := client.(*nativeHelmChart)
require.True(t, ok)
extractValues := func() string {
t.Helper()
path, closer, err := client.ExtractChart(t.Context(), chartName, chartVersion, false, math.MaxInt64, false)
require.NoError(t, err)
defer utilio.Close(closer)
values, err := os.ReadFile(filepath.Join(path, "values.yaml"))
require.NoError(t, err)
return string(values)
}
requireChartRequests := func(expected int) {
t.Helper()
mu.Lock()
defer mu.Unlock()
require.Equal(t, expected, chartRequests)
}
require.Equal(t, "foo: first\n", extractValues())
requireChartRequests(1)
secondChartArchive := testHelmChartArchive(t, chartName, chartVersion, "foo: second\n")
mu.Lock()
chartArchive = secondChartArchive
mu.Unlock()
require.Equal(t, "foo: first\n", extractValues())
requireChartRequests(1)
cachedChartPath, err := nativeClient.getCachedChartPath(chartName, chartVersion)
require.NoError(t, err)
expired := time.Now().Add(-2 * time.Hour)
require.NoError(t, os.Chtimes(cachedChartPath, expired, expired))
require.Equal(t, "foo: second\n", extractValues())
requireChartRequests(2)
}
func testHelmChartArchive(t *testing.T, chartName, chartVersion, values string) []byte {
t.Helper()
var archive bytes.Buffer
gzipWriter := gzip.NewWriter(&archive)
tarWriter := tar.NewWriter(gzipWriter)
writeFile := func(name, contents string) {
t.Helper()
header := &tar.Header{
Name: name,
Mode: 0o644,
Size: int64(len(contents)),
}
require.NoError(t, tarWriter.WriteHeader(header))
_, err := tarWriter.Write([]byte(contents))
require.NoError(t, err)
}
writeFile(filepath.ToSlash(filepath.Join(chartName, "Chart.yaml")), fmt.Sprintf(`apiVersion: v2
name: %s
version: %s
`, chartName, chartVersion))
writeFile(filepath.ToSlash(filepath.Join(chartName, "values.yaml")), values)
require.NoError(t, tarWriter.Close())
require.NoError(t, gzipWriter.Close())
return archive.Bytes()
}