Skip to content

Commit

Permalink
fix tf runtime uts (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed May 8, 2023
1 parent 1e29e8e commit 2d235ab
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
57 changes: 57 additions & 0 deletions pkg/engine/runtime/terraform/terraform_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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{
Expand All @@ -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
})
}
8 changes: 4 additions & 4 deletions pkg/engine/runtime/terraform/tfops/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestWriteHCL(t *testing.T) {
}

type want struct {
maintf string
mainTF string
}

cases := map[string]struct {
Expand All @@ -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}",
},
},
}
Expand All @@ -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)
}
})
}
Expand Down

0 comments on commit 2d235ab

Please sign in to comment.