mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
go.mod still declared `module github.com/hanzoai/deploy` after the repo was renamed. A Go module path is not cosmetic — it is the resolvable location. So `go get github.com/hanzoai/cd` could not work, and anything importing this module had to spell a name that no longer exists at that address. The build log of v3.6.0 is where it surfaced: the compiler was still emitting github.com/hanzoai/deploy/cmd/cd/commands. 3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from the module path, replaced separately so neither could clobber the other. go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean. Co-authored-by: Hanzo Dev <[email protected]>
263 lines
12 KiB
Go
263 lines
12 KiB
Go
package controller
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
)
|
|
|
|
func createFakeNamespace(uid string, resourceVersion string, labels map[string]string, annotations map[string]string) *unstructured.Unstructured {
|
|
un := unstructured.Unstructured{}
|
|
un.SetUID(types.UID(uid))
|
|
un.SetResourceVersion(resourceVersion)
|
|
un.SetLabels(labels)
|
|
un.SetAnnotations(annotations)
|
|
un.SetKind("Namespace")
|
|
un.SetName("some-namespace")
|
|
return &un
|
|
}
|
|
|
|
func Test_shouldNamespaceSync(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
syncPolicy *v1alpha1.SyncPolicy
|
|
managedNs *unstructured.Unstructured
|
|
liveNs *unstructured.Unstructured
|
|
expected bool
|
|
expectedLabels map[string]string
|
|
expectedAnnotations map[string]string
|
|
}{
|
|
{
|
|
name: "liveNs is nil and syncPolicy is nil",
|
|
expected: false,
|
|
managedNs: nil,
|
|
liveNs: nil,
|
|
syncPolicy: nil,
|
|
},
|
|
{
|
|
name: "liveNs is nil and syncPolicy is not nil",
|
|
expected: false,
|
|
managedNs: nil,
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "liveNs is nil and syncPolicy has labels and annotations",
|
|
expected: false,
|
|
managedNs: nil,
|
|
liveNs: nil,
|
|
expectedLabels: map[string]string{"my-cool-label": "some-value"},
|
|
expectedAnnotations: map[string]string{"my-cool-annotation": "some-value"},
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{"my-cool-label": "some-value"},
|
|
Annotations: map[string]string{"my-cool-annotation": "some-value"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata nil",
|
|
expected: true,
|
|
expectedLabels: map[string]string{},
|
|
expectedAnnotations: map[string]string{},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata not nil",
|
|
expected: true,
|
|
expectedAnnotations: map[string]string{"cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata has empty labels map",
|
|
expected: true,
|
|
expectedLabels: map[string]string{},
|
|
expectedAnnotations: map[string]string{"cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata has empty annotations map",
|
|
expected: true,
|
|
expectedAnnotations: map[string]string{"cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Annotations: map[string]string{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata has empty annotations and labels map",
|
|
expected: true,
|
|
expectedLabels: map[string]string{},
|
|
expectedAnnotations: map[string]string{"cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{},
|
|
Annotations: map[string]string{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata has labels",
|
|
expected: true,
|
|
expectedLabels: map[string]string{"my-cool-label": "some-value"},
|
|
expectedAnnotations: map[string]string{"cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{"my-cool-label": "some-value"},
|
|
Annotations: nil,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata has annotations",
|
|
expected: true,
|
|
expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: nil,
|
|
Annotations: map[string]string{"my-cool-annotation": "some-value"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace does not yet exist and managedNamespaceMetadata has annotations and labels",
|
|
expected: true,
|
|
expectedLabels: map[string]string{"my-cool-label": "some-value"},
|
|
expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: nil,
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{"my-cool-label": "some-value"},
|
|
Annotations: map[string]string{"my-cool-annotation": "some-value"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace exists with no labels or annotations and managedNamespaceMetadata has labels",
|
|
expected: true,
|
|
expectedLabels: map[string]string{"my-cool-label": "some-value"},
|
|
expectedAnnotations: map[string]string{"cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: createFakeNamespace("something", "1", map[string]string{}, map[string]string{}),
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{"my-cool-label": "some-value"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace exists with no labels or annotations and managedNamespaceMetadata has annotations",
|
|
expected: true,
|
|
expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: createFakeNamespace("something", "1", map[string]string{}, map[string]string{}),
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Annotations: map[string]string{"my-cool-annotation": "some-value"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace exists with no labels or annotations and managedNamespaceMetadata has annotations and labels",
|
|
expected: true,
|
|
expectedLabels: map[string]string{"my-cool-label": "some-value"},
|
|
expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: createFakeNamespace("something", "1", map[string]string{}, map[string]string{}),
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{"my-cool-label": "some-value"},
|
|
Annotations: map[string]string{"my-cool-annotation": "some-value"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace exists with labels and managedNamespaceMetadata has mismatching labels",
|
|
expected: true,
|
|
expectedAnnotations: map[string]string{"cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
expectedLabels: map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: createFakeNamespace("something", "1", map[string]string{"my-cool-label": "some-value"}, map[string]string{}),
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
|
|
Annotations: map[string]string{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace exists with annotations and managedNamespaceMetadata has mismatching annotations",
|
|
expected: true,
|
|
expectedLabels: map[string]string{},
|
|
expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: createFakeNamespace("something", "1", map[string]string{}, map[string]string{"my-cool-annotation": "some-value", "my-other-annotation": "some-other-value"}),
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{},
|
|
Annotations: map[string]string{"my-cool-annotation": "some-value"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "namespace exists with annotations and labels managedNamespaceMetadata has mismatching annotations and labels",
|
|
expected: true,
|
|
expectedLabels: map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
|
|
expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "my-other-annotation": "some-other-value", "cd.hanzo.ai/sync-options": "ServerSideApply=true"},
|
|
managedNs: createFakeNamespace("", "", map[string]string{}, map[string]string{}),
|
|
liveNs: createFakeNamespace("something", "1", map[string]string{"my-cool-label": "some-value"}, map[string]string{"my-cool-annotation": "some-value"}),
|
|
syncPolicy: &v1alpha1.SyncPolicy{
|
|
ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
|
|
Labels: map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
|
|
Annotations: map[string]string{"my-cool-annotation": "some-value", "my-other-annotation": "some-other-value"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
actual, err := syncNamespace(tt.syncPolicy)(tt.managedNs, tt.liveNs)
|
|
require.NoError(t, err)
|
|
|
|
if tt.managedNs != nil {
|
|
assert.Equal(t, tt.expectedLabels, tt.managedNs.GetLabels())
|
|
assert.Equal(t, tt.expectedAnnotations, tt.managedNs.GetAnnotations())
|
|
}
|
|
|
|
assert.Equalf(t, tt.expected, actual, "syncNamespace(%v)", tt.syncPolicy)
|
|
})
|
|
}
|
|
}
|