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

Add acceptance test for bug #810 and update how secret names are generated #1066

Merged
merged 10 commits into from
Nov 15, 2021
2 changes: 1 addition & 1 deletion .github/actions/run-acceptance-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ runs:
sudo mv -f chromedriver /usr/local/bin/chromedriver
wget https://selenium-release.storage.googleapis.com/3.14/selenium-server-standalone-3.14.0.jar
xvfb-run -a --server-args="-screen 0 1280x1024x24" java -jar ./selenium-server-standalone-3.14.0.jar &
- name: Setup Kubernetes
- name: Setup Kubernetes Tools
uses: engineerd/setup-kind@v0.5.0
with:
name: "${{ format('katc-{0}', github.run_id) }}"
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/app/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ var _ = Describe("Add Gitlab", func() {
Expect(name).To(Equal("bar"))
Expect(url.String()).To(Equal("ssh://git@gitlab.com/foo/bar.git"))
Expect(branch).To(Equal("main"))
Expect(secretRef).To(Equal("wego-test-cluster-bar"))
Expect(secretRef).To(Equal("wego-gitlab-bar"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Expect(namespace).To(Equal(wego.DefaultNamespace))
})
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/services/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (a *authSvc) GetGitProvider() gitproviders.GitProvider {
// This ensures that git operations are done with stored deploy keys instead of a user's local ssh-agent or equivalent.
func (a *authSvc) CreateGitClient(ctx context.Context, repoUrl gitproviders.RepoURL, targetName string, namespace string) (git.Git, error) {
secretName := SecretName{
Name: automation.CreateRepoSecretName(targetName, repoUrl),
Name: automation.CreateRepoSecretName(repoUrl),
Namespace: namespace,
}

Expand Down Expand Up @@ -173,7 +173,7 @@ func (a *authSvc) provisionDeployKey(ctx context.Context, targetName string, nam

// Generates an ssh keypair for upload to the Git Provider and for use in a git.Git client.
func (a *authSvc) generateDeployKey(targetName string, secretName SecretName, repo gitproviders.RepoURL) (*ssh.PublicKeys, *corev1.Secret, error) {
secret, err := a.createKeyPairSecret(targetName, secretName, repo)
secret, err := a.createKeyPairSecret(secretName, repo)
if err != nil {
return nil, nil, fmt.Errorf("could not create key-pair secret: %w", err)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func (a *authSvc) retrieveDeployKey(ctx context.Context, name SecretName) (*core
}

// Uses flux to create a ssh key pair secret.
func (a *authSvc) createKeyPairSecret(targetName string, name SecretName, repo gitproviders.RepoURL) (*corev1.Secret, error) {
func (a *authSvc) createKeyPairSecret(name SecretName, repo gitproviders.RepoURL) (*corev1.Secret, error) {
secretData, err := a.fluxClient.CreateSecretGit(name.Name.String(), repo, name.Namespace)
if err != nil {
return nil, fmt.Errorf("could not create git secret: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = Describe("auth", func() {
)
BeforeEach(func() {
ctx = context.Background()
secretName = automation.CreateRepoSecretName(testClustername, repoUrl)
secretName = automation.CreateRepoSecretName(repoUrl)
Expect(err).NotTo(HaveOccurred())
osysClient = osys.New()
gp = gitprovidersfakes.FakeGitProvider{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/automation/automation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var _ = Describe("Generate manifests", func() {
Expect(name).To(Equal("bar"))
Expect(url.String()).To(Equal("ssh://git@github.com/foo/bar.git"))
Expect(branch).To(Equal("main"))
Expect(secretRef).To(Equal("wego-test-cluster-bar"))
Expect(secretRef).To(Equal("wego-github-bar"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

appManifest, nonApps := extractApp(app, results)
Expand Down Expand Up @@ -224,7 +224,7 @@ var _ = Describe("Generate manifests", func() {
Expect(name).To(Equal("bar"))
Expect(url.String()).To(Equal("ssh://git@github.com/user/repo.git"))
Expect(branch).To(Equal("main"))
Expect(secretRef).To(Equal("wego-test-cluster-repo"))
Expect(secretRef).To(Equal("wego-github-repo"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

appManifest, nonApps := extractApp(app, results)
Expand Down
29 changes: 19 additions & 10 deletions pkg/services/automation/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func NewAutomationGenerator(gp gitproviders.GitProvider, flux flux.Flux, logger
}
}

func (a *AutomationGen) getAppSecretRef(ctx context.Context, app models.Application, clusterName string) (GeneratedSecretName, error) {
func (a *AutomationGen) getAppSecretRef(ctx context.Context, app models.Application) (GeneratedSecretName, error) {
if app.SourceType != models.SourceTypeHelm {
return a.getSecretRef(ctx, app, app.GitSourceURL, clusterName)
return a.getSecretRef(ctx, app, app.GitSourceURL)
}

return "", nil
}

func (a *AutomationGen) getSecretRef(ctx context.Context, app models.Application, url gitproviders.RepoURL, clusterName string) (GeneratedSecretName, error) {
func (a *AutomationGen) getSecretRef(ctx context.Context, app models.Application, url gitproviders.RepoURL) (GeneratedSecretName, error) {
var secretRef GeneratedSecretName

visibility, err := a.GitProvider.GetRepoVisibility(ctx, url)
Expand All @@ -104,7 +104,7 @@ func (a *AutomationGen) getSecretRef(ctx context.Context, app models.Application
}

if *visibility != gitprovider.RepositoryVisibilityPublic {
secretRef = CreateRepoSecretName(clusterName, url)
secretRef = CreateRepoSecretName(url)
}

return secretRef, nil
Expand All @@ -128,13 +128,13 @@ func (a *AutomationGen) generateAppAutomation(ctx context.Context, app models.Ap
return []AutomationManifest{appManifest, appGoatManifest}, nil
}

func (a *AutomationGen) generateAppSource(ctx context.Context, app models.Application, clusterName string) (AutomationManifest, error) {
func (a *AutomationGen) generateAppSource(ctx context.Context, app models.Application) (AutomationManifest, error) {
var (
source []byte
err error
)

appSecretRef, err := a.getAppSecretRef(ctx, app, clusterName)
appSecretRef, err := a.getAppSecretRef(ctx, app)
if err != nil {
return AutomationManifest{}, err
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func (a *AutomationGen) GenerateAutomation(ctx context.Context, app models.Appli
return nil, err
}

source, err := a.generateAppSource(ctx, app, clusterName)
source, err := a.generateAppSource(ctx, app)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -390,8 +390,13 @@ func AppDeployName(a models.Application) string {
return a.Name
}

func CreateRepoSecretName(targetName string, gitSourceURL gitproviders.RepoURL) GeneratedSecretName {
return GeneratedSecretName(hashNameIfTooLong(fmt.Sprintf("wego-%s-%s", targetName, GenerateResourceName(gitSourceURL))))
func CreateRepoSecretName(gitSourceURL gitproviders.RepoURL) GeneratedSecretName {
provider := string(gitSourceURL.Provider())
cleanRepoName := replaceUnderscores(gitSourceURL.RepositoryName())
qualifiedName := fmt.Sprintf("wego-%s-%s", provider, cleanRepoName)
lengthConstrainedName := hashNameIfTooLong(qualifiedName)

return GeneratedSecretName(lengthConstrainedName)
}

func SourceKind(a models.Application) ResourceKind {
Expand Down Expand Up @@ -436,7 +441,11 @@ func GenerateResourceName(url gitproviders.RepoURL) string {
}

func ConstrainResourceName(str string) string {
return hashNameIfTooLong(strings.ReplaceAll(str, "_", "-"))
return hashNameIfTooLong(replaceUnderscores(str))
}

func replaceUnderscores(str string) string {
return strings.ReplaceAll(str, "_", "-")
}

func (rk ResourceKind) ToGVR() (schema.GroupVersionResource, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/services/gitops/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (g *Gitops) storeManifests(gitClient git.Git, gitProvider gitproviders.GitP
manifests := make(map[string][]byte, 3)
clusterPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname)

gitsource, sourceName, err := g.genSource(cname, configBranch, params.Namespace, normalizedURL)
gitsource, sourceName, err := g.genSource(configBranch, params.Namespace, normalizedURL)
if err != nil {
return nil, fmt.Errorf("failed to create source manifest: %w", err)
}
Expand Down Expand Up @@ -189,8 +189,8 @@ func (g *Gitops) storeManifests(gitClient git.Git, gitProvider gitproviders.GitP
return manifests, gitrepo.CommitAndPush(ctx, gitClient, "Add GitOps runtime manifests", g.logger)
}

func (g *Gitops) genSource(cname, branch string, namespace string, normalizedUrl gitproviders.RepoURL) ([]byte, string, error) {
secretRef := automation.CreateRepoSecretName(cname, normalizedUrl).String()
func (g *Gitops) genSource(branch string, namespace string, normalizedUrl gitproviders.RepoURL) ([]byte, string, error) {
secretRef := automation.CreateRepoSecretName(normalizedUrl).String()

sourceManifest, err := g.flux.CreateSourceGit(secretRef, normalizedUrl, branch, secretRef, namespace)
if err != nil {
Expand Down
84 changes: 76 additions & 8 deletions test/acceptance/test/add_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

var clusterName string

var clusterContext string

var _ = Describe("Weave GitOps Add App Tests", func() {

deleteWegoRuntime := false
Expand All @@ -32,10 +34,8 @@ var _ = Describe("Weave GitOps Add App Tests", func() {
By("Given I have a brand new cluster", func() {
var err error

_, err = ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, deleteWegoRuntime)
clusterName, clusterContext, err = ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, deleteWegoRuntime)
Expect(err).ShouldNot(HaveOccurred())

clusterName = getClusterName()
})

By("And I have a gitops binary installed on my local machine", func() {
Expand Down Expand Up @@ -254,7 +254,7 @@ var _ = Describe("Weave GitOps Add App Tests", func() {
})

By("Then I should see app removing message", func() {
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName, clusterName)))
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName, clusterContext)))
Eventually(appRemoveOutput).Should(gbytes.Say("► Committing and pushing gitops updates for application"))
Eventually(appRemoveOutput).Should(gbytes.Say("► Pushing app changes to repository"))
})
Expand Down Expand Up @@ -425,7 +425,7 @@ var _ = Describe("Weave GitOps Add App Tests", func() {
})

By("Then I should see app removing message", func() {
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName, clusterName)))
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName, clusterContext)))
Eventually(appRemoveOutput).Should(gbytes.Say("► Committing and pushing gitops updates for application"))
Eventually(appRemoveOutput).Should(gbytes.Say("► Pushing app changes to repository"))
})
Expand Down Expand Up @@ -568,6 +568,74 @@ var _ = Describe("Weave GitOps Add App Tests", func() {
})
})

It("Verify that gitops can deploy a single workload to multiple clusters with app manifests in config repo (Bug #810)", func() {
var repoAbsolutePath string
tip := generateTestInputs()
appRepoName := "wego-test-app-" + RandString(8)
appName := appRepoName
appRepoRemoteURL := "ssh://git@github.com/" + GITHUB_ORG + "/" + appRepoName + ".git"

addCommand := "add app . --name=" + appName + " --auto-merge=true"

cluster1Context := clusterContext
cluster2Name, cluster2Context, err := ResetOrCreateClusterWithName(WEGO_DEFAULT_NAMESPACE, deleteWegoRuntime, "", true)
Expect(err).ShouldNot(HaveOccurred())

defer deleteRepo(appRepoName, gitproviders.GitProviderGitHub, GITHUB_ORG)
defer func() {
selectCluster(cluster1Context)
deleteWorkload(tip.workloadName, tip.workloadNamespace)
deleteCluster(cluster2Name)
}()

By("And application repos do not already exist", func() {
deleteRepo(appRepoName, gitproviders.GitProviderGitHub, GITHUB_ORG)
})

By("And application workload is not already deployed to clusters", func() {
selectCluster(cluster1Context)
deleteWorkload(tip.workloadName, tip.workloadNamespace)
selectCluster(cluster2Context)
deleteWorkload(tip.workloadName, tip.workloadNamespace)
})

By("When I create an empty private repo for app", func() {
repoAbsolutePath = initAndCreateEmptyRepo(appRepoName, gitproviders.GitProviderGitHub, true, GITHUB_ORG)
})

By("And I git add-commit-push for app", func() {
gitAddCommitPush(repoAbsolutePath, tip.appManifestFilePath)
})

By("And I install gitops to my active clusters", func() {
selectCluster(cluster1Context)
installAndVerifyWego(WEGO_DEFAULT_NAMESPACE, appRepoRemoteURL)
selectCluster(cluster2Context)
installAndVerifyWego(WEGO_DEFAULT_NAMESPACE, appRepoRemoteURL)
})

By("And I run gitops add command for app", func() {
selectCluster(cluster1Context)
runWegoAddCommand(repoAbsolutePath, addCommand, WEGO_DEFAULT_NAMESPACE)
selectCluster(cluster2Context)
runWegoAddCommand(repoAbsolutePath, addCommand, WEGO_DEFAULT_NAMESPACE)
})

By("Then I should see gitops add command linked the repo to the cluster", func() {
selectCluster(cluster1Context)
verifyWegoAddCommand(appName, WEGO_DEFAULT_NAMESPACE)
selectCluster(cluster2Context)
verifyWegoAddCommand(appName, WEGO_DEFAULT_NAMESPACE)
})

By("And I should see workload for app is deployed to the cluster", func() {
selectCluster(cluster1Context)
verifyWorkloadIsDeployed(tip.workloadName, tip.workloadNamespace)
selectCluster(cluster2Context)
verifyWorkloadIsDeployed(tip.workloadName, tip.workloadNamespace)
})
})

It("Test2 - Verify that gitops can add multiple apps dir to the cluster using single repo for gitops config", func() {
var repoAbsolutePath string
var configRepoRemoteURL string
Expand Down Expand Up @@ -1032,7 +1100,7 @@ var _ = Describe("Weave GitOps Add App Tests", func() {
})

By("Then I should see app deleting message", func() {
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName2, clusterName)))
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName2, clusterContext)))
Eventually(appRemoveOutput).Should(gbytes.Say("► Committing and pushing gitops updates for application"))
Eventually(appRemoveOutput).Should(gbytes.Say("► Pushing app changes to repository"))
})
Expand Down Expand Up @@ -1332,7 +1400,7 @@ var _ = Describe("Weave GitOps Add App Tests", func() {
})

By("Then I should see app removing message", func() {
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName, clusterName)))
Eventually(appRemoveOutput).Should(gbytes.Say(fmt.Sprintf("► Removing application %q from cluster %q and repository", appName, clusterContext)))
Eventually(appRemoveOutput).Should(gbytes.Say("► Committing and pushing gitops updates for application"))
Eventually(appRemoveOutput).Should(gbytes.Say("► Pushing app changes to repository"))
})
Expand Down Expand Up @@ -1569,7 +1637,7 @@ var _ = Describe("Weave GitOps Add Tests With Long Cluster Name", func() {
var err error

clusterName = "kind-123456789012345678901234567890"
_, err = ResetOrCreateClusterWithName(WEGO_DEFAULT_NAMESPACE, deleteWegoRuntime, clusterName)
_, _, err = ResetOrCreateClusterWithName(WEGO_DEFAULT_NAMESPACE, deleteWegoRuntime, clusterName, false)
Expect(err).ShouldNot(HaveOccurred())
})

Expand Down
10 changes: 5 additions & 5 deletions test/acceptance/test/install_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var _ = Describe("Weave GitOps Install Tests", func() {
defer deleteNamespace(namespace)

By("And I have a brand new cluster", func() {
_, err := ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, true)
_, _, err := ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, true)
Expect(err).ShouldNot(HaveOccurred())
})

Expand All @@ -85,7 +85,7 @@ var _ = Describe("Weave GitOps Install Tests", func() {
namespace := "test-namespace"

By("And I have a brand new cluster", func() {
_, err := ResetOrCreateCluster(namespace, true)
_, _, err := ResetOrCreateCluster(namespace, true)
Expect(err).ShouldNot(HaveOccurred())
})

Expand Down Expand Up @@ -120,7 +120,7 @@ var _ = Describe("Weave GitOps Install Tests", func() {
namespace := "test-namespace"

By("And I have a brand new cluster", func() {
_, err := ResetOrCreateCluster(namespace, true)
_, _, err := ResetOrCreateCluster(namespace, true)
Expect(err).ShouldNot(HaveOccurred())
})

Expand Down Expand Up @@ -165,7 +165,7 @@ var _ = Describe("Weave GitOps Install Tests", func() {
var uninstallDryRunOutput string

By("And I have a brand new cluster", func() {
_, err := ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, true)
_, _, err := ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, true)
Expect(err).ShouldNot(HaveOccurred())
})

Expand Down Expand Up @@ -234,7 +234,7 @@ var _ = Describe("Weave GitOps Install Tests", func() {
namespace := "wego-system"

By("And I have a brand new cluster", func() {
_, err := ResetOrCreateCluster(namespace, true)
_, _, err := ResetOrCreateCluster(namespace, true)
Expect(err).ShouldNot(HaveOccurred())
})

Expand Down
4 changes: 4 additions & 0 deletions test/acceptance/test/scripts/kind-multi-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "Create a new kind cluster with name "$1
kind create cluster --name=$1 --image=$2 --config=./configs/kind-config.yaml
2 changes: 1 addition & 1 deletion test/acceptance/test/ui_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var _ = Describe("Weave GitOps UI Test", func() {

By("Given I have a brand new cluster", func() {

_, err = ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, deleteWegoRuntime)
_, _, err = ResetOrCreateCluster(WEGO_DEFAULT_NAMESPACE, deleteWegoRuntime)
Expect(err).ShouldNot(HaveOccurred())

Expect(FileExists(WEGO_BIN_PATH)).To(BeTrue())
Expand Down