Skip to content

Commit 733c72a

Browse files
authored
test(bigquery): add integration test for Column ACLs (#3895)
* testing(bigquery): add integration test for Column ACLs Now that we have a v1 PTM client, we can test the policy tag integration with column ACLs. This also plumbed in a new client as part of the integration testing setup, much like we make a cloud storage client available.
1 parent 9cc6d2c commit 733c72a

1 file changed

Lines changed: 96 additions & 4 deletions

File tree

bigquery/integration_test.go

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"time"
3131

3232
"cloud.google.com/go/civil"
33+
datacatalog "cloud.google.com/go/datacatalog/apiv1"
3334
"cloud.google.com/go/httpreplay"
3435
"cloud.google.com/go/iam"
3536
"cloud.google.com/go/internal"
@@ -43,17 +44,19 @@ import (
4344
"google.golang.org/api/googleapi"
4445
"google.golang.org/api/iterator"
4546
"google.golang.org/api/option"
47+
datacatalogpb "google.golang.org/genproto/googleapis/cloud/datacatalog/v1"
4648
)
4749

4850
const replayFilename = "bigquery.replay"
4951

5052
var record = flag.Bool("record", false, "record RPCs")
5153

5254
var (
53-
client *Client
54-
storageClient *storage.Client
55-
dataset *Dataset
56-
schema = Schema{
55+
client *Client
56+
storageClient *storage.Client
57+
policyTagManagerClient *datacatalog.PolicyTagManagerClient
58+
dataset *Dataset
59+
schema = Schema{
5760
{Name: "name", Type: StringFieldType},
5861
{Name: "nums", Type: IntegerFieldType, Repeated: true},
5962
{Name: "rec", Type: RecordFieldType, Schema: Schema{
@@ -119,6 +122,10 @@ func initIntegrationTest() func() {
119122
if err != nil {
120123
log.Fatal(err)
121124
}
125+
policyTagManagerClient, err = datacatalog.NewPolicyTagManagerClient(ctx)
126+
if err != nil {
127+
log.Fatal(err)
128+
}
122129
cleanup := initTestState(client, t)
123130
return func() {
124131
cleanup()
@@ -142,6 +149,7 @@ func initIntegrationTest() func() {
142149
}
143150
bqOpts := []option.ClientOption{option.WithTokenSource(ts)}
144151
sOpts := []option.ClientOption{option.WithTokenSource(testutil.TokenSource(ctx, storage.ScopeFullControl))}
152+
ptmOpts := []option.ClientOption{option.WithTokenSource(testutil.TokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform"))}
145153
cleanup := func() {}
146154
now := time.Now().UTC()
147155
if *record {
@@ -179,6 +187,7 @@ func initIntegrationTest() func() {
179187
// incompatible with gRPC options.
180188
bqOpts = append(bqOpts, grpcHeadersChecker.CallOptions()...)
181189
sOpts = append(sOpts, grpcHeadersChecker.CallOptions()...)
190+
ptmOpts = append(ptmOpts, grpcHeadersChecker.CallOptions()...)
182191
}
183192
var err error
184193
client, err = NewClient(ctx, projID, bqOpts...)
@@ -189,6 +198,7 @@ func initIntegrationTest() func() {
189198
if err != nil {
190199
log.Fatalf("storage.NewClient: %v", err)
191200
}
201+
policyTagManagerClient, err = datacatalog.NewPolicyTagManagerClient(ctx, ptmOpts...)
192202
c := initTestState(client, now)
193203
return func() { c(); cleanup() }
194204
}
@@ -889,6 +899,88 @@ func TestIntegration_Tables(t *testing.T) {
889899
}
890900
}
891901

902+
// setupPolicyTag is a helper for setting up policy tags in the datacatalog service.
903+
//
904+
// It returns a string for a policy tag identifier and a cleanup function, or an error.
905+
func setupPolicyTag(ctx context.Context) (string, func(), error) {
906+
location := "us"
907+
req := &datacatalogpb.CreateTaxonomyRequest{
908+
Parent: fmt.Sprintf("projects/%s/locations/%s", testutil.ProjID(), location),
909+
Taxonomy: &datacatalogpb.Taxonomy{
910+
DisplayName: "google-cloud-go bigquery testing taxonomy",
911+
Description: "Taxonomy created for google-cloud-go integration tests",
912+
ActivatedPolicyTypes: []datacatalogpb.Taxonomy_PolicyType{
913+
datacatalogpb.Taxonomy_FINE_GRAINED_ACCESS_CONTROL,
914+
},
915+
},
916+
}
917+
resp, err := policyTagManagerClient.CreateTaxonomy(ctx, req)
918+
if err != nil {
919+
return "", nil, fmt.Errorf("datacatalog.CreateTaxonomy: %v", err)
920+
}
921+
taxonomyID := resp.GetName()
922+
cleanupFunc := func() {
923+
policyTagManagerClient.DeleteTaxonomy(ctx, &datacatalogpb.DeleteTaxonomyRequest{
924+
Name: taxonomyID,
925+
})
926+
}
927+
928+
tagReq := &datacatalogpb.CreatePolicyTagRequest{
929+
Parent: resp.GetName(),
930+
PolicyTag: &datacatalogpb.PolicyTag{
931+
DisplayName: "ExamplePolicyTag",
932+
},
933+
}
934+
tagResp, err := policyTagManagerClient.CreatePolicyTag(ctx, tagReq)
935+
if err != nil {
936+
// we're failed to create tags, but we did create taxonomy. clean it up and signal error.
937+
cleanupFunc()
938+
return "", nil, fmt.Errorf("datacatalog.CreatePolicyTag: %v", err)
939+
}
940+
return tagResp.GetName(), cleanupFunc, nil
941+
}
942+
943+
func TestIntegration_ColumnACLs(t *testing.T) {
944+
if client == nil {
945+
t.Skip("Integration tests skipped")
946+
}
947+
ctx := context.Background()
948+
testSchema := Schema{
949+
{Name: "name", Type: StringFieldType},
950+
{Name: "ssn", Type: StringFieldType},
951+
{Name: "acct_balance", Type: NumericFieldType},
952+
}
953+
table := newTable(t, testSchema)
954+
defer table.Delete(ctx)
955+
956+
tagID, cleanupFunc, err := setupPolicyTag(ctx)
957+
if err != nil {
958+
t.Fatalf("failed to setup policy tag resources: %v", err)
959+
}
960+
defer cleanupFunc()
961+
// amend the test schema to add a policy tag
962+
testSchema[1].PolicyTags = &PolicyTagList{
963+
Names: []string{tagID},
964+
}
965+
966+
// Test: Amend an existing schema with a policy tag.
967+
_, err = table.Update(ctx, TableMetadataToUpdate{
968+
Schema: testSchema,
969+
}, "")
970+
if err != nil {
971+
t.Errorf("update with policyTag failed: %v", err)
972+
}
973+
974+
// Test: Create a new table with a policy tag defined.
975+
newTable := dataset.Table(tableIDs.New())
976+
if err = newTable.Create(ctx, &TableMetadata{
977+
Schema: schema,
978+
Description: "foo",
979+
}); err != nil {
980+
t.Errorf("failed to create new table with policy tag: %v", err)
981+
}
982+
}
983+
892984
func TestIntegration_TableIAM(t *testing.T) {
893985
if client == nil {
894986
t.Skip("Integration tests skipped")

0 commit comments

Comments
 (0)