mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-17 03:00:43 -04:00
wrote test for ssh subcommand
This commit is contained in:
parent
bee3f6c159
commit
b5fddcd254
3 changed files with 111 additions and 8 deletions
97
cli/internal/cmd/ssh_test.go
Normal file
97
cli/internal/cmd/ssh_test.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||
"github.com/edgelesssys/constellation/v2/internal/file"
|
||||
"github.com/edgelesssys/constellation/v2/internal/logger"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSSH(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
someSSHPubKey := "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDA1yYg1PIJNjAGjyuv66r8AJtpfBDFLdp3u9lVwkgbVKv1AzcaeTF/NEw+nhNJOjuCZ61LTPj12LZ8Wy/oSm0A= motte@lolcatghost"
|
||||
someSSHPubKeyPath := "some-key.pub"
|
||||
someMasterSecret := `
|
||||
{
|
||||
"key": "MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAK",
|
||||
"salt": "MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAK"
|
||||
}
|
||||
`
|
||||
|
||||
newFsWithDirectory := func() file.Handler {
|
||||
fh := file.NewHandler(afero.NewMemMapFs())
|
||||
require.NoError(fh.MkdirAll(constants.TerraformWorkingDir))
|
||||
return fh
|
||||
}
|
||||
newFsNoDirectory := func() file.Handler {
|
||||
fh := file.NewHandler(afero.NewMemMapFs())
|
||||
return fh
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
fh file.Handler
|
||||
pubKey string
|
||||
masterSecret string
|
||||
wantErr bool
|
||||
}{
|
||||
"everything exists": {
|
||||
fh: newFsWithDirectory(),
|
||||
pubKey: someSSHPubKey,
|
||||
masterSecret: someMasterSecret,
|
||||
},
|
||||
"no public key": {
|
||||
fh: newFsWithDirectory(),
|
||||
masterSecret: someMasterSecret,
|
||||
wantErr: true,
|
||||
},
|
||||
"no master secret": {
|
||||
fh: newFsWithDirectory(),
|
||||
pubKey: someSSHPubKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"malformatted public key": {
|
||||
fh: newFsWithDirectory(),
|
||||
pubKey: "asdf",
|
||||
masterSecret: someMasterSecret,
|
||||
wantErr: true,
|
||||
},
|
||||
"malformatted master secret": {
|
||||
fh: newFsWithDirectory(),
|
||||
masterSecret: "asdf",
|
||||
pubKey: someSSHPubKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"directory does not exist": {
|
||||
fh: newFsNoDirectory(),
|
||||
pubKey: someSSHPubKey,
|
||||
masterSecret: someMasterSecret,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
if tc.pubKey != "" {
|
||||
tc.fh.Write(someSSHPubKeyPath, []byte(tc.pubKey))
|
||||
}
|
||||
if tc.masterSecret != "" {
|
||||
tc.fh.Write(constants.MasterSecretFilename, []byte(tc.masterSecret))
|
||||
}
|
||||
|
||||
err := generateKey(context.Background(), someSSHPubKeyPath, tc.fh, logger.NewTest(t))
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue