Skip to content

Commit 9dcdd07

Browse files
authored
feat: expanded integration testing (#288)
1 parent 70d2863 commit 9dcdd07

File tree

4 files changed

+187
-88
lines changed

4 files changed

+187
-88
lines changed

examples/data_warehouse/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module "data_warehouse" {
1818
source = "../../modules/data_warehouse"
1919

2020
project_id = var.project_id
21-
region = "us-central1"
21+
region = "asia-southeast1"
2222
deletion_protection = false
2323
force_destroy = true
2424
}

test/integration/data_warehouse/data_warehouse_test.go

+90-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
package multiple_buckets
1616

1717
import (
18+
"fmt"
19+
"log"
20+
"os"
21+
"reflect"
1822
"testing"
1923
"time"
2024

25+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/bq"
2126
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
2227
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
28+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2329
"github.com/stretchr/testify/assert"
2430
)
2531

32+
// TODO: Remove because Eventarc is no longer in use
2633
// Retry if these errors are encountered.
2734
var retryErrors = map[string]string{
2835
// IAM for Eventarc service agent is eventually consistent
@@ -37,10 +44,91 @@ func TestDataWarehouse(t *testing.T) {
3744

3845
projectID := dwh.GetTFSetupStringOutput("project_id")
3946
bucket := dwh.GetStringOutput("raw_bucket")
47+
workflow := "initial-workflow"
4048

49+
// Assert that the bucket is in asia-southeast1
4150
bucketOP := gcloud.Runf(t, "storage buckets describe gs://%s --project %s", bucket, projectID)
42-
assert.Equal("US-CENTRAL1", bucketOP.Get("location").String(), "should be in us-central1")
43-
//TODO: Add additional asserts for other resources
51+
assert.Equal("ASIA-SOUTHEAST1", bucketOP.Get("location").String(), "Bucket should be in asia-southeast1")
52+
53+
// Assert that Workflow ran successfully
54+
verifyWorkflows := func() (bool, error) {
55+
workflowState := gcloud.Runf(t, "workflows executions list %s --project %s --location=asia-southeast1 --limit=1", workflow, projectID).Array()
56+
state := workflowState[0].Get("state").String()
57+
assert.NotEqual(t, state, "FAILED")
58+
if state == "SUCCEEDED" {
59+
return false, nil
60+
} else {
61+
return true, nil
62+
}
63+
}
64+
utils.Poll(t, verifyWorkflows, 8, 30*time.Second)
65+
66+
homeDir, err := os.UserHomeDir()
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
file, err := os.Create(homeDir + "/.bigqueryrc")
71+
if err != nil {
72+
log.Fatal(err)
73+
}
74+
file.Close()
75+
76+
// Assert BigQuery tables & views are not empty
77+
test_tables := func (){
78+
79+
tables := []string{
80+
"thelook.distribution_centers",
81+
"thelook.events",
82+
"thelook.inventory_items",
83+
"thelook.order_items",
84+
"thelook.orders",
85+
"thelook.products",
86+
"thelook.users",
87+
"thelook.lookerstudio_report_distribution_centers",
88+
"thelook.lookerstudio_report_profit",
89+
}
90+
91+
query_template := "SELECT COUNT(*) AS count_rows FROM `%[1]s.%[2]s`;"
92+
for _, table := range tables {
93+
query := fmt.Sprintf(query_template, projectID, table)
94+
op := bq.Runf(t, "--project_id=%[1]s --headless=true query --nouse_legacy_sql %[2]s", projectID, query)
95+
fmt.Print(op)
96+
97+
count := op.Get("0.count_rows").Int()
98+
fmt.Printf("Table has %d rows \n", count)
99+
count_kind := reflect.TypeOf(count).Kind()
100+
fmt.Printf("count has type %s \n", count_kind)
101+
test_result := assert.Greater(count, int64(0))
102+
if test_result == true {
103+
fmt.Printf("Table `%s` has %d rows. Test passed! \n", table, count)
104+
} else {
105+
fmt.Printf("Some kind of error occurred while running the count query for the %s table. We think it has %d rows. Test failed. \n", table, count)
106+
}
107+
}
108+
}
109+
110+
test_tables()
111+
112+
// Assert BigQuery connection to Vertex GenAI was successfully created and works as expected
113+
test_llms := func() {
114+
115+
llm_query_template := "SELECT COUNT(*) AS count_rows FROM ML.GENERATE_TEXT(MODEL `%[1]s.thelook.text_generate_model`, (with clusters AS(SELECT CONCAT('cluster', CAST(centroid_id as STRING)) as centroid, avg_spend as average_spend, count_orders as count_of_orders, days_since_order FROM (SELECT centroid_id, feature, ROUND(numerical_value, 2) as value FROM ML.CENTROIDS(MODEL `%[1]s.thelook.customer_segment_clustering`)) PIVOT (SUM(value) FOR feature IN ('avg_spend', 'count_orders', 'days_since_order')) ORDER BY centroid_id) SELECT 'Pretend you are a creative strategist, given the following clusters come up with creative brand persona and title labels for each of these clusters, and explain step by step; what would be the next marketing step for these clusters' || ' ' || clusters.centroid || ', Average Spend $' || clusters.average_spend || ', Count of orders per person ' || clusters.count_of_orders || ', Days since last order ' || clusters.days_since_order AS prompt FROM clusters), STRUCT(800 AS max_output_tokens, 0.8 AS temperature, 40 AS top_k, 0.8 AS top_p, TRUE AS flatten_json_output));"
116+
query := fmt.Sprintf(llm_query_template, projectID)
117+
llm_op := bq.Runf(t, "--project_id=%[1]s --headless=true query --nouse_legacy_sql %[2]s", projectID, query)
118+
119+
llm_count := llm_op.Get("0.count_rows").Int()
120+
count_llm_kind := reflect.TypeOf(llm_count).Kind()
121+
fmt.Printf("llm_count has type %s", count_llm_kind)
122+
llm_test_result := assert.Greater(llm_count, int64(0))
123+
if llm_test_result == true {
124+
fmt.Printf("LLM table has %d rows. Test passed! \n", llm_count)
125+
} else {
126+
fmt.Printf("Some kind of error occurred while running the count query for the LLM table. We think it has %d rows. Test failed. \n", llm_count)
127+
}
128+
}
129+
130+
test_llms()
44131
})
45132
dwh.Test()
46133
}
134+

test/integration/go.mod

+29-30
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,47 @@
11
module github.com/terraform-google-modules/terraform-google-bigquery/test/integration
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.10.1
77
github.com/stretchr/testify v1.8.4
88
)
99

1010
require (
11-
cloud.google.com/go v0.110.7 // indirect
12-
cloud.google.com/go/compute v1.23.0 // indirect
11+
cloud.google.com/go v0.110.10 // indirect
12+
cloud.google.com/go/compute v1.23.3 // indirect
1313
cloud.google.com/go/compute/metadata v0.2.3 // indirect
14-
cloud.google.com/go/iam v1.1.2 // indirect
15-
cloud.google.com/go/storage v1.33.0 // indirect
14+
cloud.google.com/go/iam v1.1.5 // indirect
15+
cloud.google.com/go/storage v1.35.1 // indirect
1616
github.com/agext/levenshtein v1.2.3 // indirect
1717
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
18-
github.com/aws/aws-sdk-go v1.45.5 // indirect
18+
github.com/aws/aws-sdk-go v1.47.13 // indirect
1919
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
2020
github.com/davecgh/go-spew v1.1.1 // indirect
21-
github.com/go-errors/errors v1.5.0 // indirect
21+
github.com/go-errors/errors v1.5.1 // indirect
2222
github.com/go-openapi/jsonpointer v0.20.0 // indirect
2323
github.com/go-openapi/jsonreference v0.20.2 // indirect
2424
github.com/go-openapi/swag v0.22.4 // indirect
2525
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
2626
github.com/golang/protobuf v1.5.3 // indirect
2727
github.com/google/gnostic-models v0.6.8 // indirect
28-
github.com/google/go-cmp v0.5.9 // indirect
29-
github.com/google/gofuzz v1.2.0 // indirect
3028
github.com/google/s2a-go v0.1.7 // indirect
31-
github.com/google/uuid v1.3.1 // indirect
32-
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
29+
github.com/google/uuid v1.4.0 // indirect
30+
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
3331
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
34-
github.com/gruntwork-io/terratest v0.46.6 // indirect
32+
github.com/gruntwork-io/terratest v0.46.7 // indirect
3533
github.com/hashicorp/errwrap v1.1.0 // indirect
3634
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
37-
github.com/hashicorp/go-getter v1.7.2 // indirect
35+
github.com/hashicorp/go-getter v1.7.3 // indirect
3836
github.com/hashicorp/go-multierror v1.1.1 // indirect
3937
github.com/hashicorp/go-safetemp v1.0.0 // indirect
4038
github.com/hashicorp/go-version v1.6.0 // indirect
41-
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
42-
github.com/hashicorp/terraform-json v0.17.1 // indirect
39+
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
40+
github.com/hashicorp/terraform-json v0.18.0 // indirect
4341
github.com/jinzhu/copier v0.4.0 // indirect
4442
github.com/jmespath/go-jmespath v0.4.0 // indirect
4543
github.com/josharian/intern v1.0.0 // indirect
46-
github.com/klauspost/compress v1.16.7 // indirect
44+
github.com/klauspost/compress v1.17.3 // indirect
4745
github.com/mailru/easyjson v0.7.7 // indirect
4846
github.com/mattn/go-zglob v0.0.4 // indirect
4947
github.com/mitchellh/go-homedir v1.1.0 // indirect
@@ -56,25 +54,26 @@ require (
5654
github.com/tidwall/sjson v1.2.5 // indirect
5755
github.com/tmccombs/hcl2json v0.6.0 // indirect
5856
github.com/ulikunitz/xz v0.5.11 // indirect
59-
github.com/zclconf/go-cty v1.14.0 // indirect
57+
github.com/zclconf/go-cty v1.14.1 // indirect
6058
go.opencensus.io v0.24.0 // indirect
61-
golang.org/x/crypto v0.14.0 // indirect
59+
golang.org/x/crypto v0.15.0 // indirect
6260
golang.org/x/mod v0.14.0 // indirect
63-
golang.org/x/net v0.17.0 // indirect
64-
golang.org/x/oauth2 v0.12.0 // indirect
65-
golang.org/x/sync v0.3.0 // indirect
66-
golang.org/x/sys v0.13.0 // indirect
67-
golang.org/x/text v0.13.0 // indirect
68-
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
69-
google.golang.org/api v0.138.0 // indirect
61+
golang.org/x/net v0.18.0 // indirect
62+
golang.org/x/oauth2 v0.14.0 // indirect
63+
golang.org/x/sync v0.5.0 // indirect
64+
golang.org/x/sys v0.14.0 // indirect
65+
golang.org/x/text v0.14.0 // indirect
66+
golang.org/x/time v0.4.0 // indirect
67+
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
68+
google.golang.org/api v0.151.0 // indirect
7069
google.golang.org/appengine v1.6.8 // indirect
71-
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
72-
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
73-
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
74-
google.golang.org/grpc v1.58.3 // indirect
70+
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
71+
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
72+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
73+
google.golang.org/grpc v1.59.0 // indirect
7574
google.golang.org/protobuf v1.31.0 // indirect
7675
gopkg.in/yaml.v2 v2.4.0 // indirect
7776
gopkg.in/yaml.v3 v3.0.1 // indirect
78-
k8s.io/kube-openapi v0.0.0-20230905202853-d090da108d2f // indirect
77+
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e // indirect
7978
sigs.k8s.io/kustomize/kyaml v0.15.0 // indirect
8079
)

0 commit comments

Comments
 (0)