|
| 1 | +package ci_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func requireContains(t *testing.T, path string, want string) { |
| 11 | + t.Helper() |
| 12 | + |
| 13 | + content, err := os.ReadFile(path) |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("read %s: %v", path, err) |
| 16 | + } |
| 17 | + |
| 18 | + if !strings.Contains(string(content), want) { |
| 19 | + t.Fatalf("expected %s to contain %q", path, want) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func requireNotContains(t *testing.T, path string, want string) { |
| 24 | + t.Helper() |
| 25 | + |
| 26 | + content, err := os.ReadFile(path) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("read %s: %v", path, err) |
| 29 | + } |
| 30 | + |
| 31 | + if strings.Contains(string(content), want) { |
| 32 | + t.Fatalf("expected %s not to contain %q", path, want) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func readVariableBlock(t *testing.T, path string, variableName string) string { |
| 37 | + t.Helper() |
| 38 | + |
| 39 | + content, err := os.ReadFile(path) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("read %s: %v", path, err) |
| 42 | + } |
| 43 | + |
| 44 | + marker := "variable \"" + variableName + "\" {" |
| 45 | + src := string(content) |
| 46 | + start := strings.Index(src, marker) |
| 47 | + if start == -1 { |
| 48 | + t.Fatalf("could not find variable %q in %s", variableName, path) |
| 49 | + } |
| 50 | + |
| 51 | + braceDepth := 0 |
| 52 | + for i := start; i < len(src); i++ { |
| 53 | + switch src[i] { |
| 54 | + case '{': |
| 55 | + braceDepth++ |
| 56 | + case '}': |
| 57 | + braceDepth-- |
| 58 | + if braceDepth == 0 { |
| 59 | + return src[start : i+1] |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + t.Fatalf("unterminated variable block for %q in %s", variableName, path) |
| 65 | + return "" |
| 66 | +} |
| 67 | + |
| 68 | +func TestLinuxCiDefaultsUseBootstrapScript(t *testing.T) { |
| 69 | + t.Helper() |
| 70 | + |
| 71 | + requireContains(t, "variables.tf", "default = \"scripts/bootstrap-buildkite-agent.sh\"") |
| 72 | + requireContains(t, filepath.Join("..", "..", "modules", "linux-ci", "variables.tf"), "default = \"scripts/bootstrap-buildkite-agent.sh\"") |
| 73 | + requireContains(t, "terraform.tfvars.example", "setup_script_path = \"scripts/bootstrap-buildkite-agent.sh\"") |
| 74 | +} |
| 75 | + |
| 76 | +func TestBootstrapScriptConfiguresBuildkiteAgent(t *testing.T) { |
| 77 | + t.Helper() |
| 78 | + |
| 79 | + scriptPath := filepath.Join("..", "..", "..", "..", "scripts", "bootstrap-buildkite-agent.sh") |
| 80 | + requireContains(t, scriptPath, "buildkite-agent start") |
| 81 | + requireContains(t, scriptPath, "BUILDKITE_TOKEN_PARAM") |
| 82 | +} |
| 83 | + |
| 84 | +func TestUserDataInstallsAwsCliWithoutAptAwscliDependency(t *testing.T) { |
| 85 | + t.Helper() |
| 86 | + |
| 87 | + templatePath := filepath.Join("..", "..", "modules", "linux-ci", "templates", "user_data.sh.tftpl") |
| 88 | + requireNotContains(t, templatePath, "apt-get install -y git jq curl tar ca-certificates openssh-client awscli") |
| 89 | + requireContains(t, templatePath, "awscli-exe-linux") |
| 90 | +} |
| 91 | + |
| 92 | +func TestLinuxCiEnablesNestedVirtualization(t *testing.T) { |
| 93 | + t.Helper() |
| 94 | + |
| 95 | + moduleMainPath := filepath.Join("..", "..", "modules", "linux-ci", "main.tf") |
| 96 | + requireContains(t, moduleMainPath, "nested_virtualization = \"enabled\"") |
| 97 | +} |
| 98 | + |
| 99 | +func TestGitDeployKeyIsRequired(t *testing.T) { |
| 100 | + t.Helper() |
| 101 | + |
| 102 | + files := []string{ |
| 103 | + "variables.tf", |
| 104 | + filepath.Join("..", "..", "modules", "linux-ci", "variables.tf"), |
| 105 | + } |
| 106 | + |
| 107 | + for _, path := range files { |
| 108 | + block := readVariableBlock(t, path, "git_deploy_key_parameter_name") |
| 109 | + |
| 110 | + if strings.Contains(block, "default") { |
| 111 | + t.Fatalf("expected git_deploy_key_parameter_name to have no default in %s", path) |
| 112 | + } |
| 113 | + |
| 114 | + if !strings.Contains(block, "trimspace(var.git_deploy_key_parameter_name) != \"\"") { |
| 115 | + t.Fatalf("expected git_deploy_key_parameter_name validation in %s", path) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments