Skip to content

Commit

Permalink
feat: add tests for secret references
Browse files Browse the repository at this point in the history
Signed-off-by: Dario Zachow <dario.zachow@deutschebahn.com>
  • Loading branch information
dariozachow committed Aug 22, 2024
1 parent c81f75a commit 6fcb3f8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 36 deletions.
10 changes: 8 additions & 2 deletions pkg/clients/projects/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,15 @@ func getTokenValueFromSecret(p *v1alpha1.HookParameters, client client.Client, c

}

value := string(secret.Data[p.Token.SecretRef.Key])
value := secret.Data[p.Token.SecretRef.Key]

return &value, nil
if value == nil {
return nil, errors.Errorf("Could not find key %v in the referenced secret", p.Token.SecretRef.Key)
}

data := string(value)

return &data, nil
}

// GenerateEditHookOptions generates project edit options
Expand Down
125 changes: 95 additions & 30 deletions pkg/clients/projects/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"time"

v1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"github.com/xanzy/go-gitlab"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -138,10 +140,15 @@ func TestLateInitializeHook(t *testing.T) {
func TestGenerateCreateHookOptions(t *testing.T) {
type args struct {
parameters *v1alpha1.HookParameters
secret *corev1.Secret
}
type want struct {
addProjectHookOptions *gitlab.AddProjectHookOptions
err error
}
cases := map[string]struct {
args args
want *gitlab.AddProjectHookOptions
want want
}{
"AllFields": {
args: args{
Expand All @@ -160,22 +167,31 @@ func TestGenerateCreateHookOptions(t *testing.T) {
WikiPageEvents: &wikiPageEvents,
EnableSSLVerification: &enableSSLVerification,
Token: &token},
secret: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Namespace: "test", Name: "test"},
Data: map[string][]byte{
"token": []byte(tokenValue),
},
},
},
want: &gitlab.AddProjectHookOptions{
URL: &url,
ConfidentialNoteEvents: &confidentialNoteEvents,
PushEvents: &pushEvents,
PushEventsBranchFilter: &pushEventsBranchFilter,
IssuesEvents: &issuesEvents,
ConfidentialIssuesEvents: &confidentialIssuesEvents,
MergeRequestsEvents: &mergeRequestsEvents,
TagPushEvents: &tagPushEvents,
NoteEvents: &noteEvents,
JobEvents: &jobEvents,
PipelineEvents: &pipelineEvents,
WikiPageEvents: &wikiPageEvents,
EnableSSLVerification: &enableSSLVerification,
Token: &tokenValue,
want: want{
err: nil,
addProjectHookOptions: &gitlab.AddProjectHookOptions{
URL: &url,
ConfidentialNoteEvents: &confidentialNoteEvents,
PushEvents: &pushEvents,
PushEventsBranchFilter: &pushEventsBranchFilter,
IssuesEvents: &issuesEvents,
ConfidentialIssuesEvents: &confidentialIssuesEvents,
MergeRequestsEvents: &mergeRequestsEvents,
TagPushEvents: &tagPushEvents,
NoteEvents: &noteEvents,
JobEvents: &jobEvents,
PipelineEvents: &pipelineEvents,
WikiPageEvents: &wikiPageEvents,
EnableSSLVerification: &enableSSLVerification,
Token: &tokenValue,
},
},
},
"SomeFields": {
Expand All @@ -186,27 +202,76 @@ func TestGenerateCreateHookOptions(t *testing.T) {
IssuesEvents: &issuesEvents,
Token: &token,
},
secret: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Namespace: "test", Name: "test"},
Data: map[string][]byte{
"token": []byte(tokenValue),
},
},
},
want: &gitlab.AddProjectHookOptions{
PushEvents: &pushEvents,
PushEventsBranchFilter: &pushEventsBranchFilter,
IssuesEvents: &issuesEvents,
Token: &tokenValue,
want: want{
err: nil,
addProjectHookOptions: &gitlab.AddProjectHookOptions{
PushEvents: &pushEvents,
PushEventsBranchFilter: &pushEventsBranchFilter,
IssuesEvents: &issuesEvents,
Token: &tokenValue,
},
},
},
"FailNoSecret": {
args: args{
parameters: &v1alpha1.HookParameters{
PushEvents: &pushEvents,
PushEventsBranchFilter: &pushEventsBranchFilter,
IssuesEvents: &issuesEvents,
Token: &token,
},
secret: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Namespace: "other", Name: "other"},
Data: map[string][]byte{
"token": []byte(tokenValue),
},
},
},
want: want{
err: errors.New(`Cannot get referenced Secret: secrets "test" not found`),
addProjectHookOptions: nil,
},
},
"FailWrongKey": {
args: args{
parameters: &v1alpha1.HookParameters{
PushEvents: &pushEvents,
PushEventsBranchFilter: &pushEventsBranchFilter,
IssuesEvents: &issuesEvents,
Token: &token,
},
secret: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Namespace: "test", Name: "test"},
Data: map[string][]byte{
"wrongKey": []byte(tokenValue),
},
},
},
want: want{
err: errors.New("Could not find key token in the referenced secret"),
addProjectHookOptions: nil,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Namespace: "test", Name: "test"},
Data: map[string][]byte{
"token": []byte(tokenValue),
},
}
client := fake.NewClientBuilder().WithObjects(secret).Build()

got, _ := GenerateCreateHookOptions(tc.args.parameters, client, context.Background())
if diff := cmp.Diff(tc.want, got); diff != "" {
client := fake.NewClientBuilder().WithObjects(tc.args.secret).Build()

got, err := GenerateCreateHookOptions(tc.args.parameters, client, context.Background())
if err != nil && tc.want.err != nil {
if diff := cmp.Diff(tc.want.err.Error(), err.Error(), test.EquateErrors()); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
}
if diff := cmp.Diff(tc.want.addProjectHookOptions, got); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
})
Expand Down
6 changes: 2 additions & 4 deletions pkg/controller/projects/hooks/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ func withDefaultValues() projectHookModifier {
EnableSSLVerification: &f,
Token: &v1alpha1.Token{
SecretRef: &v1.SecretKeySelector{
Key: "test", SecretReference: v1.SecretReference{Name: "test", Namespace: "test"},
Key: "token", SecretReference: v1.SecretReference{Name: "test", Namespace: "test"},
},
},
}
// s, _ := json.MarshalIndent(ph.Spec.ForProvider, "", "\t")
// fmt.Println(string(s))
}
}

Expand All @@ -105,7 +103,7 @@ func withTokenRef() projectHookModifier {
return func(r *v1alpha1.Hook) {
r.Spec.ForProvider.Token = &v1alpha1.Token{
SecretRef: &v1.SecretKeySelector{
Key: "test", SecretReference: v1.SecretReference{Name: "test", Namespace: "test"},
Key: "token", SecretReference: v1.SecretReference{Name: "test", Namespace: "test"},
},
}
}
Expand Down

0 comments on commit 6fcb3f8

Please sign in to comment.