Skip to content

Commit

Permalink
Rename package "cache" to "db"
Browse files Browse the repository at this point in the history
We don't use our database as a cache anymore. It's time to reflect this
in the package name.
  • Loading branch information
adombeck committed Feb 4, 2025
1 parent 1ad9fad commit b2b08d2
Show file tree
Hide file tree
Showing 94 changed files with 563 additions and 207 deletions.
4 changes: 2 additions & 2 deletions cmd/authd/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/ubuntu/authd/cmd/authd/daemon"
"github.com/ubuntu/authd/internal/consts"
"github.com/ubuntu/authd/internal/testutils"
"github.com/ubuntu/authd/internal/users/cache"
"github.com/ubuntu/authd/internal/users/db"
)

func TestHelp(t *testing.T) {
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestAppRunFailsOnComponentsCreationAndQuit(t *testing.T) {
err := os.MkdirAll(config.Paths.Cache, 0700)
require.NoError(t, err, "Setup: could not create cache directory")
//nolint: gosec // This is a file with invalid permission for tests.
err = os.WriteFile(filepath.Join(config.Paths.Cache, cache.Z_ForTests_DBName()), nil, 0644)
err = os.WriteFile(filepath.Join(config.Paths.Cache, db.Z_ForTests_DBName()), nil, 0644)
require.NoError(t, err, "Setup: could not create database with invalid permissions")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/services/nss/nss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/ubuntu/authd/internal/testutils"
"github.com/ubuntu/authd/internal/testutils/golden"
"github.com/ubuntu/authd/internal/users"
"github.com/ubuntu/authd/internal/users/cache"
"github.com/ubuntu/authd/internal/users/db"
"github.com/ubuntu/authd/internal/users/idgenerator"
localgroupstestutils "github.com/ubuntu/authd/internal/users/localentries/testutils"
"github.com/ubuntu/authd/log"
Expand Down Expand Up @@ -330,7 +330,7 @@ func newUserManagerForTests(t *testing.T, sourceDB string) *users.Manager {
if sourceDB == "" {
sourceDB = "cache.db.yaml"
}
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", sourceDB), cacheDir)
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", sourceDB), cacheDir)

managerOpts := []users.Option{
users.WithIDGenerator(&idgenerator.IDGeneratorMock{
Expand Down
14 changes: 7 additions & 7 deletions internal/services/pam/pam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/ubuntu/authd/internal/testutils"
"github.com/ubuntu/authd/internal/testutils/golden"
"github.com/ubuntu/authd/internal/users"
"github.com/ubuntu/authd/internal/users/cache"
"github.com/ubuntu/authd/internal/users/db"
"github.com/ubuntu/authd/internal/users/idgenerator"
localgroupstestutils "github.com/ubuntu/authd/internal/users/localentries/testutils"
userstestutils "github.com/ubuntu/authd/internal/users/testutils"
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestGetPreviousBroker(t *testing.T) {
d, err := io.ReadAll(f)
require.NoError(t, err, "Setup: could not read fixture database file")
d = bytes.ReplaceAll(d, []byte("MOCKBROKERID"), []byte(mockBrokerGeneratedID))
err = cache.Z_ForTests_FromYAML(bytes.NewBuffer(d), cacheDir)
err = db.Z_ForTests_FromYAML(bytes.NewBuffer(d), cacheDir)
require.NoError(t, err, "Setup: could not prepare cache database file")

m, err := users.NewManager(users.DefaultConfig, cacheDir)
Expand Down Expand Up @@ -458,7 +458,7 @@ func TestIsAuthenticated(t *testing.T) {

cacheDir := t.TempDir()
if tc.existingDB != "" {
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join(testutils.TestFamilyPath(t), tc.existingDB), cacheDir)
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join(testutils.TestFamilyPath(t), tc.existingDB), cacheDir)
}

managerOpts := []users.Option{
Expand Down Expand Up @@ -533,7 +533,7 @@ func TestIsAuthenticated(t *testing.T) {
golden.CheckOrUpdate(t, got, golden.WithPath("IsAuthenticated"))

// Check that cache has been updated too.
gotDB, err := cache.Z_ForTests_DumpNormalizedYAML(userstestutils.GetManagerCache(m))
gotDB, err := db.Z_ForTests_DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Setup: failed to dump database for comparing")
golden.CheckOrUpdate(t, gotDB, golden.WithPath("cache.db"))

Expand Down Expand Up @@ -579,7 +579,7 @@ func TestIDGeneration(t *testing.T) {
require.NoError(t, err, "Setup: could not authenticate user")
require.Equal(t, "granted", resp.GetAccess(), "Setup: authentication should be granted")

gotDB, err := cache.Z_ForTests_DumpNormalizedYAML(userstestutils.GetManagerCache(m))
gotDB, err := db.Z_ForTests_DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Setup: failed to dump database for comparing")
golden.CheckOrUpdate(t, gotDB, golden.WithPath("cache.db"))
})
Expand Down Expand Up @@ -610,7 +610,7 @@ func TestSetDefaultBrokerForUser(t *testing.T) {
t.Parallel()

cacheDir := t.TempDir()
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join(testutils.TestFamilyPath(t), "set-default-broker.db"), cacheDir)
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join(testutils.TestFamilyPath(t), "set-default-broker.db"), cacheDir)

m, err := users.NewManager(users.DefaultConfig, cacheDir)
require.NoError(t, err, "Setup: could not create user manager")
Expand Down Expand Up @@ -638,7 +638,7 @@ func TestSetDefaultBrokerForUser(t *testing.T) {
require.Equal(t, tc.brokerID, gpbResp.GetPreviousBroker(), "SetDefaultBrokerForUser should set the default broker as expected")

// Check that cache has been updated too.
gotDB, err := cache.Z_ForTests_DumpNormalizedYAML(userstestutils.GetManagerCache(m))
gotDB, err := db.Z_ForTests_DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Setup: failed to dump database for comparing")
golden.CheckOrUpdate(t, gotDB, golden.WithPath("cache.db"))
})
Expand Down
4 changes: 2 additions & 2 deletions internal/testutils/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/grpcutils"
"github.com/ubuntu/authd/internal/services/errmessages"
"github.com/ubuntu/authd/internal/users/cache"
"github.com/ubuntu/authd/internal/users/db"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
Expand Down Expand Up @@ -77,7 +77,7 @@ func RunDaemon(ctx context.Context, t *testing.T, execPath string, args ...Daemo
}

if opts.existentDB != "" {
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", "db", opts.existentDB+".db.yaml"), opts.cachePath)
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", "db", opts.existentDB+".db.yaml"), opts.cachePath)
}

if opts.socketPath == "" {
Expand Down

This file was deleted.

14 changes: 7 additions & 7 deletions internal/users/cache/db.go → internal/users/db/db.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package cache handles transaction with an underlying database to cache user and group information.
package cache
// Package db handles transaction with an underlying database to store user and group information.
package db

import (
"encoding/json"
Expand Down Expand Up @@ -40,8 +40,8 @@ var (
}
)

// Cache is our database API.
type Cache struct {
// Database is our database API.
type Database struct {
db *bbolt.DB
mu sync.RWMutex
}
Expand Down Expand Up @@ -85,7 +85,7 @@ type groupToUsersDB struct {
}

// New creates a new database cache by creating or opening the underlying db.
func New(cacheDir string) (cache *Cache, err error) {
func New(cacheDir string) (cache *Database, err error) {
dbPath := filepath.Join(cacheDir, dbName)
defer decorate.OnError(&err, "could not create new database object at %q", dbPath)

Expand All @@ -102,7 +102,7 @@ func New(cacheDir string) (cache *Cache, err error) {
return nil, err
}

return &Cache{db: db, mu: sync.RWMutex{}}, nil
return &Database{db: db, mu: sync.RWMutex{}}, nil
}

// openAndInitDB open a pre-existing database and potentially initializes its buckets.
Expand Down Expand Up @@ -158,7 +158,7 @@ func openAndInitDB(path string) (*bbolt.DB, error) {
}

// Close closes the db and signal the monitoring goroutine to stop.
func (c *Cache) Close() error {
func (c *Database) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
return c.db.Close()
Expand Down
54 changes: 27 additions & 27 deletions internal/users/cache/db_test.go → internal/users/db/db_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cache_test
package db_test

import (
"io/fs"
Expand All @@ -9,7 +9,7 @@ import (

"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/testutils/golden"
"github.com/ubuntu/authd/internal/users/cache"
"github.com/ubuntu/authd/internal/users/db"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -40,13 +40,13 @@ func TestNew(t *testing.T) {
t.Parallel()

cacheDir := t.TempDir()
dbDestPath := filepath.Join(cacheDir, cache.Z_ForTests_DBName())
dbDestPath := filepath.Join(cacheDir, db.Z_ForTests_DBName())

if tc.dbFile == "-" {
err := os.RemoveAll(cacheDir)
require.NoError(t, err, "Setup: could not remove temporary cache directory")
} else if tc.dbFile != "" {
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", tc.dbFile+".db.yaml"), cacheDir)
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", tc.dbFile+".db.yaml"), cacheDir)
}
if tc.corruptedDbFile {
err := os.WriteFile(dbDestPath, []byte("corrupted"), 0600)
Expand All @@ -66,15 +66,15 @@ func TestNew(t *testing.T) {
}
}

c, err := cache.New(cacheDir)
c, err := db.New(cacheDir)
if tc.wantErr {
require.Error(t, err, "New should return an error but didn't")
return
}
require.NoError(t, err)
defer c.Close()

got, err := cache.Z_ForTests_DumpNormalizedYAML(c)
got, err := db.Z_ForTests_DumpNormalizedYAML(c)
require.NoError(t, err, "Created database should be valid yaml content")

golden.CheckOrUpdate(t, got)
Expand All @@ -91,7 +91,7 @@ func TestNew(t *testing.T) {
func TestUpdateUserEntry(t *testing.T) {
t.Parallel()

userCases := map[string]cache.UserDB{
userCases := map[string]db.UserDB{
"user1": {
Name: "user1",
UID: 1111,
Expand Down Expand Up @@ -146,12 +146,12 @@ func TestUpdateUserEntry(t *testing.T) {
LastPwdChange: -1, MaxPwdAge: -1, PwdWarnPeriod: -1, PwdInactivity: -1, MinPwdAge: -1, ExpirationDate: -1,
},
}
groupCases := map[string]cache.GroupDB{
"group1": cache.NewGroupDB("group1", 11111, "12345678", nil),
"newgroup1-same-ugid": cache.NewGroupDB("newgroup1-same-ugid", 11111, "12345678", nil),
"newgroup1-diff-ugid": cache.NewGroupDB("newgroup1-diff-ugid", 11111, "99999999", nil),
"group2": cache.NewGroupDB("group2", 22222, "56781234", nil),
"group3": cache.NewGroupDB("group3", 33333, "34567812", nil),
groupCases := map[string]db.GroupDB{
"group1": db.NewGroupDB("group1", 11111, "12345678", nil),
"newgroup1-same-ugid": db.NewGroupDB("newgroup1-same-ugid", 11111, "12345678", nil),
"newgroup1-diff-ugid": db.NewGroupDB("newgroup1-diff-ugid", 11111, "99999999", nil),
"group2": db.NewGroupDB("group2", 22222, "56781234", nil),
"group3": db.NewGroupDB("group3", 33333, "34567812", nil),
}

tests := map[string]struct {
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestUpdateUserEntry(t *testing.T) {
}

user := userCases[tc.userCase]
var groups []cache.GroupDB
var groups []db.GroupDB
for _, g := range tc.groupCases {
groups = append(groups, groupCases[g])
}
Expand All @@ -228,7 +228,7 @@ func TestUpdateUserEntry(t *testing.T) {
}
require.NoError(t, err)

got, err := cache.Z_ForTests_DumpNormalizedYAML(c)
got, err := db.Z_ForTests_DumpNormalizedYAML(c)
require.NoError(t, err, "Created database should be valid yaml content")

golden.CheckOrUpdate(t, got)
Expand All @@ -247,7 +247,7 @@ func TestUserByID(t *testing.T) {
}{
"Get_existing_user": {dbFile: "one_user_and_group"},

"Error_on_missing_user": {wantErrType: cache.NoDataFoundError{}},
"Error_on_missing_user": {wantErrType: db.NoDataFoundError{}},
"Error_on_invalid_database_entry": {dbFile: "invalid_entry_in_userByID", wantErr: true},
}
for name, tc := range tests {
Expand All @@ -273,7 +273,7 @@ func TestUserByName(t *testing.T) {
}{
"Get_existing_user": {dbFile: "one_user_and_group"},

"Error_on_missing_user": {wantErrType: cache.NoDataFoundError{}},
"Error_on_missing_user": {wantErrType: db.NoDataFoundError{}},
"Error_on_invalid_database_entry": {dbFile: "invalid_entry_in_userByName", wantErr: true},
}
for name, tc := range tests {
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestGroupByID(t *testing.T) {
}{
"Get_existing_group": {dbFile: "one_user_and_group"},

"Error_on_missing_group": {wantErrType: cache.NoDataFoundError{}},
"Error_on_missing_group": {wantErrType: db.NoDataFoundError{}},
"Error_on_invalid_database_entry": {dbFile: "invalid_entry_in_groupByID", wantErr: true},
"Error_as_missing_userByID": {dbFile: "partially_valid_multiple_users_and_groups_groupByID_groupToUsers", wantErr: true},
}
Expand All @@ -353,7 +353,7 @@ func TestGroupByName(t *testing.T) {
}{
"Get_existing_group": {dbFile: "one_user_and_group"},

"Error_on_missing_group": {wantErrType: cache.NoDataFoundError{}},
"Error_on_missing_group": {wantErrType: db.NoDataFoundError{}},
"Error_on_invalid_database_entry": {dbFile: "invalid_entry_in_groupByName", wantErr: true},
"Error_as_missing_userByID": {dbFile: "partially_valid_multiple_users_and_groups_groupByID_groupToUsers", wantErr: true},
}
Expand All @@ -380,7 +380,7 @@ func TestUserGroups(t *testing.T) {
}{
"Get_groups_of_existing_user": {dbFile: "one_user_and_group"},

"Error_on_missing_user": {wantErrType: cache.NoDataFoundError{}},
"Error_on_missing_user": {wantErrType: db.NoDataFoundError{}},
"Error_on_invalid_database_entry": {dbFile: "invalid_entry_in_userToGroups", wantErr: true},
"Error_on_missing_groupByID": {dbFile: "invalid_entry_in_groupByID", wantErr: true},
}
Expand Down Expand Up @@ -468,11 +468,11 @@ func TestRemoveDb(t *testing.T) {
cacheDir := filepath.Dir(c.DbPath())

// First call should return with no error.
require.NoError(t, cache.RemoveDb(cacheDir), "RemoveDb should not return an error on the first call")
require.NoError(t, db.RemoveDb(cacheDir), "RemoveDb should not return an error on the first call")
require.NoFileExists(t, cacheDir, "RemoveDb should remove the database file")

// Second call should return ErrNotExist as the database file was already removed.
require.ErrorIs(t, cache.RemoveDb(cacheDir), fs.ErrNotExist, "RemoveDb should return os.ErrNotExist on the second call")
require.ErrorIs(t, db.RemoveDb(cacheDir), fs.ErrNotExist, "RemoveDb should return os.ErrNotExist on the second call")
}

func TestDeleteUser(t *testing.T) {
Expand All @@ -487,7 +487,7 @@ func TestDeleteUser(t *testing.T) {
"Deleting_last_user_from_a_group_keeps_the_group_record": {dbFile: "one_user_and_group"},
"Deleting_existing_user_keeps_other_group_members_intact": {dbFile: "multiple_users_and_groups"},

"Error_on_missing_user": {wantErrType: cache.NoDataFoundError{}},
"Error_on_missing_user": {wantErrType: db.NoDataFoundError{}},
"Error_on_invalid_database_entry": {dbFile: "invalid_entry_in_userByID", wantErr: true},
}
for name, tc := range tests {
Expand All @@ -507,23 +507,23 @@ func TestDeleteUser(t *testing.T) {
}
require.NoError(t, err)

got, err := cache.Z_ForTests_DumpNormalizedYAML(c)
got, err := db.Z_ForTests_DumpNormalizedYAML(c)
require.NoError(t, err, "Created database should be valid yaml content")
golden.CheckOrUpdate(t, got)
})
}
}

// initCache returns a new cache ready to be used alongside its cache directory.
func initCache(t *testing.T, dbFile string) (c *cache.Cache) {
func initCache(t *testing.T, dbFile string) (c *db.Database) {
t.Helper()

cacheDir := t.TempDir()
if dbFile != "" {
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", dbFile+".db.yaml"), cacheDir)
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", dbFile+".db.yaml"), cacheDir)
}

c, err := cache.New(cacheDir)
c, err := db.New(cacheDir)
require.NoError(t, err)
t.Cleanup(func() { c.Close() })

Expand Down
Loading

0 comments on commit b2b08d2

Please sign in to comment.