Files
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

935 lines
25 KiB
Go

package plugin
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path"
"path/filepath"
"testing"
"time"
"github.com/golang/protobuf/ptypes/empty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/hanzoai/deploy/cmpserver/apiclient"
repoclient "github.com/hanzoai/deploy/reposerver/apiclient"
"github.com/hanzoai/deploy/test"
"github.com/hanzoai/deploy/util/cmp"
"github.com/hanzoai/deploy/util/tgzstream"
)
func newService(configFilePath string) (*Service, error) {
config, err := ReadPluginConfig(configFilePath)
if err != nil {
return nil, err
}
initConstants := CMPServerInitConstants{
PluginConfig: *config,
}
service := &Service{
initConstants: initConstants,
}
return service, nil
}
func (s *Service) WithGenerateCommand(command Command) *Service {
s.initConstants.PluginConfig.Spec.Generate = command
return s
}
type pluginOpt func(*CMPServerInitConstants)
func withDiscover(d Discover) pluginOpt {
return func(cic *CMPServerInitConstants) {
cic.PluginConfig.Spec.Discover = d
}
}
func buildPluginConfig(opts ...pluginOpt) *CMPServerInitConstants {
cic := &CMPServerInitConstants{
PluginConfig: PluginConfig{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigManagementPlugin",
APIVersion: "argoproj.io/v1alpha1",
},
Metadata: metav1.ObjectMeta{
Name: "some-plugin",
},
Spec: PluginConfigSpec{
Version: "v1.0",
},
},
}
for _, opt := range opts {
opt(cic)
}
return cic
}
func TestMatchRepository(t *testing.T) {
t.Parallel()
type fixture struct {
service *Service
path string
env []*apiclient.EnvEntry
}
setup := func(t *testing.T, opts ...pluginOpt) *fixture {
t.Helper()
cic := buildPluginConfig(opts...)
path := filepath.Join(test.GetTestDir(t), "testdata", "kustomize")
s := NewService(*cic)
return &fixture{
service: s,
path: path,
env: []*apiclient.EnvEntry{{Name: "ENV_VAR", Value: "1"}},
}
}
t.Run("will match plugin by filename", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
FileName: "kustomization.yaml",
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
t.Run("will not match plugin by filename if file not found", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
FileName: "not_found.yaml",
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
t.Run("will not match a pattern with a syntax error", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
FileName: "[",
}
f := setup(t, withDiscover(d))
// when
_, _, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.ErrorContains(t, err, "syntax error")
})
t.Run("will match plugin by glob", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Glob: "**/*/plugin.yaml",
},
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
t.Run("will not match plugin by glob if not found", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Glob: "**/*/not_found.yaml",
},
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
t.Run("will throw an error for a bad pattern", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Glob: "does-not-exist",
},
}
f := setup(t, withDiscover(d))
// when
_, _, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.ErrorContains(t, err, "error finding glob match for pattern")
})
t.Run("will match plugin by command when returns any output", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Command: Command{
Command: []string{"echo", "test"},
},
},
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
t.Run("will not match plugin by command when returns no output", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Command: Command{
Command: []string{"echo"},
},
},
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
t.Run("will match plugin because env var defined", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Command: Command{
Command: []string{"sh", "-c", "echo -n $ENV_VAR"},
},
},
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
t.Run("will not match plugin because no env var defined", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Command: Command{
// Use printf instead of echo since OSX prints the "-n" when there's no additional arg.
Command: []string{"sh", "-c", `printf "%s" "$ENV_NO_VAR"`},
},
},
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
t.Run("will not match plugin by command when command fails", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Command: Command{
Command: []string{"cat", "nil"},
},
},
}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.Error(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
t.Run("will not match plugin as discovery is not set", func(t *testing.T) {
// given
t.Parallel()
d := Discover{}
f := setup(t, withDiscover(d))
// when
match, discovery, err := f.service.matchRepository(t.Context(), f.path, f.env, ".")
// then
require.NoError(t, err)
assert.False(t, match)
assert.False(t, discovery)
})
}
func Test_Negative_ConfigFile_DoesnotExist(t *testing.T) {
configFilePath := "./testdata/kustomize-neg/config"
service, err := newService(configFilePath)
require.Error(t, err)
require.Nil(t, service)
}
func TestGenerateManifest(t *testing.T) {
t.Parallel()
configFilePath := "./testdata/kustomize/config"
t.Run("successful generate", func(t *testing.T) {
t.Parallel()
service, err := newService(configFilePath)
require.NoError(t, err)
res1, err := service.generateManifest(t.Context(), "testdata/kustomize", nil)
require.NoError(t, err)
require.NotNil(t, res1)
expectedOutput := "{\"apiVersion\":\"v1\",\"data\":{\"foo\":\"bar\"},\"kind\":\"ConfigMap\",\"metadata\":{\"name\":\"my-map\"}}"
if res1 != nil {
require.Equal(t, expectedOutput, res1.Manifests[0])
}
})
t.Run("bad generate command", func(t *testing.T) {
t.Parallel()
service, err := newService(configFilePath)
require.NoError(t, err)
service.WithGenerateCommand(Command{Command: []string{"bad-command"}})
res, err := service.generateManifest(t.Context(), "testdata/kustomize", nil)
require.ErrorContains(t, err, "executable file not found")
assert.Nil(t, res.Manifests)
})
t.Run("bad yaml output", func(t *testing.T) {
t.Parallel()
service, err := newService(configFilePath)
require.NoError(t, err)
service.WithGenerateCommand(Command{Command: []string{"echo", "invalid yaml: }"}})
res, err := service.generateManifest(t.Context(), "testdata/kustomize", nil)
require.ErrorContains(t, err, "failed to unmarshal manifest")
assert.Nil(t, res.Manifests)
})
}
func TestGenerateManifest_deadline_exceeded(t *testing.T) {
t.Parallel()
configFilePath := "./testdata/kustomize/config"
service, err := newService(configFilePath)
require.NoError(t, err)
expiredCtx, cancel := context.WithTimeout(t.Context(), time.Second*0)
defer cancel()
_, err = service.generateManifest(expiredCtx, "", nil)
require.ErrorContains(t, err, "context deadline exceeded")
}
// TestRunCommandContextTimeout makes sure the command dies at timeout rather than sleeping past the timeout.
func TestRunCommandContextTimeout(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(t.Context(), 990*time.Millisecond)
defer cancel()
// Use a subshell so there's a child command.
command := Command{
Command: []string{"sh", "-c"},
Args: []string{"sleep 5"},
}
before := time.Now()
_, err := runCommand(ctx, command, "", []string{})
after := time.Now()
require.Error(t, err) // The command should time out, causing an error.
assert.Less(t, after.Sub(before), 1*time.Second)
}
func TestRunCommandEmptyCommand(t *testing.T) {
t.Parallel()
_, err := runCommand(t.Context(), Command{}, "", nil)
require.ErrorContains(t, err, "Command is empty")
}
// TestRunCommandContextTimeoutWithCleanup makes sure that the process is given enough time to cleanup before sending SIGKILL.
func TestRunCommandContextTimeoutWithCleanup(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(t.Context(), 900*time.Millisecond)
defer cancel()
// Use a subshell so there's a child command.
// This command sleeps for 4 seconds which is currently less than the 5 second delay between SIGTERM and SIGKILL signal and then exits successfully.
command := Command{
Command: []string{"sh", "-c"},
Args: []string{`(trap 'echo "cleanup completed"; exit' TERM; sleep 4)`},
}
before := time.Now()
output, err := runCommand(ctx, command, "", []string{})
after := time.Now()
require.Error(t, err) // The command should time out, causing an error.
assert.Less(t, after.Sub(before), 1*time.Second)
// The command should still have completed the cleanup after termination.
assert.Contains(t, output, "cleanup completed")
}
func Test_getParametersAnnouncement_empty_command(t *testing.T) {
staticYAML := `
- name: static-a
- name: static-b
`
static := &[]*repoclient.ParameterAnnouncement{}
err := yaml.Unmarshal([]byte(staticYAML), static)
require.NoError(t, err)
command := Command{
Command: []string{"echo"},
Args: []string{`[]`},
}
res, err := getParametersAnnouncement(t.Context(), "", *static, command, []*apiclient.EnvEntry{})
require.NoError(t, err)
assert.Equal(t, []*repoclient.ParameterAnnouncement{{Name: "static-a"}, {Name: "static-b"}}, res.ParameterAnnouncements)
}
func Test_getParametersAnnouncement_no_command(t *testing.T) {
staticYAML := `
- name: static-a
- name: static-b
`
static := &[]*repoclient.ParameterAnnouncement{}
err := yaml.Unmarshal([]byte(staticYAML), static)
require.NoError(t, err)
command := Command{}
res, err := getParametersAnnouncement(t.Context(), "", *static, command, []*apiclient.EnvEntry{})
require.NoError(t, err)
assert.Equal(t, []*repoclient.ParameterAnnouncement{{Name: "static-a"}, {Name: "static-b"}}, res.ParameterAnnouncements)
}
func Test_getParametersAnnouncement_static_and_dynamic(t *testing.T) {
staticYAML := `
- name: static-a
- name: static-b
`
static := &[]*repoclient.ParameterAnnouncement{}
err := yaml.Unmarshal([]byte(staticYAML), static)
require.NoError(t, err)
command := Command{
Command: []string{"echo"},
Args: []string{`[{"name": "dynamic-a"}, {"name": "dynamic-b"}]`},
}
res, err := getParametersAnnouncement(t.Context(), "", *static, command, []*apiclient.EnvEntry{})
require.NoError(t, err)
expected := []*repoclient.ParameterAnnouncement{
{Name: "dynamic-a"},
{Name: "dynamic-b"},
{Name: "static-a"},
{Name: "static-b"},
}
assert.Equal(t, expected, res.ParameterAnnouncements)
}
func Test_getParametersAnnouncement_invalid_json(t *testing.T) {
command := Command{
Command: []string{"echo"},
Args: []string{`[`},
}
_, err := getParametersAnnouncement(t.Context(), "", []*repoclient.ParameterAnnouncement{}, command, []*apiclient.EnvEntry{})
assert.ErrorContains(t, err, "unexpected end of JSON input")
}
func Test_getParametersAnnouncement_bad_command(t *testing.T) {
command := Command{
Command: []string{"exit"},
Args: []string{"1"},
}
_, err := getParametersAnnouncement(t.Context(), "", []*repoclient.ParameterAnnouncement{}, command, []*apiclient.EnvEntry{})
assert.ErrorContains(t, err, "error executing dynamic parameter output command")
}
func Test_getTempDirMustCleanup(t *testing.T) {
tempDir := t.TempDir()
// Induce a directory create error to verify error handling.
err := os.Chmod(tempDir, 0o000)
require.NoError(t, err)
_, _, err = getTempDirMustCleanup(path.Join(tempDir, "test"))
require.ErrorContains(t, err, "error creating temp dir")
err = os.Chmod(tempDir, 0o700)
require.NoError(t, err)
workDir, cleanup, err := getTempDirMustCleanup(tempDir)
require.NoError(t, err)
require.DirExists(t, workDir)
cleanup()
assert.NoDirExists(t, workDir)
}
func TestService_Init(t *testing.T) {
t.Parallel()
// Set up a base directory containing a test directory and a test file.
tempDir := t.TempDir()
workDir := path.Join(tempDir, "workDir")
err := os.MkdirAll(workDir, 0o700)
require.NoError(t, err)
testfile := path.Join(workDir, "testfile")
file, err := os.Create(testfile)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
// Make the base directory read-only so Init's cleanup fails.
err = os.Chmod(tempDir, 0o000)
require.NoError(t, err)
s := NewService(CMPServerInitConstants{PluginConfig: PluginConfig{}})
err = s.Init(workDir)
require.ErrorContains(t, err, "error removing workdir", "Init must throw an error if it can't remove the work directory")
// Make the base directory writable so Init's cleanup succeeds.
err = os.Chmod(tempDir, 0o700)
require.NoError(t, err)
err = s.Init(workDir)
require.NoError(t, err)
assert.DirExists(t, workDir)
assert.NoFileExists(t, testfile)
}
func TestEnviron(t *testing.T) {
t.Parallel()
t.Run("empty environ", func(t *testing.T) {
t.Parallel()
env := environ([]*apiclient.EnvEntry{})
assert.Nil(t, env)
})
t.Run("env vars with empty names", func(t *testing.T) {
t.Parallel()
env := environ([]*apiclient.EnvEntry{
{Value: "test"},
{Name: "test"},
})
assert.Equal(t, []string{"test="}, env)
})
t.Run("proper env vars", func(t *testing.T) {
t.Parallel()
env := environ([]*apiclient.EnvEntry{
{Name: "name1", Value: "value1"},
{Name: "name2", Value: "value2"},
{Name: "name3", Value: ""},
})
assert.Equal(t, []string{"name1=value1", "name2=value2", "name3="}, env)
})
}
func TestIsDiscoveryConfigured(t *testing.T) {
t.Parallel()
type fixture struct {
service *Service
}
setup := func(t *testing.T, opts ...pluginOpt) *fixture {
t.Helper()
cic := buildPluginConfig(opts...)
s := NewService(*cic)
return &fixture{
service: s,
}
}
t.Run("discovery is enabled when is configured by FileName", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
FileName: "kustomization.yaml",
}
f := setup(t, withDiscover(d))
// when
isDiscoveryConfigured := f.service.isDiscoveryConfigured()
// then
assert.True(t, isDiscoveryConfigured)
})
t.Run("discovery is enabled when is configured by Glob", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Glob: "**/*/plugin.yaml",
},
}
f := setup(t, withDiscover(d))
// when
isDiscoveryConfigured := f.service.isDiscoveryConfigured()
// then
assert.True(t, isDiscoveryConfigured)
})
t.Run("discovery is enabled when is configured by Command", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
Find: Find{
Command: Command{
Command: []string{"echo", "test"},
},
},
}
f := setup(t, withDiscover(d))
// when
isDiscoveryConfigured := f.service.isDiscoveryConfigured()
// then
assert.True(t, isDiscoveryConfigured)
})
t.Run("discovery is disabled when discover is not configured", func(t *testing.T) {
// given
t.Parallel()
d := Discover{}
f := setup(t, withDiscover(d))
// when
isDiscoveryConfigured := f.service.isDiscoveryConfigured()
// then
assert.False(t, isDiscoveryConfigured)
})
}
type MockGenerateManifestStream struct {
metadataSent bool
fileSent bool
metadataRequest *apiclient.AppStreamRequest
fileRequest *apiclient.AppStreamRequest
response *apiclient.ManifestResponse
}
func NewMockGenerateManifestStream(repoPath, appPath string, env []string) (*MockGenerateManifestStream, error) {
tgz, mr, err := cmp.GetCompressedRepoAndMetadata(repoPath, appPath, env, nil, nil)
if err != nil {
return nil, err
}
defer tgzstream.CloseAndDelete(tgz)
tgzBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(tgzBuffer, tgz)
if err != nil {
return nil, fmt.Errorf("failed to copy manifest targz to a byte buffer: %w", err)
}
return &MockGenerateManifestStream{
metadataRequest: mr,
fileRequest: cmp.AppFileRequest(tgzBuffer.Bytes()),
}, nil
}
func (m *MockGenerateManifestStream) SendAndClose(response *apiclient.ManifestResponse) error {
m.response = response
return nil
}
func (m *MockGenerateManifestStream) Recv() (*apiclient.AppStreamRequest, error) {
if !m.metadataSent {
m.metadataSent = true
return m.metadataRequest, nil
}
if !m.fileSent {
m.fileSent = true
return m.fileRequest, nil
}
return nil, io.EOF
}
func (m *MockGenerateManifestStream) Context() context.Context {
return context.Background()
}
func TestService_GenerateManifest(t *testing.T) {
t.Parallel()
configFilePath := "./testdata/kustomize/config"
service, err := newService(configFilePath)
require.NoError(t, err)
t.Run("successful generate", func(t *testing.T) {
t.Parallel()
s, err := NewMockGenerateManifestStream("./testdata/kustomize", "./testdata/kustomize", nil)
require.NoError(t, err)
err = service.generateManifestGeneric(s)
require.NoError(t, err)
require.NotNil(t, s.response)
assert.Equal(t, []string{"{\"apiVersion\":\"v1\",\"data\":{\"foo\":\"bar\"},\"kind\":\"ConfigMap\",\"metadata\":{\"name\":\"my-map\"}}"}, s.response.Manifests)
})
t.Run("out-of-bounds app path", func(t *testing.T) {
t.Parallel()
s, err := NewMockGenerateManifestStream("./testdata/kustomize", "./testdata/kustomize", nil)
require.NoError(t, err)
// set a malicious app path on the metadata
s.metadataRequest.Request.(*apiclient.AppStreamRequest_Metadata).Metadata.AppRelPath = "../out-of-bounds"
err = service.generateManifestGeneric(s)
require.ErrorContains(t, err, "illegal appPath")
assert.Nil(t, s.response)
})
}
type MockMatchRepositoryStream struct {
metadataSent bool
fileSent bool
metadataRequest *apiclient.AppStreamRequest
fileRequest *apiclient.AppStreamRequest
response *apiclient.RepositoryResponse
}
func NewMockMatchRepositoryStream(repoPath, appPath string, env []string) (*MockMatchRepositoryStream, error) {
tgz, mr, err := cmp.GetCompressedRepoAndMetadata(repoPath, appPath, env, nil, nil)
if err != nil {
return nil, err
}
defer tgzstream.CloseAndDelete(tgz)
tgzBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(tgzBuffer, tgz)
if err != nil {
return nil, fmt.Errorf("failed to copy manifest targz to a byte buffer: %w", err)
}
return &MockMatchRepositoryStream{
metadataRequest: mr,
fileRequest: cmp.AppFileRequest(tgzBuffer.Bytes()),
}, nil
}
func (m *MockMatchRepositoryStream) SendAndClose(response *apiclient.RepositoryResponse) error {
m.response = response
return nil
}
func (m *MockMatchRepositoryStream) Recv() (*apiclient.AppStreamRequest, error) {
if !m.metadataSent {
m.metadataSent = true
return m.metadataRequest, nil
}
if !m.fileSent {
m.fileSent = true
return m.fileRequest, nil
}
return nil, io.EOF
}
func (m *MockMatchRepositoryStream) Context() context.Context {
return context.Background()
}
func TestService_MatchRepository(t *testing.T) {
t.Parallel()
configFilePath := "./testdata/kustomize/config"
service, err := newService(configFilePath)
require.NoError(t, err)
t.Run("supported app", func(t *testing.T) {
t.Parallel()
s, err := NewMockMatchRepositoryStream("./testdata/kustomize", "./testdata/kustomize", nil)
require.NoError(t, err)
err = service.matchRepositoryGeneric(s)
require.NoError(t, err)
require.NotNil(t, s.response)
assert.True(t, s.response.IsSupported)
})
t.Run("unsupported app", func(t *testing.T) {
t.Parallel()
s, err := NewMockMatchRepositoryStream("./testdata/ksonnet", "./testdata/ksonnet", nil)
require.NoError(t, err)
err = service.matchRepositoryGeneric(s)
require.NoError(t, err)
require.NotNil(t, s.response)
assert.False(t, s.response.IsSupported)
})
}
type MockParametersAnnouncementStream struct {
metadataSent bool
fileSent bool
metadataRequest *apiclient.AppStreamRequest
fileRequest *apiclient.AppStreamRequest
response *apiclient.ParametersAnnouncementResponse
}
func NewMockParametersAnnouncementStream(repoPath, appPath string, env []string) (*MockParametersAnnouncementStream, error) {
tgz, mr, err := cmp.GetCompressedRepoAndMetadata(repoPath, appPath, env, nil, nil)
if err != nil {
return nil, err
}
defer tgzstream.CloseAndDelete(tgz)
tgzBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(tgzBuffer, tgz)
if err != nil {
return nil, fmt.Errorf("failed to copy manifest targz to a byte buffer: %w", err)
}
return &MockParametersAnnouncementStream{
metadataRequest: mr,
fileRequest: cmp.AppFileRequest(tgzBuffer.Bytes()),
}, nil
}
func (m *MockParametersAnnouncementStream) SendAndClose(response *apiclient.ParametersAnnouncementResponse) error {
m.response = response
return nil
}
func (m *MockParametersAnnouncementStream) Recv() (*apiclient.AppStreamRequest, error) {
if !m.metadataSent {
m.metadataSent = true
return m.metadataRequest, nil
}
if !m.fileSent {
m.fileSent = true
return m.fileRequest, nil
}
return nil, io.EOF
}
func (m *MockParametersAnnouncementStream) SetHeader(metadata.MD) error {
return nil
}
func (m *MockParametersAnnouncementStream) SendHeader(metadata.MD) error {
return nil
}
func (m *MockParametersAnnouncementStream) SetTrailer(metadata.MD) {}
func (m *MockParametersAnnouncementStream) Context() context.Context {
return context.Background()
}
func (m *MockParametersAnnouncementStream) SendMsg(any) error {
return nil
}
func (m *MockParametersAnnouncementStream) RecvMsg(any) error {
return nil
}
func TestService_GetParametersAnnouncement(t *testing.T) {
t.Parallel()
configFilePath := "./testdata/kustomize/config"
service, err := newService(configFilePath)
require.NoError(t, err)
t.Run("successful response", func(t *testing.T) {
t.Parallel()
s, err := NewMockParametersAnnouncementStream("./testdata/kustomize", "./testdata/kustomize", []string{"MUST_BE_SET=yep"})
require.NoError(t, err)
err = service.GetParametersAnnouncement(s)
require.NoError(t, err)
require.NotNil(t, s.response)
require.Len(t, s.response.ParameterAnnouncements, 2)
assert.Equal(t, repoclient.ParameterAnnouncement{Name: "dynamic-test-param", String_: "yep"}, *s.response.ParameterAnnouncements[0])
assert.Equal(t, repoclient.ParameterAnnouncement{Name: "test-param", String_: "test-value"}, *s.response.ParameterAnnouncements[1])
})
t.Run("out of bounds app", func(t *testing.T) {
t.Parallel()
s, err := NewMockParametersAnnouncementStream("./testdata/kustomize", "./testdata/kustomize", []string{"MUST_BE_SET=yep"})
require.NoError(t, err)
// set a malicious app path on the metadata
s.metadataRequest.Request.(*apiclient.AppStreamRequest_Metadata).Metadata.AppRelPath = "../out-of-bounds"
err = service.GetParametersAnnouncement(s)
require.ErrorContains(t, err, "illegal appPath")
require.Nil(t, s.response)
})
t.Run("fails when script fails", func(t *testing.T) {
t.Parallel()
s, err := NewMockParametersAnnouncementStream("./testdata/kustomize", "./testdata/kustomize", []string{"WRONG_ENV_VAR=oops"})
require.NoError(t, err)
err = service.GetParametersAnnouncement(s)
require.ErrorContains(t, err, "error executing dynamic parameter output command")
require.Nil(t, s.response)
})
}
func TestService_CheckPluginConfiguration(t *testing.T) {
t.Parallel()
type fixture struct {
service *Service
}
setup := func(t *testing.T, opts ...pluginOpt) *fixture {
t.Helper()
cic := buildPluginConfig(opts...)
s := NewService(*cic)
return &fixture{
service: s,
}
}
t.Run("discovery is enabled when is configured", func(t *testing.T) {
// given
t.Parallel()
d := Discover{
FileName: "kustomization.yaml",
}
f := setup(t, withDiscover(d))
// when
resp, err := f.service.CheckPluginConfiguration(t.Context(), &empty.Empty{})
// then
require.NoError(t, err)
assert.True(t, resp.IsDiscoveryConfigured)
})
t.Run("discovery is disabled when is not configured", func(t *testing.T) {
// given
t.Parallel()
d := Discover{}
f := setup(t, withDiscover(d))
// when
resp, err := f.service.CheckPluginConfiguration(t.Context(), &empty.Empty{})
// then
require.NoError(t, err)
assert.False(t, resp.IsDiscoveryConfigured)
})
}