Skip to content

Commit 69b42f1

Browse files
committed
tests: Use GoldenTracker to check if golden files are being used
1 parent b7759d9 commit 69b42f1

File tree

10 files changed

+163
-59
lines changed

10 files changed

+163
-59
lines changed

internal/brokers/broker_test.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ var supportedLayouts = map[string]map[string]string{
3939
func TestNewBroker(t *testing.T) {
4040
t.Parallel()
4141

42+
goldenTracker := testutils.NewGoldenTracker(t)
43+
4244
tests := map[string]struct {
4345
configFile string
4446

@@ -83,7 +85,8 @@ func TestNewBroker(t *testing.T) {
8385

8486
gotString := fmt.Sprintf("ID: %s\nName: %s\nBrand Icon: %s\n", got.ID, got.Name, got.BrandIconPath)
8587

86-
wantString := testutils.LoadWithUpdateFromGolden(t, gotString)
88+
wantString := testutils.LoadWithUpdateFromGolden(t, gotString,
89+
testutils.WithGoldenTracker(&goldenTracker))
8790
require.Equal(t, wantString, gotString, "NewBroker should return the expected broker, but did not")
8891
})
8992
}
@@ -93,6 +96,7 @@ func TestGetAuthenticationModes(t *testing.T) {
9396
t.Parallel()
9497

9598
b := newBrokerForTests(t, "", "")
99+
goldenTracker := testutils.NewGoldenTracker(t)
96100

97101
tests := map[string]struct {
98102
sessionID string
@@ -135,7 +139,8 @@ func TestGetAuthenticationModes(t *testing.T) {
135139
require.NoError(t, err, "Post: error when marshaling result")
136140

137141
got := "MODES:\n" + string(modesStr) + "\n\nVALIDATORS:\n" + b.LayoutValidatorsString(prefixID(t, tc.sessionID))
138-
want := testutils.LoadWithUpdateFromGolden(t, got)
142+
want := testutils.LoadWithUpdateFromGolden(t, got,
143+
testutils.WithGoldenTracker(&goldenTracker))
139144
require.Equal(t, want, got, "GetAuthenticationModes should return the expected modes, but did not")
140145
})
141146
}
@@ -145,6 +150,7 @@ func TestSelectAuthenticationMode(t *testing.T) {
145150
t.Parallel()
146151

147152
b := newBrokerForTests(t, "", "")
153+
goldenTracker := testutils.NewGoldenTracker(t)
148154

149155
tests := map[string]struct {
150156
sessionID string
@@ -195,7 +201,8 @@ func TestSelectAuthenticationMode(t *testing.T) {
195201
}
196202
require.NoError(t, err, "SelectAuthenticationMode should not return an error, but did")
197203

198-
wantUI := testutils.LoadWithUpdateFromGoldenYAML(t, gotUI)
204+
wantUI := testutils.LoadWithUpdateFromGoldenYAML(t, gotUI,
205+
testutils.WithGoldenTracker(&goldenTracker))
199206
require.Equal(t, wantUI, gotUI, "SelectAuthenticationMode should return the expected mode UI, but did not")
200207
})
201208
}
@@ -205,6 +212,7 @@ func TestIsAuthenticated(t *testing.T) {
205212
t.Parallel()
206213

207214
b := newBrokerForTests(t, "", "")
215+
goldenTracker := testutils.NewGoldenTracker(t)
208216

209217
tests := map[string]struct {
210218
sessionID string
@@ -274,7 +282,8 @@ func TestIsAuthenticated(t *testing.T) {
274282

275283
<-done
276284
gotStr := firstCallReturn + secondCallReturn
277-
want := testutils.LoadWithUpdateFromGolden(t, gotStr)
285+
want := testutils.LoadWithUpdateFromGolden(t, gotStr,
286+
testutils.WithGoldenTracker(&goldenTracker))
278287
require.Equal(t, want, gotStr, "IsAuthenticated should return the expected combined data, but did not")
279288
})
280289
}
@@ -321,6 +330,7 @@ func TestUserPreCheck(t *testing.T) {
321330
t.Parallel()
322331

323332
b := newBrokerForTests(t, "", "")
333+
goldenTracker := testutils.NewGoldenTracker(t)
324334

325335
tests := map[string]struct {
326336
username string
@@ -342,7 +352,8 @@ func TestUserPreCheck(t *testing.T) {
342352
}
343353
require.NoError(t, err, "UserPreCheck should not return an error, but did")
344354

345-
want := testutils.LoadWithUpdateFromGolden(t, got)
355+
want := testutils.LoadWithUpdateFromGolden(t, got,
356+
testutils.WithGoldenTracker(&goldenTracker))
346357
require.Equal(t, want, got, "UserPreCheck should return the expected data, but did not")
347358
})
348359
}

internal/brokers/internal_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ var (
6363
func TestUnmarshalUserInfo(t *testing.T) {
6464
t.Parallel()
6565

66+
goldenTracker := testutils.NewGoldenTracker(t)
67+
6668
tests := map[string]struct {
6769
jsonInput string
6870

@@ -89,7 +91,8 @@ func TestUnmarshalUserInfo(t *testing.T) {
8991
gotJSON, err := json.Marshal(got)
9092
require.NoError(t, err, "Marshaling the result should not return an error, but did")
9193

92-
want := testutils.LoadWithUpdateFromGolden(t, string(gotJSON))
94+
want := testutils.LoadWithUpdateFromGolden(t, string(gotJSON),
95+
testutils.WithGoldenTracker(&goldenTracker))
9396
require.Equal(t, want, string(gotJSON), "unmarshalUserInfo should return the expected format, but did not")
9497
})
9598
}

internal/brokers/manager_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
)
2020

2121
func TestNewManager(t *testing.T) {
22+
goldenTracker := testutils.NewGoldenTracker(t)
2223
tests := map[string]struct {
2324
brokerConfigDir string
2425
configuredBrokers []string
@@ -58,7 +59,8 @@ func TestNewManager(t *testing.T) {
5859
brokers = append(brokers, broker.Name)
5960
}
6061

61-
want := testutils.LoadWithUpdateFromGoldenYAML(t, brokers)
62+
want := testutils.LoadWithUpdateFromGoldenYAML(t, brokers,
63+
testutils.WithGoldenTracker(&goldenTracker))
6264
require.Equal(t, want, brokers, "NewManager should return the expected brokers, but did not")
6365
})
6466
}
@@ -169,6 +171,7 @@ func TestBrokerFromSessionID(t *testing.T) {
169171
func TestNewSession(t *testing.T) {
170172
t.Parallel()
171173

174+
goldenTracker := testutils.NewGoldenTracker(t)
172175
tests := map[string]struct {
173176
brokerID string
174177
username string
@@ -244,7 +247,8 @@ func TestNewSession(t *testing.T) {
244247

245248
// Replaces the autogenerated part of the ID with a placeholder before saving the file.
246249
gotStr := fmt.Sprintf("ID: %s\nEncryption Key: %s\n", strings.ReplaceAll(gotID, wantBroker.ID, "BROKER_ID"), gotEKey)
247-
wantStr := testutils.LoadWithUpdateFromGolden(t, gotStr)
250+
wantStr := testutils.LoadWithUpdateFromGolden(t, gotStr,
251+
testutils.WithGoldenTracker(&goldenTracker))
248252
require.Equal(t, wantStr, gotStr, "NewSession should return the expected session, but did not")
249253

250254
gotBroker, err := m.BrokerFromSessionID(gotID)

internal/services/nss/nss_test.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestNewService(t *testing.T) {
4343
}
4444

4545
func TestGetPasswdByName(t *testing.T) {
46+
goldenTracker := testutils.NewGoldenTracker(t)
4647
tests := map[string]struct {
4748
username string
4849

@@ -73,12 +74,14 @@ func TestGetPasswdByName(t *testing.T) {
7374
client := newNSSClient(t, tc.sourceDB, false)
7475

7576
got, err := client.GetPasswdByName(context.Background(), &authd.GetPasswdByNameRequest{Name: tc.username, ShouldPreCheck: tc.shouldPreCheck})
76-
requireExpectedResult(t, "GetPasswdByName", got, err, tc.wantErr, tc.wantErrNotExists)
77+
requireExpectedResult(t, &goldenTracker, "GetPasswdByName", got, err, tc.wantErr, tc.wantErrNotExists)
7778
})
7879
}
7980
}
8081

82+
//nolint:dupl // This is not a duplicate test
8183
func TestGetPasswdByUID(t *testing.T) {
84+
goldenTracker := testutils.NewGoldenTracker(t)
8285
tests := map[string]struct {
8386
uid uint32
8487

@@ -101,12 +104,13 @@ func TestGetPasswdByUID(t *testing.T) {
101104
client := newNSSClient(t, tc.sourceDB, false)
102105

103106
got, err := client.GetPasswdByUID(context.Background(), &authd.GetByIDRequest{Id: tc.uid})
104-
requireExpectedResult(t, "GetPasswdByUID", got, err, tc.wantErr, tc.wantErrNotExists)
107+
requireExpectedResult(t, &goldenTracker, "GetPasswdByUID", got, err, tc.wantErr, tc.wantErrNotExists)
105108
})
106109
}
107110
}
108111

109112
func TestGetPasswdEntries(t *testing.T) {
113+
goldenTracker := testutils.NewGoldenTracker(t)
110114
tests := map[string]struct {
111115
sourceDB string
112116

@@ -125,12 +129,14 @@ func TestGetPasswdEntries(t *testing.T) {
125129
client := newNSSClient(t, tc.sourceDB, false)
126130

127131
got, err := client.GetPasswdEntries(context.Background(), &authd.Empty{})
128-
requireExpectedEntriesResult(t, "GetPasswdEntries", got.GetEntries(), err, tc.wantErr)
132+
requireExpectedEntriesResult(t, &goldenTracker, "GetPasswdEntries", got.GetEntries(), err, tc.wantErr)
129133
})
130134
}
131135
}
132136

137+
//nolint:dupl // This is not a duplicate test
133138
func TestGetGroupByName(t *testing.T) {
139+
goldenTracker := testutils.NewGoldenTracker(t)
134140
tests := map[string]struct {
135141
groupname string
136142

@@ -153,12 +159,14 @@ func TestGetGroupByName(t *testing.T) {
153159
client := newNSSClient(t, tc.sourceDB, false)
154160

155161
got, err := client.GetGroupByName(context.Background(), &authd.GetGroupByNameRequest{Name: tc.groupname})
156-
requireExpectedResult(t, "GetGroupByName", got, err, tc.wantErr, tc.wantErrNotExists)
162+
requireExpectedResult(t, &goldenTracker, "GetGroupByName", got, err, tc.wantErr, tc.wantErrNotExists)
157163
})
158164
}
159165
}
160166

167+
//nolint:dupl // This is not a duplicate test
161168
func TestGetGroupByGID(t *testing.T) {
169+
goldenTracker := testutils.NewGoldenTracker(t)
162170
tests := map[string]struct {
163171
gid uint32
164172

@@ -181,12 +189,13 @@ func TestGetGroupByGID(t *testing.T) {
181189
client := newNSSClient(t, tc.sourceDB, false)
182190

183191
got, err := client.GetGroupByGID(context.Background(), &authd.GetByIDRequest{Id: tc.gid})
184-
requireExpectedResult(t, "GetGroupByGID", got, err, tc.wantErr, tc.wantErrNotExists)
192+
requireExpectedResult(t, &goldenTracker, "GetGroupByGID", got, err, tc.wantErr, tc.wantErrNotExists)
185193
})
186194
}
187195
}
188196

189197
func TestGetGroupEntries(t *testing.T) {
198+
goldenTracker := testutils.NewGoldenTracker(t)
190199
tests := map[string]struct {
191200
sourceDB string
192201

@@ -205,12 +214,13 @@ func TestGetGroupEntries(t *testing.T) {
205214
client := newNSSClient(t, tc.sourceDB, false)
206215

207216
got, err := client.GetGroupEntries(context.Background(), &authd.Empty{})
208-
requireExpectedEntriesResult(t, "GetGroupEntries", got.GetEntries(), err, tc.wantErr)
217+
requireExpectedEntriesResult(t, &goldenTracker, "GetGroupEntries", got.GetEntries(), err, tc.wantErr)
209218
})
210219
}
211220
}
212221

213222
func TestGetShadowByName(t *testing.T) {
223+
goldenTracker := testutils.NewGoldenTracker(t)
214224
tests := map[string]struct {
215225
username string
216226

@@ -235,12 +245,13 @@ func TestGetShadowByName(t *testing.T) {
235245
client := newNSSClient(t, tc.sourceDB, tc.currentUserNotRoot)
236246

237247
got, err := client.GetShadowByName(context.Background(), &authd.GetShadowByNameRequest{Name: tc.username})
238-
requireExpectedResult(t, "GetShadowByName", got, err, tc.wantErr, tc.wantErrNotExists)
248+
requireExpectedResult(t, &goldenTracker, "GetShadowByName", got, err, tc.wantErr, tc.wantErrNotExists)
239249
})
240250
}
241251
}
242252

243253
func TestGetShadowEntries(t *testing.T) {
254+
goldenTracker := testutils.NewGoldenTracker(t)
244255
tests := map[string]struct {
245256
sourceDB string
246257
currentUserNotRoot bool
@@ -261,7 +272,7 @@ func TestGetShadowEntries(t *testing.T) {
261272
client := newNSSClient(t, tc.sourceDB, tc.currentUserNotRoot)
262273

263274
got, err := client.GetShadowEntries(context.Background(), &authd.Empty{})
264-
requireExpectedEntriesResult(t, "GetShadowEntries", got.GetEntries(), err, tc.wantErr)
275+
requireExpectedEntriesResult(t, &goldenTracker, "GetShadowEntries", got.GetEntries(), err, tc.wantErr)
265276
})
266277
}
267278
}
@@ -354,7 +365,7 @@ func newBrokersManagerForTests(t *testing.T) *brokers.Manager {
354365
}
355366

356367
// requireExpectedResult asserts expected behaviour from any get* NSS requests and can update them from golden content.
357-
func requireExpectedResult[T authd.PasswdEntry | authd.GroupEntry | authd.ShadowEntry](t *testing.T, funcName string, got *T, err error, wantErr, wantErrNotExists bool) {
368+
func requireExpectedResult[T authd.PasswdEntry | authd.GroupEntry | authd.ShadowEntry](t *testing.T, goldenTracker *testutils.GoldenTracker, funcName string, got *T, err error, wantErr, wantErrNotExists bool) {
358369
t.Helper()
359370

360371
if wantErr {
@@ -368,12 +379,13 @@ func requireExpectedResult[T authd.PasswdEntry | authd.GroupEntry | authd.Shadow
368379
}
369380
require.NoError(t, err, fmt.Sprintf("%s should not return an error, but did", funcName))
370381

371-
want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
382+
want := testutils.LoadWithUpdateFromGoldenYAML(t, got,
383+
testutils.WithGoldenTracker(goldenTracker))
372384
requireExportedEquals(t, want, got, fmt.Sprintf("%s should return the expected entry, but did not", funcName))
373385
}
374386

375387
// requireExpectedEntriesResult asserts expected behaviour from any get* NSS request returning a list and can update them from golden content.
376-
func requireExpectedEntriesResult[T authd.PasswdEntry | authd.GroupEntry | authd.ShadowEntry](t *testing.T, funcName string, got []*T, err error, wantErr bool) {
388+
func requireExpectedEntriesResult[T authd.PasswdEntry | authd.GroupEntry | authd.ShadowEntry](t *testing.T, goldenTracker *testutils.GoldenTracker, funcName string, got []*T, err error, wantErr bool) {
377389
t.Helper()
378390

379391
if wantErr {
@@ -385,7 +397,8 @@ func requireExpectedEntriesResult[T authd.PasswdEntry | authd.GroupEntry | authd
385397
}
386398
require.NoError(t, err, fmt.Sprintf("%s should not return an error, but did", funcName))
387399

388-
want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
400+
want := testutils.LoadWithUpdateFromGoldenYAML(t, got,
401+
testutils.WithGoldenTracker(goldenTracker))
389402
if len(want) != len(got) {
390403
require.Equal(t, len(want), len(got), "Not the expected number of elements in the list. Wanted: %v\nGot: %v", want, got)
391404
}

0 commit comments

Comments
 (0)