mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-18 14:10:39 -04:00
cli: write Terraform migration output directly to constellation-id.json
(#2107)
* backup `constellation-id.json` before upgrade * remove superfluous `file.Handler` arguments * merge `constellation-id.json` on upgrade * fix typo
This commit is contained in:
parent
5cbdb3a519
commit
5f71934f56
11 changed files with 255 additions and 120 deletions
|
@ -23,8 +23,8 @@ import (
|
|||
)
|
||||
|
||||
func TestCheckTerraformMigrations(t *testing.T) {
|
||||
upgrader := func() *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(&stubTerraformClient{}, bytes.NewBuffer(nil))
|
||||
upgrader := func(fileHandler file.Handler) *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(&stubTerraformClient{}, bytes.NewBuffer(nil), fileHandler)
|
||||
require.NoError(t, err)
|
||||
|
||||
return u
|
||||
|
@ -48,11 +48,6 @@ func TestCheckTerraformMigrations(t *testing.T) {
|
|||
upgradeID: "1234",
|
||||
workspace: workspace(nil),
|
||||
},
|
||||
"migration output file already exists": {
|
||||
upgradeID: "1234",
|
||||
workspace: workspace([]string{constants.TerraformMigrationOutputFile}),
|
||||
wantErr: true,
|
||||
},
|
||||
"terraform backup dir already exists": {
|
||||
upgradeID: "1234",
|
||||
workspace: workspace([]string{filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir)}),
|
||||
|
@ -62,8 +57,8 @@ func TestCheckTerraformMigrations(t *testing.T) {
|
|||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
u := upgrader()
|
||||
err := u.CheckTerraformMigrations(tc.workspace, tc.upgradeID)
|
||||
u := upgrader(tc.workspace)
|
||||
err := u.CheckTerraformMigrations(tc.upgradeID)
|
||||
if tc.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
|
@ -75,48 +70,74 @@ func TestCheckTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPlanTerraformMigrations(t *testing.T) {
|
||||
upgrader := func(tf tfClient) *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(tf, bytes.NewBuffer(nil))
|
||||
upgrader := func(tf tfClient, fileHandler file.Handler) *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(tf, bytes.NewBuffer(nil), fileHandler)
|
||||
require.NoError(t, err)
|
||||
|
||||
return u
|
||||
}
|
||||
workspace := func(existingFiles []string) file.Handler {
|
||||
fs := afero.NewMemMapFs()
|
||||
for _, f := range existingFiles {
|
||||
require.NoError(t, afero.WriteFile(fs, f, []byte{}, 0o644))
|
||||
}
|
||||
|
||||
return file.NewHandler(fs)
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
upgradeID string
|
||||
tf tfClient
|
||||
workspace file.Handler
|
||||
want bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success no diff": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
||||
},
|
||||
"success diff": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
hasDiff: true,
|
||||
},
|
||||
want: true,
|
||||
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
||||
want: true,
|
||||
},
|
||||
"prepare workspace error": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
prepareWorkspaceErr: assert.AnError,
|
||||
},
|
||||
wantErr: true,
|
||||
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
||||
wantErr: true,
|
||||
},
|
||||
"constellation-id.json does not exist": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
workspace: workspace(nil),
|
||||
wantErr: true,
|
||||
},
|
||||
"constellation-id backup already exists": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
workspace: workspace([]string{filepath.Join(constants.UpgradeDir, "1234", constants.ClusterIDsFileName+".old")}),
|
||||
wantErr: true,
|
||||
},
|
||||
"plan error": {
|
||||
tf: &stubTerraformClient{
|
||||
planErr: assert.AnError,
|
||||
},
|
||||
wantErr: true,
|
||||
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
||||
wantErr: true,
|
||||
},
|
||||
"show plan error no diff": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
showErr: assert.AnError,
|
||||
},
|
||||
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
||||
},
|
||||
"show plan error diff": {
|
||||
upgradeID: "1234",
|
||||
|
@ -124,7 +145,8 @@ func TestPlanTerraformMigrations(t *testing.T) {
|
|||
showErr: assert.AnError,
|
||||
hasDiff: true,
|
||||
},
|
||||
wantErr: true,
|
||||
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -132,7 +154,7 @@ func TestPlanTerraformMigrations(t *testing.T) {
|
|||
t.Run(name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
u := upgrader(tc.tf)
|
||||
u := upgrader(tc.tf, tc.workspace)
|
||||
|
||||
opts := TerraformUpgradeOptions{
|
||||
LogLevel: terraform.LogLevelDebug,
|
||||
|
@ -152,8 +174,8 @@ func TestPlanTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestApplyTerraformMigrations(t *testing.T) {
|
||||
upgrader := func(tf tfClient) *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(tf, bytes.NewBuffer(nil))
|
||||
upgrader := func(tf tfClient, fileHandler file.Handler) *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(tf, bytes.NewBuffer(nil), fileHandler)
|
||||
require.NoError(t, err)
|
||||
|
||||
return u
|
||||
|
@ -161,6 +183,7 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
|
||||
fileHandler := func(upgradeID string, existingFiles ...string) file.Handler {
|
||||
fh := file.NewHandler(afero.NewMemMapFs())
|
||||
|
||||
require.NoError(t,
|
||||
fh.Write(
|
||||
filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformUpgradeWorkingDir, "someFile"),
|
||||
|
@ -173,54 +196,35 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
upgradeID string
|
||||
tf tfClient
|
||||
policyPatcher stubPolicyPatcher
|
||||
fs file.Handler
|
||||
outputFileName string
|
||||
wantErr bool
|
||||
upgradeID string
|
||||
tf tfClient
|
||||
policyPatcher stubPolicyPatcher
|
||||
fs file.Handler
|
||||
skipIDFileCreation bool // if true, do not create the constellation-id.json file
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "test.json",
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
},
|
||||
"create cluster error": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
CreateClusterErr: assert.AnError,
|
||||
},
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "test.json",
|
||||
wantErr: true,
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
wantErr: true,
|
||||
},
|
||||
"patch error": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{
|
||||
patchErr: assert.AnError,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
"empty file name": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "",
|
||||
wantErr: true,
|
||||
},
|
||||
"file already exists": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("1234", "test.json"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "test.json",
|
||||
wantErr: true,
|
||||
"constellation-id.json does not exist": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
skipIDFileCreation: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -228,16 +232,23 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
t.Run(name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
u := upgrader(tc.tf)
|
||||
u := upgrader(tc.tf, tc.fs)
|
||||
|
||||
opts := TerraformUpgradeOptions{
|
||||
LogLevel: terraform.LogLevelDebug,
|
||||
CSP: cloudprovider.Unknown,
|
||||
Vars: &terraform.QEMUVariables{},
|
||||
OutputFile: tc.outputFileName,
|
||||
if !tc.skipIDFileCreation {
|
||||
require.NoError(
|
||||
tc.fs.Write(
|
||||
filepath.Join(constants.ClusterIDsFileName),
|
||||
[]byte("{}"),
|
||||
))
|
||||
}
|
||||
|
||||
err := u.ApplyTerraformMigrations(context.Background(), tc.fs, opts, tc.upgradeID)
|
||||
opts := TerraformUpgradeOptions{
|
||||
LogLevel: terraform.LogLevelDebug,
|
||||
CSP: cloudprovider.Unknown,
|
||||
Vars: &terraform.QEMUVariables{},
|
||||
}
|
||||
|
||||
err := u.ApplyTerraformMigrations(context.Background(), opts, tc.upgradeID)
|
||||
if tc.wantErr {
|
||||
require.Error(err)
|
||||
} else {
|
||||
|
@ -248,8 +259,8 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestCleanUpTerraformMigrations(t *testing.T) {
|
||||
upgrader := func() *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(&stubTerraformClient{}, bytes.NewBuffer(nil))
|
||||
upgrader := func(fileHandler file.Handler) *TerraformUpgrader {
|
||||
u, err := NewTerraformUpgrader(&stubTerraformClient{}, bytes.NewBuffer(nil), fileHandler)
|
||||
require.NoError(t, err)
|
||||
|
||||
return u
|
||||
|
@ -315,9 +326,9 @@ func TestCleanUpTerraformMigrations(t *testing.T) {
|
|||
require := require.New(t)
|
||||
|
||||
workspace := workspace(tc.workspaceFiles)
|
||||
u := upgrader()
|
||||
u := upgrader(workspace)
|
||||
|
||||
err := u.CleanUpTerraformMigrations(workspace, tc.upgradeID)
|
||||
err := u.CleanUpTerraformMigrations(tc.upgradeID)
|
||||
if tc.wantErr {
|
||||
require.Error(err)
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue