-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap_test.go
More file actions
104 lines (84 loc) · 3.14 KB
/
Copy pathbootstrap_test.go
File metadata and controls
104 lines (84 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cmd
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/user-cube/cluster-bootstrap/cli/internal/config"
)
func TestBuildDryRunObjects(t *testing.T) {
envSecrets := &config.EnvironmentSecrets{
Repo: config.RepoSecrets{
URL: "ssh://git@example.com/repo.git",
TargetRevision: "main",
SSHPrivateKey: "test-key",
},
}
repoSecret, appOfApps := buildDryRunObjects(envSecrets, "dev", "apps")
metadata, ok := repoSecret["metadata"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "repo-ssh-key", metadata["name"])
appSpec, ok := appOfApps["spec"].(map[string]interface{})
require.True(t, ok)
source, ok := appSpec["source"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "apps", source["path"])
assert.Equal(t, "main", source["targetRevision"])
}
func TestRenderDryRunOutput_Golden(t *testing.T) {
envSecrets := &config.EnvironmentSecrets{
Repo: config.RepoSecrets{
URL: "ssh://git@example.com/repo.git",
TargetRevision: "main",
SSHPrivateKey: "test-key",
},
}
output, err := renderDryRunOutput(envSecrets, "dev", "apps")
require.NoError(t, err)
goldenPath := filepath.Join("testdata", "dry-run.dev.golden.txt")
golden, err := os.ReadFile(goldenPath)
require.NoError(t, err)
assert.Equal(t, string(golden), output)
}
func TestValidateBootstrapInputs(t *testing.T) {
prevBaseDir := baseDir
prevAppPath := appPath
prevEncryption := encryption
prevSecretsFile := secretsFile
t.Cleanup(func() {
baseDir = prevBaseDir
appPath = prevAppPath
encryption = prevEncryption
secretsFile = prevSecretsFile
})
tmpDir := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "apps"), 0755))
baseDir = tmpDir
appPath = "apps"
encryption = "sops"
secretsFile = filepath.Join(tmpDir, "secrets.dev.enc.yaml")
_, err := validateBootstrapInputs("dev", "apps")
require.NoError(t, err)
secretsFile = filepath.Join(tmpDir, "secrets.dev.yaml")
_, err = validateBootstrapInputs("dev", "apps")
assert.ErrorContains(t, err, "must end with .enc.yaml")
encryption = "git-crypt"
secretsFile = filepath.Join(tmpDir, "secrets.dev.enc.yaml")
_, err = validateBootstrapInputs("dev", "apps")
assert.ErrorContains(t, err, "not .enc.yaml")
_, err = validateBootstrapInputs("dev", "/abs/path")
assert.ErrorContains(t, err, "app-path must be relative")
appPath = "apps"
encryption = "sops"
secretsFile = filepath.Join(tmpDir, "secrets.dev.enc.yaml")
require.NoError(t, os.RemoveAll(filepath.Join(tmpDir, "apps")))
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "k8s", "apps", "templates"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "k8s", "apps", "Chart.yaml"), []byte("apiVersion: v2\n"), 0644))
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "k8s", "apps", "templates", "application.yaml"), []byte("kind: Application\n"), 0644))
// Test with baseDir pointing to k8s subfolder (simulating --base-dir ./k8s)
baseDir = filepath.Join(tmpDir, "k8s")
localPath, err := validateBootstrapInputs("dev", "k8s/apps")
require.NoError(t, err)
assert.Equal(t, "apps", localPath)
}