mirror of
https://github.com/hanzoai/cd.git
synced 2026-10-30 14:56:45 +00:00
go fix applied the modernizers: promoted-field keys in struct literals,
strings.Cut, SplitSeq, new(expr), slices and maps helpers, wg.Go, reflect.TypeFor
and errors.AsType. omitzero is left out, since omitting a zero struct such as
time.Time{} is a per-field wire decision and not a mechanical one.
Two changes the modernizers could not make for themselves. AsType needs its type
argument to be an error, so the git exit-status reader embeds error in the
interface it asks for. And an error built by concatenating a tag list into its
own format string now says so with a verb, which is what vet was pointing at.
Co-authored-by: Hanzo Dev <[email protected]>
190 lines
6.3 KiB
Go
190 lines
6.3 KiB
Go
//go:build !race
|
|
|
|
package db
|
|
|
|
import (
|
|
"testing"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"github.com/hanzoai/cd/common"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/cd/util/settings"
|
|
)
|
|
|
|
func TestWatchClusters_CreateRemoveCluster(t *testing.T) {
|
|
// !race:
|
|
// Intermittent failure when running TestWatchClusters_LocalClusterModifications with -race, likely due to race condition
|
|
// https://github.com/argoproj/argo-cd/issues/4755
|
|
emptyConfigMap := &corev1.ConfigMap{
|
|
Name: common.ConfigMapName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string]string{},
|
|
}
|
|
cdSecret := &corev1.Secret{
|
|
Name: common.SecretName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string][]byte{
|
|
"admin.password": nil,
|
|
"server.secretkey": nil,
|
|
},
|
|
}
|
|
kubeclientset := fake.NewClientset(emptyConfigMap, cdSecret)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), kubeclientset, fakeNamespace)
|
|
db := NewDB(fakeNamespace, settingsManager, kubeclientset)
|
|
completed := runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
|
|
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
|
|
assert.Nil(t, old)
|
|
assert.Equal(t, v1alpha1.KubernetesInternalAPIServerAddr, new.Server)
|
|
|
|
_, err := db.CreateCluster(t.Context(), &v1alpha1.Cluster{
|
|
Server: "https://minikube",
|
|
Name: "minikube",
|
|
})
|
|
assert.NoError(t, err)
|
|
},
|
|
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
|
|
assert.Nil(t, old)
|
|
assert.Equal(t, "https://minikube", new.Server)
|
|
assert.Equal(t, "minikube", new.Name)
|
|
|
|
assert.NoError(t, db.DeleteCluster(t.Context(), "https://minikube"))
|
|
},
|
|
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
|
|
assert.Nil(t, new)
|
|
assert.Equal(t, "https://minikube", old.Server)
|
|
},
|
|
})
|
|
assert.True(t, completed, "Failed due to timeout")
|
|
}
|
|
|
|
func TestWatchClusters_LocalClusterModifications(t *testing.T) {
|
|
// !race:
|
|
// Intermittent failure when running TestWatchClusters_LocalClusterModifications with -race, likely due to race condition
|
|
// https://github.com/argoproj/argo-cd/issues/4755
|
|
emptyConfigMap := &corev1.ConfigMap{
|
|
Name: common.ConfigMapName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string]string{},
|
|
}
|
|
cdSecret := &corev1.Secret{
|
|
Name: common.SecretName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string][]byte{
|
|
"admin.password": nil,
|
|
"server.secretkey": nil,
|
|
},
|
|
}
|
|
kubeclientset := fake.NewClientset(emptyConfigMap, cdSecret)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), kubeclientset, fakeNamespace)
|
|
db := NewDB(fakeNamespace, settingsManager, kubeclientset)
|
|
completed := runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
|
|
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
|
|
assert.Nil(t, old)
|
|
assert.Equal(t, v1alpha1.KubernetesInternalAPIServerAddr, new.Server)
|
|
|
|
_, err := db.CreateCluster(t.Context(), &v1alpha1.Cluster{
|
|
Server: v1alpha1.KubernetesInternalAPIServerAddr,
|
|
Name: "some name",
|
|
})
|
|
assert.NoError(t, err)
|
|
},
|
|
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
|
|
assert.NotNil(t, old)
|
|
assert.Equal(t, v1alpha1.KubernetesInternalAPIServerAddr, new.Server)
|
|
assert.Equal(t, "some name", new.Name)
|
|
|
|
assert.NoError(t, db.DeleteCluster(t.Context(), v1alpha1.KubernetesInternalAPIServerAddr))
|
|
},
|
|
func(_ *v1alpha1.Cluster, new *v1alpha1.Cluster) {
|
|
assert.Equal(t, v1alpha1.KubernetesInternalAPIServerAddr, new.Server)
|
|
assert.Equal(t, "in-cluster", new.Name)
|
|
},
|
|
})
|
|
assert.True(t, completed, "Failed due to timeout")
|
|
}
|
|
|
|
func TestWatchClusters_MissingServerSecretKey(t *testing.T) {
|
|
// !race:
|
|
// Intermittent failure when running with -race, likely due to race condition
|
|
// https://github.com/argoproj/argo-cd/issues/4755
|
|
emptyConfigMap := &corev1.ConfigMap{
|
|
Name: common.ConfigMapName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string]string{},
|
|
}
|
|
secretWithoutSecretKey := &corev1.Secret{
|
|
Name: common.SecretName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string][]byte{
|
|
"admin.password": nil,
|
|
},
|
|
}
|
|
kubeclientset := fake.NewClientset(emptyConfigMap, secretWithoutSecretKey)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), kubeclientset, fakeNamespace)
|
|
db := NewDB(fakeNamespace, settingsManager, kubeclientset)
|
|
completed := runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
|
|
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
|
|
assert.Nil(t, old)
|
|
assert.Equal(t, v1alpha1.KubernetesInternalAPIServerAddr, new.Server)
|
|
},
|
|
})
|
|
assert.True(t, completed, "WatchClusters should work even when server.secretkey is missing")
|
|
}
|
|
|
|
func TestWatchClusters_LocalClusterModificationsWhenDisabled(t *testing.T) {
|
|
// !race:
|
|
// Intermittent failure when running TestWatchClusters_LocalClusterModifications with -race, likely due to race condition
|
|
// https://github.com/argoproj/argo-cd/issues/4755
|
|
configMapWithInClusterServerAddressDisabled := &corev1.ConfigMap{
|
|
Name: common.ConfigMapName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string]string{"cluster.inClusterEnabled": "false"},
|
|
}
|
|
cdSecret := &corev1.Secret{
|
|
Name: common.SecretName,
|
|
Namespace: fakeNamespace,
|
|
Labels: map[string]string{
|
|
"app.kubernetes.io/part-of": "cd",
|
|
},
|
|
Data: map[string][]byte{
|
|
"admin.password": nil,
|
|
"server.secretkey": nil,
|
|
},
|
|
}
|
|
kubeclientset := fake.NewClientset(configMapWithInClusterServerAddressDisabled, cdSecret)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), kubeclientset, fakeNamespace)
|
|
db := NewDB(fakeNamespace, settingsManager, kubeclientset)
|
|
completed := runWatchTest(t, db, []func(_ *v1alpha1.Cluster, _ *v1alpha1.Cluster){
|
|
func(_ *v1alpha1.Cluster, _ *v1alpha1.Cluster) {
|
|
assert.Fail(t, "The in-cluster should not be added when disabled")
|
|
},
|
|
})
|
|
assert.False(t, completed, "Expecting the method to never complete because no cluster is ever added")
|
|
}
|