Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mayursinh Sarvaiya <[email protected]>
  • Loading branch information
Marvin9 committed Dec 10, 2024
1 parent 166e713 commit d513593
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 30 deletions.
6 changes: 3 additions & 3 deletions internal/api/create_credentials_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func (s *server) CreateCredentials(
}

Check warning on line 48 in internal/api/create_credentials_v1alpha1.go

View check run for this annotation

Codecov / codecov/patch

internal/api/create_credentials_v1alpha1.go#L47-L48

Added lines #L47 - L48 were not covered by tests

switch credType {
case kargoapi.CredentialTypeLabelValueGit:
case kargoapi.CredentialTypeLabelValueHelm:
case kargoapi.CredentialTypeLabelValueImage:
case kargoapi.CredentialTypeLabelValueGit,
kargoapi.CredentialTypeLabelValueHelm,
kargoapi.CredentialTypeLabelValueImage:
creds := specificCredentials{
project: req.Msg.GetProject(),
name: req.Msg.GetName(),
Expand Down
111 changes: 84 additions & 27 deletions internal/api/create_credentials_v1alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,81 @@ import (
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/api/config"
"github.com/akuity/kargo/internal/api/kubernetes"
libCreds "github.com/akuity/kargo/internal/credentials"
svcv1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1"
)

func TestCreateCredentials(t *testing.T) {
t.Run("create generic secret", func(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := context.Background()

cfg := config.ServerConfigFromEnv()
cfg := config.ServerConfigFromEnv()

cfg.SecretManagementEnabled = true
cfg.SecretManagementEnabled = true

client, err := kubernetes.NewClient(ctx, &rest.Config{}, kubernetes.ClientOptions{
SkipAuthorization: true,
NewInternalClient: func(_ context.Context, _ *rest.Config, s *runtime.Scheme) (client.Client, error) {
return fake.NewClientBuilder().
WithScheme(s).
WithObjects(
mustNewObject[corev1.Namespace]("testdata/namespace.yaml"),
mustNewObject[corev1.Namespace]("testdata/stage.yaml"),
).Build(), nil
},
})
cl, err := kubernetes.NewClient(ctx, &rest.Config{}, kubernetes.ClientOptions{
SkipAuthorization: true,
NewInternalClient: func(_ context.Context, _ *rest.Config, s *runtime.Scheme) (client.Client, error) {
return fake.NewClientBuilder().
WithScheme(s).
WithObjects(
mustNewObject[corev1.Namespace]("testdata/namespace.yaml"),
mustNewObject[corev1.Namespace]("testdata/stage.yaml"),
).Build(), nil
},
})

require.NoError(t, err)

s := &server{
client: cl,
cfg: cfg,
}

t.Run("create repo secret", func(t *testing.T) {
t.Parallel()

resp, err := s.CreateCredentials(ctx, connect.NewRequest(&svcv1alpha1.CreateCredentialsRequest{
Project: "kargo-demo",
Name: "repo",
Description: "my repo secret",
Type: "git",
RepoUrl: "https://github.com/foo/bar",
Username: "username",
Password: "password",
}))

require.NoError(t, err)

s := &server{
client: client,
cfg: cfg,
}
respSecret := resp.Msg.GetCredentials()

assert.Equal(t, "kargo-demo", respSecret.Namespace)
assert.Equal(t, "repo", respSecret.ObjectMeta.Name)
assert.Equal(t, "my repo secret", respSecret.ObjectMeta.Annotations[kargoapi.AnnotationKeyDescription])
assert.Equal(t, "https://github.com/foo/bar", respSecret.StringData[libCreds.FieldRepoURL])
assert.Equal(t, "username", respSecret.StringData[libCreds.FieldUsername])
assert.Equal(t, redacted, respSecret.StringData[libCreds.FieldPassword])

kubernetesSecret := corev1.Secret{}

require.NoError(t, cl.Get(ctx, types.NamespacedName{
Namespace: "kargo-demo",
Name: "repo",
}, &kubernetesSecret),
)

d := kubernetesSecret.DeepCopy().Data

assert.Equal(t, "kargo-demo", kubernetesSecret.Namespace)
assert.Equal(t, "repo", kubernetesSecret.ObjectMeta.Name)
assert.Equal(t, "my repo secret", kubernetesSecret.ObjectMeta.Annotations[kargoapi.AnnotationKeyDescription])
assert.Equal(t, "https://github.com/foo/bar", string(d[libCreds.FieldRepoURL]))
assert.Equal(t, "username", string(d[libCreds.FieldUsername]))
assert.Equal(t, "password", string(d[libCreds.FieldPassword]))
})

t.Run("create generic secret", func(t *testing.T) {
t.Parallel()

resp, err := s.CreateCredentials(ctx, connect.NewRequest(&svcv1alpha1.CreateCredentialsRequest{
Project: "kargo-demo",
Expand All @@ -64,26 +108,39 @@ func TestCreateCredentials(t *testing.T) {

respSecret := resp.Msg.GetCredentials()

assert.Equal(t, respSecret.Namespace, "kargo-demo")
assert.Equal(t, respSecret.ObjectMeta.Name, "external")
assert.Equal(t, respSecret.ObjectMeta.Annotations[kargoapi.AnnotationKeyDescription], "my external secret")
assert.Equal(t, "kargo-demo", respSecret.Namespace)
assert.Equal(t, "external", respSecret.ObjectMeta.Name)
assert.Equal(t, "my external secret", respSecret.ObjectMeta.Annotations[kargoapi.AnnotationKeyDescription])
assert.Equal(t, redacted, respSecret.StringData["TOKEN_1"])
assert.Equal(t, redacted, respSecret.StringData["TOKEN_2"])

kubernetesSecret := corev1.Secret{}

require.NoError(t, client.Get(ctx, types.NamespacedName{
require.NoError(t, cl.Get(ctx, types.NamespacedName{
Namespace: "kargo-demo",
Name: "external",
}, &kubernetesSecret),
)

d := kubernetesSecret.DeepCopy().Data

assert.Equal(t, kubernetesSecret.Namespace, "kargo-demo")
assert.Equal(t, kubernetesSecret.ObjectMeta.Name, "external")
assert.Equal(t, kubernetesSecret.ObjectMeta.Annotations[kargoapi.AnnotationKeyDescription], "my external secret")
assert.Equal(t, "kargo-demo", kubernetesSecret.Namespace)
assert.Equal(t, "external", kubernetesSecret.ObjectMeta.Name)
assert.Equal(t, "my external secret", kubernetesSecret.ObjectMeta.Annotations[kargoapi.AnnotationKeyDescription])
assert.Equal(t, "foo", string(d["TOKEN_1"]))
assert.Equal(t, "bar", string(d["TOKEN_2"]))
})

t.Run("invalid secret", func(t *testing.T) {
t.Parallel()

_, err := s.CreateCredentials(ctx, connect.NewRequest(&svcv1alpha1.CreateCredentialsRequest{
Project: "kargo-demo",
Name: "invalid",
Description: "my invalid secret",
Type: "invalid",
}))

require.Error(t, err)
})
}

0 comments on commit d513593

Please sign in to comment.