mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-25 23:49:37 -05:00
7cada2c9e8
* Run goleak as part of all tests We are already using goleak in various tests. This commit adds a TestMain to all remaining tests and calls goleak.VerifyTestMain in them. * Add goleak to debugd/deploy package and fix bug. * Run go mod tidy * Fix integration tests * Move goleak invocation for mount integration test * Ignore leak in state integration tests Co-authored-by: Fabian Kammel <fk@edgelss.systems>
129 lines
2.7 KiB
Go
129 lines
2.7 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/spf13/afero"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/goleak"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
goleak.VerifyTestMain(m)
|
|
}
|
|
|
|
func TestGetLinuxUser(t *testing.T) {
|
|
username := "user"
|
|
|
|
testCases := map[string]struct {
|
|
passwdContents string
|
|
wantErr bool
|
|
wantUser LinuxUser
|
|
}{
|
|
"get works": {
|
|
passwdContents: "user:x:1000:1000:user:/var/home/user:/bin/bash\n",
|
|
wantErr: false,
|
|
wantUser: LinuxUser{
|
|
Username: "user",
|
|
Home: "/var/home/user",
|
|
UID: 1000,
|
|
GID: 1000,
|
|
},
|
|
},
|
|
"user does not exist": {
|
|
passwdContents: "",
|
|
wantErr: true,
|
|
},
|
|
"parse fails": {
|
|
passwdContents: "invalid contents\n",
|
|
wantErr: true,
|
|
},
|
|
"invalid uid": {
|
|
passwdContents: "user:x:invalid:1000:user:/var/home/user:/bin/bash\n",
|
|
wantErr: true,
|
|
},
|
|
"invalid gid": {
|
|
passwdContents: "user:x:1000:invalid:user:/var/home/user:/bin/bash\n",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
require := require.New(t)
|
|
|
|
fs := afero.NewMemMapFs()
|
|
assert.NoError(afero.WriteFile(fs, "/etc/passwd", []byte(tc.passwdContents), 0o755))
|
|
manager := NewLinuxUserManagerFake(fs)
|
|
user, err := manager.GetLinuxUser(username)
|
|
|
|
if tc.wantErr {
|
|
assert.Error(err)
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
assert.Equal(tc.wantUser, user)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnsureLinuxUserExists(t *testing.T) {
|
|
username := "user"
|
|
|
|
testCases := map[string]struct {
|
|
userCreator *StubUserCreator
|
|
wantErr bool
|
|
wantUser LinuxUser
|
|
}{
|
|
"create works": {
|
|
userCreator: &StubUserCreator{},
|
|
wantErr: false,
|
|
wantUser: LinuxUser{
|
|
Username: "user",
|
|
Home: "/var/home/user",
|
|
UID: 1000,
|
|
GID: 1000,
|
|
},
|
|
},
|
|
"create fails": {
|
|
userCreator: &StubUserCreator{
|
|
createUserErr: errors.New("create fails"),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
require := require.New(t)
|
|
|
|
fs := afero.NewMemMapFs()
|
|
manager := NewLinuxUserManagerFake(fs)
|
|
tc.userCreator.fs = fs
|
|
manager.Creator = tc.userCreator
|
|
user, err := manager.EnsureLinuxUserExists(context.Background(), username)
|
|
|
|
if tc.wantErr {
|
|
assert.Error(err)
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
assert.Equal(tc.wantUser, user)
|
|
assert.ElementsMatch([]string{username}, tc.userCreator.usernames)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStringInSlice(t *testing.T) {
|
|
assert := assert.New(t)
|
|
testSlice := []string{"abc", "efg", "xyz"}
|
|
|
|
assert.True(stringInSlice("efg", testSlice))
|
|
assert.False(stringInSlice("hij", testSlice))
|
|
}
|