github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/serverccl/admin_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Licensed as a CockroachDB Enterprise file under the Cockroach Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt 8 9 package serverccl 10 11 import ( 12 "context" 13 "reflect" 14 "strings" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/base" 18 "github.com/cockroachdb/cockroach/pkg/server/serverpb" 19 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" 20 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 21 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 22 ) 23 24 var adminPrefix = "/_admin/v1/" 25 26 // TestAdminAPIDataDistributionPartitioning partitions a table and verifies 27 // that we see all zone configs (#27718). 28 func TestAdminAPIDataDistributionPartitioning(t *testing.T) { 29 defer leaktest.AfterTest(t)() 30 31 testCluster := serverutils.StartTestCluster(t, 3, base.TestClusterArgs{}) 32 defer testCluster.Stopper().Stop(context.Background()) 33 34 firstServer := testCluster.Server(0) 35 sqlDB := sqlutils.MakeSQLRunner(testCluster.ServerConn(0)) 36 37 sqlDB.Exec(t, `CREATE DATABASE roachblog`) 38 sqlDB.Exec(t, `USE roachblog`) 39 sqlDB.Exec(t, `CREATE TABLE posts (id INT PRIMARY KEY, title text, body text)`) 40 sqlDB.Exec(t, `CREATE TABLE comments ( 41 id INT, 42 post_id INT REFERENCES posts, 43 user_region STRING, 44 body text, 45 PRIMARY KEY (user_region, id) 46 ) PARTITION BY LIST (user_region) ( 47 PARTITION us VALUES IN ('US'), 48 PARTITION eu VALUES IN ('EU'), 49 PARTITION DEFAULT VALUES IN (default) 50 )`) 51 52 // Create a zone config for each partition. 53 // Would use locality constraints except this test cluster hasn't been started up with localities. 54 sqlDB.Exec(t, `ALTER PARTITION us OF TABLE comments CONFIGURE ZONE USING gc.ttlseconds = 9001`) 55 sqlDB.Exec(t, `ALTER PARTITION eu OF TABLE comments CONFIGURE ZONE USING gc.ttlseconds = 9002`) 56 57 // Assert that we get all roachblog zone configs back. 58 expectedZoneConfigNames := map[string]struct{}{ 59 "PARTITION eu OF INDEX roachblog.public.comments@primary": {}, 60 "PARTITION us OF INDEX roachblog.public.comments@primary": {}, 61 } 62 63 var resp serverpb.DataDistributionResponse 64 if err := serverutils.GetJSONProto(firstServer, adminPrefix+"data_distribution", &resp); err != nil { 65 t.Fatal(err) 66 } 67 68 actualZoneConfigNames := map[string]struct{}{} 69 for name := range resp.ZoneConfigs { 70 if strings.Contains(name, "roachblog") { 71 actualZoneConfigNames[name] = struct{}{} 72 } 73 } 74 if !reflect.DeepEqual(actualZoneConfigNames, expectedZoneConfigNames) { 75 t.Fatalf("expected zone config names %v; got %v", expectedZoneConfigNames, actualZoneConfigNames) 76 } 77 }