Skip to content

Commit

Permalink
Normalize getent output in nss integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adombeck committed Dec 2, 2024
1 parent 81cc456 commit cb2ba1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions nss/integration-tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -147,6 +148,18 @@ func TestIntegration(t *testing.T) {
got, status := getentOutputForLib(t, libPath, socketPath, rustCovEnv, tc.shouldPreCheck, cmds...)
require.Equal(t, tc.wantStatus, status, "Expected status %d, but got %d", tc.wantStatus, status)

if tc.shouldPreCheck && tc.db == "passwd" {
// When pre-checking, the `getent passwd` output contains a randomly generated UID.
// To make the test deterministic, we replace the UID with a placeholder.
// The output looks something like this:
// user-pre-check:x:1776689191:0:gecos for user-pre-check:/home/user-pre-check:/usr/bin/bash\n
fields := strings.Split(got, ":")
require.Len(t, fields, 7, "Invalid number of fields in the output: %q", got)
// The UID is the third field.
fields[2] = "{{UID}}"
got = strings.Join(fields, ":")
}

// If the exit status is NotFound, there is no need to create an empty golden file.
// But we need to ensure that the output is indeed empty.
if tc.wantStatus == codeNotFound {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
user-pre-check:x:1234:0:gecos for user-pre-check:/home/user-pre-check:/usr/bin/bash
user-pre-check:x:{{UID}}:0:gecos for user-pre-check:/home/user-pre-check:/usr/bin/bash

0 comments on commit cb2ba1b

Please sign in to comment.