Skip to content
Draft
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
3 changes: 2 additions & 1 deletion cmd/authd/daemon/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/consts"
"github.com/ubuntu/authd/internal/fileutils"
Expand Down Expand Up @@ -211,7 +212,7 @@ func TestMaybeMigrateBBoltToSQLite(t *testing.T) {
database, err := db.New(dbDir)
t.Cleanup(func() {
err := database.Close()
require.NoError(t, err)
assert.NoError(t, err)
})
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion internal/brokers/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/brokers"
"github.com/ubuntu/authd/internal/brokers/auth"
Expand Down Expand Up @@ -363,7 +364,7 @@ func newBrokerForTests(t *testing.T, cfgDir, brokerCfg string) (b brokers.Broker

conn, err := testutils.GetSystemBusConnection(t)
require.NoError(t, err, "Setup: could not connect to system bus")
t.Cleanup(func() { require.NoError(t, conn.Close(), "Teardown: Failed to close the connection") })
t.Cleanup(func() { assert.NoError(t, conn.Close(), "Teardown: Failed to close the connection") })

b, err = brokers.NewBroker(context.Background(), cfgPath, conn)
require.NoError(t, err, "Setup: could not create broker")
Expand Down
26 changes: 15 additions & 11 deletions internal/testutils/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testutils

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand All @@ -10,7 +11,6 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,11 +20,11 @@ var (
)

// fqdnToPath allows to return the fqdn path for this file relative to go.mod.
func fqdnToPath(t *testing.T, path string) string {
t.Helper()

func fqdnToPath(path string) (res string, err error) {
srcPath, err := filepath.Abs(path)
require.NoError(t, err, "Setup: can't calculate absolute path")
if err != nil {
return "", err
}

d := srcPath
for d != "/" {
Expand All @@ -33,22 +33,26 @@ func fqdnToPath(t *testing.T, path string) string {
d = filepath.Dir(d)
continue
}
defer func() { assert.NoError(t, f.Close(), "Setup: can’t close go.mod") }()
defer func() {
err = errors.Join(err, f.Close())
}()

r := bufio.NewReader(f)
l, err := r.ReadString('\n')
require.NoError(t, err, "can't read go.mod first line")
if err != nil {
return "", err
}

if !strings.HasPrefix(l, "module ") {
t.Fatal(`Setup: failed to find "module" line in go.mod`)
return "", fmt.Errorf("go.mod doesn't contain a module declaration: %s", l)
}

prefix := strings.TrimSpace(strings.TrimPrefix(l, "module "))
relpath := strings.TrimPrefix(srcPath, d)
return filepath.Join(prefix, relpath)
return filepath.Join(prefix, relpath), nil
}

t.Fatal("failed to find go.mod")
return ""
return "", fmt.Errorf("can't find go.mod for %s", srcPath)
}

// CoverDirEnv returns the cover dir env variable to run a go binary, if coverage is enabled.
Expand Down
3 changes: 2 additions & 1 deletion internal/testutils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func MakeReadOnly(t *testing.T, dest string) {
}

err = os.Chmod(dest, mode)
require.NoError(t, err)
assert.NoError(t, err)
})
}

Expand Down
36 changes: 28 additions & 8 deletions internal/testutils/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,39 @@ func trackRustCoverage(t *testing.T, target, src string) []string {
cmd.Env = append(os.Environ(), "LLVM_PROFILE_FILE="+coverDir)

out, err := cmd.CombinedOutput()
require.NoError(t, err, "Teardown: could not convert coverage to json format: %s", out)
assert.NoError(t, err, "Teardown: could not convert coverage to json format: %s", out)
if err != nil {
return
}

// Load our converted JSON profile.
var results map[string]interface{}
d, err := os.ReadFile(rustJSONCoverage)
require.NoError(t, err, "Teardown: can't read our json coverage file")
assert.NoError(t, err, "Teardown: can't read our json coverage file")
if err != nil {
return
}
err = json.Unmarshal(d, &results)
require.NoError(t, err, "Teardown: decode our json coverage file")
assert.NoError(t, err, "Teardown: decode our json coverage file")
if err != nil {
return
}

// This is the destination file for rust coverage in go format.
outF, err := os.Create(filepath.Join(coverDir, "rust2go_coverage"))
require.NoErrorf(t, err, "Teardown: failed opening output golang compatible cover file: %s", err)
assert.NoErrorf(t, err, "Teardown: failed opening output golang compatible cover file: %s", err)
if err != nil {
return
}
defer func() { assert.NoError(t, outF.Close(), "Teardown: can’t close golang compatible cover file") }()

// Scan our results to write to it.
scan(t, results, fqdnToPath(t, src), outF)
path, err := fqdnToPath(src)
assert.NoError(t, err, "Teardown: can't get source path for coverage")
if err != nil {
return
}
scan(t, results, path, outF)
})

return []string{
Expand All @@ -195,7 +212,8 @@ func scan(t *testing.T, results map[string]interface{}, p string, w io.Writer) {
if r != nil {
res, ok := r.([]interface{})
if !ok {
t.Fatalf("%v for coverage report is not a slice of floats in interface", r)
t.Errorf("%v for coverage report is not a slice of floats in interface", r)
return
}
convertRustFileResult(t, res, p, w)
return
Expand All @@ -206,7 +224,8 @@ func scan(t *testing.T, results map[string]interface{}, p string, w io.Writer) {
if r != nil {
res, ok := r.(map[string]interface{})
if !ok {
t.Fatalf("children %v is not a map of data", r)
t.Errorf("children %v is not a map of data", r)
return
}
// Iterate over files or dir.
for elem, subResults := range res {
Expand All @@ -232,7 +251,8 @@ func convertRustFileResult(t *testing.T, results []interface{}, p string, w io.W
for l, r := range results {
v, ok := r.(float64)
if !ok {
t.Fatalf("%v for coverage report is not a float", r)
t.Errorf("%v for coverage report is not a float", r)
return
}
var covered string
switch v {
Expand Down
3 changes: 2 additions & 1 deletion internal/users/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/consts"
"github.com/ubuntu/authd/internal/fileutils"
Expand Down Expand Up @@ -929,7 +930,7 @@ func initDB(t *testing.T, dbFile string) *db.Manager {
if os.Getenv("SKIP_TEST_CLEANUP") == "" {
t.Cleanup(func() {
err := os.RemoveAll(dbDir)
require.NoError(t, err, "Cleanup: could not remove temporary database directory")
assert.NoError(t, err, "Cleanup: could not remove temporary database directory")
})
}

Expand Down
3 changes: 2 additions & 1 deletion internal/users/idgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/users/localentries"
)
Expand Down Expand Up @@ -376,7 +377,7 @@ func TestGenerateIDMocked(t *testing.T) {
if tc.noCleanupCheck {
return
}
require.Empty(t, tc.generator.pendingIDs, "Expected generator to be empty after cleanup")
assert.Empty(t, tc.generator.pendingIDs, "Expected generator to be empty after cleanup")
})

require.GreaterOrEqual(t, int(id), int(tc.genID.minID), "Id %d is less than minID %d",
Expand Down
5 changes: 3 additions & 2 deletions internal/users/localentries/localgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/fileutils"
"github.com/ubuntu/authd/internal/testutils"
Expand Down Expand Up @@ -120,7 +121,7 @@ func TestUpdatelocalentries(t *testing.T) {
require.NoError(t, err, "Failed to lock the local entries")
t.Cleanup(func() {
err := entriesUnlock()
require.NoError(t, err, "entriesUnlock should not fail to unlock the local entries")
assert.NoError(t, err, "entriesUnlock should not fail to unlock the local entries")
})

err = localentries.UpdateGroups(entries, tc.username, tc.newGroups, tc.oldGroups)
Expand Down Expand Up @@ -400,7 +401,7 @@ func TestRacingGroupsLockingActions(t *testing.T) {
require.NoError(t, err, "Failed to lock the local entries")
t.Cleanup(func() {
err := entriesUnlock()
require.NoError(t, err, "entriesUnlock should not fail to unlock the local entries")
assert.NoError(t, err, "entriesUnlock should not fail to unlock the local entries")
})

groups, err := localentries.GetGroupEntries(entries)
Expand Down
3 changes: 2 additions & 1 deletion internal/users/localentries/localusers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/users/localentries"
"github.com/ubuntu/authd/internal/users/types"
Expand Down Expand Up @@ -152,7 +153,7 @@ func TestParseLocalPasswdFile(t *testing.T) {
require.NoError(t, err, "Failed to lock the local entries")
t.Cleanup(func() {
err := entriesUnlock()
require.NoError(t, err, "entriesUnlock should not fail to unlock the local entries")
assert.NoError(t, err, "entriesUnlock should not fail to unlock the local entries")
})

got, err := le.GetLocalUserEntries()
Expand Down
9 changes: 5 additions & 4 deletions internal/users/localentries/lockedentries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/users/localentries"
"github.com/ubuntu/authd/internal/users/types"
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestIsUniqueUserName(t *testing.T) {

t.Cleanup(func() {
err := unlock()
require.NoError(t, err, "TearDown: Unlock should not fail, but it did")
assert.NoError(t, err, "TearDown: Unlock should not fail, but it did")
})

users, err := le.GetUserEntries()
Expand Down Expand Up @@ -141,7 +142,7 @@ func TestIsUniqueGroupName(t *testing.T) {

t.Cleanup(func() {
err := unlock()
require.NoError(t, err, "TearDown: Unlock should not fail, but it did")
assert.NoError(t, err, "TearDown: Unlock should not fail, but it did")
})

groups, err := le.GetGroupEntries()
Expand Down Expand Up @@ -176,7 +177,7 @@ func TestIsUniqueUID(t *testing.T) {

t.Cleanup(func() {
err := unlock()
require.NoError(t, err, "TearDown: Unlock should not fail, but it did")
assert.NoError(t, err, "TearDown: Unlock should not fail, but it did")
})

users, err := le.GetUserEntries()
Expand Down Expand Up @@ -220,7 +221,7 @@ func TestIsUniqueGID(t *testing.T) {

t.Cleanup(func() {
err := unlock()
require.NoError(t, err, "TearDown: Unlock should not fail, but it did")
assert.NoError(t, err, "TearDown: Unlock should not fail, but it did")
})

groups, err := le.GetGroupEntries()
Expand Down
11 changes: 6 additions & 5 deletions internal/users/locking/locking_bwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
userslocking "github.com/ubuntu/authd/internal/users/locking"
)
Expand Down Expand Up @@ -78,7 +79,7 @@ testgroup:x:1001:testuser`
require.NoError(t, err, "Locking once it is allowed")
t.Cleanup(func() {
err := userslocking.WriteUnlock()
require.NoError(t, err, "Unlocking should be allowed")
assert.NoError(t, err, "Unlocking should be allowed")
})

output, err := runCmd(t, "getent", "group", "root", "testgroup")
Expand Down Expand Up @@ -186,9 +187,9 @@ func TestLockingLockedDatabase(t *testing.T) {
t.Cleanup(func() {
cancel()
syscall.Kill(lockerProcess.Pid, syscall.SIGKILL)
require.Error(t, <-lockerExited, "Stopping locking process")
require.NoError(t, <-writeLockExited, "Final locking")
require.NoError(t, userslocking.WriteUnlock(), "Final unlocking")
assert.Error(t, <-lockerExited, "Stopping locking process")
assert.NoError(t, <-writeLockExited, "Final locking")
assert.NoError(t, userslocking.WriteUnlock(), "Final unlocking")
})

go func() {
Expand Down Expand Up @@ -249,7 +250,7 @@ func TestLockingLockedDatabaseFailsAfterTimeout(t *testing.T) {
t.Cleanup(func() {
cancel()
syscall.Kill(lockerProcess.Pid, syscall.SIGKILL)
require.Error(t, <-lockerExited, "Stopping locking process")
assert.Error(t, <-lockerExited, "Stopping locking process")
})

go func() {
Expand Down
3 changes: 2 additions & 1 deletion internal/users/locking/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/testsdetection"
"github.com/ubuntu/authd/log"
Expand Down Expand Up @@ -212,7 +213,7 @@ func Z_ForTests_SetMaxWaitTime(t *testing.T, maxWaitTime time.Duration) {
maxWait = maxWaitTime

t.Cleanup(func() {
require.True(t, overrideMaxWait.CompareAndSwap(int64(maxWaitTime), int64(defaultMaxWait)),
assert.True(t, overrideMaxWait.CompareAndSwap(int64(maxWaitTime), int64(defaultMaxWait)),
"Waiting time has been changed: %v", overrideMaxWait.Load())
maxWait = defaultMaxWait
})
Expand Down
3 changes: 2 additions & 1 deletion pam/integration-tests/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/godbus/dbus/v5"
"github.com/msteinert/pam/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/testutils"
"github.com/ubuntu/authd/pam/internal/pam_test"
Expand Down Expand Up @@ -936,7 +937,7 @@ func preparePamTransactionForServiceFile(t *testing.T, serviceFile string, user
saveArtifactsForDebugOnCleanup(t, []string{serviceFile})
require.NoError(t, err, "PAM: Error to initialize module")
require.NotNil(t, tx, "PAM: Transaction is not set")
t.Cleanup(func() { require.NoError(t, tx.End(), "PAM: can't end transaction") })
t.Cleanup(func() { assert.NoError(t, tx.End(), "PAM: can't end transaction") })

return tx
}
Expand Down
Loading
Loading