diff --git a/pkg/engine/runtime/terraform/terraform_runtime_test.go b/pkg/engine/runtime/terraform/terraform_runtime_test.go index 81e7370b..d31f0e16 100644 --- a/pkg/engine/runtime/terraform/terraform_runtime_test.go +++ b/pkg/engine/runtime/terraform/terraform_runtime_test.go @@ -2,11 +2,14 @@ package terraform import ( "context" + "encoding/json" + "fmt" "os" "path/filepath" "sync" "testing" + "bou.ke/monkey" "github.com/spf13/afero" "github.com/stretchr/testify/assert" @@ -29,6 +32,12 @@ var testResource = models.Resource{ }, } +var fakeSR = &tfops.StateRepresentation{ + FormatVersion: "0.2", + TerraformVersion: "1.0.6", + Values: nil, +} + func TestTerraformRuntime(t *testing.T) { cwd, _ := os.Getwd() stack := &projectstack.Stack{ @@ -42,21 +51,69 @@ func TestTerraformRuntime(t *testing.T) { } t.Run("ApplyDryRun", func(t *testing.T) { + defer monkey.UnpatchAll() + + mockApplySetup() + + data, err := os.ReadFile(filepath.Join("tfops", "test_data", "plan.out.json")) + if err != nil { + panic(err) + } + monkey.Patch((*tfops.WorkSpace).Plan, func(ws *tfops.WorkSpace, ctx context.Context) (*tfops.PlanRepresentation, error) { + s := &tfops.PlanRepresentation{} + if err = json.Unmarshal(data, s); err != nil { + return nil, fmt.Errorf("json umarshal plan representation failed: %v", err) + } + return s, nil + }) + response := tfRuntime.Apply(context.TODO(), &runtime.ApplyRequest{PlanResource: &testResource, DryRun: true, Stack: stack}) assert.Equalf(t, nil, response.Status, "Execute(%v)", "Apply") }) t.Run("Apply", func(t *testing.T) { + defer monkey.UnpatchAll() + + mockApplySetup() + response := tfRuntime.Apply(context.TODO(), &runtime.ApplyRequest{PlanResource: &testResource, DryRun: false, Stack: stack}) assert.Equalf(t, nil, response.Status, "Execute(%v)", "Apply") }) t.Run("Read", func(t *testing.T) { + defer monkey.UnpatchAll() + + monkey.Patch((*tfops.WorkSpace).InitWorkSpace, func(ws *tfops.WorkSpace, ctx context.Context) error { + return nil + }) + response := tfRuntime.Read(context.TODO(), &runtime.ReadRequest{PlanResource: &testResource, Stack: stack}) assert.Equalf(t, nil, response.Status, "Execute(%v)", "Read") }) t.Run("Delete", func(t *testing.T) { + defer monkey.UnpatchAll() + + monkey.Patch((*tfops.WorkSpace).InitWorkSpace, func(ws *tfops.WorkSpace, ctx context.Context) error { + return nil + }) + // mock destroy + monkey.Patch((*tfops.WorkSpace).Destroy, func(ws *tfops.WorkSpace, ctx context.Context) error { + return nil + }) + response := tfRuntime.Delete(context.TODO(), &runtime.DeleteRequest{Resource: &testResource, Stack: stack}) assert.Equalf(t, nil, response.Status, "Execute(%v)", "Delete") }) } + +func mockApplySetup() { + monkey.Patch((*tfops.WorkSpace).InitWorkSpace, func(ws *tfops.WorkSpace, ctx context.Context) error { + return nil + }) + monkey.Patch((*tfops.WorkSpace).Apply, func(ws *tfops.WorkSpace, ctx context.Context) (*tfops.StateRepresentation, error) { + return fakeSR, nil + }) + monkey.Patch((*tfops.WorkSpace).GetProvider, func(ws *tfops.WorkSpace) (string, error) { + return "registry.terraform.io/hashicorp/local/2.2.3", nil + }) +} diff --git a/pkg/engine/runtime/terraform/tfops/workspace_test.go b/pkg/engine/runtime/terraform/tfops/workspace_test.go index 3965acd2..f4f916ac 100644 --- a/pkg/engine/runtime/terraform/tfops/workspace_test.go +++ b/pkg/engine/runtime/terraform/tfops/workspace_test.go @@ -48,7 +48,7 @@ func TestWriteHCL(t *testing.T) { } type want struct { - maintf string + mainTF string } cases := map[string]struct { @@ -60,7 +60,7 @@ func TestWriteHCL(t *testing.T) { w: NewWorkSpace(fs), }, want: want{ - maintf: "{\n \"provider\": {\n \"local\": null\n },\n \"resource\": {\n \"local_file\": {\n \"kusion_example\": {\n \"content\": \"kusion\",\n \"filename\": \"test.txt\"\n }\n }\n },\n \"terraform\": {\n \"required_providers\": {\n \"local\": {\n \"source\": \"registry.terraform.io/hashicorp/local\",\n \"version\": \"2.2.3\"\n }\n }\n }\n}", + mainTF: "{\n \"provider\": {\n \"local\": null\n },\n \"resource\": {\n \"local_file\": {\n \"kusion_example\": {\n \"content\": \"kusion\",\n \"filename\": \"test.txt\"\n }\n }\n },\n \"terraform\": {\n \"required_providers\": {\n \"local\": {\n \"source\": \"registry.terraform.io/hashicorp/local\",\n \"version\": \"2.2.3\"\n }\n }\n }\n}", }, }, } @@ -74,8 +74,8 @@ func TestWriteHCL(t *testing.T) { } s, _ := fs.ReadFile(filepath.Join(tt.w.tfCacheDir, "main.tf.json")) - if diff := cmp.Diff(string(s), tt.want.maintf); diff != "" { - t.Errorf("\n%s\nWriteHCL(...): -want maintf, +got maintf:\n%s", name, diff) + if diff := cmp.Diff(string(s), tt.want.mainTF); diff != "" { + t.Errorf("\n%s\nWriteHCL(...): -want mainTF, +got mainTF:\n%s", name, diff) } }) }