mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-07-28 09:44:08 -04:00
cli: refactor terraform code to be update/create agnostic (#2501)
* Move upgrade specific functions out of Terraform module * Always allow overwriting Terraform files * Ensure constellation-terraform dir does not exist on create --------- Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
parent
f9989728f7
commit
ec424b260d
15 changed files with 158 additions and 322 deletions
|
@ -9,6 +9,7 @@ package cloudcmd
|
|||
import (
|
||||
"context"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
||||
|
@ -19,6 +20,19 @@ import (
|
|||
)
|
||||
|
||||
func TestPlanUpgrade(t *testing.T) {
|
||||
const (
|
||||
templateDir = "templateDir"
|
||||
existingWorkspace = "existing"
|
||||
backupDir = "backup"
|
||||
testFile = "testfile"
|
||||
)
|
||||
fsWithWorkspace := func(require *require.Assertions) file.Handler {
|
||||
fs := file.NewHandler(afero.NewMemMapFs())
|
||||
require.NoError(fs.MkdirAll(existingWorkspace))
|
||||
require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}))
|
||||
return fs
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
prepareFs func(require *require.Assertions) file.Handler
|
||||
tf *stubUpgradePlanner
|
||||
|
@ -26,51 +40,48 @@ func TestPlanUpgrade(t *testing.T) {
|
|||
wantErr bool
|
||||
}{
|
||||
"success no diff": {
|
||||
prepareFs: func(require *require.Assertions) file.Handler {
|
||||
return file.NewHandler(afero.NewMemMapFs())
|
||||
},
|
||||
tf: &stubUpgradePlanner{},
|
||||
prepareFs: fsWithWorkspace,
|
||||
tf: &stubUpgradePlanner{},
|
||||
},
|
||||
"success diff": {
|
||||
prepareFs: func(require *require.Assertions) file.Handler {
|
||||
return file.NewHandler(afero.NewMemMapFs())
|
||||
},
|
||||
prepareFs: fsWithWorkspace,
|
||||
tf: &stubUpgradePlanner{
|
||||
planDiff: true,
|
||||
},
|
||||
wantDiff: true,
|
||||
},
|
||||
"workspace does not exist": {
|
||||
prepareFs: func(require *require.Assertions) file.Handler {
|
||||
return file.NewHandler(afero.NewMemMapFs())
|
||||
},
|
||||
tf: &stubUpgradePlanner{},
|
||||
wantErr: true,
|
||||
},
|
||||
"workspace not clean": {
|
||||
prepareFs: func(require *require.Assertions) file.Handler {
|
||||
fs := file.NewHandler(afero.NewMemMapFs())
|
||||
require.NoError(fs.MkdirAll("backup"))
|
||||
fs := fsWithWorkspace(require)
|
||||
require.NoError(fs.MkdirAll(backupDir))
|
||||
return fs
|
||||
},
|
||||
tf: &stubUpgradePlanner{},
|
||||
wantErr: true,
|
||||
},
|
||||
"prepare workspace error": {
|
||||
prepareFs: func(require *require.Assertions) file.Handler {
|
||||
return file.NewHandler(afero.NewMemMapFs())
|
||||
},
|
||||
prepareFs: fsWithWorkspace,
|
||||
tf: &stubUpgradePlanner{
|
||||
prepareWorkspaceErr: assert.AnError,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
"plan error": {
|
||||
prepareFs: func(require *require.Assertions) file.Handler {
|
||||
return file.NewHandler(afero.NewMemMapFs())
|
||||
},
|
||||
prepareFs: fsWithWorkspace,
|
||||
tf: &stubUpgradePlanner{
|
||||
planErr: assert.AnError,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
"show plan error": {
|
||||
prepareFs: func(require *require.Assertions) file.Handler {
|
||||
return file.NewHandler(afero.NewMemMapFs())
|
||||
},
|
||||
prepareFs: fsWithWorkspace,
|
||||
tf: &stubUpgradePlanner{
|
||||
planDiff: true,
|
||||
showPlanErr: assert.AnError,
|
||||
|
@ -87,7 +98,7 @@ func TestPlanUpgrade(t *testing.T) {
|
|||
hasDiff, err := planUpgrade(
|
||||
context.Background(), tc.tf, fs, io.Discard, terraform.LogLevelDebug,
|
||||
&terraform.QEMUVariables{},
|
||||
"test", "backup",
|
||||
templateDir, existingWorkspace, backupDir,
|
||||
)
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
|
@ -95,6 +106,8 @@ func TestPlanUpgrade(t *testing.T) {
|
|||
}
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.wantDiff, hasDiff)
|
||||
_, err = fs.Stat(filepath.Join(backupDir, testFile))
|
||||
assert.NoError(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -208,7 +221,7 @@ type stubUpgradePlanner struct {
|
|||
showPlanErr error
|
||||
}
|
||||
|
||||
func (s *stubUpgradePlanner) PrepareUpgradeWorkspace(_, _ string, _ terraform.Variables) error {
|
||||
func (s *stubUpgradePlanner) PrepareWorkspace(_ string, _ terraform.Variables) error {
|
||||
return s.prepareWorkspaceErr
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue