-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
randgen: add PopulateTableWithRandomData
PopulateRandTable populates the caller's table with random data. This helper function aims to make it easier for engineers to develop randomized tests that leverage randgen / sqlsmith. I considered adding random insert statements into sqlsmith's randtables setup, however the high probably of a faulty insert statement would cause the whole setup to fail. See #75159 In the future, I'd like to develop a new helper function PopulateDatabaseWithRandData which calls PopulateTableWithRandData on each table in the order of the fk dependency graph. Informs #72345 Release note: None
- Loading branch information
Showing
6 changed files
with
262 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package randgen | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/security" | ||
"github.com/cockroachdb/cockroach/pkg/security/securitytest" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/randutil" | ||
) | ||
|
||
//go:generate ../../util/leaktest/add-leaktest.sh *_test.go | ||
|
||
func TestMain(m *testing.M) { | ||
security.SetAssetLoader(securitytest.EmbeddedAssets) | ||
randutil.SeedForTests() | ||
serverutils.InitTestServerFactory(server.TestServerFactory) | ||
serverutils.InitTestClusterFactory(testcluster.TestClusterFactory) | ||
os.Exit(m.Run()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package randgen | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/randutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestPopulateTableWithRandData generates some random tables and passes if it | ||
// at least one of those tables will be successfully populated. | ||
func TestPopulateTableWithRandData(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
ctx := context.Background() | ||
s, dbConn, _ := serverutils.StartServer(t, base.TestServerArgs{}) | ||
defer s.Stopper().Stop(ctx) | ||
|
||
rng, _ := randutil.NewTestRand() | ||
|
||
sqlDB := sqlutils.MakeSQLRunner(dbConn) | ||
sqlDB.Exec(t, "CREATE DATABASE rand") | ||
|
||
// Turn off auto stats collection to prevent out of memory errors on stress tests | ||
sqlDB.Exec(t, "SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false") | ||
|
||
tablePrefix := "table" | ||
numTables := 10 | ||
|
||
stmts := RandCreateTables(rng, tablePrefix, numTables, | ||
PartialIndexMutator, | ||
ForeignKeyMutator, | ||
) | ||
|
||
var sb strings.Builder | ||
for _, stmt := range stmts { | ||
sb.WriteString(tree.SerializeForDisplay(stmt)) | ||
sb.WriteString(";\n") | ||
} | ||
sqlDB.Exec(t, sb.String()) | ||
|
||
// To prevent the test from being flaky, pass the test if PopulateTableWithRandomData | ||
// inserts at least one row in at least one table. | ||
success := false | ||
for i := 1; i <= numTables; i++ { | ||
tableName := tablePrefix + fmt.Sprint(i) | ||
numRows := 30 | ||
numRowsInserted, err := PopulateTableWithRandData(rng, dbConn, tableName, numRows) | ||
require.NoError(t, err) | ||
res := sqlDB.QueryStr(t, fmt.Sprintf("SELECT count(*) FROM %s", tableName)) | ||
require.Equal(t, fmt.Sprint(numRowsInserted), res[0][0]) | ||
if numRowsInserted > 0 { | ||
success = true | ||
break | ||
} | ||
} | ||
require.Equal(t, true, success) | ||
} |