|
| 1 | +// Copyright 2025 Stefan Prodan. |
| 2 | +// SPDX-License-Identifier: AGPL-3.0 |
| 3 | + |
| 4 | +package builder |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + |
| 11 | + fluxcdv1 "github.com/controlplaneio-fluxcd/flux-operator/api/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestValidateAndApplyWorkloadIdentityConfig(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + version string |
| 18 | + cluster fluxcdv1.Cluster |
| 19 | + expectError bool |
| 20 | + expectErrorMsg string |
| 21 | + expectRemovePermission bool |
| 22 | + expectPatchContains string |
| 23 | + expectPatchNotContains string |
| 24 | + expectMultitenantWorkloadIdentityPatchApplied bool |
| 25 | + }{ |
| 26 | + // Invalid version. |
| 27 | + { |
| 28 | + name: "invalid version returns error", |
| 29 | + version: "not-a-version", |
| 30 | + expectError: true, |
| 31 | + }, |
| 32 | + // Flux < 2.6.0 tests. |
| 33 | + { |
| 34 | + name: "pre-2.6.0 with defaults succeeds with no patches", |
| 35 | + version: "2.5.0", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "pre-2.6.0 with objectLevelWorkloadIdentity returns error", |
| 39 | + version: "2.5.0", |
| 40 | + cluster: fluxcdv1.Cluster{ObjectLevelWorkloadIdentity: true}, |
| 41 | + expectError: true, |
| 42 | + expectErrorMsg: "not supported in Flux versions < 2.6.0", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "pre-2.6.0 with multitenantWorkloadIdentity returns error", |
| 46 | + version: "2.5.0", |
| 47 | + cluster: fluxcdv1.Cluster{MultitenantWorkloadIdentity: true}, |
| 48 | + expectError: true, |
| 49 | + expectErrorMsg: "not supported in Flux versions < 2.6.0", |
| 50 | + }, |
| 51 | + // Flux 2.6.x tests. |
| 52 | + { |
| 53 | + name: "2.6.x with defaults removes SA token permission", |
| 54 | + version: "2.6.0", |
| 55 | + expectRemovePermission: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "2.6.x with objectLevelWorkloadIdentity adds feature gate patch", |
| 59 | + version: "2.6.5", |
| 60 | + cluster: fluxcdv1.Cluster{ObjectLevelWorkloadIdentity: true}, |
| 61 | + expectPatchContains: "ObjectLevelWorkloadIdentity=true", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "2.6.x with multitenantWorkloadIdentity returns error", |
| 65 | + version: "2.6.0", |
| 66 | + cluster: fluxcdv1.Cluster{MultitenantWorkloadIdentity: true}, |
| 67 | + expectError: true, |
| 68 | + expectErrorMsg: "not supported in Flux versions 2.6.x", |
| 69 | + }, |
| 70 | + // Flux >= 2.7.0 tests. |
| 71 | + { |
| 72 | + name: "2.7.0 with defaults disables feature gate and removes SA token permission", |
| 73 | + version: "2.7.0", |
| 74 | + expectPatchContains: "ObjectLevelWorkloadIdentity=false", |
| 75 | + expectRemovePermission: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "2.7.0 objectLevel=false multitenant=true returns error", |
| 79 | + version: "2.7.0", |
| 80 | + cluster: fluxcdv1.Cluster{MultitenantWorkloadIdentity: true}, |
| 81 | + expectError: true, |
| 82 | + expectErrorMsg: "objectLevelWorkloadIdentity must be set to true", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "2.7.0 objectLevel=true adds feature gate for all controllers", |
| 86 | + version: "2.7.0", |
| 87 | + cluster: fluxcdv1.Cluster{ObjectLevelWorkloadIdentity: true}, |
| 88 | + expectPatchContains: "helm-controller", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "2.7.0 objectLevel=true multitenant=true adds multitenant patch", |
| 92 | + version: "2.7.0", |
| 93 | + cluster: fluxcdv1.Cluster{ |
| 94 | + ObjectLevelWorkloadIdentity: true, |
| 95 | + MultitenantWorkloadIdentity: true, |
| 96 | + }, |
| 97 | + expectMultitenantWorkloadIdentityPatchApplied: true, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "2.7.0 multitenant with custom service accounts", |
| 101 | + version: "2.7.0", |
| 102 | + cluster: fluxcdv1.Cluster{ |
| 103 | + ObjectLevelWorkloadIdentity: true, |
| 104 | + MultitenantWorkloadIdentity: true, |
| 105 | + TenantDefaultServiceAccount: "custom-sa", |
| 106 | + }, |
| 107 | + expectPatchContains: "custom-sa", |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + g := NewWithT(t) |
| 114 | + |
| 115 | + opts := MakeDefaultOptions() |
| 116 | + opts.Version = tt.version |
| 117 | + |
| 118 | + err := opts.ValidateAndApplyWorkloadIdentityConfig(tt.cluster) |
| 119 | + |
| 120 | + if tt.expectError { |
| 121 | + g.Expect(err).To(HaveOccurred()) |
| 122 | + if tt.expectErrorMsg != "" { |
| 123 | + g.Expect(err.Error()).To(ContainSubstring(tt.expectErrorMsg)) |
| 124 | + } |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + g.Expect(err).NotTo(HaveOccurred()) |
| 129 | + |
| 130 | + if tt.expectRemovePermission { |
| 131 | + g.Expect(opts.RemovePermissionForCreatingServiceAccountTokens).To(BeTrue()) |
| 132 | + } |
| 133 | + |
| 134 | + if tt.expectPatchContains != "" { |
| 135 | + g.Expect(opts.Patches).To(ContainSubstring(tt.expectPatchContains)) |
| 136 | + } |
| 137 | + |
| 138 | + if tt.expectPatchNotContains != "" { |
| 139 | + g.Expect(opts.Patches).NotTo(ContainSubstring(tt.expectPatchNotContains)) |
| 140 | + } |
| 141 | + |
| 142 | + if tt.expectMultitenantWorkloadIdentityPatchApplied { |
| 143 | + g.Expect(opts.Patches).To(ContainSubstring("default-service-account")) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments