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

fix(pam/integration-tests): Use absolute paths for compiling and reference external files #274

Open
wants to merge 4 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
14 changes: 14 additions & 0 deletions internal/testutils/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
}

type goldenOptions struct {
goldenDir string
goldenPath string
}

Expand All @@ -40,6 +41,15 @@ func WithGoldenPath(path string) GoldenOption {
}
}

// WithGoldenDir overrides the default directory for golden files used.
func WithGoldenDir(dir string) GoldenOption {
return func(o *goldenOptions) {
if dir != "" {
o.goldenDir = dir
}
}
}

// LoadWithUpdateFromGolden loads the element from a plaintext golden file.
// It will update the file if the update flag is used prior to loading it.
func LoadWithUpdateFromGolden(t *testing.T, data string, opts ...GoldenOption) string {
Expand All @@ -53,6 +63,10 @@ func LoadWithUpdateFromGolden(t *testing.T, data string, opts ...GoldenOption) s
opt(&o)
}

if o.goldenDir != "" {
o.goldenPath = filepath.Join(o.goldenDir, o.goldenPath)
}

if update {
t.Logf("updating golden file %s", o.goldenPath)
err := os.MkdirAll(filepath.Dir(o.goldenPath), 0750)
Expand Down
7 changes: 7 additions & 0 deletions internal/testutils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"github.com/stretchr/testify/require"
)

// CurrentDir returns the current file directory.
func CurrentDir() string {
// p is the path to the caller file
_, p, _, _ := runtime.Caller(1)
return filepath.Dir(p)
}

// ProjectRoot returns the absolute path to the project root.
func ProjectRoot() string {
// p is the path to the current file, in this case -> {PROJECT_ROOT}/internal/testutils/path.go
Expand Down
26 changes: 14 additions & 12 deletions pam/integration-tests/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ func TestCLIAuthenticate(t *testing.T) {
outDir := t.TempDir()
prepareCLITest(t, outDir)

currentDir := testutils.CurrentDir()

err := os.MkdirAll(filepath.Join(outDir, "gpasswd"), 0700)
require.NoError(t, err, "Setup: Could not create gpasswd output directory")
gpasswdOutput := filepath.Join(outDir, "gpasswd", "authenticate.output")
groupsFile := filepath.Join(testutils.TestFamilyPath(t), "gpasswd.group")
groupsFile := filepath.Join(currentDir, testutils.TestFamilyPath(t), "gpasswd.group")

const socketPathEnv = "AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK"
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -42,9 +44,6 @@ func TestCLIAuthenticate(t *testing.T) {
// If vhs is installed with "go install", we need to add GOPATH to PATH.
pathEnv := prependBinToPath(t)

currentDir, err := os.Getwd()
require.NoError(t, err, "Setup: Could not get current directory for the tests")

tests := map[string]struct {
tape string
}{
Expand Down Expand Up @@ -104,12 +103,13 @@ func TestCLIAuthenticate(t *testing.T) {
break
}
}
want := testutils.LoadWithUpdateFromGolden(t, got)
goldenDir := testutils.WithGoldenDir(currentDir)
want := testutils.LoadWithUpdateFromGolden(t, got, goldenDir)
require.Equal(t, want, got, "Output of tape %q does not match golden file", tc.tape)

if tc.tape == "local_group" {
got := grouptests.IdempotentGPasswdOutput(t, gpasswdOutput)
want := testutils.LoadWithUpdateFromGolden(t, got, testutils.WithGoldenPath(testutils.GoldenPath(t)+".gpasswd_out"))
want := testutils.LoadWithUpdateFromGolden(t, got, goldenDir, testutils.WithGoldenPath(testutils.GoldenPath(t)+".gpasswd_out"))
require.Equal(t, want, got, "UpdateLocalGroups should do the expected gpasswd operation, but did not")
}
})
Expand All @@ -122,11 +122,13 @@ func TestCLIChangeAuthTok(t *testing.T) {
outDir := t.TempDir()
prepareCLITest(t, outDir)

currentDir := testutils.CurrentDir()

// we don't care about the output of gpasswd for this test, but we still need to mock it.
err := os.MkdirAll(filepath.Join(outDir, "gpasswd"), 0700)
require.NoError(t, err, "Setup: Could not create gpasswd output directory")
gpasswdOutput := filepath.Join(outDir, "gpasswd", "chauthtok.output")
groupsFile := filepath.Join(testutils.TestFamilyPath(t), "gpasswd.group")
groupsFile := filepath.Join(currentDir, testutils.TestFamilyPath(t), "gpasswd.group")

const socketPathEnv = "AUTHD_TESTS_CLI_AUTHTOK_TESTS_SOCK"
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -141,9 +143,6 @@ func TestCLIChangeAuthTok(t *testing.T) {
// If vhs is installed with "go install", we need to add GOPATH to PATH.
pathEnv := prependBinToPath(t)

currentDir, err := os.Getwd()
require.NoError(t, err, "Setup: Could not get current directory for the tests")

tests := map[string]struct {
tape string
}{
Expand Down Expand Up @@ -194,7 +193,8 @@ func TestCLIChangeAuthTok(t *testing.T) {
break
}
}
want := testutils.LoadWithUpdateFromGolden(t, got)
goldenDir := testutils.WithGoldenDir(currentDir)
want := testutils.LoadWithUpdateFromGolden(t, got, goldenDir)
require.Equal(t, want, got, "Output of tape %q does not match golden file", tc.tape)
})
}
Expand Down Expand Up @@ -233,11 +233,13 @@ func prepareCLILogging(t *testing.T) string {
// buildPAM builds the PAM module in a temporary directory and returns a cleanup function.
func buildPAM(execPath string) (cleanup func(), err error) {
cmd := exec.Command("go", "build")
cmd.Args = append(cmd.Args, "-C", "pam")
cmd.Dir = testutils.ProjectRoot()
if testutils.CoverDir() != "" {
// -cover is a "positional flag", so it needs to come right after the "build" command.
cmd.Args = append(cmd.Args, "-cover")
}
cmd.Args = append(cmd.Args, "-tags=pam_binary_cli", "-o", filepath.Join(execPath, "pam_authd"), "../.")
cmd.Args = append(cmd.Args, "-tags=pam_binary_cli", "-o", filepath.Join(execPath, "pam_authd"))
if out, err := cmd.CombinedOutput(); err != nil {
return func() {}, fmt.Errorf("%v: %s", err, out)
}
Expand Down
4 changes: 3 additions & 1 deletion pam/integration-tests/gdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ func TestGdmModuleWithCWrapper(t *testing.T) {
func buildPAMModule(t *testing.T) string {
t.Helper()

cmd := exec.Command("go", "build", "-C", "..")
cmd := exec.Command("go", "build", "-C", "pam")
cmd.Dir = testutils.ProjectRoot()
if testutils.CoverDir() != "" {
// -cover is a "positional flag", so it needs to come right after the "build" command.
cmd.Args = append(cmd.Args, "-cover")
Expand Down Expand Up @@ -392,6 +393,7 @@ func buildPAMWrapperModule(t *testing.T) string {

//nolint:gosec // G204 it's a test so we should allow using any compiler safely.
cmd := exec.Command(compiler)
cmd.Dir = testutils.CurrentDir()
soname := "pam_authd_loader"
libPath := filepath.Join(t.TempDir(), soname+".so")

Expand Down
Loading