Files
zeekayandHanzo Dev cbf668a980 Build on Go 1.27
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]>
2026-09-17 01:47:46 -07:00

149 lines
3.3 KiB
Go

package utils
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
)
func TestGetSecretRef(t *testing.T) {
t.Parallel()
secret := &corev1.Secret{
Name: "test-secret", Namespace: "test",
Data: map[string][]byte{
"my-token": []byte("secret"),
},
}
client := fake.NewClientBuilder().WithObjects(secret).Build()
ctx := t.Context()
cases := []struct {
name, namespace, token string
ref *v1alpha1.SecretRef
hasError bool
}{
{
name: "valid ref",
ref: &v1alpha1.SecretRef{SecretName: "test-secret", Key: "my-token"},
namespace: "test",
token: "secret",
hasError: false,
},
{
name: "nil ref",
ref: nil,
namespace: "test",
token: "",
hasError: false,
},
{
name: "wrong name",
ref: &v1alpha1.SecretRef{SecretName: "other", Key: "my-token"},
namespace: "test",
token: "",
hasError: true,
},
{
name: "wrong key",
ref: &v1alpha1.SecretRef{SecretName: "test-secret", Key: "other-token"},
namespace: "test",
token: "",
hasError: true,
},
{
name: "wrong namespace",
ref: &v1alpha1.SecretRef{SecretName: "test-secret", Key: "my-token"},
namespace: "other",
token: "",
hasError: true,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Parallel()
token, err := GetSecretRef(ctx, client, c.ref, c.namespace, false)
if c.hasError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
assert.Equal(t, c.token, token)
})
}
}
func TestGetConfigMapData(t *testing.T) {
t.Parallel()
configMap := &corev1.ConfigMap{
Name: "test-configmap", Namespace: "test",
Data: map[string]string{
"my-data": "configmap-data",
},
}
client := fake.NewClientBuilder().WithObjects(configMap).Build()
ctx := t.Context()
cases := []struct {
name, namespace, data string
ref *v1alpha1.ConfigMapKeyRef
hasError bool
}{
{
name: "valid ref",
ref: &v1alpha1.ConfigMapKeyRef{ConfigMapName: "test-configmap", Key: "my-data"},
namespace: "test",
data: "configmap-data",
hasError: false,
},
{
name: "nil ref",
ref: nil,
namespace: "test",
data: "",
hasError: false,
},
{
name: "wrong name",
ref: &v1alpha1.ConfigMapKeyRef{ConfigMapName: "other", Key: "my-data"},
namespace: "test",
data: "",
hasError: true,
},
{
name: "wrong key",
ref: &v1alpha1.ConfigMapKeyRef{ConfigMapName: "test-configmap", Key: "other-data"},
namespace: "test",
data: "",
hasError: true,
},
{
name: "wrong namespace",
ref: &v1alpha1.ConfigMapKeyRef{ConfigMapName: "test-configmap", Key: "my-data"},
namespace: "other",
data: "",
hasError: true,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Parallel()
data, err := GetConfigMapData(ctx, client, c.ref, c.namespace)
if c.hasError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
if !c.hasError {
assert.Equal(t, c.data, string(data))
}
})
}
}