Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3473 bootstrap cli allow namespaces eneko #3542

Draft
wants to merge 2 commits into
base: 3473-bootstrap-cli-allow-namespaces
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 52 additions & 27 deletions cmd/gitops/app/bootstrap/cmd_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ const (
defaultInterval = time.Second
)

var fluxSystemNamespace = corev1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "flux-system",
},
}

func createEntitlementSecretFromEnv(t *testing.T) corev1.Secret {
func createEntitlementSecretFromEnv(t *testing.T, namespace string) corev1.Secret {

username := os.Getenv("WGE_ENTITLEMENT_USERNAME")
require.NotEmpty(t, username)
Expand All @@ -53,7 +43,7 @@ func createEntitlementSecretFromEnv(t *testing.T) corev1.Secret {
},
ObjectMeta: metav1.ObjectMeta{
Name: "weave-gitops-enterprise-credentials",
Namespace: "flux-system",
Namespace: namespace,
},
Type: corev1.SecretTypeOpaque,
Data: map[string][]byte{
Expand All @@ -64,6 +54,13 @@ func createEntitlementSecretFromEnv(t *testing.T) corev1.Secret {
}
}

type testConfig struct {
KubeconfigFlag string
NamespaceFlag string
Namespace string
Log logr.Logger
}

// TestBootstrapCmd is an integration test for bootstrapping command.
// It uses envtest to simulate a cluster.
func TestBootstrapCmd(t *testing.T) {
Expand All @@ -76,8 +73,27 @@ func TestBootstrapCmd(t *testing.T) {
g.Expect(privateKeyFile).NotTo(BeEmpty())
privateKeyFlag := fmt.Sprintf("--private-key=%s", privateKeyFile)
kubeconfigFlag := fmt.Sprintf("--kubeconfig=%s", kubeconfigPath)
namespace := "acceptance"
namespaceFlag := fmt.Sprintf("--namespace=%s", namespace)

tc := testConfig{
KubeconfigFlag: kubeconfigFlag,
NamespaceFlag: namespaceFlag,
Namespace: namespace,
Log: testLog,
}

var bootstrappingNamespace = corev1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}

_ = k8sClient.Create(context.Background(), &fluxSystemNamespace)
_ = k8sClient.Create(context.Background(), &bootstrappingNamespace)

tests := []struct {
name string
Expand All @@ -89,20 +105,21 @@ func TestBootstrapCmd(t *testing.T) {
{
name: "should install with ssh repo",
flags: []string{kubeconfigFlag,
namespaceFlag,
"--version=0.33.0",
privateKeyFlag, "--private-key-password=\"\"",
"--username=admin",
"--password=admin123",
"--domain-type=localhost",
},
setup: func(t *testing.T) {
bootstrapFluxSsh(g, kubeconfigFlag)
createEntitlements(t, testLog)
bootstrapFluxSsh(g, tc)
createEntitlements(t, tc)
},
reset: func(t *testing.T) {
deleteEntitlements(t, testLog)
deleteClusterUser(t, testLog)
uninstallFlux(g, kubeconfigFlag)
deleteEntitlements(t, tc)
deleteClusterUser(t, tc)
uninstallFlux(g, tc)
},
expectedErrorStr: "",
},
Expand All @@ -122,6 +139,7 @@ func TestBootstrapCmd(t *testing.T) {
bootstrapCmdArgs := []string{"bootstrap"}
bootstrapCmdArgs = append(bootstrapCmdArgs, tt.flags...)
cmd.SetArgs(bootstrapCmdArgs)
fmt.Println(bootstrapCmdArgs)

err := cmd.Execute()
if tt.expectedErrorStr != "" {
Expand All @@ -134,8 +152,10 @@ func TestBootstrapCmd(t *testing.T) {
}
}

func bootstrapFluxSsh(g *WithT, kubeconfigFlag string) {
func bootstrapFluxSsh(g *WithT, tc testConfig) {
var runner runner.CLIRunner
kubeconfigFlag := tc.KubeconfigFlag
namespaceFlag := tc.NamespaceFlag

repoUrl := os.Getenv("GIT_URL_SSH")
g.Expect(repoUrl).NotTo(BeEmpty())
Expand All @@ -145,7 +165,7 @@ func bootstrapFluxSsh(g *WithT, kubeconfigFlag string) {
g.Expect(privateKeyFile).NotTo(BeEmpty())
fmt.Println(privateKeyFile)

args := []string{"bootstrap", "git", kubeconfigFlag, "-s", fmt.Sprintf("--url=%s", repoUrl), fmt.Sprintf("--private-key-file=%s", privateKeyFile), "--path=clusters/management"}
args := []string{"bootstrap", "git", kubeconfigFlag, namespaceFlag, "-s", fmt.Sprintf("--url=%s", repoUrl), fmt.Sprintf("--private-key-file=%s", privateKeyFile), "--path=clusters/management"}
fmt.Println(args)

s, err := runner.Run("flux", args...)
Expand All @@ -166,30 +186,35 @@ func deleteKindCluster(name string) ([]byte, error) {
return runner.Run("kind", args...)
}

func uninstallFlux(g *WithT, kubeconfigFlag string) {
func uninstallFlux(g *WithT, tc testConfig) {
kubeconfigFlag := tc.KubeconfigFlag
namespaceFlag := tc.NamespaceFlag
var runner runner.CLIRunner
args := []string{"uninstall", kubeconfigFlag, "-s", "--keep-namespace"}
args := []string{"uninstall", kubeconfigFlag, namespaceFlag, "-s", "--keep-namespace"}
_, err := runner.Run("flux", args...)
g.Expect(err).To(BeNil())
}

func createEntitlements(t *testing.T, testLog logr.Logger) {
secret := createEntitlementSecretFromEnv(t)
func createEntitlements(t *testing.T, tc testConfig) {
testLog := tc.Log
secret := createEntitlementSecretFromEnv(t, tc.Namespace)
objects := []client.Object{
&secret,
}
createResources(testLog, t, k8sClient, objects...)
}

func deleteEntitlements(t *testing.T, testLog logr.Logger) {
secret := createEntitlementSecretFromEnv(t)
func deleteEntitlements(t *testing.T, tc testConfig) {
testLog := tc.Log
secret := createEntitlementSecretFromEnv(t, tc.Namespace)
objects := []client.Object{
&secret,
}
deleteResources(testLog, t, k8sClient, objects...)
}

func deleteClusterUser(t *testing.T, testLog logr.Logger) {
func deleteClusterUser(t *testing.T, tc testConfig) {
testLog := tc.Log
secret := corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
Expand Down