2023-08-23 04:35:42 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2023-11-20 05:17:16 -05:00
|
|
|
"os"
|
2023-10-26 04:55:50 -04:00
|
|
|
"path/filepath"
|
2023-08-23 04:35:42 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-10-31 07:46:40 -04:00
|
|
|
func TestTFPlan(t *testing.T) {
|
2023-10-26 04:55:50 -04:00
|
|
|
const (
|
|
|
|
templateDir = "templateDir"
|
|
|
|
existingWorkspace = "existing"
|
|
|
|
backupDir = "backup"
|
|
|
|
testFile = "testfile"
|
|
|
|
)
|
|
|
|
fsWithWorkspace := func(require *require.Assertions) file.Handler {
|
|
|
|
fs := file.NewHandler(afero.NewMemMapFs())
|
2023-10-31 07:46:40 -04:00
|
|
|
require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}, file.OptMkdirAll))
|
2023-10-26 04:55:50 -04:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
testCases := map[string]struct {
|
2023-10-31 07:46:40 -04:00
|
|
|
prepareFs func(require *require.Assertions) file.Handler
|
|
|
|
tf *stubUpgradePlanner
|
|
|
|
wantDiff bool
|
|
|
|
wantBackup bool
|
|
|
|
wantErr bool
|
2023-08-23 04:35:42 -04:00
|
|
|
}{
|
|
|
|
"success no diff": {
|
2023-10-31 07:46:40 -04:00
|
|
|
prepareFs: fsWithWorkspace,
|
|
|
|
tf: &stubUpgradePlanner{},
|
|
|
|
wantBackup: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
},
|
|
|
|
"success diff": {
|
2023-10-26 04:55:50 -04:00
|
|
|
prepareFs: fsWithWorkspace,
|
2023-08-23 04:35:42 -04:00
|
|
|
tf: &stubUpgradePlanner{
|
|
|
|
planDiff: true,
|
|
|
|
},
|
2023-10-31 07:46:40 -04:00
|
|
|
wantDiff: true,
|
|
|
|
wantBackup: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
},
|
2023-10-31 07:46:40 -04:00
|
|
|
"workspace is empty": {
|
2023-10-26 04:55:50 -04:00
|
|
|
prepareFs: func(require *require.Assertions) file.Handler {
|
|
|
|
return file.NewHandler(afero.NewMemMapFs())
|
|
|
|
},
|
2023-10-31 07:46:40 -04:00
|
|
|
tf: &stubUpgradePlanner{},
|
2023-10-26 04:55:50 -04:00
|
|
|
},
|
2023-10-31 07:46:40 -04:00
|
|
|
"backup dir already exists": {
|
2023-08-23 04:35:42 -04:00
|
|
|
prepareFs: func(require *require.Assertions) file.Handler {
|
2023-10-26 04:55:50 -04:00
|
|
|
fs := fsWithWorkspace(require)
|
|
|
|
require.NoError(fs.MkdirAll(backupDir))
|
2023-08-23 04:35:42 -04:00
|
|
|
return fs
|
|
|
|
},
|
|
|
|
tf: &stubUpgradePlanner{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"prepare workspace error": {
|
2023-10-26 04:55:50 -04:00
|
|
|
prepareFs: fsWithWorkspace,
|
2023-08-23 04:35:42 -04:00
|
|
|
tf: &stubUpgradePlanner{
|
|
|
|
prepareWorkspaceErr: assert.AnError,
|
|
|
|
},
|
2023-10-31 07:46:40 -04:00
|
|
|
wantBackup: true,
|
|
|
|
wantErr: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
},
|
|
|
|
"plan error": {
|
2023-10-26 04:55:50 -04:00
|
|
|
prepareFs: fsWithWorkspace,
|
2023-08-23 04:35:42 -04:00
|
|
|
tf: &stubUpgradePlanner{
|
|
|
|
planErr: assert.AnError,
|
|
|
|
},
|
2023-10-31 07:46:40 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantBackup: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
},
|
|
|
|
"show plan error": {
|
2023-10-26 04:55:50 -04:00
|
|
|
prepareFs: fsWithWorkspace,
|
2023-08-23 04:35:42 -04:00
|
|
|
tf: &stubUpgradePlanner{
|
|
|
|
planDiff: true,
|
|
|
|
showPlanErr: assert.AnError,
|
|
|
|
},
|
2023-10-31 07:46:40 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantBackup: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
fs := tc.prepareFs(require.New(t))
|
|
|
|
|
2023-10-31 07:46:40 -04:00
|
|
|
hasDiff, planErr := plan(
|
2023-08-23 04:35:42 -04:00
|
|
|
context.Background(), tc.tf, fs, io.Discard, terraform.LogLevelDebug,
|
|
|
|
&terraform.QEMUVariables{},
|
2023-10-26 04:55:50 -04:00
|
|
|
templateDir, existingWorkspace, backupDir,
|
2023-08-23 04:35:42 -04:00
|
|
|
)
|
2023-10-31 07:46:40 -04:00
|
|
|
|
|
|
|
if tc.wantBackup {
|
|
|
|
_, err := fs.Stat(filepath.Join(backupDir, testFile))
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
if tc.wantErr {
|
2023-10-31 07:46:40 -04:00
|
|
|
assert.Error(planErr)
|
2023-08-23 04:35:42 -04:00
|
|
|
return
|
|
|
|
}
|
2023-10-31 07:46:40 -04:00
|
|
|
assert.NoError(planErr)
|
2023-08-23 04:35:42 -04:00
|
|
|
assert.Equal(tc.wantDiff, hasDiff)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 05:51:20 -04:00
|
|
|
func TestRestoreBackup(t *testing.T) {
|
2023-08-23 04:35:42 -04:00
|
|
|
existingWorkspace := "foo"
|
2023-09-14 05:51:20 -04:00
|
|
|
backupDir := "bar"
|
2023-11-20 05:17:16 -05:00
|
|
|
testFile := "file"
|
2023-08-23 04:35:42 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2023-11-20 05:17:16 -05:00
|
|
|
prepareFs func(require *require.Assertions) file.Handler
|
|
|
|
wantRemoveWorkingDir bool
|
|
|
|
wantErr bool
|
2023-08-23 04:35:42 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
prepareFs: func(require *require.Assertions) file.Handler {
|
|
|
|
fs := file.NewHandler(afero.NewMemMapFs())
|
2023-11-20 05:17:16 -05:00
|
|
|
require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}, file.OptMkdirAll))
|
|
|
|
require.NoError(fs.Write(filepath.Join(backupDir, testFile), []byte{}, file.OptMkdirAll))
|
2023-08-23 04:35:42 -04:00
|
|
|
return fs
|
|
|
|
},
|
|
|
|
},
|
2023-11-20 05:17:16 -05:00
|
|
|
"only backup exists": {
|
2023-08-23 04:35:42 -04:00
|
|
|
prepareFs: func(require *require.Assertions) file.Handler {
|
|
|
|
fs := file.NewHandler(afero.NewMemMapFs())
|
2023-11-20 05:17:16 -05:00
|
|
|
require.NoError(fs.Write(filepath.Join(backupDir, testFile), []byte{}, file.OptMkdirAll))
|
2023-08-23 04:35:42 -04:00
|
|
|
return fs
|
|
|
|
},
|
|
|
|
},
|
2023-11-20 05:17:16 -05:00
|
|
|
"only existingWorkspace exists": {
|
2023-08-23 04:35:42 -04:00
|
|
|
prepareFs: func(require *require.Assertions) file.Handler {
|
|
|
|
fs := file.NewHandler(afero.NewMemMapFs())
|
2023-11-20 05:17:16 -05:00
|
|
|
require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}, file.OptMkdirAll))
|
2023-08-23 04:35:42 -04:00
|
|
|
return fs
|
|
|
|
},
|
2023-11-20 05:17:16 -05:00
|
|
|
wantRemoveWorkingDir: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
},
|
|
|
|
"read only file system": {
|
|
|
|
prepareFs: func(require *require.Assertions) file.Handler {
|
|
|
|
memFS := afero.NewMemMapFs()
|
|
|
|
fs := file.NewHandler(memFS)
|
2023-11-20 05:17:16 -05:00
|
|
|
require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}, file.OptMkdirAll))
|
|
|
|
require.NoError(fs.Write(filepath.Join(backupDir, testFile), []byte{}, file.OptMkdirAll))
|
2023-08-23 04:35:42 -04:00
|
|
|
return file.NewHandler(afero.NewReadOnlyFs(memFS))
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
fs := tc.prepareFs(require.New(t))
|
|
|
|
|
2023-09-14 05:51:20 -04:00
|
|
|
err := restoreBackup(fs, existingWorkspace, backupDir)
|
2023-08-23 04:35:42 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
2023-11-20 05:17:16 -05:00
|
|
|
_, err = fs.Stat(filepath.Join(backupDir, testFile))
|
|
|
|
assert.ErrorIs(err, os.ErrNotExist)
|
|
|
|
_, err = fs.Stat(filepath.Join(existingWorkspace, testFile))
|
|
|
|
if tc.wantRemoveWorkingDir {
|
|
|
|
assert.ErrorIs(err, os.ErrNotExist)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
2023-08-23 04:35:42 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnsureFileNotExist(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
fs file.Handler
|
|
|
|
fileName string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"file does not exist": {
|
|
|
|
fs: file.NewHandler(afero.NewMemMapFs()),
|
|
|
|
fileName: "foo",
|
|
|
|
},
|
|
|
|
"file exists": {
|
|
|
|
fs: func() file.Handler {
|
|
|
|
fs := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
err := fs.Write("foo", []byte{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
fileName: "foo",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"directory exists": {
|
|
|
|
fs: func() file.Handler {
|
|
|
|
fs := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
err := fs.MkdirAll("foo/bar")
|
|
|
|
require.NoError(t, err)
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
fileName: "foo/bar",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
err := ensureFileNotExist(tc.fs, tc.fileName)
|
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubUpgradePlanner struct {
|
|
|
|
prepareWorkspaceErr error
|
|
|
|
planDiff bool
|
|
|
|
planErr error
|
|
|
|
showPlanErr error
|
|
|
|
}
|
|
|
|
|
2023-10-26 04:55:50 -04:00
|
|
|
func (s *stubUpgradePlanner) PrepareWorkspace(_ string, _ terraform.Variables) error {
|
2023-08-23 04:35:42 -04:00
|
|
|
return s.prepareWorkspaceErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubUpgradePlanner) Plan(_ context.Context, _ terraform.LogLevel) (bool, error) {
|
|
|
|
return s.planDiff, s.planErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubUpgradePlanner) ShowPlan(_ context.Context, _ terraform.LogLevel, _ io.Writer) error {
|
|
|
|
return s.showPlanErr
|
|
|
|
}
|