2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/cli/file"
|
2022-04-06 04:36:58 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckDirClean(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
fileHandler file.Handler
|
|
|
|
existingFiles []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"no file exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
|
|
|
},
|
|
|
|
"adminconf exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.AdminConfFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"master secret exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.MasterSecretFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"state file exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.StateFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"multiple exist": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.AdminConfFilename, constants.MasterSecretFilename, constants.StateFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
for _, f := range tc.existingFiles {
|
2022-04-13 03:15:27 -04:00
|
|
|
require.NoError(tc.fileHandler.Write(f, []byte{1, 2, 3}, file.OptNone))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-06 04:36:58 -04:00
|
|
|
err := checkDirClean(tc.fileHandler)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|