|
| 1 | +package preview_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io/fs" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "slices" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/coder/preview" |
| 16 | + "github.com/coder/preview/internal/verify" |
| 17 | + "github.com/coder/preview/types" |
| 18 | +) |
| 19 | + |
| 20 | +// Test_VerifyE2E will fully evaluate with `terraform apply` |
| 21 | +// and verify the output of `preview` against the tfstate. This |
| 22 | +// is the e2e test for the preview package. |
| 23 | +// |
| 24 | +// 1. Terraform versions listed from 'verify.TerraformTestVersions' are |
| 25 | +// installed into a temp directory. If multiple versions exist, a e2e test |
| 26 | +// for each tf version is run. |
| 27 | +// 2. For each test directory in `testdata`, the following steps are performed: |
| 28 | +// a. If the directory contains a file named `skipe2e`, skip the test. |
| 29 | +// Some tests are not meant to be run in e2e mode as they require external |
| 30 | +// credentials, or are invalid terraform configurations. |
| 31 | +// b. For each terraform version, the following steps are performed: |
| 32 | +// ---- Core Test ---- |
| 33 | +// i. Create a working directory for the terraform version in a temp dir. |
| 34 | +// ii. Copy the test data into the working directory. |
| 35 | +// iii. Run `terraform init`. |
| 36 | +// iv. Run `terraform plan` and save the output to a file. |
| 37 | +// v. Run `terraform apply`. |
| 38 | +// vi. Run `terraform show` to get the state. |
| 39 | +// vii. Run `preview --plan=out.plan` to get the preview state. |
| 40 | +// viii. Compare the preview state with the terraform state. |
| 41 | +// ix. If the preview state is not equal to the terraform state, fail the test. |
| 42 | +// ---- ---- |
| 43 | +// |
| 44 | +// The goal of the test is to compare `tfstate` with the output of `preview`. |
| 45 | +// If `preview`'s implementation of terraform is incorrect, the test will fail. |
| 46 | +// TODO: Adding varied parameter inputs would be a good idea. |
| 47 | +func Test_VerifyE2E(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + |
| 50 | + installCtx, cancel := context.WithCancel(context.Background()) |
| 51 | + |
| 52 | + versions := verify.TerraformTestVersions(installCtx) |
| 53 | + tfexecs := verify.InstallTerraforms(installCtx, t, versions...) |
| 54 | + cancel() |
| 55 | + |
| 56 | + dirFs := os.DirFS("testdata") |
| 57 | + entries, err := fs.ReadDir(dirFs, ".") |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + for _, entry := range entries { |
| 61 | + entry := entry |
| 62 | + if !entry.IsDir() { |
| 63 | + t.Logf("skipping non directory file %q", entry.Name()) |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + entryFiles, err := fs.ReadDir(dirFs, filepath.Join(entry.Name())) |
| 68 | + require.NoError(t, err, "reading test data dir") |
| 69 | + if !slices.ContainsFunc(entryFiles, func(entry fs.DirEntry) bool { |
| 70 | + return filepath.Ext(entry.Name()) == ".tf" |
| 71 | + }) { |
| 72 | + t.Logf("skipping test data dir %q, no .tf files", entry.Name()) |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + if slices.ContainsFunc(entryFiles, func(entry fs.DirEntry) bool { |
| 77 | + return entry.Name() == "skipe2e" |
| 78 | + }) { |
| 79 | + t.Logf("skipping test data dir %q, skip file found", entry.Name()) |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + name := entry.Name() |
| 84 | + t.Run(name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + entryWrkPath := t.TempDir() |
| 88 | + |
| 89 | + for _, tfexec := range tfexecs { |
| 90 | + tfexec := tfexec |
| 91 | + |
| 92 | + t.Run(tfexec.Version, func(t *testing.T) { |
| 93 | + wp := filepath.Join(entryWrkPath, tfexec.Version) |
| 94 | + err := os.MkdirAll(wp, 0755) |
| 95 | + require.NoError(t, err, "creating working dir") |
| 96 | + |
| 97 | + t.Logf("working dir %q", wp) |
| 98 | + |
| 99 | + subFS, err := fs.Sub(dirFs, entry.Name()) |
| 100 | + require.NoError(t, err, "creating sub fs") |
| 101 | + |
| 102 | + err = verify.CopyTFFS(wp, subFS) |
| 103 | + require.NoError(t, err, "copying test data to working dir") |
| 104 | + |
| 105 | + exe, err := tfexec.WorkingDir(wp) |
| 106 | + require.NoError(t, err, "creating working executable") |
| 107 | + |
| 108 | + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2) |
| 109 | + defer cancel() |
| 110 | + err = exe.Init(ctx) |
| 111 | + require.NoError(t, err, "terraform init") |
| 112 | + |
| 113 | + planOutFile := "tfplan" |
| 114 | + planOutPath := filepath.Join(wp, planOutFile) |
| 115 | + _, err = exe.Plan(ctx, planOutPath) |
| 116 | + require.NoError(t, err, "terraform plan") |
| 117 | + |
| 118 | + plan, err := exe.ShowPlan(ctx, planOutPath) |
| 119 | + require.NoError(t, err, "terraform show plan") |
| 120 | + |
| 121 | + pd, err := json.Marshal(plan) |
| 122 | + require.NoError(t, err, "marshalling plan") |
| 123 | + |
| 124 | + err = os.WriteFile(filepath.Join(wp, "plan.json"), pd, 0644) |
| 125 | + require.NoError(t, err, "writing plan.json") |
| 126 | + |
| 127 | + _, err = exe.Apply(ctx) |
| 128 | + require.NoError(t, err, "terraform apply") |
| 129 | + |
| 130 | + state, err := exe.Show(ctx) |
| 131 | + require.NoError(t, err, "terraform show") |
| 132 | + |
| 133 | + output, diags := preview.Preview(context.Background(), |
| 134 | + preview.Input{ |
| 135 | + PlanJSONPath: "plan.json", |
| 136 | + ParameterValues: map[string]types.ParameterValue{}, |
| 137 | + }, |
| 138 | + os.DirFS(wp)) |
| 139 | + if diags.HasErrors() { |
| 140 | + t.Logf("diags: %s", diags) |
| 141 | + } |
| 142 | + require.False(t, diags.HasErrors(), "preview errors") |
| 143 | + |
| 144 | + if state.Values == nil { |
| 145 | + t.Fatalf("state values are nil") |
| 146 | + } |
| 147 | + verify.Compare(t, output, state.Values.RootModule) |
| 148 | + }) |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments