From fc83c967c2e0704da3cf6565abe6057e5dc53fb4 Mon Sep 17 00:00:00 2001 From: Andreas Paul Date: Thu, 8 Feb 2024 12:51:40 +0100 Subject: [PATCH] Fix same branch name different sources (#222) * add TestMultipleSourcesWithSameBrancheName and use absolute path for purgeUnmanagedContent, fixes #220 * use absolute path including basedir for helper struct allEnvironments, fixes #220 * use absolute path for purgeUnmanagedContent, fixes #220 * remove too verbose env output * fix TestResolvePuppetfileUseSSHAgent by adding a test fake key --- .github/workflows/main.yml | 2 +- g10k_test.go | 83 ++++++++++++++----- helper.go | 6 +- puppetfile.go | 4 +- stale.go | 14 ++-- ...nfig2SourcesSameBranchNameDiffBaseDir.yaml | 10 +++ tests/TestConfigUseSSHAgent.yaml | 8 +- tests/test-fake-key | 38 +++++++++ 8 files changed, 126 insertions(+), 39 deletions(-) create mode 100644 tests/TestConfig2SourcesSameBranchNameDiffBaseDir.yaml create mode 100644 tests/test-fake-key diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d7f6a31..ffe3a38 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,7 +21,7 @@ jobs: steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v3 - - name: setup go environment 1.19.3 + - name: setup go environment stable uses: actions/setup-go@v3 with: go-version: '1.19.3' diff --git a/g10k_test.go b/g10k_test.go index e85b068..d4b6206 100755 --- a/g10k_test.go +++ b/g10k_test.go @@ -2153,14 +2153,14 @@ func TestPurgeStaleDeploymentOnly(t *testing.T) { expectedLines := []string{ "DEBUG purgeUnmanagedContent(): Glob'ing with path /tmp/full/full_*", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_another", - "DEBUG purgeUnmanagedContent(): Not purging environment full_another", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_master", - "DEBUG purgeUnmanagedContent(): Not purging environment full_master", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_qa", - "DEBUG purgeUnmanagedContent(): Not purging environment full_qa", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_stale", - "Removing unmanaged environment full_stale", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_another", + "DEBUG purgeUnmanagedContent(): Not purging environment /tmp/full/full_another", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_master", + "DEBUG purgeUnmanagedContent(): Not purging environment /tmp/full/full_master", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_qa", + "DEBUG purgeUnmanagedContent(): Not purging environment /tmp/full/full_qa", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_stale", + "Removing unmanaged environment /tmp/full/full_stale", } for _, expectedLine := range expectedLines { @@ -2221,14 +2221,14 @@ func TestPurgeStaleDeploymentOnlyWithAllowList(t *testing.T) { expectedLines := []string{ "DEBUG purgeUnmanagedContent(): Glob'ing with path /tmp/full/full_*", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_another", - "DEBUG purgeUnmanagedContent(): Not purging environment full_another", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_master", - "DEBUG purgeUnmanagedContent(): Not purging environment full_master", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_qa", - "DEBUG purgeUnmanagedContent(): Not purging environment full_qa", - "DEBUG purgeUnmanagedContent(): Checking if environment should exist: full_stale", - "Removing unmanaged environment full_stale", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_another", + "DEBUG purgeUnmanagedContent(): Not purging environment /tmp/full/full_another", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_master", + "DEBUG purgeUnmanagedContent(): Not purging environment /tmp/full/full_master", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_qa", + "DEBUG purgeUnmanagedContent(): Not purging environment /tmp/full/full_qa", + "DEBUG purgeUnmanagedContent(): Checking if environment should exist: /tmp/full/full_stale", + "Removing unmanaged environment /tmp/full/full_stale", } for _, expectedLine := range expectedLines { @@ -2882,7 +2882,7 @@ func TestResolvePuppetfileUseSSHAgent(t *testing.T) { if exitCode != 1 { t.Errorf("terminated with %v, but we expected exit status %v Output: %s", exitCode, 1, string(out)) } - //fmt.Println(string(out)) + // fmt.Println(string(out)) sshAddCmd := "ssh-add" if runtime.GOOS == "darwin" { @@ -2891,11 +2891,11 @@ func TestResolvePuppetfileUseSSHAgent(t *testing.T) { expectedLines := []string{ "DEBUG git repo url git@local.git.server:foo/git_module_with_ssh_agent.git with loaded SSH keys from ssh-agent", - "DEBUG git repo url git@github.com:foobar/github_module_without_ssh_add.git with SSH key tests/TestConfigUseSSHAgent.yaml", - "DEBUG git repo url git@local.git.server:bar/git_module_with_ssh_add.git with SSH key tests/TestConfigUseSSHAgent.yaml", + "DEBUG git repo url git@github.com:foobar/github_module_without_ssh_add.git with SSH key tests/test-fake-key", + "DEBUG git repo url git@local.git.server:bar/git_module_with_ssh_add.git with SSH key tests/test-fake-key", "DEBUG executeCommand(): Executing git clone --mirror git@local.git.server:foo/git_module_with_ssh_agent.git /tmp/g10k/modules/git@local.git.server-foo_git_module_with_ssh_agent.git", "DEBUG executeCommand(): Executing git clone --mirror git@github.com:foobar/github_module_without_ssh_add.git /tmp/g10k/modules/git@github.com-foobar_github_module_without_ssh_add.git", - "DEBUG executeCommand(): Executing ssh-agent bash -c '" + sshAddCmd + " tests/TestConfigUseSSHAgent.yaml; git clone --mirror git@local.git.server:bar/git_module_with_ssh_add.git /tmp/g10k/modules/git@local.git.server-bar_git_module_with_ssh_add.git'", + "DEBUG executeCommand(): Executing ssh-agent bash -c '" + sshAddCmd + " tests/test-fake-key; git clone --mirror git@local.git.server:bar/git_module_with_ssh_add.git /tmp/g10k/modules/git@local.git.server-bar_git_module_with_ssh_add.git'", } for _, expectedLine := range expectedLines { @@ -3262,3 +3262,46 @@ func TestNoProxy(t *testing.T) { } } } + +func TestMultipleSourcesWithSameBrancheName(t *testing.T) { + funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1] + config = readConfigfile(filepath.Join("tests", "TestConfig2SourcesSameBranchNameDiffBaseDir.yaml")) + if os.Getenv("TEST_FOR_CRASH_"+funcName) == "1" { + debug = true + info = true + resolvePuppetEnvironment(false, "") + return + } + purgeDir("/tmp/example/", funcName) + purgeDir("/tmp/out/", funcName) + purgeDir("/tmp/out-clone/", funcName) + + cmd := exec.Command(os.Args[0], "-test.run="+funcName+"$") + cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+funcName+"=1") + _, err := cmd.CombinedOutput() + + exitCode := 0 + if msg, ok := err.(*exec.ExitError); ok { // there is error code + exitCode = msg.Sys().(syscall.WaitStatus).ExitStatus() + } + + expectedExitCode := 0 + if exitCode != expectedExitCode { + t.Errorf("terminated with %v, but we expected exit status %v", exitCode, expectedExitCode) + } + // fmt.Println(string(out)) + + expectedFiles := []string{ + "/tmp/out-clone/another/Puppetfile", + "/tmp/out-clone/master/.g10k-deploy.json", + "/tmp/out/master/Puppetfile", + "/tmp/out/ref/uuui/d0", + } + + for _, expectedFile := range expectedFiles { + if !fileExists(expectedFile) { + t.Errorf("files and/or directory missing that should be there! " + expectedFile) + } + } + +} diff --git a/helper.go b/helper.go index a4189cb..56732eb 100644 --- a/helper.go +++ b/helper.go @@ -201,12 +201,8 @@ func executeCommand(command string, commandDir string, timeout int, allowFail bo os.Unsetenv("https_proxy") os.Unsetenv("HTTP_PROXY") os.Unsetenv("HTTPS_PROXY") - execCommand.Env = os.Environ() - Debugf("exec OS env:" + strings.Join(execCommand.Env, ",") + " for command " + command) - } else { - execCommand.Env = os.Environ() - Debugf("exec OS env:" + strings.Join(execCommand.Env, ",") + " for command " + command) } + execCommand.Env = os.Environ() out, err := execCommand.CombinedOutput() duration := time.Since(before).Seconds() er := ExecResult{0, string(out)} diff --git a/puppetfile.go b/puppetfile.go index 7cd14d8..61eea4a 100644 --- a/puppetfile.go +++ b/puppetfile.go @@ -150,8 +150,8 @@ func resolvePuppetEnvironment(tags bool, outputNameTag string) { } mutex.Lock() - if _, ok := allEnvironments[prefix+renamedBranch]; !ok { - allEnvironments[prefix+renamedBranch] = true + if _, ok := allEnvironments[filepath.Join(sa.Basedir, prefix+renamedBranch)]; !ok { + allEnvironments[filepath.Join(sa.Basedir, prefix+renamedBranch)] = true } else { Fatalf("Renamed environment naming conflict detected with renamed environment " + prefix + renamedBranch) } diff --git a/stale.go b/stale.go index 2dbb088..f258446 100644 --- a/stale.go +++ b/stale.go @@ -50,15 +50,15 @@ func purgeUnmanagedContent(allBasedirs map[string]bool, allEnvironments map[stri } } if stringSliceContains(config.PurgeLevels, "deployment") { - Debugf("Checking if environment should exist: " + envName) - if allEnvironments[envName] { - Debugf("Not purging environment " + envName) - } else if stringSliceContains(allowlistEnvironments, filepath.Join(basedir, envName)) { - Debugf("Not purging environment " + envName + " due to deployment_purge_allowlist match") + Debugf("Checking if environment should exist: " + env) + if allEnvironments[env] { + Debugf("Not purging environment " + env) + } else if stringSliceContains(allowlistEnvironments, env) { + Debugf("Not purging environment " + env + " due to deployment_purge_allowlist match") } else { - Infof("Removing unmanaged environment " + envName) + Infof("Removing unmanaged environment " + env) if !dryRun { - purgeDir(filepath.Join(basedir, envName), "purgeStaleContent()") + purgeDir(env, "purgeStaleContent()") } } } diff --git a/tests/TestConfig2SourcesSameBranchNameDiffBaseDir.yaml b/tests/TestConfig2SourcesSameBranchNameDiffBaseDir.yaml new file mode 100644 index 0000000..89a1eee --- /dev/null +++ b/tests/TestConfig2SourcesSameBranchNameDiffBaseDir.yaml @@ -0,0 +1,10 @@ +--- +cachedir: '/tmp/g10k' + +sources: + example: + remote: 'https://github.com/xorpaul/g10k-fullworking-env-clone.git' + basedir: '/tmp/out/' + full: + remote: 'https://github.com/xorpaul/g10k-fullworking-env.git' + basedir: '/tmp/out-clone/' diff --git a/tests/TestConfigUseSSHAgent.yaml b/tests/TestConfigUseSSHAgent.yaml index 4723789..c2e1a20 100644 --- a/tests/TestConfigUseSSHAgent.yaml +++ b/tests/TestConfigUseSSHAgent.yaml @@ -1,8 +1,8 @@ --- -:cachedir: '/tmp/g10k' +:cachedir: "/tmp/g10k" sources: example: - remote: 'https://github.com/xorpaul/g10k-environment.git' - basedir: '/tmp/example/' - private_key: 'tests/TestConfigUseSSHAgent.yaml' # just some existing file, we just want to check if the ssh-add command isn't used + remote: "https://github.com/xorpaul/g10k-environment.git" + basedir: "/tmp/example/" + private_key: "tests/test-fake-key" # just some existing file, we just want to check if the ssh-add command isn't used diff --git a/tests/test-fake-key b/tests/test-fake-key new file mode 100644 index 0000000..9307a31 --- /dev/null +++ b/tests/test-fake-key @@ -0,0 +1,38 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn +NhAAAAAwEAAQAAAYEA2Bk3OVSuVcP0Wdw57JzKMxKbf3qwBPHZHrMN8MJ7S/qZ11zAUzAY +57Y4e6sE8dd+nC1hK2E8vBZcyib/W+mWJw3JzUHsGTJgZCi+kRieXwZmztwNC4+sUDwDf6 +lT8K95kXV36r5IYoo16Fh5/yxYON+HBtDAYDIZp01ez5VDhjNMpnUX39Km5Jt9950CdkhN +pns66QYPAZ3sKBhwGt7l7+X15aKxNaVshLJtPd4V21hDbyqyQUJOwRjj6RvnDibM8XhDuA +Y/yXH6vHCHsHBd/QOaiLmIl+Jkv1Zl5pSRJmTFMrrbMDTojsoiqZel3NNWBJnCCf8B+A8g +hFOp3q+ggrcXmXGE9+uutbrTmBbZrOOfuHPnvTHMyYJ9ogkdcVX751+6w5ujJP8eD0cFLf +GDr6pNX9tT4qI/jQIo8XiBbKRAPFnTX9qydESBagr6Iy83eUGYvQ12zdYyZy/Uzmj/++3g +kOncqGNp5cy2p9ASDcmQI7bsN8yMAHVgRMlvK9CrAAAFkJ5wrQqecK0KAAAAB3NzaC1yc2 +EAAAGBANgZNzlUrlXD9FncOeycyjMSm396sATx2R6zDfDCe0v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