mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-09-25 19:11:18 -04:00
Ref/want err from err expected (#82)
consistent naming for test values using 'want' instead of 'expect/ed'
This commit is contained in:
parent
6265b307af
commit
51068abc27
91 changed files with 2319 additions and 2319 deletions
|
@ -18,8 +18,8 @@ func TestWriteStream(t *testing.T) {
|
|||
readChunkStream fakeReadChunkStream
|
||||
fs afero.Fs
|
||||
showProgress bool
|
||||
expectedFile []byte
|
||||
expectErr bool
|
||||
wantFile []byte
|
||||
wantErr bool
|
||||
}{
|
||||
"stream works": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
|
@ -27,9 +27,9 @@ func TestWriteStream(t *testing.T) {
|
|||
[]byte("test"),
|
||||
},
|
||||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
expectedFile: []byte("test"),
|
||||
expectErr: false,
|
||||
fs: afero.NewMemMapFs(),
|
||||
wantFile: []byte("test"),
|
||||
wantErr: false,
|
||||
},
|
||||
"chunking works": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
|
@ -38,9 +38,9 @@ func TestWriteStream(t *testing.T) {
|
|||
[]byte("st"),
|
||||
},
|
||||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
expectedFile: []byte("test"),
|
||||
expectErr: false,
|
||||
fs: afero.NewMemMapFs(),
|
||||
wantFile: []byte("test"),
|
||||
wantErr: false,
|
||||
},
|
||||
"showProgress works": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
|
@ -50,19 +50,19 @@ func TestWriteStream(t *testing.T) {
|
|||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
showProgress: true,
|
||||
expectedFile: []byte("test"),
|
||||
expectErr: false,
|
||||
wantFile: []byte("test"),
|
||||
wantErr: false,
|
||||
},
|
||||
"Open fails": {
|
||||
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
|
||||
expectErr: true,
|
||||
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
|
||||
wantErr: true,
|
||||
},
|
||||
"recv fails": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
recvErr: errors.New("someErr"),
|
||||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
expectErr: true,
|
||||
fs: afero.NewMemMapFs(),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -74,14 +74,14 @@ func TestWriteStream(t *testing.T) {
|
|||
writer := NewFileStreamer(tc.fs)
|
||||
err := writer.WriteStream(filename, &tc.readChunkStream, tc.showProgress)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
fileContents, err := afero.ReadFile(tc.fs, filename)
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedFile, fileContents)
|
||||
assert.Equal(tc.wantFile, fileContents)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -94,48 +94,48 @@ func TestReadStream(t *testing.T) {
|
|||
filename string
|
||||
chunksize uint
|
||||
showProgress bool
|
||||
expectedChunks [][]byte
|
||||
expectErr bool
|
||||
wantChunks [][]byte
|
||||
wantErr bool
|
||||
}{
|
||||
"stream works": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 4,
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"chunking works": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 2,
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("te"),
|
||||
[]byte("st"),
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"chunksize of 0 detected": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 0,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"showProgress works": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 4,
|
||||
showProgress: true,
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"Open fails": {
|
||||
filename: "incorrect-filename",
|
||||
chunksize: 4,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"send fails": {
|
||||
writeChunkStream: stubWriteChunkStream{
|
||||
|
@ -143,7 +143,7 @@ func TestReadStream(t *testing.T) {
|
|||
},
|
||||
filename: correctFilename,
|
||||
chunksize: 4,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -157,12 +157,12 @@ func TestReadStream(t *testing.T) {
|
|||
reader := NewFileStreamer(fs)
|
||||
err := reader.ReadStream(tc.filename, &tc.writeChunkStream, tc.chunksize, tc.showProgress)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedChunks, tc.writeChunkStream.chunks)
|
||||
assert.Equal(tc.wantChunks, tc.writeChunkStream.chunks)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,28 +22,28 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
filename := "/opt/coordinator"
|
||||
|
||||
testCases := map[string]struct {
|
||||
server fakeOnlyDownloadServer
|
||||
downloadClient stubDownloadClient
|
||||
serviceManager stubServiceManager
|
||||
attemptedDownloads map[string]time.Time
|
||||
expectedChunks [][]byte
|
||||
expectDownloadErr bool
|
||||
expectFile bool
|
||||
expectSystemdAction bool
|
||||
expectDeployed bool
|
||||
server fakeOnlyDownloadServer
|
||||
downloadClient stubDownloadClient
|
||||
serviceManager stubServiceManager
|
||||
attemptedDownloads map[string]time.Time
|
||||
wantChunks [][]byte
|
||||
wantDownloadErr bool
|
||||
wantFile bool
|
||||
wantSystemdAction bool
|
||||
wantDeployed bool
|
||||
}{
|
||||
"download works": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
chunks: [][]byte{[]byte("test")},
|
||||
},
|
||||
attemptedDownloads: map[string]time.Time{},
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectDownloadErr: false,
|
||||
expectFile: true,
|
||||
expectSystemdAction: true,
|
||||
expectDeployed: true,
|
||||
wantDownloadErr: false,
|
||||
wantFile: true,
|
||||
wantSystemdAction: true,
|
||||
wantDeployed: true,
|
||||
},
|
||||
"second download is not attempted twice": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
|
@ -52,14 +52,14 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
attemptedDownloads: map[string]time.Time{
|
||||
"192.0.2.0:4000": time.Now(),
|
||||
},
|
||||
expectDownloadErr: true,
|
||||
wantDownloadErr: true,
|
||||
},
|
||||
"download rpc call error is detected": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
downladErr: errors.New("download rpc error"),
|
||||
},
|
||||
attemptedDownloads: map[string]time.Time{},
|
||||
expectDownloadErr: true,
|
||||
wantDownloadErr: true,
|
||||
},
|
||||
"service restart error is detected": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
|
@ -69,13 +69,13 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
systemdActionErr: errors.New("systemd error"),
|
||||
},
|
||||
attemptedDownloads: map[string]time.Time{},
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectDownloadErr: true,
|
||||
expectFile: true,
|
||||
expectDeployed: true,
|
||||
expectSystemdAction: false,
|
||||
wantDownloadErr: true,
|
||||
wantFile: true,
|
||||
wantDeployed: true,
|
||||
wantSystemdAction: false,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -101,17 +101,17 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
err := download.DownloadCoordinator(context.Background(), ip)
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectDownloadErr {
|
||||
if tc.wantDownloadErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
||||
if tc.expectFile {
|
||||
assert.Equal(tc.expectedChunks, writer.chunks)
|
||||
if tc.wantFile {
|
||||
assert.Equal(tc.wantChunks, writer.chunks)
|
||||
assert.Equal(filename, writer.filename)
|
||||
}
|
||||
if tc.expectSystemdAction {
|
||||
if tc.wantSystemdAction {
|
||||
assert.ElementsMatch(
|
||||
[]ServiceManagerRequest{
|
||||
{Unit: debugd.CoordinatorSystemdUnitName, Action: Restart},
|
||||
|
|
|
@ -17,14 +17,14 @@ func TestGetLinuxUser(t *testing.T) {
|
|||
testCases := map[string]struct {
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
expectErr bool
|
||||
expectedUser LinuxUser
|
||||
wantErr bool
|
||||
wantUser LinuxUser
|
||||
}{
|
||||
"get works": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectedUser: LinuxUser{
|
||||
wantErr: false,
|
||||
wantUser: LinuxUser{
|
||||
Username: "user",
|
||||
Home: "/home/user",
|
||||
Uid: 1000,
|
||||
|
@ -34,22 +34,22 @@ func TestGetLinuxUser(t *testing.T) {
|
|||
"user does not exist": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"parse fails": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "invalid contents\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid uid": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:invalid:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid gid": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:invalid:user:/home/user:/bin/bash\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -67,12 +67,12 @@ func TestGetLinuxUser(t *testing.T) {
|
|||
}
|
||||
user, err := manager.getLinuxUser(username)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedUser, user)
|
||||
assert.Equal(tc.wantUser, user)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -83,14 +83,14 @@ func TestEnsureLinuxUserExists(t *testing.T) {
|
|||
testCases := map[string]struct {
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
expectErr bool
|
||||
expectedUser LinuxUser
|
||||
wantErr bool
|
||||
wantUser LinuxUser
|
||||
}{
|
||||
"create works": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectedUser: LinuxUser{
|
||||
wantErr: false,
|
||||
wantUser: LinuxUser{
|
||||
Username: "user",
|
||||
Home: "/home/user",
|
||||
Uid: 1000,
|
||||
|
@ -102,7 +102,7 @@ func TestEnsureLinuxUserExists(t *testing.T) {
|
|||
createUserErr: errors.New("create fails"),
|
||||
},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -120,12 +120,12 @@ func TestEnsureLinuxUserExists(t *testing.T) {
|
|||
}
|
||||
user, err := manager.EnsureLinuxUserExists(context.Background(), username)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedUser, user)
|
||||
assert.Equal(tc.wantUser, user)
|
||||
assert.ElementsMatch([]string{username}, tc.userCreator.usernames)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -12,15 +12,15 @@ func TestParse(t *testing.T) {
|
|||
filename := "/etc/passwd"
|
||||
|
||||
testCases := map[string]struct {
|
||||
passwdContents string
|
||||
createFile bool
|
||||
expectedEntries Entries
|
||||
expectErr bool
|
||||
passwdContents string
|
||||
createFile bool
|
||||
wantEntries Entries
|
||||
wantErr bool
|
||||
}{
|
||||
"parse works": {
|
||||
passwdContents: "root:x:0:0:root:/root:/bin/bash\n",
|
||||
createFile: true,
|
||||
expectedEntries: Entries{
|
||||
wantEntries: Entries{
|
||||
"root": {
|
||||
Pass: "x",
|
||||
Uid: "0",
|
||||
|
@ -30,16 +30,16 @@ func TestParse(t *testing.T) {
|
|||
Shell: "/bin/bash",
|
||||
},
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"passwd is corrupt": {
|
||||
passwdContents: "too:few:fields\n",
|
||||
createFile: true,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"file does not exist": {
|
||||
createFile: false,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -55,12 +55,12 @@ func TestParse(t *testing.T) {
|
|||
passwd := Passwd{}
|
||||
entries, err := passwd.Parse(fs)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedEntries, entries)
|
||||
assert.Equal(tc.wantEntries, entries)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,9 +16,9 @@ func TestSystemdAction(t *testing.T) {
|
|||
unitName := "example.service"
|
||||
|
||||
testCases := map[string]struct {
|
||||
dbus stubDbus
|
||||
action SystemdAction
|
||||
expectErr bool
|
||||
dbus stubDbus
|
||||
action SystemdAction
|
||||
wantErr bool
|
||||
}{
|
||||
"start works": {
|
||||
dbus: stubDbus{
|
||||
|
@ -26,8 +26,8 @@ func TestSystemdAction(t *testing.T) {
|
|||
result: "done",
|
||||
},
|
||||
},
|
||||
action: Start,
|
||||
expectErr: false,
|
||||
action: Start,
|
||||
wantErr: false,
|
||||
},
|
||||
"stop works": {
|
||||
dbus: stubDbus{
|
||||
|
@ -35,8 +35,8 @@ func TestSystemdAction(t *testing.T) {
|
|||
result: "done",
|
||||
},
|
||||
},
|
||||
action: Stop,
|
||||
expectErr: false,
|
||||
action: Stop,
|
||||
wantErr: false,
|
||||
},
|
||||
"restart works": {
|
||||
dbus: stubDbus{
|
||||
|
@ -44,22 +44,22 @@ func TestSystemdAction(t *testing.T) {
|
|||
result: "done",
|
||||
},
|
||||
},
|
||||
action: Restart,
|
||||
expectErr: false,
|
||||
action: Restart,
|
||||
wantErr: false,
|
||||
},
|
||||
"reload works": {
|
||||
dbus: stubDbus{
|
||||
conn: &fakeDbusConn{},
|
||||
},
|
||||
action: Reload,
|
||||
expectErr: false,
|
||||
action: Reload,
|
||||
wantErr: false,
|
||||
},
|
||||
"unknown action": {
|
||||
dbus: stubDbus{
|
||||
conn: &fakeDbusConn{},
|
||||
},
|
||||
action: Unknown,
|
||||
expectErr: true,
|
||||
action: Unknown,
|
||||
wantErr: true,
|
||||
},
|
||||
"action fails": {
|
||||
dbus: stubDbus{
|
||||
|
@ -67,8 +67,8 @@ func TestSystemdAction(t *testing.T) {
|
|||
actionErr: errors.New("action fails"),
|
||||
},
|
||||
},
|
||||
action: Start,
|
||||
expectErr: true,
|
||||
action: Start,
|
||||
wantErr: true,
|
||||
},
|
||||
"action result is failure": {
|
||||
dbus: stubDbus{
|
||||
|
@ -76,15 +76,15 @@ func TestSystemdAction(t *testing.T) {
|
|||
result: "failure",
|
||||
},
|
||||
},
|
||||
action: Start,
|
||||
expectErr: true,
|
||||
action: Start,
|
||||
wantErr: true,
|
||||
},
|
||||
"newConn fails": {
|
||||
dbus: stubDbus{
|
||||
connErr: errors.New("newConn fails"),
|
||||
},
|
||||
action: Start,
|
||||
expectErr: true,
|
||||
action: Start,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@ func TestSystemdAction(t *testing.T) {
|
|||
Action: tc.action,
|
||||
})
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -115,11 +115,11 @@ func TestSystemdAction(t *testing.T) {
|
|||
|
||||
func TestWriteSystemdUnitFile(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
dbus stubDbus
|
||||
unit SystemdUnit
|
||||
readonly bool
|
||||
expectErr bool
|
||||
expectedFileContents string
|
||||
dbus stubDbus
|
||||
unit SystemdUnit
|
||||
readonly bool
|
||||
wantErr bool
|
||||
wantFileContents string
|
||||
}{
|
||||
"start works": {
|
||||
dbus: stubDbus{
|
||||
|
@ -131,8 +131,8 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
|||
Name: "test.service",
|
||||
Contents: "testservicefilecontents",
|
||||
},
|
||||
expectErr: false,
|
||||
expectedFileContents: "testservicefilecontents",
|
||||
wantErr: false,
|
||||
wantFileContents: "testservicefilecontents",
|
||||
},
|
||||
"write fails": {
|
||||
dbus: stubDbus{
|
||||
|
@ -144,8 +144,8 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
|||
Name: "test.service",
|
||||
Contents: "testservicefilecontents",
|
||||
},
|
||||
readonly: true,
|
||||
expectErr: true,
|
||||
readonly: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"systemd reload fails": {
|
||||
dbus: stubDbus{
|
||||
|
@ -157,8 +157,8 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
|||
Name: "test.service",
|
||||
Contents: "testservicefilecontents",
|
||||
},
|
||||
readonly: false,
|
||||
expectErr: true,
|
||||
readonly: false,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -179,14 +179,14 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
|||
}
|
||||
err := manager.WriteSystemdUnitFile(context.Background(), tc.unit)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
fileContents, err := afero.ReadFile(fs, fmt.Sprintf("%s/%s", systemdUnitFolder, tc.unit.Name))
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.expectedFileContents, string(fileContents))
|
||||
assert.Equal(tc.wantFileContents, string(fileContents))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,51 +19,51 @@ func TestDeploySSHAuthorizedKey(t *testing.T) {
|
|||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
fs afero.Fs
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
alreadyDeployed bool
|
||||
readonly bool
|
||||
expectErr bool
|
||||
expectFile bool
|
||||
expectedFileContents string
|
||||
fs afero.Fs
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
alreadyDeployed bool
|
||||
readonly bool
|
||||
wantErr bool
|
||||
wantFile bool
|
||||
wantFileContents string
|
||||
}{
|
||||
"deploy works": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectFile: true,
|
||||
expectedFileContents: "ssh-rsa testkey user\n",
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
wantErr: false,
|
||||
wantFile: true,
|
||||
wantFileContents: "ssh-rsa testkey user\n",
|
||||
},
|
||||
"appending ssh key works": {
|
||||
fs: memMapFsWithFile("/home/user/.ssh/authorized_keys.d/debugd", "ssh-rsa preexistingkey user\n"),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectFile: true,
|
||||
expectedFileContents: "ssh-rsa preexistingkey user\nssh-rsa testkey user\n",
|
||||
fs: memMapFsWithFile("/home/user/.ssh/authorized_keys.d/debugd", "ssh-rsa preexistingkey user\n"),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
wantErr: false,
|
||||
wantFile: true,
|
||||
wantFileContents: "ssh-rsa preexistingkey user\nssh-rsa testkey user\n",
|
||||
},
|
||||
"redeployment avoided": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
alreadyDeployed: true,
|
||||
expectFile: false,
|
||||
wantFile: false,
|
||||
},
|
||||
"user does not exist": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"readonly fs": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
readonly: true,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -92,15 +92,15 @@ func TestDeploySSHAuthorizedKey(t *testing.T) {
|
|||
}
|
||||
err := sshAccess.DeploySSHAuthorizedKey(context.Background(), authorizedKey)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
if tc.expectFile {
|
||||
if tc.wantFile {
|
||||
fileContents, err := afero.ReadFile(tc.fs, "/home/user/.ssh/authorized_keys.d/debugd")
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.expectedFileContents, string(fileContents))
|
||||
assert.Equal(tc.wantFileContents, string(fileContents))
|
||||
} else {
|
||||
exists, err := afero.Exists(tc.fs, "/home/user/.ssh/authorized_keys.d/debugd")
|
||||
assert.NoError(err)
|
||||
|
|
|
@ -15,9 +15,9 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
|||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
meta stubMetadata
|
||||
expectedIPs []string
|
||||
expectErr bool
|
||||
meta stubMetadata
|
||||
wantIPs []string
|
||||
wantErr bool
|
||||
}{
|
||||
"disovery works": {
|
||||
meta: stubMetadata{
|
||||
|
@ -33,7 +33,7 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
expectedIPs: []string{
|
||||
wantIPs: []string{
|
||||
"192.0.2.1", "192.0.2.2",
|
||||
},
|
||||
},
|
||||
|
@ -41,7 +41,7 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
|||
meta: stubMetadata{
|
||||
listErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -55,12 +55,12 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
|||
}
|
||||
ips, err := fetcher.DiscoverDebugdIPs(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedIPs, ips)
|
||||
assert.ElementsMatch(tc.wantIPs, ips)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -69,9 +69,9 @@ func TestFetchSSHKeys(t *testing.T) {
|
|||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
meta stubMetadata
|
||||
expectedKeys []ssh.SSHKey
|
||||
expectErr bool
|
||||
meta stubMetadata
|
||||
wantKeys []ssh.SSHKey
|
||||
wantErr bool
|
||||
}{
|
||||
"fetch works": {
|
||||
meta: stubMetadata{
|
||||
|
@ -81,7 +81,7 @@ func TestFetchSSHKeys(t *testing.T) {
|
|||
SSHKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
||||
},
|
||||
},
|
||||
expectedKeys: []ssh.SSHKey{
|
||||
wantKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "bob",
|
||||
KeyValue: "ssh-rsa bobskey",
|
||||
|
@ -92,7 +92,7 @@ func TestFetchSSHKeys(t *testing.T) {
|
|||
meta: stubMetadata{
|
||||
selfErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -106,12 +106,12 @@ func TestFetchSSHKeys(t *testing.T) {
|
|||
}
|
||||
keys, err := fetcher.FetchSSHKeys(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedKeys, keys)
|
||||
assert.ElementsMatch(tc.wantKeys, keys)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,12 +13,12 @@ import (
|
|||
|
||||
func TestSchedulerStart(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
fetcher stubFetcher
|
||||
ssh stubSSHDeployer
|
||||
downloader stubDownloader
|
||||
timeout time.Duration
|
||||
expectedSSHKeys []ssh.SSHKey
|
||||
expectedDebugdDownloads []string
|
||||
fetcher stubFetcher
|
||||
ssh stubSSHDeployer
|
||||
downloader stubDownloader
|
||||
timeout time.Duration
|
||||
wantSSHKeys []ssh.SSHKey
|
||||
wantDebugdDownloads []string
|
||||
}{
|
||||
"scheduler works and calls fetcher functions at least once": {},
|
||||
"ssh keys are fetched": {
|
||||
|
@ -30,7 +30,7 @@ func TestSchedulerStart(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
expectedSSHKeys: []ssh.SSHKey{
|
||||
wantSSHKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "test",
|
||||
KeyValue: "testkey",
|
||||
|
@ -45,14 +45,14 @@ func TestSchedulerStart(t *testing.T) {
|
|||
err: errors.New("download fails"),
|
||||
},
|
||||
|
||||
expectedDebugdDownloads: []string{"192.0.2.1", "192.0.2.2"},
|
||||
wantDebugdDownloads: []string{"192.0.2.1", "192.0.2.2"},
|
||||
},
|
||||
"if download is successful, second download is not attempted": {
|
||||
fetcher: stubFetcher{
|
||||
ips: []string{"192.0.2.1", "192.0.2.2"},
|
||||
},
|
||||
|
||||
expectedDebugdDownloads: []string{"192.0.2.1"},
|
||||
wantDebugdDownloads: []string{"192.0.2.1"},
|
||||
},
|
||||
"endpoint discovery can fail": {
|
||||
fetcher: stubFetcher{
|
||||
|
@ -82,8 +82,8 @@ func TestSchedulerStart(t *testing.T) {
|
|||
go scheduler.Start(ctx, wg)
|
||||
|
||||
wg.Wait()
|
||||
assert.Equal(tc.expectedSSHKeys, tc.ssh.sshKeys)
|
||||
assert.Equal(tc.expectedDebugdDownloads, tc.downloader.ips)
|
||||
assert.Equal(tc.wantSSHKeys, tc.ssh.sshKeys)
|
||||
assert.Equal(tc.wantDebugdDownloads, tc.downloader.ips)
|
||||
assert.Greater(tc.fetcher.discoverCalls, 0)
|
||||
assert.Greater(tc.fetcher.fetchSSHKeysCalls, 0)
|
||||
})
|
||||
|
|
|
@ -23,12 +23,12 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
|||
endpoint := "192.0.2.1:4000"
|
||||
|
||||
testCases := map[string]struct {
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadAuthorizedKeysRequest
|
||||
expectErr bool
|
||||
expectedResponseStatus pb.UploadAuthorizedKeysStatus
|
||||
expectedKeys []ssh.SSHKey
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadAuthorizedKeysRequest
|
||||
wantErr bool
|
||||
wantResponseStatus pb.UploadAuthorizedKeysStatus
|
||||
wantKeys []ssh.SSHKey
|
||||
}{
|
||||
"upload authorized keys works": {
|
||||
request: &pb.UploadAuthorizedKeysRequest{
|
||||
|
@ -39,8 +39,8 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
expectedResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_SUCCESS,
|
||||
expectedKeys: []ssh.SSHKey{
|
||||
wantResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_SUCCESS,
|
||||
wantKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "testuser",
|
||||
KeyValue: "teskey",
|
||||
|
@ -56,9 +56,9 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
ssh: stubSSHDeployer{deployErr: errors.New("ssh key deployment error")},
|
||||
expectedResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_FAILURE,
|
||||
expectedKeys: []ssh.SSHKey{
|
||||
ssh: stubSSHDeployer{deployErr: errors.New("ssh key deployment error")},
|
||||
wantResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_FAILURE,
|
||||
wantKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "testuser",
|
||||
KeyValue: "teskey",
|
||||
|
@ -86,13 +86,13 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
|||
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.ssh.sshKeys, tc.expectedKeys)
|
||||
assert.Equal(tc.wantResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.ssh.sshKeys, tc.wantKeys)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -101,31 +101,31 @@ func TestUploadCoordinator(t *testing.T) {
|
|||
endpoint := "192.0.2.1:4000"
|
||||
|
||||
testCases := map[string]struct {
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
streamer fakeStreamer
|
||||
uploadChunks [][]byte
|
||||
expectErr bool
|
||||
expectedResponseStatus pb.UploadCoordinatorStatus
|
||||
expectFile bool
|
||||
expectedChunks [][]byte
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
streamer fakeStreamer
|
||||
uploadChunks [][]byte
|
||||
wantErr bool
|
||||
wantResponseStatus pb.UploadCoordinatorStatus
|
||||
wantFile bool
|
||||
wantChunks [][]byte
|
||||
}{
|
||||
"upload works": {
|
||||
uploadChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectFile: true,
|
||||
expectedChunks: [][]byte{
|
||||
wantFile: true,
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_SUCCESS,
|
||||
wantResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_SUCCESS,
|
||||
},
|
||||
"recv fails": {
|
||||
streamer: fakeStreamer{
|
||||
writeStreamErr: errors.New("recv error"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_UPLOAD_FAILED,
|
||||
expectErr: true,
|
||||
wantResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_UPLOAD_FAILED,
|
||||
wantErr: true,
|
||||
},
|
||||
"starting coordinator fails": {
|
||||
uploadChunks: [][]byte{
|
||||
|
@ -134,11 +134,11 @@ func TestUploadCoordinator(t *testing.T) {
|
|||
serviceManager: stubServiceManager{
|
||||
systemdActionErr: errors.New("starting coordinator error"),
|
||||
},
|
||||
expectFile: true,
|
||||
expectedChunks: [][]byte{
|
||||
wantFile: true,
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_START_FAILED,
|
||||
wantResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_START_FAILED,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -164,14 +164,14 @@ func TestUploadCoordinator(t *testing.T) {
|
|||
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedResponseStatus, resp.Status)
|
||||
if tc.expectFile {
|
||||
assert.Equal(tc.expectedChunks, tc.streamer.writeStreamChunks)
|
||||
assert.Equal(tc.wantResponseStatus, resp.Status)
|
||||
if tc.wantFile {
|
||||
assert.Equal(tc.wantChunks, tc.streamer.writeStreamChunks)
|
||||
assert.Equal("/opt/coordinator", tc.streamer.writeStreamFilename)
|
||||
} else {
|
||||
assert.Empty(tc.streamer.writeStreamChunks)
|
||||
|
@ -188,8 +188,8 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
serviceManager stubServiceManager
|
||||
request *pb.DownloadCoordinatorRequest
|
||||
streamer fakeStreamer
|
||||
expectErr bool
|
||||
expectedChunks [][]byte
|
||||
wantErr bool
|
||||
wantChunks [][]byte
|
||||
}{
|
||||
"download works": {
|
||||
request: &pb.DownloadCoordinatorRequest{},
|
||||
|
@ -198,8 +198,8 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
[]byte("test"),
|
||||
},
|
||||
},
|
||||
expectErr: false,
|
||||
expectedChunks: [][]byte{
|
||||
wantErr: false,
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
},
|
||||
|
@ -208,7 +208,7 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
streamer: fakeStreamer{
|
||||
readStreamErr: errors.New("read coordinator fails"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -232,12 +232,12 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
chunks, err := fakeRead(stream)
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedChunks, chunks)
|
||||
assert.Equal(tc.wantChunks, chunks)
|
||||
assert.Equal("/opt/coordinator", tc.streamer.readStreamFilename)
|
||||
})
|
||||
}
|
||||
|
@ -246,12 +246,12 @@ func TestDownloadCoordinator(t *testing.T) {
|
|||
func TestUploadSystemServiceUnits(t *testing.T) {
|
||||
endpoint := "192.0.2.1:4000"
|
||||
testCases := map[string]struct {
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadSystemdServiceUnitsRequest
|
||||
expectErr bool
|
||||
expectedResponseStatus pb.UploadSystemdServiceUnitsStatus
|
||||
expectedUnitFiles []deploy.SystemdUnit
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadSystemdServiceUnitsRequest
|
||||
wantErr bool
|
||||
wantResponseStatus pb.UploadSystemdServiceUnitsStatus
|
||||
wantUnitFiles []deploy.SystemdUnit
|
||||
}{
|
||||
"upload systemd service units": {
|
||||
request: &pb.UploadSystemdServiceUnitsRequest{
|
||||
|
@ -262,8 +262,8 @@ func TestUploadSystemServiceUnits(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
expectedResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_SUCCESS,
|
||||
expectedUnitFiles: []deploy.SystemdUnit{
|
||||
wantResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_SUCCESS,
|
||||
wantUnitFiles: []deploy.SystemdUnit{
|
||||
{
|
||||
Name: "test.service",
|
||||
Contents: "testcontents",
|
||||
|
@ -282,8 +282,8 @@ func TestUploadSystemServiceUnits(t *testing.T) {
|
|||
serviceManager: stubServiceManager{
|
||||
writeSystemdUnitFileErr: errors.New("write error"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_FAILURE,
|
||||
expectedUnitFiles: []deploy.SystemdUnit{
|
||||
wantResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_FAILURE,
|
||||
wantUnitFiles: []deploy.SystemdUnit{
|
||||
{
|
||||
Name: "test.service",
|
||||
Contents: "testcontents",
|
||||
|
@ -310,14 +310,14 @@ func TestUploadSystemServiceUnits(t *testing.T) {
|
|||
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
require.NotNil(resp.Status)
|
||||
assert.Equal(tc.expectedResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.expectedUnitFiles, tc.serviceManager.unitFiles)
|
||||
assert.Equal(tc.wantResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.wantUnitFiles, tc.serviceManager.unitFiles)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue