forked from microsoft/azure-linux-dev-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_test.go
More file actions
235 lines (186 loc) · 7 KB
/
component_test.go
File metadata and controls
235 lines (186 loc) · 7 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package projectconfig_test
import (
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/go-playground/validator/v10"
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestComponentGroupConfigWithAbsolutePaths(t *testing.T) {
const testRefDir = "/ref/dir"
t.Run("empty", func(t *testing.T) {
comp := projectconfig.ComponentGroupConfig{}
absComp := comp.WithAbsolutePaths(testRefDir)
require.Equal(t, comp, absComp)
})
t.Run("relative paths", func(t *testing.T) {
comp := projectconfig.ComponentGroupConfig{
SpecPathPatterns: []string{"dir/**/*.spec"},
}
absComp := comp.WithAbsolutePaths(testRefDir)
assert.NotEqual(t, comp, absComp)
assert.Equal(t, []string{"/ref/dir/dir/**/*.spec"}, absComp.SpecPathPatterns)
})
}
func TestComponentConfigWithAbsolutePaths(t *testing.T) {
const testRefDir = "/ref/dir"
t.Run("empty", func(t *testing.T) {
comp := projectconfig.ComponentConfig{}
absComp := *comp.WithAbsolutePaths(testRefDir)
require.Equal(t, comp, absComp)
})
t.Run("project file ptr", func(t *testing.T) {
comp := projectconfig.ComponentConfig{
SourceConfigFile: &projectconfig.ConfigFile{},
}
absComp := comp.WithAbsolutePaths(testRefDir)
// We *require* that the SourceConfigFile pointer is aliased. Deep-copying it would
// be cost-prohibitive and unnecessary.
require.Equal(t, comp.SourceConfigFile, absComp.SourceConfigFile)
})
t.Run("relative paths", func(t *testing.T) {
comp := projectconfig.ComponentConfig{
Name: "test",
Spec: projectconfig.SpecSource{
SourceType: projectconfig.SpecSourceTypeLocal,
Path: "file.spec",
},
}
absComp := *comp.WithAbsolutePaths(testRefDir)
assert.Equal(t, comp.Name, absComp.Name)
assert.Equal(t, comp.Spec.SourceType, absComp.Spec.SourceType)
assert.Equal(t, filepath.Join(testRefDir, comp.Spec.Path), absComp.Spec.Path)
})
t.Run("absolute paths", func(t *testing.T) {
comp := projectconfig.ComponentConfig{
Name: "test",
Spec: projectconfig.SpecSource{
SourceType: projectconfig.SpecSourceTypeLocal,
Path: "/some/file.spec",
},
}
absComp := *comp.WithAbsolutePaths(testRefDir)
require.Equal(t, comp, absComp)
})
t.Run("overlays", func(t *testing.T) {
comp := projectconfig.ComponentConfig{
Name: "test",
Overlays: []projectconfig.ComponentOverlay{
{
Type: projectconfig.ComponentOverlayAddFile,
Source: "somefile.txt",
},
},
}
absComp := *comp.WithAbsolutePaths(testRefDir)
require.Equal(t, comp.Name, absComp.Name)
require.Len(t, absComp.Overlays, 1)
require.Equal(t, comp.Overlays[0].Type, absComp.Overlays[0].Type)
require.Equal(t, filepath.Join(testRefDir, comp.Overlays[0].Source), absComp.Overlays[0].Source)
})
}
func TestComponentGroupConfigWithAbsolutePaths_DefaultComponentConfig(t *testing.T) {
const testRefDir = "/ref/dir"
t.Run("default config with relative spec path", func(t *testing.T) {
group := projectconfig.ComponentGroupConfig{
Components: []string{"comp-a"},
DefaultComponentConfig: projectconfig.ComponentConfig{
Spec: projectconfig.SpecSource{
SourceType: projectconfig.SpecSourceTypeLocal,
Path: "specs/test.spec",
},
},
}
absGroup := group.WithAbsolutePaths(testRefDir)
// The default component config's spec path should be made absolute.
assert.Equal(t, "/ref/dir/specs/test.spec", absGroup.DefaultComponentConfig.Spec.Path)
// Members should be preserved.
assert.Equal(t, []string{"comp-a"}, absGroup.Components)
})
t.Run("default config with empty fields", func(t *testing.T) {
group := projectconfig.ComponentGroupConfig{
Components: []string{"comp-a"},
DefaultComponentConfig: projectconfig.ComponentConfig{},
}
absGroup := group.WithAbsolutePaths(testRefDir)
// Empty default config should remain empty.
assert.Equal(t, projectconfig.ComponentConfig{}, absGroup.DefaultComponentConfig)
})
t.Run("default config with build settings", func(t *testing.T) {
group := projectconfig.ComponentGroupConfig{
DefaultComponentConfig: projectconfig.ComponentConfig{
Build: projectconfig.ComponentBuildConfig{
With: []string{"tests"},
Without: []string{"docs"},
},
},
}
absGroup := group.WithAbsolutePaths(testRefDir)
// Build config should be preserved as-is (no paths to fix).
assert.Equal(t, []string{"tests"}, absGroup.DefaultComponentConfig.Build.With)
assert.Equal(t, []string{"docs"}, absGroup.DefaultComponentConfig.Build.Without)
})
}
func TestMergeComponentUpdates(t *testing.T) {
base := projectconfig.ComponentConfig{
Build: projectconfig.ComponentBuildConfig{
Without: []string{"x", "y"},
},
}
updates := projectconfig.ComponentConfig{
Name: "test",
Build: projectconfig.ComponentBuildConfig{
Without: []string{"w"},
},
}
err := base.MergeUpdatesFrom(&updates)
require.NoError(t, err)
require.Equal(t, "test", base.Name)
require.Equal(t, []string{"x", "y", "w"}, base.Build.Without)
}
func TestAllowedSourceFilesHashTypes_MatchesJSONSchemaEnum(t *testing.T) {
// Extract enum values from the jsonschema tag on
// [projectconfig.SourceFileReference.HashType].
field, ok := reflect.TypeOf(projectconfig.SourceFileReference{}).FieldByName("HashType")
require.True(t, ok, "SourceFileReference must have a 'HashType' field")
tag := field.Tag.Get("jsonschema")
require.NotEmpty(t, tag, "HashType field must have a 'jsonschema' tag")
// Parse "enum=X,enum=Y,..." entries from the tag.
var schemaEnums []string
for _, part := range strings.Split(tag, ",") {
if strings.HasPrefix(part, "enum=") {
schemaEnums = append(schemaEnums, strings.TrimPrefix(part, "enum="))
}
}
assert.Len(t, schemaEnums, len(projectconfig.AllowedSourceFilesHashTypes),
"number of 'enum=' entries in 'jsonschema' tag must match number of entries in 'AllowedSourceFilesHashTypes'")
// Every enum value must be present in AllowedSourceFilesHashTypes.
for _, enumVal := range schemaEnums {
hashType := fileutils.HashType(enumVal)
assert.True(t, projectconfig.AllowedSourceFilesHashTypes[hashType],
"'jsonschema' enum value %#q is not in 'AllowedSourceFilesHashTypes'", enumVal)
}
}
func TestReleaseCalculationValidation(t *testing.T) {
validate := validator.New()
// Empty (omitted) is valid — resolved to "auto" by the component resolver.
require.NoError(t, validate.Struct(&projectconfig.ComponentConfig{}))
// Explicit "auto" is valid.
require.NoError(t, validate.Struct(&projectconfig.ComponentConfig{
ReleaseCalculation: projectconfig.ReleaseCalculationAuto,
}))
// Explicit "manual" is valid.
require.NoError(t, validate.Struct(&projectconfig.ComponentConfig{
ReleaseCalculation: projectconfig.ReleaseCalculationManual,
}))
// Invalid value is rejected.
require.Error(t, validate.Struct(&projectconfig.ComponentConfig{
ReleaseCalculation: "manaul",
}))
}