Skip to content

Commit

Permalink
feat: expanded integration testing (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanecglass authored Dec 1, 2023
1 parent 70d2863 commit 9dcdd07
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 88 deletions.
2 changes: 1 addition & 1 deletion examples/data_warehouse/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module "data_warehouse" {
source = "../../modules/data_warehouse"

project_id = var.project_id
region = "us-central1"
region = "asia-southeast1"
deletion_protection = false
force_destroy = true
}
92 changes: 90 additions & 2 deletions test/integration/data_warehouse/data_warehouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
package multiple_buckets

import (
"fmt"
"log"
"os"
"reflect"
"testing"
"time"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/bq"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
"github.com/stretchr/testify/assert"
)

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

projectID := dwh.GetTFSetupStringOutput("project_id")
bucket := dwh.GetStringOutput("raw_bucket")
workflow := "initial-workflow"

// Assert that the bucket is in asia-southeast1
bucketOP := gcloud.Runf(t, "storage buckets describe gs://%s --project %s", bucket, projectID)
assert.Equal("US-CENTRAL1", bucketOP.Get("location").String(), "should be in us-central1")
//TODO: Add additional asserts for other resources
assert.Equal("ASIA-SOUTHEAST1", bucketOP.Get("location").String(), "Bucket should be in asia-southeast1")

// Assert that Workflow ran successfully
verifyWorkflows := func() (bool, error) {
workflowState := gcloud.Runf(t, "workflows executions list %s --project %s --location=asia-southeast1 --limit=1", workflow, projectID).Array()
state := workflowState[0].Get("state").String()
assert.NotEqual(t, state, "FAILED")
if state == "SUCCEEDED" {
return false, nil
} else {
return true, nil
}
}
utils.Poll(t, verifyWorkflows, 8, 30*time.Second)

homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
file, err := os.Create(homeDir + "/.bigqueryrc")
if err != nil {
log.Fatal(err)
}
file.Close()

// Assert BigQuery tables & views are not empty
test_tables := func (){

tables := []string{
"thelook.distribution_centers",
"thelook.events",
"thelook.inventory_items",
"thelook.order_items",
"thelook.orders",
"thelook.products",
"thelook.users",
"thelook.lookerstudio_report_distribution_centers",
"thelook.lookerstudio_report_profit",
}

query_template := "SELECT COUNT(*) AS count_rows FROM `%[1]s.%[2]s`;"
for _, table := range tables {
query := fmt.Sprintf(query_template, projectID, table)
op := bq.Runf(t, "--project_id=%[1]s --headless=true query --nouse_legacy_sql %[2]s", projectID, query)
fmt.Print(op)

count := op.Get("0.count_rows").Int()
fmt.Printf("Table has %d rows \n", count)
count_kind := reflect.TypeOf(count).Kind()
fmt.Printf("count has type %s \n", count_kind)
test_result := assert.Greater(count, int64(0))
if test_result == true {
fmt.Printf("Table `%s` has %d rows. Test passed! \n", table, count)
} else {
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)
}
}
}

test_tables()

// Assert BigQuery connection to Vertex GenAI was successfully created and works as expected
test_llms := func() {

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));"
query := fmt.Sprintf(llm_query_template, projectID)
llm_op := bq.Runf(t, "--project_id=%[1]s --headless=true query --nouse_legacy_sql %[2]s", projectID, query)

llm_count := llm_op.Get("0.count_rows").Int()
count_llm_kind := reflect.TypeOf(llm_count).Kind()
fmt.Printf("llm_count has type %s", count_llm_kind)
llm_test_result := assert.Greater(llm_count, int64(0))
if llm_test_result == true {
fmt.Printf("LLM table has %d rows. Test passed! \n", llm_count)
} else {
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)
}
}

test_llms()
})
dwh.Test()
}

59 changes: 29 additions & 30 deletions test/integration/go.mod
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
module github.com/terraform-google-modules/terraform-google-bigquery/test/integration

go 1.20
go 1.21

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

require (
cloud.google.com/go v0.110.7 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go v0.110.10 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.2 // indirect
cloud.google.com/go/storage v1.33.0 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/storage v1.35.1 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aws/aws-sdk-go v1.45.5 // indirect
github.com/aws/aws-sdk-go v1.47.13 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.5.0 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gruntwork-io/terratest v0.46.6 // indirect
github.com/gruntwork-io/terratest v0.46.7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.2 // indirect
github.com/hashicorp/go-getter v1.7.3 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
github.com/hashicorp/terraform-json v0.17.1 // indirect
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
github.com/hashicorp/terraform-json v0.18.0 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/compress v1.17.3 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-zglob v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand All @@ -56,25 +54,26 @@ require (
github.com/tidwall/sjson v1.2.5 // indirect
github.com/tmccombs/hcl2json v0.6.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/zclconf/go-cty v1.14.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.138.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/api v0.151.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/kube-openapi v0.0.0-20230905202853-d090da108d2f // indirect
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e // indirect
sigs.k8s.io/kustomize/kyaml v0.15.0 // indirect
)
Loading

0 comments on commit 9dcdd07

Please sign in to comment.