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 support for strip_component #204

Merged
merged 8 commits into from
Nov 18, 2022
Merged
1 change: 1 addition & 0 deletions g10k.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type Source struct {
AutoCorrectEnvironmentNames string `yaml:"invalid_branches"`
FilterCommand string `yaml:"filter_command"`
FilterRegex string `yaml:"filter_regex"`
StripComponent string `yaml:"strip_component"`
}

// Puppetfile contains the key value pairs from the Puppetfile
Expand Down
141 changes: 141 additions & 0 deletions g10k_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3080,3 +3080,144 @@ func TestPurgeControlRepoExceptModuledir(t *testing.T) {
t.Errorf("terminated with the correct exit code and the correct output, but the resulting module was missing")
}
}

func TestStripComponentString(t *testing.T) {
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
config = readConfigfile(filepath.Join("tests", "TestConfigStripComponentString.yaml"))
branchParam = ""
if os.Getenv("TEST_FOR_CRASH_"+funcName) == "1" {
debug = true
info = true
resolvePuppetEnvironment(false, "")
return
}
purgeDir("/tmp/strip/", funcName)

cmd := exec.Command(os.Args[0], "-test.run="+funcName+"$")
cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+funcName+"=1")
out, 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))
expectedLines := []string{
"Resolving environment env/test of source strip",
"Renaming branch env/test to test, because of strip_component in source strip https://github.com/xorpaul/g10k-environment-strip-component.git",
"Need to sync /tmp/strip/test",
"Need to sync /tmp/strip/test/external_modules/apt",
// also check that the non-prefix part has not been renamed
"Need to sync /tmp/strip/prefix_env_foobar/external_modules/apt",
}
for _, expectedLine := range expectedLines {
if !strings.Contains(string(out), expectedLine) {
t.Errorf("Could not find expected line '" + expectedLine + "' in output")
}
}

expectedFiles := []string{
"/tmp/strip/test/external_modules/apt/metadata.json",
"/tmp/strip/prefix_env_foobar/external_modules/apt/metadata.json"}

for _, expectedFile := range expectedFiles {
if !fileExists(expectedFile) {
t.Errorf("files and/or directory missing that should be there! " + expectedFile)
}
}

}

func TestStripComponentRegex(t *testing.T) {
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
config = readConfigfile(filepath.Join("tests", "TestConfigStripComponentRegex.yaml"))
branchParam = ""
if os.Getenv("TEST_FOR_CRASH_"+funcName) == "1" {
debug = true
info = true
resolvePuppetEnvironment(false, "")
return
}
purgeDir("/tmp/strip/", funcName)

cmd := exec.Command(os.Args[0], "-test.run="+funcName+"$")
cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+funcName+"=1")
out, 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))
expectedLines := []string{
"Resolving environment env/test of source strip",
"Resolving environment prefix/env2/foo of source strip",
"Renaming branch prefix/env2/foo to prefixfoo, because of strip_component in source strip",
"Need to sync /tmp/strip/prefixfoobar/external_modules/apt",
// also check that the non-regex part has not been renamed
"Need to sync /tmp/strip/env_test",
"Need to sync /tmp/strip/env_test/external_modules/apt",
}
for _, expectedLine := range expectedLines {
if !strings.Contains(string(out), expectedLine) {
t.Errorf("Could not find expected line '" + expectedLine + "' in output")
}
}

expectedFiles := []string{
"/tmp/strip/env_test/external_modules/apt/metadata.json",
"/tmp/strip/prefixfoobar/external_modules/apt/metadata.json"}

for _, expectedFile := range expectedFiles {
if !fileExists(expectedFile) {
t.Errorf("files and/or directory missing that should be there! " + expectedFile)
}
}
}

func TestStripComponentConflict(t *testing.T) {
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
config = readConfigfile(filepath.Join("tests", "TestConfigStripComponentStringConflict.yaml"))
branchParam = ""
if os.Getenv("TEST_FOR_CRASH_"+funcName) == "1" {
debug = true
info = true
resolvePuppetEnvironment(false, "")
return
}
purgeDir("/tmp/strip/", funcName)

cmd := exec.Command(os.Args[0], "-test.run="+funcName+"$")
cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+funcName+"=1")
out, err := cmd.CombinedOutput()

exitCode := 0
if msg, ok := err.(*exec.ExitError); ok { // there is error code
exitCode = msg.Sys().(syscall.WaitStatus).ExitStatus()
}

expectedExitCode := 1
if exitCode != expectedExitCode {
t.Errorf("terminated with %v, but we expected exit status %v", exitCode, expectedExitCode)
}
// fmt.Println(string(out))

expectedLines := []string{
"Renamed environment naming conflict detected with renamed environment main",
}
for _, expectedLine := range expectedLines {
if !strings.Contains(string(out), expectedLine) {
t.Errorf("Could not find expected line '" + expectedLine + "' in output")
}
}
}
9 changes: 9 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -323,3 +324,11 @@ func readDeployResultFile(file string) DeployResult {
return dr

}

func stripComponent(component string, env string) string {
if regexp.MustCompile(`^/.*/$`).MatchString(component) {
return regexp.MustCompile(component[1:len(component)-1]).ReplaceAllString(env, "")
} else {
return strings.TrimPrefix(env, component)
}
}
16 changes: 15 additions & 1 deletion puppetfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ func resolvePuppetEnvironment(tags bool, outputNameTag string) {
Debugf("Renaming branch " + branch + " to " + renamedBranch + " from source " + source + " " + sa.Remote)
}

// https://github.com/puppetlabs/r10k/blob/main/doc/dynamic-environments/configuration.mkd#strip_component
if len(sa.StripComponent) != 0 {
stripRenamedBranch := stripComponent(sa.StripComponent, renamedBranch)
if stripRenamedBranch != renamedBranch {
// only print this if the branch was definately renamed, because of the strip component
Debugf("Renaming branch " + renamedBranch + " to " + stripRenamedBranch + ", because of strip_component in source " + source + " " + sa.Remote)
renamedBranch = stripRenamedBranch
}
}

if sa.AutoCorrectEnvironmentNames == "correct" || sa.AutoCorrectEnvironmentNames == "correct_and_warn" {
oldBranch := renamedBranch
renamedBranch = reInvalidCharacters.ReplaceAllString(renamedBranch, "_")
Expand All @@ -141,7 +151,11 @@ func resolvePuppetEnvironment(tags bool, outputNameTag string) {
}

mutex.Lock()
allEnvironments[prefix+renamedBranch] = true
if _, ok := allEnvironments[prefix+renamedBranch]; !ok {
allEnvironments[prefix+renamedBranch] = true
} else {
Fatalf("Renamed environment naming conflict detected with renamed environment " + prefix + renamedBranch)
}
mutex.Unlock()
targetDir := filepath.Join(sa.Basedir, prefix+strings.Replace(renamedBranch, "/", "_", -1))
targetDir = normalizeDir(targetDir)
Expand Down
9 changes: 9 additions & 0 deletions tests/TestConfigStripComponentRegex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
:cachedir: '/tmp/g10k'
use_cache_fallback: true

sources:
strip:
remote: 'https://github.com/xorpaul/g10k-environment-strip-component.git'
basedir: '/tmp/strip/'
strip_component: '/\/env\d*\//'
9 changes: 9 additions & 0 deletions tests/TestConfigStripComponentString.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
:cachedir: '/tmp/g10k'
use_cache_fallback: true

sources:
strip:
remote: 'https://github.com/xorpaul/g10k-environment-strip-component.git'
basedir: '/tmp/strip/'
strip_component: 'env/'
9 changes: 9 additions & 0 deletions tests/TestConfigStripComponentStringConflict.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
:cachedir: '/tmp/g10k'
use_cache_fallback: true

sources:
strip:
remote: 'https://github.com/xorpaul/g10k-environment-strip-component-naming-conflict.git'
basedir: '/tmp/strip/'
strip_component: 'prefix/'