Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maint(testutils/golden): Add GoldenTracker to track the used golden files and use it #601

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions internal/brokers/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var supportedLayouts = map[string]map[string]string{
func TestNewBroker(t *testing.T) {
t.Parallel()

goldenTracker := testutils.NewGoldenTracker(t)

tests := map[string]struct {
configFile string

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

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

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

b := newBrokerForTests(t, "", "")
goldenTracker := testutils.NewGoldenTracker(t)

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

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

b := newBrokerForTests(t, "", "")
goldenTracker := testutils.NewGoldenTracker(t)

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

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

b := newBrokerForTests(t, "", "")
goldenTracker := testutils.NewGoldenTracker(t)

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

<-done
gotStr := firstCallReturn + secondCallReturn
want := testutils.LoadWithUpdateFromGolden(t, gotStr)
want := testutils.LoadWithUpdateFromGolden(t, gotStr,
testutils.WithGoldenTracker(&goldenTracker))
require.Equal(t, want, gotStr, "IsAuthenticated should return the expected combined data, but did not")
})
}
Expand Down Expand Up @@ -321,6 +330,7 @@ func TestUserPreCheck(t *testing.T) {
t.Parallel()

b := newBrokerForTests(t, "", "")
goldenTracker := testutils.NewGoldenTracker(t)

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

want := testutils.LoadWithUpdateFromGolden(t, got)
want := testutils.LoadWithUpdateFromGolden(t, got,
testutils.WithGoldenTracker(&goldenTracker))
require.Equal(t, want, got, "UserPreCheck should return the expected data, but did not")
})
}
Expand Down
5 changes: 4 additions & 1 deletion internal/brokers/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ var (
func TestUnmarshalUserInfo(t *testing.T) {
t.Parallel()

goldenTracker := testutils.NewGoldenTracker(t)

tests := map[string]struct {
jsonInput string

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

want := testutils.LoadWithUpdateFromGolden(t, string(gotJSON))
want := testutils.LoadWithUpdateFromGolden(t, string(gotJSON),
testutils.WithGoldenTracker(&goldenTracker))
require.Equal(t, want, string(gotJSON), "unmarshalUserInfo should return the expected format, but did not")
})
}
Expand Down
8 changes: 6 additions & 2 deletions internal/brokers/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
)

func TestNewManager(t *testing.T) {
goldenTracker := testutils.NewGoldenTracker(t)
tests := map[string]struct {
brokerConfigDir string
configuredBrokers []string
Expand Down Expand Up @@ -58,7 +59,8 @@ func TestNewManager(t *testing.T) {
brokers = append(brokers, broker.Name)
}

want := testutils.LoadWithUpdateFromGoldenYAML(t, brokers)
want := testutils.LoadWithUpdateFromGoldenYAML(t, brokers,
testutils.WithGoldenTracker(&goldenTracker))
require.Equal(t, want, brokers, "NewManager should return the expected brokers, but did not")
})
}
Expand Down Expand Up @@ -169,6 +171,7 @@ func TestBrokerFromSessionID(t *testing.T) {
func TestNewSession(t *testing.T) {
t.Parallel()

goldenTracker := testutils.NewGoldenTracker(t)
tests := map[string]struct {
brokerID string
username string
Expand Down Expand Up @@ -244,7 +247,8 @@ func TestNewSession(t *testing.T) {

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

gotBroker, err := m.BrokerFromSessionID(gotID)
Expand Down
37 changes: 25 additions & 12 deletions internal/services/nss/nss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestNewService(t *testing.T) {
}

func TestGetPasswdByName(t *testing.T) {
goldenTracker := testutils.NewGoldenTracker(t)
tests := map[string]struct {
username string

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

got, err := client.GetPasswdByName(context.Background(), &authd.GetPasswdByNameRequest{Name: tc.username, ShouldPreCheck: tc.shouldPreCheck})
requireExpectedResult(t, "GetPasswdByName", got, err, tc.wantErr, tc.wantErrNotExists)
requireExpectedResult(t, &goldenTracker, "GetPasswdByName", got, err, tc.wantErr, tc.wantErrNotExists)
})
}
}

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

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

got, err := client.GetPasswdByUID(context.Background(), &authd.GetByIDRequest{Id: tc.uid})
requireExpectedResult(t, "GetPasswdByUID", got, err, tc.wantErr, tc.wantErrNotExists)
requireExpectedResult(t, &goldenTracker, "GetPasswdByUID", got, err, tc.wantErr, tc.wantErrNotExists)
})
}
}

func TestGetPasswdEntries(t *testing.T) {
goldenTracker := testutils.NewGoldenTracker(t)
tests := map[string]struct {
sourceDB string

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

got, err := client.GetPasswdEntries(context.Background(), &authd.Empty{})
requireExpectedEntriesResult(t, "GetPasswdEntries", got.GetEntries(), err, tc.wantErr)
requireExpectedEntriesResult(t, &goldenTracker, "GetPasswdEntries", got.GetEntries(), err, tc.wantErr)
})
}
}

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

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

got, err := client.GetGroupByName(context.Background(), &authd.GetGroupByNameRequest{Name: tc.groupname})
requireExpectedResult(t, "GetGroupByName", got, err, tc.wantErr, tc.wantErrNotExists)
requireExpectedResult(t, &goldenTracker, "GetGroupByName", got, err, tc.wantErr, tc.wantErrNotExists)
})
}
}

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

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

got, err := client.GetGroupByGID(context.Background(), &authd.GetByIDRequest{Id: tc.gid})
requireExpectedResult(t, "GetGroupByGID", got, err, tc.wantErr, tc.wantErrNotExists)
requireExpectedResult(t, &goldenTracker, "GetGroupByGID", got, err, tc.wantErr, tc.wantErrNotExists)
})
}
}

func TestGetGroupEntries(t *testing.T) {
goldenTracker := testutils.NewGoldenTracker(t)
tests := map[string]struct {
sourceDB string

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

got, err := client.GetGroupEntries(context.Background(), &authd.Empty{})
requireExpectedEntriesResult(t, "GetGroupEntries", got.GetEntries(), err, tc.wantErr)
requireExpectedEntriesResult(t, &goldenTracker, "GetGroupEntries", got.GetEntries(), err, tc.wantErr)
})
}
}

func TestGetShadowByName(t *testing.T) {
goldenTracker := testutils.NewGoldenTracker(t)
tests := map[string]struct {
username string

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

got, err := client.GetShadowByName(context.Background(), &authd.GetShadowByNameRequest{Name: tc.username})
requireExpectedResult(t, "GetShadowByName", got, err, tc.wantErr, tc.wantErrNotExists)
requireExpectedResult(t, &goldenTracker, "GetShadowByName", got, err, tc.wantErr, tc.wantErrNotExists)
})
}
}

func TestGetShadowEntries(t *testing.T) {
goldenTracker := testutils.NewGoldenTracker(t)
tests := map[string]struct {
sourceDB string
currentUserNotRoot bool
Expand All @@ -261,7 +272,7 @@ func TestGetShadowEntries(t *testing.T) {
client := newNSSClient(t, tc.sourceDB, tc.currentUserNotRoot)

got, err := client.GetShadowEntries(context.Background(), &authd.Empty{})
requireExpectedEntriesResult(t, "GetShadowEntries", got.GetEntries(), err, tc.wantErr)
requireExpectedEntriesResult(t, &goldenTracker, "GetShadowEntries", got.GetEntries(), err, tc.wantErr)
})
}
}
Expand Down Expand Up @@ -354,7 +365,7 @@ func newBrokersManagerForTests(t *testing.T) *brokers.Manager {
}

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

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

want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
want := testutils.LoadWithUpdateFromGoldenYAML(t, got,
testutils.WithGoldenTracker(goldenTracker))
requireExportedEquals(t, want, got, fmt.Sprintf("%s should return the expected entry, but did not", funcName))
}

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

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

want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
want := testutils.LoadWithUpdateFromGoldenYAML(t, got,
testutils.WithGoldenTracker(goldenTracker))
if len(want) != len(got) {
require.Equal(t, len(want), len(got), "Not the expected number of elements in the list. Wanted: %v\nGot: %v", want, got)
}
Expand Down
Loading
Loading