Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matias Charriere <[email protected]>
  • Loading branch information
mcharriere committed Oct 28, 2023
1 parent 109b06b commit d01d643
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
45 changes: 33 additions & 12 deletions cmd/template/policyexception/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
"github.com/giantswarm/micrologger"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

//nolint:staticcheck

polexdraftv1alpha1 "github.com/giantswarm/exception-recommender/api/v1alpha1"
"github.com/giantswarm/kubectl-gs/v2/test/goldenfile"
"github.com/giantswarm/kubectl-gs/v2/test/kubeclient"
)
Expand All @@ -29,6 +32,7 @@ func Test_run(t *testing.T) {
clusterName string
expectedGoldenFile string
errorMatcher func(error) bool
storage []runtime.Object
}{
{
name: "template polex",
Expand All @@ -37,6 +41,7 @@ func Test_run(t *testing.T) {
},
args: nil,
expectedGoldenFile: "run_template_polex.golden",
storage: []runtime.Object{newPolexDraft("test-app", "policy-exceptions")},
},
}

Expand All @@ -58,7 +63,7 @@ func Test_run(t *testing.T) {
stdout: out,
}

k8sClient := kubeclient.FakeK8sClient()
k8sClient := kubeclient.FakeK8sClient(tc.storage...)
err = runner.run(ctx, k8sClient.CtrlClient(), tc.args)
if tc.errorMatcher != nil {
if !tc.errorMatcher(err) {
Expand All @@ -73,17 +78,9 @@ func Test_run(t *testing.T) {
var expectedResult []byte
{
gf := goldenfile.New("testdata", tc.expectedGoldenFile)
if *update {
err = gf.Update(out.Bytes())
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
expectedResult = out.Bytes()
} else {
expectedResult, err = gf.Read()
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
expectedResult, err = gf.Read()
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
}

Expand All @@ -94,3 +91,27 @@ func Test_run(t *testing.T) {
})
}
}

func newPolexDraft(name string, namespace string) *polexdraftv1alpha1.PolicyExceptionDraft {
p := &polexdraftv1alpha1.PolicyExceptionDraft{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy.giantswarm.io",
Kind: "PolicyExceptionDraft",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: polexdraftv1alpha1.PolicyExceptionDraftSpec{
Targets: []polexdraftv1alpha1.Target{
{
Kind: "Deployment",
Names: []string{"test-app*"},
Namespaces: []string{"test-app"},
},
},
Policies: []string{"restrict-policy"},
},
}
return p
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apiVersion: policy.giantswarm.io/v1alpha1
kind: PolicyExceptionDraft
kind: PolicyException
metadata:
creationTimestamp: null
name: test-app
namespace: policy-exceptions
spec:
Expand All @@ -12,4 +13,3 @@ spec:
- test-app*
namespaces:
- test-app

3 changes: 2 additions & 1 deletion pkg/template/policyexception/policyexception.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func NewPolicyExceptionCR(polexdraft polexdraftv1alpha1.PolicyExceptionDraft) *p
APIVersion: "policy.giantswarm.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: polexdraft.GetName(),
Name: polexdraft.GetName(),
Namespace: polexdraft.GetNamespace(),
},
Spec: policyExceptionSpecDeepCopy(polexdraft.Spec),
}
Expand Down

0 comments on commit d01d643

Please sign in to comment.