This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a default tag configuration
Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
- Loading branch information
1 parent
fab7bbf
commit 63f4218
Showing
4 changed files
with
178 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/crossplane/crossplane-runtime/pkg/errors" | ||
"github.com/crossplane/crossplane-runtime/pkg/fieldpath" | ||
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" | ||
"github.com/crossplane/crossplane-runtime/pkg/resource/fake" | ||
"github.com/crossplane/crossplane-runtime/pkg/test" | ||
"github.com/google/go-cmp/cmp" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"testing" | ||
) | ||
|
||
const ( | ||
kind = "ACoolService" | ||
name = "example-service" | ||
provider = "ACoolProvider" | ||
) | ||
|
||
func TestTagger_Initialize(t *testing.T) { | ||
errBoom := errors.New("boom") | ||
|
||
type args struct { | ||
mg xpresource.Managed | ||
kube client.Client | ||
} | ||
type want struct { | ||
err error | ||
} | ||
cases := map[string]struct { | ||
args | ||
want | ||
}{ | ||
"Successful": { | ||
args: args{ | ||
mg: &fake.Managed{}, | ||
kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, | ||
}, | ||
want: want{ | ||
err: nil, | ||
}, | ||
}, | ||
"Failure": { | ||
args: args{ | ||
mg: &fake.Managed{}, | ||
kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, | ||
}, | ||
want: want{ | ||
err: errBoom, | ||
}, | ||
}, | ||
} | ||
for n, tc := range cases { | ||
t.Run(n, func(t *testing.T) { | ||
tagger := NewTagger(tc.kube, "tags") | ||
gotErr := tagger.Initialize(context.TODO(), tc.mg) | ||
if diff := cmp.Diff(tc.want.err, gotErr, test.EquateErrors()); diff != "" { | ||
t.Fatalf("generateTypeName(...): -want error, +got error: %s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSetExternalTagsWithPaved(t *testing.T) { | ||
type args struct { | ||
externalTags map[string]string | ||
paved *fieldpath.Paved | ||
fieldName string | ||
} | ||
type want struct { | ||
pavedString string | ||
err error | ||
} | ||
cases := map[string]struct { | ||
args | ||
want | ||
}{ | ||
"Successful": { | ||
args: args{ | ||
externalTags: map[string]string{ | ||
xpresource.ExternalResourceTagKeyKind: kind, | ||
xpresource.ExternalResourceTagKeyName: name, | ||
xpresource.ExternalResourceTagKeyProvider: provider, | ||
}, | ||
paved: fieldpath.Pave(map[string]interface{}{}), | ||
fieldName: "tags", | ||
}, | ||
want: want{ | ||
pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s"}}}}`, | ||
xpresource.ExternalResourceTagKeyKind, kind, | ||
xpresource.ExternalResourceTagKeyName, name, | ||
xpresource.ExternalResourceTagKeyProvider, provider), | ||
}, | ||
}, | ||
} | ||
for n, tc := range cases { | ||
t.Run(n, func(t *testing.T) { | ||
gotByte, gotErr := setExternalTagsWithPaved(tc.externalTags, tc.paved, tc.fieldName) | ||
if diff := cmp.Diff(tc.want.err, gotErr, test.EquateErrors()); diff != "" { | ||
t.Fatalf("generateTypeName(...): -want error, +got error: %s", diff) | ||
} | ||
if diff := cmp.Diff(tc.want.pavedString, string(gotByte), test.EquateErrors()); diff != "" { | ||
t.Fatalf("generateTypeName(...): -want gotByte, +got gotByte: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters