Skip to content

Commit

Permalink
tests: various fixes & improvements to support packaging (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielNagy authored Nov 29, 2023
2 parents 4093f4c + 4fb41d5 commit 570a972
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
16 changes: 15 additions & 1 deletion cmd/authd/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daemon_test

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/ubuntu/authd/cmd/authd/daemon"
cachetests "github.com/ubuntu/authd/internal/cache/tests"
"github.com/ubuntu/authd/internal/consts"
"github.com/ubuntu/authd/internal/testutils"
)

func TestHelp(t *testing.T) {
Expand Down Expand Up @@ -50,7 +52,7 @@ func TestVersion(t *testing.T) {
want := "authd"

require.Equal(t, want, fields[0], "Wrong executable name")
require.Equal(t, "Dev", fields[1], "Wrong version")
require.Equal(t, consts.Version, fields[1], "Wrong version")
}

func TestNoUsageError(t *testing.T) {
Expand Down Expand Up @@ -398,3 +400,15 @@ func captureStdout(t *testing.T) func() string {
return out.String()
}
}

func TestMain(m *testing.M) {
// Start system bus mock.
cleanup, err := testutils.StartSystemBusMock()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer cleanup()

m.Run()
}
3 changes: 1 addition & 2 deletions internal/cache/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ func createDBFile(t *testing.T, src, destDir string) {
func requireNoDirtyFileInDir(t *testing.T, cacheDir string) {
t.Helper()

_, err := os.Stat(filepath.Join(cacheDir, cachetests.DirtyFlagDbName))
require.ErrorIs(t, err, fs.ErrNotExist, "Dirty flag should have been removed")
require.NoFileExists(t, filepath.Join(cacheDir, cachetests.DirtyFlagDbName), "Dirty flag should have been removed")
}

func requireClearedDatabase(t *testing.T, c *cache.Cache) {
Expand Down
21 changes: 17 additions & 4 deletions internal/cache/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"io"
"os/user"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -62,12 +63,18 @@ func (c *Cache) dumpToYaml() (string, error) {
c.mu.RLock()
defer c.mu.RUnlock()

username := "root"
currentUser, _ := user.Current()
if currentUser.Name != "" {
username = currentUser.Name
}

if err := c.db.View(func(tx *bbolt.Tx) error {
return tx.ForEach(func(name []byte, bucket *bbolt.Bucket) error {
d[string(name)] = make(map[string]string)
return bucket.ForEach(func(key, value []byte) error {
key = []byte(strings.Replace(string(key), "root", "ACTIVE_USER", 1))
value = []byte(strings.ReplaceAll(string(value), "root", "ACTIVE_USER"))
key = []byte(strings.Replace(string(key), username, "ACTIVE_USER", 1))
value = []byte(strings.ReplaceAll(string(value), username, "ACTIVE_USER"))

d[string(name)][string(key)] = redactTime(string(value))
return nil
Expand Down Expand Up @@ -104,6 +111,12 @@ func dbfromYAML(r io.Reader, destDir string) error {
return err
}

username := "root"
currentUser, _ := user.Current()
if currentUser.Name != "" {
username = currentUser.Name
}

// Create buckets and content.
return db.Update(func(tx *bbolt.Tx) error {
for bucketName, bucketContent := range dbContent {
Expand All @@ -115,8 +128,8 @@ func dbfromYAML(r io.Reader, destDir string) error {
for key, val := range bucketContent {
if bucketName == userByIDBucketName || bucketName == userByNameBucketName {
// Replace ACTIVE_USER name by the uid of an active user.
val = strings.ReplaceAll(val, "ACTIVE_USER", "root")
key = strings.Replace(key, "ACTIVE_USER", "root", 1)
val = strings.ReplaceAll(val, "ACTIVE_USER", username)
key = strings.Replace(key, "ACTIVE_USER", username, 1)

// Replace the redacted time in the json value by a valid time.
for redacted, t := range redactedTimes {
Expand Down
9 changes: 9 additions & 0 deletions nss/integration-tests/nss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nss_test
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -124,6 +125,14 @@ func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

// Start system bus mock.
cleanup, err := testutils.StartSystemBusMock()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer cleanup()

execPath, cleanup, err := buildDaemon()
if err != nil {
log.Printf("Setup: failed to build daemon: %v", err)
Expand Down

0 comments on commit 570a972

Please sign in to comment.