Skip to content

Commit 7e63e60

Browse files
committed
rework tests
Signed-off-by: Matias Charriere <matias@giantswarm.io>
1 parent 515e6f7 commit 7e63e60

File tree

5 files changed

+64
-51
lines changed

5 files changed

+64
-51
lines changed

cmd/template/policyexception/runner.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"github.com/spf13/cobra"
1212
"sigs.k8s.io/yaml"
1313

14+
polexdraftv1alpha1 "github.com/giantswarm/exception-recommender/api/v1alpha1"
1415
"github.com/giantswarm/kubectl-gs/v2/pkg/commonconfig"
1516
template "github.com/giantswarm/kubectl-gs/v2/pkg/template/policyexception"
17+
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
1618
)
1719

1820
type runner struct {
@@ -30,26 +32,27 @@ func (r *runner) Run(cmd *cobra.Command, args []string) error {
3032
if err != nil {
3133
return microerror.Mask(err)
3234
}
33-
err = r.run(ctx, cmd, args)
35+
36+
client, err := r.commonConfig.GetClient(r.logger)
37+
if err != nil {
38+
return microerror.Mask(err)
39+
}
40+
41+
err = r.run(ctx, client.CtrlClient(), args)
3442
if err != nil {
3543
return microerror.Mask(err)
3644
}
3745

3846
return nil
3947
}
4048

41-
func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) error {
49+
func (r *runner) run(ctx context.Context, client runtimeclient.Client, args []string) error {
4250
var err error
4351

4452
config := template.Config{
4553
Name: r.flag.Draft,
4654
}
4755

48-
client, err := r.commonConfig.GetClient(r.logger)
49-
if err != nil {
50-
return microerror.Mask(err)
51-
}
52-
5356
var outputWriter io.Writer
5457
{
5558
if r.flag.Output == "" {
@@ -65,11 +68,14 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err
6568
}
6669
}
6770

68-
policyexceptionCR, err := template.NewPolicyExceptionCR(config, client.CtrlClient())
71+
polexdraft := polexdraftv1alpha1.PolicyExceptionDraft{}
72+
err = client.Get(ctx, runtimeclient.ObjectKey{Namespace: "policy-exceptions", Name: config.Name}, &polexdraft)
6973
if err != nil {
7074
return microerror.Mask(err)
7175
}
7276

77+
policyexceptionCR := template.NewPolicyExceptionCR(polexdraft)
78+
7379
policyexceptionCRYaml, err := yaml.Marshal(policyexceptionCR)
7480
if err != nil {
7581
return microerror.Mask(err)

cmd/template/policyexception/runner_test.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,64 @@ package policyexception
22

33
import (
44
"bytes"
5+
"context"
56
goflag "flag"
67
"testing"
78

9+
"github.com/giantswarm/micrologger"
810
"github.com/google/go-cmp/cmp"
911
"github.com/pkg/errors"
1012

13+
//nolint:staticcheck
14+
1115
"github.com/giantswarm/kubectl-gs/v2/test/goldenfile"
16+
"github.com/giantswarm/kubectl-gs/v2/test/kubeclient"
1217
)
1318

1419
var update = goflag.Bool("update", false, "update .golden reference test files")
1520

16-
// TestRunner_Run uses golden files.
21+
// Test_run uses golden files.
1722
//
18-
// go test ./cmd/template/organization -run TestRunner_Run -update
19-
func TestRunner_Run(t *testing.T) {
23+
// go test ./cmd/template/policyexception -run Test_run -update
24+
func Test_run(t *testing.T) {
2025
testCases := []struct {
2126
name string
22-
flag *flag
27+
flags *flag
28+
args []string
29+
clusterName string
2330
expectedGoldenFile string
2431
errorMatcher func(error) bool
2532
}{
2633
{
27-
name: "",
28-
flag: &flag{},
29-
errorMatcher: IsInvalidFlag,
30-
},
31-
{
32-
name: "",
33-
flag: &flag{
34-
Draft: "example",
35-
Output: "",
34+
name: "template polex",
35+
flags: &flag{
36+
Draft: "test-app",
3637
},
37-
expectedGoldenFile: "run_with_name.golden",
38+
args: nil,
39+
expectedGoldenFile: "run_template_polex.golden",
3840
},
3941
}
4042

4143
for _, tc := range testCases {
4244
t.Run(tc.name, func(t *testing.T) {
45+
ctx := context.Background()
46+
4347
out := new(bytes.Buffer)
48+
// tc.flags.print = genericclioptions.NewPrintFlags("").WithDefaultOutput(output.TypeDefault)
4449

45-
r := &runner{
46-
flag: tc.flag,
50+
logger, err := micrologger.New(micrologger.Config{})
51+
if err != nil {
52+
t.Fatalf("failed to create logger: %s", err.Error())
53+
}
54+
55+
runner := &runner{
56+
flag: tc.flags,
57+
logger: logger,
4758
stdout: out,
4859
}
4960

50-
err := r.Run(nil, nil)
61+
k8sClient := kubeclient.FakeK8sClient()
62+
err = runner.run(ctx, k8sClient.CtrlClient(), tc.args)
5163
if tc.errorMatcher != nil {
5264
if !tc.errorMatcher(err) {
5365
t.Fatalf("error not matching expected matcher, got: %s", errors.Cause(err))
@@ -62,11 +74,11 @@ func TestRunner_Run(t *testing.T) {
6274
{
6375
gf := goldenfile.New("testdata", tc.expectedGoldenFile)
6476
if *update {
65-
expectedResult = out.Bytes()
66-
err = gf.Update(expectedResult)
77+
err = gf.Update(out.Bytes())
6778
if err != nil {
6879
t.Fatalf("unexpected error: %s", err.Error())
6980
}
81+
expectedResult = out.Bytes()
7082
} else {
7183
expectedResult, err = gf.Read()
7284
if err != nil {
@@ -77,7 +89,7 @@ func TestRunner_Run(t *testing.T) {
7789

7890
diff := cmp.Diff(string(expectedResult), out.String())
7991
if diff != "" {
80-
t.Fatalf("value not expected, got:\n %s", diff)
92+
t.Fatalf("no difference from golden file %s expected, got:\n %s", tc.expectedGoldenFile, diff)
8193
}
8294
})
8395
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: policy.giantswarm.io/v1alpha1
2+
kind: PolicyExceptionDraft
3+
metadata:
4+
name: test-app
5+
namespace: policy-exceptions
6+
spec:
7+
policies:
8+
- restrict-policy
9+
targets:
10+
- kind: Deployment
11+
names:
12+
- test-app*
13+
namespaces:
14+
- test-app
15+

cmd/template/policyexception/testdata/run_with_name.golden

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/template/policyexception/policyexception.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
11
package policyexception
22

33
import (
4-
"context"
5-
6-
"github.com/giantswarm/microerror"
7-
84
polexdraftv1alpha1 "github.com/giantswarm/exception-recommender/api/v1alpha1"
95
polexv1alpha1 "github.com/giantswarm/kyverno-policy-operator/api/v1alpha1"
106
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11-
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
127
)
138

149
type Config struct {
1510
Name string
1611
}
1712

18-
func NewPolicyExceptionCR(config Config, client runtimeclient.Client) (*polexv1alpha1.PolicyException, error) {
19-
ctx := context.Background()
20-
21-
polexdraft := &polexdraftv1alpha1.PolicyExceptionDraft{}
22-
err := client.Get(ctx, runtimeclient.ObjectKey{Namespace: "policy-exceptions", Name: config.Name}, polexdraft)
23-
if err != nil {
24-
return nil, microerror.Mask(err)
25-
}
26-
13+
func NewPolicyExceptionCR(polexdraft polexdraftv1alpha1.PolicyExceptionDraft) *polexv1alpha1.PolicyException {
2714
polexCR := &polexv1alpha1.PolicyException{
2815
TypeMeta: metav1.TypeMeta{
2916
Kind: "PolicyException",
3017
APIVersion: "policy.giantswarm.io/v1alpha1",
3118
},
3219
ObjectMeta: metav1.ObjectMeta{
33-
Name: config.Name,
20+
Name: polexdraft.GetName(),
3421
},
3522
Spec: policyExceptionSpecDeepCopy(polexdraft.Spec),
3623
}
3724

38-
return polexCR, nil
25+
return polexCR
3926
}
4027

4128
func policyExceptionSpecDeepCopy(draft polexdraftv1alpha1.PolicyExceptionDraftSpec) polexv1alpha1.PolicyExceptionSpec {

0 commit comments

Comments
 (0)