Files
deploy/util/settings/settings_informer_test.go
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
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]>
2026-07-28 21:16:24 -07:00

256 lines
7.9 KiB
Go

package settings
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"github.com/hanzoai/cd/common"
)
// newChangeTrackingManager returns a SettingsManager whose onRepoOrClusterChanged
// callback signals the returned channel. onRepoOrClusterChanged dispatches the
// callback in a goroutine, so use waitFired/assertNotFired to observe it.
func newChangeTrackingManager(t *testing.T) (*SettingsManager, <-chan struct{}) {
t.Helper()
fired := make(chan struct{}, 16)
mgr := NewSettingsManager(t.Context(), fake.NewClientset(), "cd", WithRepoOrClusterChangedHandler(func() {
fired <- struct{}{}
}))
return mgr, fired
}
func waitFired(t *testing.T, ch <-chan struct{}) {
t.Helper()
select {
case <-ch:
case <-time.After(2 * time.Second):
t.Fatal("expected onRepoOrClusterChanged to fire, but it did not")
}
}
func assertNotFired(t *testing.T, ch <-chan struct{}) {
t.Helper()
select {
case <-ch:
t.Fatal("onRepoOrClusterChanged fired but was not expected to")
case <-time.After(200 * time.Millisecond):
}
}
func argoCDConfigMap() *corev1.ConfigMap {
return &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: common.ArgoCDConfigMapName, Namespace: "cd"}}
}
func repositorySecret() *corev1.Secret {
return &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
Name: "repo-secret",
Namespace: "cd",
Labels: map[string]string{common.LabelKeySecretType: common.LabelValueSecretTypeRepository},
}}
}
func TestArgoCDConfigMapEventHandler(t *testing.T) {
t.Run("add/update/delete of cd-cm triggers a change", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.argoCDConfigMapEventHandler()
h.OnAdd(argoCDConfigMap(), false)
waitFired(t, fired)
h.OnUpdate(argoCDConfigMap(), argoCDConfigMap())
waitFired(t, fired)
h.OnDelete(argoCDConfigMap())
waitFired(t, fired)
})
t.Run("a different configmap does not trigger a change", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.argoCDConfigMapEventHandler()
other := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "cd-rbac-cm", Namespace: "cd"}}
h.OnAdd(other, false)
h.OnUpdate(other, other)
h.OnDelete(other)
assertNotFired(t, fired)
})
t.Run("delete unwraps a tombstone wrapping cd-cm", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.argoCDConfigMapEventHandler()
h.OnDelete(cache.DeletedFinalStateUnknown{Key: "cd/cd-cm", Obj: argoCDConfigMap()})
waitFired(t, fired)
})
t.Run("delete of a tombstone wrapping nil or an unexpected type is ignored", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.argoCDConfigMapEventHandler()
assert.NotPanics(t, func() {
h.OnDelete(cache.DeletedFinalStateUnknown{Key: "cd/gone", Obj: nil})
h.OnDelete("not-a-configmap")
h.OnAdd("not-a-configmap", false)
})
assertNotFired(t, fired)
})
}
func TestRepositorySecretEventHandler(t *testing.T) {
t.Run("add/update/delete of a repository secret triggers a change", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.repositorySecretEventHandler()
h.OnAdd(repositorySecret(), false)
waitFired(t, fired)
h.OnUpdate(repositorySecret(), repositorySecret())
waitFired(t, fired)
h.OnDelete(repositorySecret())
waitFired(t, fired)
})
t.Run("a non-repository secret does not trigger a change", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.repositorySecretEventHandler()
plain := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "plain", Namespace: "cd"}}
h.OnAdd(plain, false)
h.OnUpdate(plain, plain)
h.OnDelete(plain)
assertNotFired(t, fired)
})
t.Run("delete unwraps a tombstone wrapping a repository secret", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.repositorySecretEventHandler()
h.OnDelete(cache.DeletedFinalStateUnknown{Key: "cd/repo-secret", Obj: repositorySecret()})
waitFired(t, fired)
})
t.Run("tombstone wrapping a non-repository object or unexpected types are ignored", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.repositorySecretEventHandler()
assert.NotPanics(t, func() {
h.OnDelete(cache.DeletedFinalStateUnknown{Key: "cd/cm", Obj: &corev1.ConfigMap{}})
h.OnDelete(cache.DeletedFinalStateUnknown{Key: "cd/gone", Obj: nil})
h.OnAdd("not-a-secret", false)
})
assertNotFired(t, fired)
})
}
func TestClusterSecretEventHandler(t *testing.T) {
// The cluster informer is pre-filtered to secret-type=cluster, so every event is
// expected to trigger a change unconditionally.
t.Run("add/update/delete always triggers a change", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.clusterSecretEventHandler()
secret := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
Name: "cluster-secret",
Namespace: "cd",
Labels: map[string]string{common.LabelKeySecretType: common.LabelValueSecretTypeCluster},
}}
h.OnAdd(secret, false)
waitFired(t, fired)
h.OnUpdate(secret, secret)
waitFired(t, fired)
h.OnDelete(secret)
waitFired(t, fired)
})
t.Run("delete unwraps a tombstone and still triggers a change without panicking", func(t *testing.T) {
mgr, fired := newChangeTrackingManager(t)
h := mgr.clusterSecretEventHandler()
assert.NotPanics(t, func() {
h.OnDelete(cache.DeletedFinalStateUnknown{Key: "cd/gone", Obj: nil})
})
waitFired(t, fired)
})
}
func TestSettingsNotificationEventHandler(t *testing.T) {
const partOfLabel = "app.kubernetes.io/part-of"
settingsObject := func(creation time.Time, resourceVersion string) *corev1.Secret {
return &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
Name: "cd-secret",
Namespace: "cd",
Labels: map[string]string{partOfLabel: "cd"},
CreationTimestamp: metav1.NewTime(creation),
ResourceVersion: resourceVersion,
}}
}
t.Run("add of a settings object created after the cutoff notifies", func(t *testing.T) {
now := time.Now()
count := 0
h := settingsNotificationEventHandler(now, func() { count++ })
h.OnAdd(settingsObject(now.Add(time.Hour), "1"), false)
assert.Equal(t, 1, count)
})
t.Run("add of a settings object created before the cutoff does not notify", func(t *testing.T) {
now := time.Now()
count := 0
h := settingsNotificationEventHandler(now, func() { count++ })
h.OnAdd(settingsObject(now.Add(-time.Hour), "1"), false)
assert.Zero(t, count)
})
t.Run("add of a non-settings object or unexpected type does not notify", func(t *testing.T) {
now := time.Now()
count := 0
h := settingsNotificationEventHandler(now, func() { count++ })
nonSettings := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "plain", CreationTimestamp: metav1.NewTime(now.Add(time.Hour))}}
assert.NotPanics(t, func() {
h.OnAdd(nonSettings, false)
h.OnAdd("not-an-object", false)
})
assert.Zero(t, count)
})
t.Run("update notifies only when the resource version changes", func(t *testing.T) {
now := time.Now()
count := 0
h := settingsNotificationEventHandler(now, func() { count++ })
h.OnUpdate(settingsObject(now, "1"), settingsObject(now, "2"))
assert.Equal(t, 1, count)
h.OnUpdate(settingsObject(now, "2"), settingsObject(now, "2"))
assert.Equal(t, 1, count, "unchanged resource version must not notify")
})
t.Run("update of a non-settings object or unexpected type does not notify", func(t *testing.T) {
now := time.Now()
count := 0
h := settingsNotificationEventHandler(now, func() { count++ })
nonSettings := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "plain", ResourceVersion: "2"}}
assert.NotPanics(t, func() {
// new object is not a settings object
h.OnUpdate(settingsObject(now, "1"), nonSettings)
// old object is not a typed metadata object
h.OnUpdate("not-an-object", settingsObject(now, "2"))
})
assert.Zero(t, count)
})
}