-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathcharts_test.go
169 lines (147 loc) · 4.93 KB
/
charts_test.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package helm
import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/Masterminds/semver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseReq(t *testing.T) {
testCases := []struct {
name string
input string
expected *Requirement
err error
}{
{
name: "valid",
input: "stable/[email protected]",
expected: &Requirement{
Chart: "stable/package",
Version: *semver.MustParse("1.0.0"),
Directory: "",
},
},
{
name: "invalid-semver",
input: "stable/package@not-semver",
err: fmt.Errorf("version is invalid: not-semver"),
},
{
name: "with-path",
input: "stable/[email protected]:my-path",
expected: &Requirement{
Chart: "stable/package-name",
Version: *semver.MustParse("1.0.0"),
Directory: "my-path",
},
},
{
name: "with-path-with-special-chars",
input: "stable/[email protected]:my weird-path_test",
expected: &Requirement{
Chart: "stable/package",
Version: *semver.MustParse("v1.24.0"),
Directory: "my weird-path_test",
},
},
{
name: "url-instead-of-repo",
input: "https://helm.releases.hashicorp.com/[email protected]",
err: errors.New("not of form 'repo/chart@version(:path)' where repo contains no special characters"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req, err := parseReq(tc.input)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.expected, req)
})
}
}
func TestAddRepos(t *testing.T) {
c, err := InitChartfile(filepath.Join(t.TempDir(), Filename))
require.NoError(t, err)
err = c.AddRepos(
Repo{Name: "foo", URL: "https://foo.com"},
Repo{Name: "foo2", URL: "https://foo2.com"},
)
assert.NoError(t, err)
// Only \w characters are allowed in repo names
err = c.AddRepos(
Repo{Name: "with-dashes", URL: "https://foo.com"},
)
assert.EqualError(t, err, "1 Repo(s) were skipped. Please check above logs for details")
err = c.AddRepos(
Repo{Name: "foo", URL: "https://foo.com"},
)
assert.EqualError(t, err, "1 Repo(s) were skipped. Please check above logs for details")
}
func TestAdd(t *testing.T) {
tempDir := t.TempDir()
c, err := InitChartfile(filepath.Join(tempDir, Filename))
require.NoError(t, err)
err = c.Add([]string{"stable/[email protected]"})
assert.NoError(t, err)
// Adding again the same chart
err = c.Add([]string{"stable/[email protected]"})
assert.EqualError(t, err, "1 Chart(s) were skipped. Please check above logs for details")
// Adding a chart with a different version to the same path, causes a conflict
err = c.Add([]string{"stable/[email protected]"})
assert.EqualError(t, err, `Output directory conflicts found:
- output directory "prometheus" is used twice, by charts "stable/[email protected]" and "stable/[email protected]"`)
// Add a chart with a specific extract directory
err = c.Add([]string{"stable/[email protected]:prometheus-11.12.0"})
assert.NoError(t, err)
// Check file contents
listResult, err := os.ReadDir(filepath.Join(tempDir, "charts"))
assert.NoError(t, err)
assert.Equal(t, 2, len(listResult))
assert.Equal(t, "prometheus", listResult[0].Name())
assert.Equal(t, "prometheus-11.12.0", listResult[1].Name())
chartContent, err := os.ReadFile(filepath.Join(tempDir, "charts", "prometheus", "Chart.yaml"))
assert.NoError(t, err)
assert.Contains(t, string(chartContent), `version: 11.12.1`)
chartContent, err = os.ReadFile(filepath.Join(tempDir, "charts", "prometheus-11.12.0", "Chart.yaml"))
assert.NoError(t, err)
assert.Contains(t, string(chartContent), `version: 11.12.0`)
}
func TestPrune(t *testing.T) {
for _, prune := range []bool{false, true} {
t.Run(fmt.Sprintf("%t", prune), func(t *testing.T) {
tempDir := t.TempDir()
c, err := InitChartfile(filepath.Join(tempDir, Filename))
require.NoError(t, err)
// Add a chart
err = c.Add([]string{"stable/[email protected]"})
require.NoError(t, err)
// Add a chart with a directory
err = c.Add([]string{"stable/[email protected]:custom-dir"})
require.NoError(t, err)
// Add unrelated files and folders
err = os.WriteFile(filepath.Join(tempDir, "charts", "foo.txt"), []byte("foo"), 0644)
err = os.Mkdir(filepath.Join(tempDir, "charts", "foo"), 0755)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tempDir, "charts", "foo", "Chart.yaml"), []byte("foo"), 0644)
require.NoError(t, err)
err = c.Vendor(prune)
require.NoError(t, err)
// Check if files are pruned
listResult, err := os.ReadDir(filepath.Join(tempDir, "charts"))
assert.NoError(t, err)
if prune {
assert.Equal(t, 2, len(listResult))
assert.Equal(t, "custom-dir", listResult[0].Name())
assert.Equal(t, "prometheus", listResult[1].Name())
} else {
assert.Equal(t, 4, len(listResult))
chartContent, err := os.ReadFile(filepath.Join(tempDir, "charts", "foo", "Chart.yaml"))
assert.NoError(t, err)
assert.Contains(t, string(chartContent), `foo`)
}
})
}
}