wrote test for ssh subcommand

This commit is contained in:
miampf 2025-01-14 13:40:21 +01:00
parent bee3f6c159
commit b5fddcd254
No known key found for this signature in database
GPG key ID: EF039364B5B6886C
3 changed files with 111 additions and 8 deletions

View 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)
}
})
}
}