-
Notifications
You must be signed in to change notification settings - Fork 10
/
common_test.go
199 lines (192 loc) · 5.54 KB
/
common_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package bundle
import (
"testing"
"github.com/defenseunicorns/uds-cli/src/types"
zarfTypes "github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
)
func Test_validateBundleVars(t *testing.T) {
type args struct {
packages []types.Package
}
tests := []struct {
name string
description string
args args
wantErr bool
}{
{
name: "ImportMatchesExport",
description: "import matches export",
args: args{
packages: []types.Package{
{Name: "foo", Exports: []types.BundleVariableExport{{Name: "foo"}}},
{Name: "bar", Imports: []types.BundleVariableImport{{Name: "foo", Package: "foo"}}},
},
},
wantErr: false,
},
{
name: "ImportDoesntMatchExport",
description: "error when import doesn't match export",
args: args{
packages: []types.Package{
{Name: "foo", Exports: []types.BundleVariableExport{{Name: "foo"}}},
{Name: "bar", Imports: []types.BundleVariableImport{{Name: "bar", Package: "foo"}}},
},
},
wantErr: true,
},
{
name: "FirstPkgHasImport",
description: "error when first pkg has an import",
args: args{
packages: []types.Package{
{Name: "foo", Imports: []types.BundleVariableImport{{Name: "foo", Package: "foo"}}},
},
},
wantErr: true,
},
{
name: "PackageNamesMustMatch",
description: "error when package name doesn't match",
args: args{
packages: []types.Package{
{Name: "foo", Exports: []types.BundleVariableExport{{Name: "foo"}}},
{Name: "bar", Imports: []types.BundleVariableImport{{Name: "foo", Package: "baz"}}},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := validateBundleVars(tt.args.packages); (err != nil) != tt.wantErr {
t.Errorf("validateBundleVars() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_validateOverrides(t *testing.T) {
type args struct {
bundlePackage types.Package
zarfPackage zarfTypes.ZarfPackage
}
tests := []struct {
name string
description string
args args
wantErr bool
}{
{
name: "validOverride",
description: "Respective components and charts exist for override",
args: args{
bundlePackage: types.Package{
Name: "foo", Overrides: map[string]map[string]types.BundleChartOverrides{"component": {"chart": {}}}},
zarfPackage: zarfTypes.ZarfPackage{
Components: []zarfTypes.ZarfComponent{
{Name: "component", Charts: []zarfTypes.ZarfChart{{Name: "chart"}}},
},
}},
wantErr: false,
},
{
name: "validOverrideMultipleComponents",
description: "Respective components and charts exist for override when multiple charts and components are present",
args: args{
bundlePackage: types.Package{
Name: "foo", Overrides: map[string]map[string]types.BundleChartOverrides{"component-a": {"chart-1": {}}}},
zarfPackage: zarfTypes.ZarfPackage{
Components: []zarfTypes.ZarfComponent{
{Name: "component-a", Charts: []zarfTypes.ZarfChart{{Name: "chart-1"}, {Name: "chart-2"}}},
{Name: "component-b", Charts: []zarfTypes.ZarfChart{{Name: "chart-b"}}},
},
}},
wantErr: false,
},
{
name: "invalidComponentOverride",
description: "Component does not exist for override",
args: args{
bundlePackage: types.Package{
Name: "foo", Overrides: map[string]map[string]types.BundleChartOverrides{"hell-unleashed": {"chart": {}}}},
zarfPackage: zarfTypes.ZarfPackage{
Components: []zarfTypes.ZarfComponent{
{Name: "hello-world", Charts: []zarfTypes.ZarfChart{{Name: "chart"}}},
},
}},
wantErr: true,
},
{
name: "invalidChartOverride",
description: "Chart does not exist for override",
args: args{
bundlePackage: types.Package{
Name: "foo", Overrides: map[string]map[string]types.BundleChartOverrides{"component": {"hell-unleashed": {}}}},
zarfPackage: zarfTypes.ZarfPackage{
Components: []zarfTypes.ZarfComponent{
{Name: "component", Charts: []zarfTypes.ZarfChart{{Name: "hello-world"}}},
},
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := validateOverrides(tt.args.bundlePackage, tt.args.zarfPackage); (err != nil) != tt.wantErr {
t.Errorf("validateOverrides() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_getPkgPath(t *testing.T) {
type args struct {
pkg types.Package
arch string
}
tests := []struct {
name string
args args
want string
}{
{
name: "init full path",
args: args{
pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path/custom-init.tar.zst"},
arch: "fake64",
},
want: "../fake/path/custom-init.tar.zst",
},
{
name: "init directory only path",
args: args{
pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path"},
arch: "fake64",
},
want: "../fake/path/zarf-init-fake64-0.0.1.tar.zst",
},
{
name: "full path",
args: args{
pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst"},
arch: "fake64",
},
want: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst",
},
{
name: "directory only path",
args: args{
pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "/fake"},
arch: "fake64",
},
want: "/fake/zarf-package-nginx-fake64-0.0.1.tar.zst",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := getPkgPath(tt.args.pkg, tt.args.arch)
require.Equal(t, tt.want, path)
})
}
}