|
| 1 | +//go:build integration |
| 2 | +// +build integration |
| 3 | + |
| 4 | +package bootstrap_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "sync" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/go-logr/logr" |
| 15 | + "github.com/go-logr/logr/testr" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "github.com/weaveworks/weave-gitops-enterprise/cmd/gitops/app/root" |
| 18 | + "github.com/weaveworks/weave-gitops-enterprise/cmd/gitops/pkg/adapters" |
| 19 | + "github.com/weaveworks/weave-gitops/pkg/runner" |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 23 | + |
| 24 | + . "github.com/onsi/gomega" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + defaultTimeout = time.Second * 5 |
| 29 | + defaultInterval = time.Second |
| 30 | +) |
| 31 | + |
| 32 | +var fluxSystemNamespace = corev1.Namespace{ |
| 33 | + TypeMeta: metav1.TypeMeta{ |
| 34 | + Kind: "Namespace", |
| 35 | + APIVersion: "v1", |
| 36 | + }, |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: "flux-system", |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +func createEntitlementSecretFromEnv(t *testing.T) corev1.Secret { |
| 43 | + |
| 44 | + username := os.Getenv("WGE_ENTITLEMENT_USERNAME") |
| 45 | + require.NotEmpty(t, username) |
| 46 | + password := os.Getenv("WGE_ENTITLEMENT_PASSWORD") |
| 47 | + require.NotEmpty(t, password) |
| 48 | + entitlement := os.Getenv("WGE_ENTITLEMENT_ENTITLEMENT") |
| 49 | + require.NotEmpty(t, entitlement) |
| 50 | + |
| 51 | + return corev1.Secret{ |
| 52 | + TypeMeta: metav1.TypeMeta{ |
| 53 | + Kind: "Secret", |
| 54 | + APIVersion: "v1", |
| 55 | + }, |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: "weave-gitops-enterprise-credentials", |
| 58 | + Namespace: "flux-system", |
| 59 | + }, |
| 60 | + Type: corev1.SecretTypeOpaque, |
| 61 | + Data: map[string][]byte{ |
| 62 | + "username": []byte(username), |
| 63 | + "password": []byte(password), |
| 64 | + "entitlement": []byte(entitlement), |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TestBootstrapCmd is an integration test for bootstrapping command. |
| 70 | +// It uses envtest to simulate a cluster. |
| 71 | +func TestBootstrapCmd(t *testing.T) { |
| 72 | + g := NewGomegaWithT(t) |
| 73 | + g.SetDefaultEventuallyTimeout(defaultTimeout) |
| 74 | + g.SetDefaultEventuallyPollingInterval(defaultInterval) |
| 75 | + testLog := testr.New(t) |
| 76 | + |
| 77 | + lock := sync.Mutex{} |
| 78 | + |
| 79 | + privateKeyFile := os.Getenv("GIT_PRIVATEKEY_PATH") |
| 80 | + g.Expect(privateKeyFile).NotTo(BeEmpty()) |
| 81 | + privateKeyArg := fmt.Sprintf("--private-key=%s", privateKeyFile) |
| 82 | + |
| 83 | + // ensure flux-system ns exists |
| 84 | + _ = k8sClient.Create(context.Background(), &fluxSystemNamespace) |
| 85 | + |
| 86 | + tests := []struct { |
| 87 | + name string |
| 88 | + flags []string |
| 89 | + expectedErrorStr string |
| 90 | + setup func(t *testing.T) |
| 91 | + reset func(t *testing.T) |
| 92 | + }{ |
| 93 | + { |
| 94 | + name: "should fail without flux bootstrapped", |
| 95 | + flags: []string{}, |
| 96 | + expectedErrorStr: "please bootstrap Flux in `flux-system` namespace: more info https://fluxcd.io/flux/installation", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "should fail without entitlements", |
| 100 | + flags: []string{}, |
| 101 | + setup: func(t *testing.T) { |
| 102 | + bootstrapFluxSsh(g) |
| 103 | + }, |
| 104 | + reset: func(t *testing.T) { |
| 105 | + uninstallFlux(g) |
| 106 | + }, |
| 107 | + |
| 108 | + expectedErrorStr: "entitlement file is not found", |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "should fail without private key", |
| 112 | + flags: []string{}, |
| 113 | + setup: func(t *testing.T) { |
| 114 | + bootstrapFluxSsh(g) |
| 115 | + createEntitlements(t, testLog) |
| 116 | + }, |
| 117 | + reset: func(t *testing.T) { |
| 118 | + deleteEntitlements(t, testLog) |
| 119 | + uninstallFlux(g) |
| 120 | + }, |
| 121 | + expectedErrorStr: "cannot process input 'private key path and password", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "should fail without selected wge version", |
| 125 | + flags: []string{ |
| 126 | + privateKeyArg, |
| 127 | + "--private-key-password=\"\"", |
| 128 | + }, |
| 129 | + setup: func(t *testing.T) { |
| 130 | + bootstrapFluxSsh(g) |
| 131 | + createEntitlements(t, testLog) |
| 132 | + }, |
| 133 | + reset: func(t *testing.T) { |
| 134 | + deleteEntitlements(t, testLog) |
| 135 | + uninstallFlux(g) |
| 136 | + }, |
| 137 | + expectedErrorStr: "cannot process input 'select WGE version'", |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "should fail without user authentication", |
| 141 | + flags: []string{"--version=0.33.0", |
| 142 | + privateKeyArg, |
| 143 | + "--private-key-password=\"\"", |
| 144 | + }, |
| 145 | + setup: func(t *testing.T) { |
| 146 | + bootstrapFluxSsh(g) |
| 147 | + createEntitlements(t, testLog) |
| 148 | + }, |
| 149 | + reset: func(t *testing.T) { |
| 150 | + deleteEntitlements(t, testLog) |
| 151 | + uninstallFlux(g) |
| 152 | + }, |
| 153 | + expectedErrorStr: "cannot process input 'user authentication'", |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "should fail without dashboard access", |
| 157 | + flags: []string{"--version=0.33.0", |
| 158 | + privateKeyArg, |
| 159 | + "--private-key-password=\"\"", |
| 160 | + "--username=admin", |
| 161 | + "--password=admin123"}, |
| 162 | + setup: func(t *testing.T) { |
| 163 | + bootstrapFluxSsh(g) |
| 164 | + createEntitlements(t, testLog) |
| 165 | + }, |
| 166 | + reset: func(t *testing.T) { |
| 167 | + deleteClusterUser(t, testLog) |
| 168 | + deleteEntitlements(t, testLog) |
| 169 | + uninstallFlux(g) |
| 170 | + }, |
| 171 | + expectedErrorStr: "cannot process input 'dashboard access'", |
| 172 | + }, |
| 173 | + } |
| 174 | + for _, tt := range tests { |
| 175 | + lock.Lock() |
| 176 | + t.Run(tt.name, func(t *testing.T) { |
| 177 | + |
| 178 | + defer lock.Unlock() |
| 179 | + |
| 180 | + if tt.setup != nil { |
| 181 | + tt.setup(t) |
| 182 | + } |
| 183 | + |
| 184 | + if tt.reset != nil { |
| 185 | + defer tt.reset(t) |
| 186 | + } |
| 187 | + |
| 188 | + client := adapters.NewHTTPClient() |
| 189 | + cmd := root.Command(client) |
| 190 | + bootstrapCmdArgs := []string{"bootstrap"} |
| 191 | + bootstrapCmdArgs = append(bootstrapCmdArgs, tt.flags...) |
| 192 | + cmd.SetArgs(bootstrapCmdArgs) |
| 193 | + |
| 194 | + err := cmd.Execute() |
| 195 | + if tt.expectedErrorStr != "" { |
| 196 | + g.Expect(err.Error()).To(ContainSubstring(tt.expectedErrorStr)) |
| 197 | + return |
| 198 | + } |
| 199 | + g.Expect(err).To(BeNil()) |
| 200 | + |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func bootstrapFluxSsh(g *WithT) { |
| 206 | + var runner runner.CLIRunner |
| 207 | + |
| 208 | + repoUrl := os.Getenv("GIT_URL_SSH") |
| 209 | + g.Expect(repoUrl).NotTo(BeEmpty()) |
| 210 | + fmt.Println(repoUrl) |
| 211 | + |
| 212 | + privateKeyFile := os.Getenv("GIT_PRIVATEKEY_PATH") |
| 213 | + g.Expect(privateKeyFile).NotTo(BeEmpty()) |
| 214 | + fmt.Println(privateKeyFile) |
| 215 | + |
| 216 | + args := []string{"bootstrap", "git", "-s", fmt.Sprintf("--url=%s", repoUrl), fmt.Sprintf("--private-key-file=%s", privateKeyFile), "--path=clusters/management"} |
| 217 | + fmt.Println(args) |
| 218 | + |
| 219 | + s, err := runner.Run("flux", args...) |
| 220 | + fmt.Println(string(s)) |
| 221 | + g.Expect(err).To(BeNil()) |
| 222 | + |
| 223 | +} |
| 224 | + |
| 225 | +func uninstallFlux(g *WithT) { |
| 226 | + var runner runner.CLIRunner |
| 227 | + args := []string{"uninstall", "-s", "--keep-namespace"} |
| 228 | + _, err := runner.Run("flux", args...) |
| 229 | + g.Expect(err).To(BeNil()) |
| 230 | +} |
| 231 | + |
| 232 | +func createEntitlements(t *testing.T, testLog logr.Logger) { |
| 233 | + secret := createEntitlementSecretFromEnv(t) |
| 234 | + objects := []client.Object{ |
| 235 | + &secret, |
| 236 | + } |
| 237 | + createResources(testLog, t, k8sClient, objects...) |
| 238 | +} |
| 239 | + |
| 240 | +func deleteEntitlements(t *testing.T, testLog logr.Logger) { |
| 241 | + secret := createEntitlementSecretFromEnv(t) |
| 242 | + objects := []client.Object{ |
| 243 | + &secret, |
| 244 | + } |
| 245 | + deleteResources(testLog, t, k8sClient, objects...) |
| 246 | +} |
| 247 | + |
| 248 | +func deleteClusterUser(t *testing.T, testLog logr.Logger) { |
| 249 | + secret := corev1.Secret{ |
| 250 | + TypeMeta: metav1.TypeMeta{ |
| 251 | + Kind: "Secret", |
| 252 | + APIVersion: "v1", |
| 253 | + }, |
| 254 | + Type: corev1.SecretTypeOpaque, |
| 255 | + ObjectMeta: metav1.ObjectMeta{ |
| 256 | + Name: "cluster-user-auth", |
| 257 | + Namespace: "flux-system", |
| 258 | + }, |
| 259 | + Data: map[string][]byte{}, |
| 260 | + } |
| 261 | + |
| 262 | + objects := []client.Object{ |
| 263 | + &secret, |
| 264 | + } |
| 265 | + deleteResources(testLog, t, k8sClient, objects...) |
| 266 | +} |
| 267 | + |
| 268 | +func createResources(log logr.Logger, t *testing.T, k client.Client, objects ...client.Object) { |
| 269 | + ctx := context.Background() |
| 270 | + t.Helper() |
| 271 | + for _, o := range objects { |
| 272 | + err := k.Create(ctx, o) |
| 273 | + if err != nil { |
| 274 | + t.Errorf("failed to create object: %s", err) |
| 275 | + } |
| 276 | + log.Info("created object", "name", o.GetName(), "ns", o.GetNamespace(), "kind", o.GetObjectKind().GroupVersionKind().Kind) |
| 277 | + } |
| 278 | +} |
| 279 | + |
| 280 | +func deleteResources(log logr.Logger, t *testing.T, k client.Client, objects ...client.Object) { |
| 281 | + ctx := context.Background() |
| 282 | + t.Helper() |
| 283 | + for _, o := range objects { |
| 284 | + err := k.Delete(ctx, o) |
| 285 | + if err != nil { |
| 286 | + t.Logf("failed to cleanup object: %s", err) |
| 287 | + } |
| 288 | + log.Info("deleted object", "name", o.GetName(), "ns", o.GetNamespace(), "kind", o.GetObjectKind().GroupVersionKind().Kind) |
| 289 | + |
| 290 | + } |
| 291 | +} |
0 commit comments