Skip to content

Commit e5aca42

Browse files
achilleas-ksupakeen
authored andcommitted
blueprint: add error conditions in GetGroups() test
Add validation error test cases to TestGetGroups().
1 parent 850f371 commit e5aca42

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

pkg/blueprint/users_groups_customizations_test.go

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func TestGetUsers(t *testing.T) {
6666

6767
func TestGetGroups(t *testing.T) {
6868
type testCase struct {
69-
groups []GroupCustomization
69+
groups []GroupCustomization
70+
expectedErrorMessage string
7071
}
7172

7273
testCases := map[string]testCase{
@@ -100,17 +101,102 @@ func TestGetGroups(t *testing.T) {
100101
},
101102
},
102103
},
104+
"duplicate-names": {
105+
groups: []GroupCustomization{
106+
{
107+
Name: "TestGroup",
108+
GID: common.ToPtr(1234),
109+
},
110+
{
111+
Name: "sysgrp",
112+
GID: common.ToPtr(998),
113+
},
114+
{
115+
Name: "wheel",
116+
GID: common.ToPtr(42),
117+
},
118+
{
119+
Name: "wheel",
120+
GID: common.ToPtr(43),
121+
},
122+
},
123+
expectedErrorMessage: "invalid group customizations:\nduplicate group name: wheel",
124+
},
125+
"duplicate-gids": {
126+
groups: []GroupCustomization{
127+
{
128+
Name: "TestGroup",
129+
GID: common.ToPtr(1234),
130+
},
131+
{
132+
Name: "sysgrp",
133+
GID: common.ToPtr(42),
134+
},
135+
{
136+
Name: "wheel",
137+
GID: common.ToPtr(42),
138+
},
139+
},
140+
expectedErrorMessage: "invalid group customizations:\nduplicate group ID: 42",
141+
},
142+
"duplicate-both": {
143+
groups: []GroupCustomization{
144+
{
145+
Name: "TestGroup",
146+
GID: common.ToPtr(1234),
147+
},
148+
{
149+
Name: "wheel",
150+
GID: common.ToPtr(42),
151+
},
152+
{
153+
Name: "wheel",
154+
GID: common.ToPtr(42),
155+
},
156+
},
157+
expectedErrorMessage: "invalid group customizations:\nduplicate group name: wheel\nduplicate group ID: 42",
158+
},
159+
"duplicate-multi": {
160+
groups: []GroupCustomization{
161+
{
162+
Name: "test",
163+
GID: common.ToPtr(1234),
164+
},
165+
{
166+
Name: "wheel",
167+
GID: common.ToPtr(42),
168+
},
169+
{
170+
Name: "wheel",
171+
GID: common.ToPtr(42),
172+
},
173+
{
174+
Name: "user",
175+
GID: common.ToPtr(1234),
176+
},
177+
{
178+
Name: "test",
179+
GID: common.ToPtr(4321),
180+
},
181+
},
182+
expectedErrorMessage: "invalid group customizations:\nduplicate group name: wheel\nduplicate group ID: 42\nduplicate group ID: 1234\nduplicate group name: test",
183+
},
103184
}
104185

105186
for name, tc := range testCases {
106187
t.Run(name, func(t *testing.T) {
188+
assert := assert.New(t)
107189
c := Customizations{
108190
Group: tc.groups,
109191
}
110192

111193
groups, err := c.GetGroups()
112-
assert.NoError(t, err)
113-
assert.ElementsMatch(t, tc.groups, groups)
194+
if tc.expectedErrorMessage != "" {
195+
assert.EqualError(err, tc.expectedErrorMessage)
196+
} else {
197+
assert.NoError(err)
198+
assert.ElementsMatch(tc.groups, groups)
199+
}
114200
})
115201
}
116202
}

0 commit comments

Comments
 (0)