github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/backupccl/restore_old_versions_test.go (about)

     1  // Copyright 2019 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 backupccl_test
    10  
    11  import (
    12  	"io/ioutil"
    13  	"os"
    14  	"path/filepath"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/base"
    18  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  // TestRestoreOldVersions ensures that we can successfully restore tables
    23  // and databases exported by old version.
    24  //
    25  // The files being restored live in testdata and are all made from the same
    26  // input SQL which lives in <testdataBase>/create.sql.
    27  //
    28  // The SSTs were created via the following commands:
    29  //
    30  //  VERSION=...
    31  //  roachprod wipe local
    32  //  roachprod stage local release ${VERSION}
    33  //  roachprod start local
    34  //  # If the version is v1.0.7 then you need to enable enterprise with the
    35  //  # enterprise.enabled cluster setting.
    36  //  roachprod sql local:1 -- -e "$(cat pkg/ccl/backupccl/testdata/restore_old_versions/create.sql)"
    37  //  # Create an S3 bucket to store the backup.
    38  //  roachprod sql local:1 -- -e "BACKUP DATABASE test TO 's3://<bucket-name>/${VERSION}?AWS_ACCESS_KEY_ID=<...>&AWS_SECRET_ACCESS_KEY=<...>'"
    39  //  # Then download the backup from s3 and plop the files into the appropriate
    40  //  # testdata directory.
    41  //
    42  func TestRestoreOldVersions(t *testing.T) {
    43  	defer leaktest.AfterTest(t)()
    44  	const (
    45  		testdataBase = "testdata/restore_old_versions"
    46  		exportDirs   = testdataBase + "/exports"
    47  	)
    48  	dirs, err := ioutil.ReadDir(exportDirs)
    49  	require.NoError(t, err)
    50  	for _, dir := range dirs {
    51  		require.True(t, dir.IsDir())
    52  		exportDir, err := filepath.Abs(filepath.Join(exportDirs, dir.Name()))
    53  		require.NoError(t, err)
    54  		t.Run(dir.Name(), restoreOldVersionTest(exportDir))
    55  	}
    56  }
    57  
    58  func restoreOldVersionTest(exportDir string) func(t *testing.T) {
    59  	return func(t *testing.T) {
    60  		params := base.TestServerArgs{}
    61  		const numAccounts = 1000
    62  		_, _, sqlDB, dir, cleanup := backupRestoreTestSetupWithParams(t, singleNode, numAccounts,
    63  			initNone, base.TestClusterArgs{ServerArgs: params})
    64  		defer cleanup()
    65  		err := os.Symlink(exportDir, filepath.Join(dir, "foo"))
    66  		require.NoError(t, err)
    67  		sqlDB.Exec(t, `CREATE DATABASE test`)
    68  		var unused string
    69  		var importedRows int
    70  		sqlDB.QueryRow(t, `RESTORE test.* FROM $1`, localFoo).Scan(
    71  			&unused, &unused, &unused, &importedRows, &unused, &unused,
    72  		)
    73  		const totalRows = 12
    74  		if importedRows != totalRows {
    75  			t.Fatalf("expected %d rows, got %d", totalRows, importedRows)
    76  		}
    77  		results := [][]string{
    78  			{"1", "1", "1"},
    79  			{"2", "2", "2"},
    80  			{"3", "3", "3"},
    81  		}
    82  		sqlDB.CheckQueryResults(t, `SELECT * FROM test.t1 ORDER BY k`, results)
    83  		sqlDB.CheckQueryResults(t, `SELECT * FROM test.t2 ORDER BY k`, results)
    84  		sqlDB.CheckQueryResults(t, `SELECT * FROM test.t4 ORDER BY k`, results)
    85  	}
    86  }