This repository was archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathread_test.go
94 lines (85 loc) · 2.81 KB
/
read_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package test
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/GoogleCloudPlatform/terraform-validator/converters/google"
"github.com/GoogleCloudPlatform/terraform-validator/tfgcv"
)
func TestReadPlannedAssetsCoverage(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode.")
return
}
cases := []struct {
name string
}{
{name: "example_bigquery_dataset"},
{name: "example_compute_disk"},
{name: "example_compute_firewall"},
{name: "example_compute_instance"},
{name: "example_compute_forwarding_rule"},
{name: "example_container_cluster"},
{name: "example_organization_iam_binding"},
{name: "example_organization_iam_member"},
{name: "example_organization_iam_policy"},
{name: "example_pubsub_topic"},
{name: "example_project"},
{name: "example_project_in_org"},
{name: "example_project_in_folder"},
{name: "example_project_organization_policy"},
{name: "example_project_iam"},
{name: "example_project_iam_binding"},
{name: "example_project_iam_member"},
{name: "example_project_iam_policy"},
{name: "example_project_service"},
{name: "example_sql_database_instance"},
{name: "example_storage_bucket"},
{name: "full_compute_firewall"},
{name: "full_compute_instance"},
{name: "full_container_cluster"},
{name: "full_container_node_pool"},
{name: "full_sql_database_instance"},
{name: "full_spanner_instance"},
{name: "full_storage_bucket"},
}
for i := range cases {
// Allocate a variable to make sure test can run in parallel.
c := cases[i]
t.Run(c.name, func(t *testing.T) {
t.Parallel()
// Create a temporary directory for running terraform.
dir, err := ioutil.TempDir(tmpDir, "terraform")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
generateTestFiles(t, "../testdata/templates", dir, c.name+".json")
generateTestFiles(t, "../testdata/templates", dir, c.name+".tfplan.json")
// Unmarshal payload from testfile into `want` variable.
f := filepath.Join(dir, c.name+".json")
payload, err := ioutil.ReadFile(f)
if err != nil {
t.Fatalf("cannot open %s, got: %s", f, err)
}
var want []google.Asset
if err := json.Unmarshal(payload, &want); err != nil {
t.Fatalf("cannot unmarshal JSON into assets: %s", err)
}
planfile := filepath.Join(dir, c.name+".tfplan.json")
ctx := context.Background()
got, err := tfgcv.ReadPlannedAssets(ctx, planfile, data.Provider["project"], data.Ancestry, true)
if err != nil {
t.Fatalf("ReadPlannedAssets(%s, %s, %s, %t): %v", planfile, data.Provider["project"], data.Ancestry, true, err)
}
gotJSON := normalizeAssets(t, got, true)
wantJSON := normalizeAssets(t, want, true)
require.JSONEq(t, string(wantJSON), string(gotJSON))
})
}
}