|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/SAP/crossplane-provider-hana/apis/admin/v1alpha1" |
| 12 | + "github.com/crossplane-contrib/xp-testing/pkg/resources" |
| 13 | + |
| 14 | + "sigs.k8s.io/e2e-framework/klient/decoder" |
| 15 | + "sigs.k8s.io/e2e-framework/klient/k8s" |
| 16 | + k8sresources "sigs.k8s.io/e2e-framework/klient/k8s/resources" |
| 17 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 18 | + "sigs.k8s.io/e2e-framework/klient/wait/conditions" |
| 19 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 20 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 21 | +) |
| 22 | + |
| 23 | +type AuditPolicyTestConfig struct { |
| 24 | + TestConfig *resources.ResourceTestConfig |
| 25 | + Resource *k8sresources.Resources |
| 26 | + Objects []k8s.Object |
| 27 | +} |
| 28 | + |
| 29 | +func TestAuditPolicy(t *testing.T) { |
| 30 | + testConfig := resources.NewResourceTestConfig(nil, "AuditPolicy") |
| 31 | + |
| 32 | + c := &AuditPolicyTestConfig{ |
| 33 | + TestConfig: testConfig, |
| 34 | + } |
| 35 | + |
| 36 | + fB := features.New(fmt.Sprintf("%v", testConfig.Kind)) |
| 37 | + fB.WithLabel("kind", testConfig.Kind) |
| 38 | + fB.Setup(c.SetupAuditPolicy) |
| 39 | + |
| 40 | + fB.Assess("create", testConfig.AssessCreate) |
| 41 | + |
| 42 | + fB.Assess("update", c.assessUpdate) |
| 43 | + |
| 44 | + fB.Assess("delete", testConfig.AssessDelete) |
| 45 | + |
| 46 | + fB.Teardown(testConfig.Teardown) |
| 47 | + |
| 48 | + testenv.Test(t, fB.Feature()) |
| 49 | +} |
| 50 | + |
| 51 | +func (c *AuditPolicyTestConfig) assessUpdate(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 52 | + for _, obj := range c.Objects { |
| 53 | + if err := c.Resource.Get(ctx, obj.GetName(), obj.GetNamespace(), obj); err != nil { |
| 54 | + t.Errorf("failed to get audit policy: %v", err) |
| 55 | + return ctx |
| 56 | + } |
| 57 | + |
| 58 | + auditPolicy, ok := obj.(*v1alpha1.AuditPolicy) |
| 59 | + if !ok { |
| 60 | + t.Errorf("failed to cast object to AuditPolicy: %v", obj) |
| 61 | + return ctx |
| 62 | + } |
| 63 | + |
| 64 | + auditPolicy.Spec.ForProvider.AuditActions = append(auditPolicy.Spec.ForProvider.AuditActions, |
| 65 | + "REVOKE ANY", |
| 66 | + ) |
| 67 | + res := cfg.Client().Resources() |
| 68 | + |
| 69 | + err := res.Update(ctx, auditPolicy) |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + |
| 74 | + var fn = func(u k8s.Object) bool { |
| 75 | + return u.GetName() == auditPolicy.GetName() && u.GetNamespace() == auditPolicy.GetNamespace() |
| 76 | + } |
| 77 | + |
| 78 | + var areAuditActionsUpToDate = func(observed, desired []string) bool { |
| 79 | + if len(observed) != len(desired) { |
| 80 | + return false |
| 81 | + } |
| 82 | + for i, action := range observed { |
| 83 | + if action != desired[i] { |
| 84 | + return false |
| 85 | + } |
| 86 | + } |
| 87 | + return true |
| 88 | + } |
| 89 | + |
| 90 | + err = wait.For( |
| 91 | + conditions.New(res).ResourceMatch(auditPolicy, fn), |
| 92 | + ) |
| 93 | + if err != nil { |
| 94 | + t.Error(err) |
| 95 | + } |
| 96 | + if err := res.Get(ctx, auditPolicy.GetName(), auditPolicy.GetNamespace(), auditPolicy); err != nil { |
| 97 | + t.Errorf("failed to get audit policy after update: %v", err) |
| 98 | + } else if !areAuditActionsUpToDate(auditPolicy.Spec.ForProvider.AuditActions, auditPolicy.Spec.ForProvider.AuditActions) { |
| 99 | + t.Errorf("audit policy update failed, expected audit actions to be [GRANT ANY, REVOKE ANY], got %v", auditPolicy.Spec.ForProvider.AuditActions) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return ctx |
| 104 | +} |
| 105 | + |
| 106 | +// Setup creates the resource in the cluster |
| 107 | +func (c *AuditPolicyTestConfig) SetupAuditPolicy(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 108 | + t.Logf("Apply AuditPolicy") |
| 109 | + |
| 110 | + resources.ImportResources(ctx, t, cfg, c.TestConfig.ResourceDirectory) |
| 111 | + |
| 112 | + objects := make([]k8s.Object, 0) |
| 113 | + err := decoder.DecodeEachFile( |
| 114 | + ctx, os.DirFS(c.TestConfig.ResourceDirectory), "*", |
| 115 | + func(ctx context.Context, obj k8s.Object) error { |
| 116 | + objects = append(objects, obj) |
| 117 | + return nil |
| 118 | + }, |
| 119 | + decoder.MutateNamespace(cfg.Namespace()), |
| 120 | + ) |
| 121 | + if err != nil { |
| 122 | + t.Errorf("failed to decode files: %v", err) |
| 123 | + return ctx |
| 124 | + } |
| 125 | + |
| 126 | + if c.Resource, err = k8sresources.New(cfg.Client().RESTConfig()); err != nil { |
| 127 | + t.Errorf("failed to create resource client: %v", err) |
| 128 | + return ctx |
| 129 | + } |
| 130 | + |
| 131 | + for _, obj := range objects { |
| 132 | + if _, ok := obj.(*v1alpha1.AuditPolicy); !ok { |
| 133 | + t.Errorf("failed to cast object to AuditPolicy: %v", obj) |
| 134 | + return ctx |
| 135 | + } |
| 136 | + |
| 137 | + c.Objects = append(c.Objects, obj) |
| 138 | + } |
| 139 | + |
| 140 | + return ctx |
| 141 | +} |
0 commit comments