mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-30 03:31:35 -04:00
cli: store upgrade files in versioned folders (#1929)
* upgrade versioning * dont pass upgrade kind as boolean * whitespace * fix godot lint check * clarify upgrade check directory suffix * cli: dry-run Terraform migrations on `upgrade check` (#1942) * dry-run Terraform migrations on upgrade check * clean whole upgrade dir * clean up check workspace after planning * fix parsing * extend upgrade check test * rename unused parameters * exclude false positives in test
This commit is contained in:
parent
f3c2198a9a
commit
b25228d175
13 changed files with 300 additions and 127 deletions
|
@ -40,18 +40,22 @@ func TestCheckTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
upgradeID string
|
||||
workspace file.Handler
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
upgradeID: "1234",
|
||||
workspace: workspace(nil),
|
||||
},
|
||||
"migration output file already exists": {
|
||||
upgradeID: "1234",
|
||||
workspace: workspace([]string{constants.TerraformMigrationOutputFile}),
|
||||
wantErr: true,
|
||||
},
|
||||
"terraform backup dir already exists": {
|
||||
workspace: workspace([]string{filepath.Join(constants.UpgradeDir, constants.TerraformUpgradeBackupDir)}),
|
||||
upgradeID: "1234",
|
||||
workspace: workspace([]string{filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir)}),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
@ -59,7 +63,7 @@ func TestCheckTerraformMigrations(t *testing.T) {
|
|||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
u := upgrader()
|
||||
err := u.CheckTerraformMigrations(tc.workspace)
|
||||
err := u.CheckTerraformMigrations(tc.workspace, tc.upgradeID)
|
||||
if tc.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
|
@ -79,20 +83,24 @@ func TestPlanTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
tf tfClient
|
||||
want bool
|
||||
wantErr bool
|
||||
upgradeID string
|
||||
tf tfClient
|
||||
want bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success no diff": {
|
||||
tf: &stubTerraformClient{},
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
},
|
||||
"success diff": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
hasDiff: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
"prepare workspace error": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
prepareWorkspaceErr: assert.AnError,
|
||||
},
|
||||
|
@ -105,11 +113,13 @@ func TestPlanTerraformMigrations(t *testing.T) {
|
|||
wantErr: true,
|
||||
},
|
||||
"show plan error no diff": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
showErr: assert.AnError,
|
||||
},
|
||||
},
|
||||
"show plan error diff": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
showErr: assert.AnError,
|
||||
hasDiff: true,
|
||||
|
@ -130,7 +140,7 @@ func TestPlanTerraformMigrations(t *testing.T) {
|
|||
Vars: &terraform.QEMUVariables{},
|
||||
}
|
||||
|
||||
diff, err := u.PlanTerraformMigrations(context.Background(), opts)
|
||||
diff, err := u.PlanTerraformMigrations(context.Background(), opts, tc.upgradeID)
|
||||
if tc.wantErr {
|
||||
require.Error(err)
|
||||
} else {
|
||||
|
@ -149,11 +159,11 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
return u
|
||||
}
|
||||
|
||||
fileHandler := func(existingFiles ...string) file.Handler {
|
||||
fileHandler := func(upgradeID string, existingFiles ...string) file.Handler {
|
||||
fh := file.NewHandler(afero.NewMemMapFs())
|
||||
require.NoError(t,
|
||||
fh.Write(
|
||||
filepath.Join(constants.UpgradeDir, constants.TerraformUpgradeWorkingDir, "someFile"),
|
||||
filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformUpgradeWorkingDir, "someFile"),
|
||||
[]byte("some content"),
|
||||
))
|
||||
for _, f := range existingFiles {
|
||||
|
@ -163,6 +173,7 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
upgradeID string
|
||||
tf tfClient
|
||||
policyPatcher stubPolicyPatcher
|
||||
fs file.Handler
|
||||
|
@ -170,38 +181,43 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler(),
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "test.json",
|
||||
},
|
||||
"create cluster error": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{
|
||||
CreateClusterErr: assert.AnError,
|
||||
},
|
||||
fs: fileHandler(),
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "test.json",
|
||||
wantErr: true,
|
||||
},
|
||||
"patch error": {
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler(),
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{
|
||||
patchErr: assert.AnError,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
"empty file name": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler(),
|
||||
fs: fileHandler("1234"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "",
|
||||
wantErr: true,
|
||||
},
|
||||
"file already exists": {
|
||||
upgradeID: "1234",
|
||||
tf: &stubTerraformClient{},
|
||||
fs: fileHandler("test.json"),
|
||||
fs: fileHandler("1234", "test.json"),
|
||||
policyPatcher: stubPolicyPatcher{},
|
||||
outputFileName: "test.json",
|
||||
wantErr: true,
|
||||
|
@ -221,7 +237,7 @@ func TestApplyTerraformMigrations(t *testing.T) {
|
|||
OutputFile: tc.outputFileName,
|
||||
}
|
||||
|
||||
err := u.ApplyTerraformMigrations(context.Background(), tc.fs, opts)
|
||||
err := u.ApplyTerraformMigrations(context.Background(), tc.fs, opts, tc.upgradeID)
|
||||
if tc.wantErr {
|
||||
require.Error(err)
|
||||
} else {
|
||||
|
@ -249,33 +265,47 @@ func TestCleanUpTerraformMigrations(t *testing.T) {
|
|||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
workspace file.Handler
|
||||
wantFiles []string
|
||||
wantErr bool
|
||||
upgradeID string
|
||||
workspaceFiles []string
|
||||
wantFiles []string
|
||||
wantErr bool
|
||||
}{
|
||||
"no files": {
|
||||
workspace: workspace(nil),
|
||||
wantFiles: []string{},
|
||||
upgradeID: "1234",
|
||||
workspaceFiles: nil,
|
||||
wantFiles: []string{},
|
||||
},
|
||||
"clean backup dir": {
|
||||
workspace: workspace([]string{
|
||||
filepath.Join(constants.UpgradeDir, constants.TerraformUpgradeBackupDir),
|
||||
}),
|
||||
upgradeID: "1234",
|
||||
workspaceFiles: []string{
|
||||
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir),
|
||||
},
|
||||
wantFiles: []string{},
|
||||
},
|
||||
"clean working dir": {
|
||||
workspace: workspace([]string{
|
||||
filepath.Join(constants.UpgradeDir, constants.TerraformUpgradeWorkingDir),
|
||||
}),
|
||||
upgradeID: "1234",
|
||||
workspaceFiles: []string{
|
||||
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeWorkingDir),
|
||||
},
|
||||
wantFiles: []string{},
|
||||
},
|
||||
"clean backup dir leave other files": {
|
||||
workspace: workspace([]string{
|
||||
filepath.Join(constants.UpgradeDir, constants.TerraformUpgradeBackupDir),
|
||||
filepath.Join(constants.UpgradeDir, "someFile"),
|
||||
}),
|
||||
"clean all": {
|
||||
upgradeID: "1234",
|
||||
workspaceFiles: []string{
|
||||
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir),
|
||||
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeWorkingDir),
|
||||
filepath.Join(constants.UpgradeDir, "1234", "abc"),
|
||||
},
|
||||
wantFiles: []string{},
|
||||
},
|
||||
"leave other files": {
|
||||
upgradeID: "1234",
|
||||
workspaceFiles: []string{
|
||||
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir),
|
||||
filepath.Join(constants.UpgradeDir, "other"),
|
||||
},
|
||||
wantFiles: []string{
|
||||
filepath.Join(constants.UpgradeDir, "someFile"),
|
||||
filepath.Join(constants.UpgradeDir, "other"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -284,8 +314,10 @@ func TestCleanUpTerraformMigrations(t *testing.T) {
|
|||
t.Run(name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
workspace := workspace(tc.workspaceFiles)
|
||||
u := upgrader()
|
||||
err := u.CleanUpTerraformMigrations(tc.workspace)
|
||||
|
||||
err := u.CleanUpTerraformMigrations(workspace, tc.upgradeID)
|
||||
if tc.wantErr {
|
||||
require.Error(err)
|
||||
return
|
||||
|
@ -293,9 +325,16 @@ func TestCleanUpTerraformMigrations(t *testing.T) {
|
|||
|
||||
require.NoError(err)
|
||||
|
||||
for _, f := range tc.wantFiles {
|
||||
_, err := tc.workspace.Stat(f)
|
||||
require.NoError(err, "file %s should exist", f)
|
||||
for _, haveFile := range tc.workspaceFiles {
|
||||
for _, wantFile := range tc.wantFiles {
|
||||
if haveFile == wantFile {
|
||||
_, err := workspace.Stat(wantFile)
|
||||
require.NoError(err, "file %s should exist", wantFile)
|
||||
} else {
|
||||
_, err := workspace.Stat(haveFile)
|
||||
require.Error(err, "file %s should not exist", haveFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -309,7 +348,7 @@ type stubTerraformClient struct {
|
|||
CreateClusterErr error
|
||||
}
|
||||
|
||||
func (u *stubTerraformClient) PrepareUpgradeWorkspace(string, string, string, terraform.Variables) error {
|
||||
func (u *stubTerraformClient) PrepareUpgradeWorkspace(string, string, string, string, terraform.Variables) error {
|
||||
return u.prepareWorkspaceErr
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue