2023-05-22 07:31:20 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package upgrade
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckTerraformMigrations(t *testing.T) {
|
2023-07-18 03:33:42 -04:00
|
|
|
upgrader := func(fileHandler file.Handler) *TerraformUpgrader {
|
|
|
|
u, err := NewTerraformUpgrader(&stubTerraformClient{}, bytes.NewBuffer(nil), fileHandler)
|
2023-05-22 07:31:20 -04:00
|
|
|
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 {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID string
|
2023-05-22 07:31:20 -04:00
|
|
|
workspace file.Handler
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
2023-05-22 07:31:20 -04:00
|
|
|
workspace: workspace(nil),
|
|
|
|
},
|
|
|
|
"terraform backup dir already exists": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
|
|
|
workspace: workspace([]string{filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir)}),
|
2023-05-22 07:31:20 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-07-18 03:33:42 -04:00
|
|
|
u := upgrader(tc.workspace)
|
2023-07-24 04:30:53 -04:00
|
|
|
err := u.CheckTerraformMigrations(tc.upgradeID, constants.TerraformUpgradeBackupDir)
|
2023-05-22 07:31:20 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlanTerraformMigrations(t *testing.T) {
|
2023-07-24 04:30:53 -04:00
|
|
|
upgrader := func(tf tfResourceClient, fileHandler file.Handler) *TerraformUpgrader {
|
2023-07-18 03:33:42 -04:00
|
|
|
u, err := NewTerraformUpgrader(tf, bytes.NewBuffer(nil), fileHandler)
|
2023-05-22 07:31:20 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
2023-07-18 03:33:42 -04:00
|
|
|
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)
|
|
|
|
}
|
2023-05-22 07:31:20 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID string
|
2023-07-24 04:30:53 -04:00
|
|
|
tf tfResourceClient
|
2023-07-18 03:33:42 -04:00
|
|
|
workspace file.Handler
|
2023-06-21 03:22:32 -04:00
|
|
|
want bool
|
|
|
|
wantErr bool
|
2023-05-22 07:31:20 -04:00
|
|
|
}{
|
|
|
|
"success no diff": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
|
|
|
tf: &stubTerraformClient{},
|
2023-07-18 03:33:42 -04:00
|
|
|
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"success diff": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
2023-05-22 07:31:20 -04:00
|
|
|
tf: &stubTerraformClient{
|
|
|
|
hasDiff: true,
|
|
|
|
},
|
2023-07-18 03:33:42 -04:00
|
|
|
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
|
|
|
want: true,
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"prepare workspace error": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
2023-05-22 07:31:20 -04:00
|
|
|
tf: &stubTerraformClient{
|
|
|
|
prepareWorkspaceErr: assert.AnError,
|
|
|
|
},
|
2023-07-18 03:33:42 -04:00
|
|
|
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,
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"plan error": {
|
|
|
|
tf: &stubTerraformClient{
|
|
|
|
planErr: assert.AnError,
|
|
|
|
},
|
2023-07-18 03:33:42 -04:00
|
|
|
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
|
|
|
wantErr: true,
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"show plan error no diff": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
2023-05-22 07:31:20 -04:00
|
|
|
tf: &stubTerraformClient{
|
|
|
|
showErr: assert.AnError,
|
|
|
|
},
|
2023-07-18 03:33:42 -04:00
|
|
|
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"show plan error diff": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
2023-05-22 07:31:20 -04:00
|
|
|
tf: &stubTerraformClient{
|
|
|
|
showErr: assert.AnError,
|
|
|
|
hasDiff: true,
|
|
|
|
},
|
2023-07-18 03:33:42 -04:00
|
|
|
workspace: workspace([]string{constants.ClusterIDsFileName}),
|
|
|
|
wantErr: true,
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-07-18 03:33:42 -04:00
|
|
|
u := upgrader(tc.tf, tc.workspace)
|
2023-05-22 07:31:20 -04:00
|
|
|
|
|
|
|
opts := TerraformUpgradeOptions{
|
|
|
|
LogLevel: terraform.LogLevelDebug,
|
|
|
|
CSP: cloudprovider.Unknown,
|
|
|
|
Vars: &terraform.QEMUVariables{},
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
diff, err := u.PlanTerraformMigrations(context.Background(), opts, tc.upgradeID)
|
2023-05-22 07:31:20 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(err)
|
|
|
|
} else {
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(tc.want, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyTerraformMigrations(t *testing.T) {
|
2023-07-24 04:30:53 -04:00
|
|
|
upgrader := func(tf tfResourceClient, fileHandler file.Handler) *TerraformUpgrader {
|
2023-07-18 03:33:42 -04:00
|
|
|
u, err := NewTerraformUpgrader(tf, bytes.NewBuffer(nil), fileHandler)
|
2023-05-22 07:31:20 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
fileHandler := func(upgradeID string, existingFiles ...string) file.Handler {
|
2023-05-22 07:31:20 -04:00
|
|
|
fh := file.NewHandler(afero.NewMemMapFs())
|
2023-07-18 03:33:42 -04:00
|
|
|
|
2023-05-22 07:31:20 -04:00
|
|
|
require.NoError(t,
|
|
|
|
fh.Write(
|
2023-06-21 03:22:32 -04:00
|
|
|
filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformUpgradeWorkingDir, "someFile"),
|
2023-05-22 07:31:20 -04:00
|
|
|
[]byte("some content"),
|
|
|
|
))
|
|
|
|
for _, f := range existingFiles {
|
|
|
|
require.NoError(t, fh.Write(f, []byte("some content")))
|
|
|
|
}
|
|
|
|
return fh
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2023-07-18 03:33:42 -04:00
|
|
|
upgradeID string
|
2023-07-24 04:30:53 -04:00
|
|
|
tf tfResourceClient
|
2023-07-18 03:33:42 -04:00
|
|
|
policyPatcher stubPolicyPatcher
|
|
|
|
fs file.Handler
|
|
|
|
skipIDFileCreation bool // if true, do not create the constellation-id.json file
|
|
|
|
wantErr bool
|
2023-05-22 07:31:20 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2023-07-18 03:33:42 -04:00
|
|
|
upgradeID: "1234",
|
|
|
|
tf: &stubTerraformClient{},
|
|
|
|
fs: fileHandler("1234"),
|
|
|
|
policyPatcher: stubPolicyPatcher{},
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"create cluster error": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
2023-05-22 07:31:20 -04:00
|
|
|
tf: &stubTerraformClient{
|
|
|
|
CreateClusterErr: assert.AnError,
|
|
|
|
},
|
2023-07-18 03:33:42 -04:00
|
|
|
fs: fileHandler("1234"),
|
|
|
|
policyPatcher: stubPolicyPatcher{},
|
|
|
|
wantErr: true,
|
2023-06-02 04:47:44 -04:00
|
|
|
},
|
2023-07-18 03:33:42 -04:00
|
|
|
"constellation-id.json does not exist": {
|
|
|
|
upgradeID: "1234",
|
|
|
|
tf: &stubTerraformClient{},
|
|
|
|
fs: fileHandler("1234"),
|
|
|
|
policyPatcher: stubPolicyPatcher{},
|
|
|
|
skipIDFileCreation: true,
|
|
|
|
wantErr: true,
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-07-18 03:33:42 -04:00
|
|
|
u := upgrader(tc.tf, tc.fs)
|
|
|
|
|
|
|
|
if !tc.skipIDFileCreation {
|
|
|
|
require.NoError(
|
|
|
|
tc.fs.Write(
|
|
|
|
filepath.Join(constants.ClusterIDsFileName),
|
|
|
|
[]byte("{}"),
|
|
|
|
))
|
|
|
|
}
|
2023-05-22 07:31:20 -04:00
|
|
|
|
|
|
|
opts := TerraformUpgradeOptions{
|
2023-07-18 03:33:42 -04:00
|
|
|
LogLevel: terraform.LogLevelDebug,
|
|
|
|
CSP: cloudprovider.Unknown,
|
|
|
|
Vars: &terraform.QEMUVariables{},
|
2023-05-22 07:31:20 -04:00
|
|
|
}
|
|
|
|
|
2023-07-18 03:33:42 -04:00
|
|
|
err := u.ApplyTerraformMigrations(context.Background(), opts, tc.upgradeID)
|
2023-05-22 07:31:20 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(err)
|
|
|
|
} else {
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanUpTerraformMigrations(t *testing.T) {
|
2023-07-18 03:33:42 -04:00
|
|
|
upgrader := func(fileHandler file.Handler) *TerraformUpgrader {
|
|
|
|
u, err := NewTerraformUpgrader(&stubTerraformClient{}, bytes.NewBuffer(nil), fileHandler)
|
2023-05-22 07:31:20 -04:00
|
|
|
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 {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID string
|
|
|
|
workspaceFiles []string
|
|
|
|
wantFiles []string
|
|
|
|
wantErr bool
|
2023-05-22 07:31:20 -04:00
|
|
|
}{
|
|
|
|
"no files": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
|
|
|
workspaceFiles: nil,
|
|
|
|
wantFiles: []string{},
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"clean backup dir": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
|
|
|
workspaceFiles: []string{
|
|
|
|
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir),
|
|
|
|
},
|
2023-05-22 07:31:20 -04:00
|
|
|
wantFiles: []string{},
|
|
|
|
},
|
|
|
|
"clean working dir": {
|
2023-06-21 03:22:32 -04:00
|
|
|
upgradeID: "1234",
|
|
|
|
workspaceFiles: []string{
|
|
|
|
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeWorkingDir),
|
|
|
|
},
|
|
|
|
wantFiles: []string{},
|
|
|
|
},
|
|
|
|
"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"),
|
|
|
|
},
|
2023-05-22 07:31:20 -04:00
|
|
|
wantFiles: []string{},
|
|
|
|
},
|
2023-06-21 03:22:32 -04:00
|
|
|
"leave other files": {
|
|
|
|
upgradeID: "1234",
|
|
|
|
workspaceFiles: []string{
|
|
|
|
filepath.Join(constants.UpgradeDir, "1234", constants.TerraformUpgradeBackupDir),
|
|
|
|
filepath.Join(constants.UpgradeDir, "other"),
|
|
|
|
},
|
2023-05-22 07:31:20 -04:00
|
|
|
wantFiles: []string{
|
2023-06-21 03:22:32 -04:00
|
|
|
filepath.Join(constants.UpgradeDir, "other"),
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
workspace := workspace(tc.workspaceFiles)
|
2023-07-18 03:33:42 -04:00
|
|
|
u := upgrader(workspace)
|
2023-06-21 03:22:32 -04:00
|
|
|
|
2023-07-18 03:33:42 -04:00
|
|
|
err := u.CleanUpTerraformMigrations(tc.upgradeID)
|
2023-05-22 07:31:20 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-05-22 07:31:20 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubTerraformClient struct {
|
|
|
|
hasDiff bool
|
|
|
|
prepareWorkspaceErr error
|
|
|
|
showErr error
|
|
|
|
planErr error
|
|
|
|
CreateClusterErr error
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
func (u *stubTerraformClient) PrepareUpgradeWorkspace(string, string, string, string, terraform.Variables) error {
|
2023-05-22 07:31:20 -04:00
|
|
|
return u.prepareWorkspaceErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *stubTerraformClient) ShowPlan(context.Context, terraform.LogLevel, string, io.Writer) error {
|
|
|
|
return u.showErr
|
|
|
|
}
|
|
|
|
|
2023-06-27 05:27:50 -04:00
|
|
|
func (u *stubTerraformClient) Plan(context.Context, terraform.LogLevel, string) (bool, error) {
|
2023-05-22 07:31:20 -04:00
|
|
|
return u.hasDiff, u.planErr
|
|
|
|
}
|
|
|
|
|
2023-07-31 04:53:05 -04:00
|
|
|
func (u *stubTerraformClient) CreateCluster(context.Context, cloudprovider.Provider, terraform.LogLevel) (terraform.ApplyOutput, error) {
|
2023-07-21 10:43:51 -04:00
|
|
|
return terraform.ApplyOutput{}, u.CreateClusterErr
|
2023-05-22 07:31:20 -04:00
|
|
|
}
|
2023-06-02 04:47:44 -04:00
|
|
|
|
|
|
|
type stubPolicyPatcher struct {
|
|
|
|
patchErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *stubPolicyPatcher) PatchPolicy(context.Context, string) error {
|
|
|
|
return p.patchErr
|
|
|
|
}
|