|
| 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 importccl |
| 10 | + |
| 11 | +import ( |
| 12 | + "compress/gzip" |
| 13 | + "fmt" |
| 14 | + "io" |
| 15 | + "os" |
| 16 | + "os/exec" |
| 17 | + "path/filepath" |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/cockroachdb/cockroach/pkg/util" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/util/envutil" |
| 23 | +) |
| 24 | + |
| 25 | +var rewriteCSVTestData = envutil.EnvOrDefaultBool("COCKROACH_REWRITE_CSV_TESTDATA", false) |
| 26 | + |
| 27 | +type csvTestFiles struct { |
| 28 | + files, gzipFiles, bzipFiles, filesWithOpts, filesWithDups []string |
| 29 | +} |
| 30 | + |
| 31 | +func getTestFiles(numFiles int) csvTestFiles { |
| 32 | + var testFiles csvTestFiles |
| 33 | + suffix := "" |
| 34 | + if util.RaceEnabled { |
| 35 | + suffix = "-race" |
| 36 | + } |
| 37 | + for i := 0; i < numFiles; i++ { |
| 38 | + testFiles.files = append(testFiles.files, fmt.Sprintf(`'nodelocal:///%s'`, fmt.Sprintf("data-%d%s", i, suffix))) |
| 39 | + testFiles.gzipFiles = append(testFiles.gzipFiles, fmt.Sprintf(`'nodelocal:///%s'`, fmt.Sprintf("data-%d%s.gz", i, suffix)+"?param=value")) |
| 40 | + testFiles.bzipFiles = append(testFiles.bzipFiles, fmt.Sprintf(`'nodelocal:///%s'`, fmt.Sprintf("data-%d%s.bz2", i, suffix))) |
| 41 | + testFiles.filesWithOpts = append(testFiles.filesWithOpts, fmt.Sprintf(`'nodelocal:///%s'`, fmt.Sprintf("data-%d-opts%s", i, suffix))) |
| 42 | + testFiles.filesWithDups = append(testFiles.filesWithDups, fmt.Sprintf(`'nodelocal:///%s'`, fmt.Sprintf("data-%d-dup%s", i, suffix))) |
| 43 | + } |
| 44 | + |
| 45 | + return testFiles |
| 46 | +} |
| 47 | + |
| 48 | +func makeFiles(t testing.TB, numFiles, rowsPerFile int, dir string, makeRaceFiles bool) { |
| 49 | + suffix := "" |
| 50 | + if makeRaceFiles { |
| 51 | + suffix = "-race" |
| 52 | + } |
| 53 | + |
| 54 | + for fn := 0; fn < numFiles; fn++ { |
| 55 | + // Create normal CSV file. |
| 56 | + fileName := filepath.Join(dir, fmt.Sprintf("data-%d%s", fn, suffix)) |
| 57 | + f, err := os.Create(fileName) |
| 58 | + if err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + |
| 62 | + // Create CSV file which tests query options. |
| 63 | + fWithOpts, err := os.Create(filepath.Join(dir, fmt.Sprintf("data-%d-opts%s", fn, suffix))) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + if _, err := fmt.Fprint(fWithOpts, "This is a header line to be skipped\n"); err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + if _, err := fmt.Fprint(fWithOpts, "So is this\n"); err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + |
| 74 | + // Create CSV file with duplicate entries. |
| 75 | + fDup, err := os.Create(filepath.Join(dir, fmt.Sprintf("data-%d-dup%s", fn, suffix))) |
| 76 | + if err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + |
| 80 | + for i := 0; i < rowsPerFile; i++ { |
| 81 | + x := fn*rowsPerFile + i |
| 82 | + if _, err := fmt.Fprintf(f, "%d,%c\n", x, 'A'+x%26); err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + if _, err := fmt.Fprintf(fDup, "1,%c\n", 'A'+x%26); err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + |
| 89 | + // Write a comment. |
| 90 | + if _, err := fmt.Fprintf(fWithOpts, "# %d\n", x); err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + // Write a pipe-delim line with trailing delim. |
| 94 | + if x%4 == 0 { // 1/4 of rows have blank val for b |
| 95 | + if _, err := fmt.Fprintf(fWithOpts, "%d||\n", x); err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + } else { |
| 99 | + if _, err := fmt.Fprintf(fWithOpts, "%d|%c|\n", x, 'A'+x%26); err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if err := f.Close(); err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + if err := fDup.Close(); err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | + if err := fWithOpts.Close(); err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + |
| 115 | + // Check in zipped versions of CSV file fileName. |
| 116 | + _ = gzipFile(t, fileName) |
| 117 | + _ = bzipFile(t, "", fileName) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func makeCSVData( |
| 122 | + t testing.TB, numFiles, rowsPerFile, numRaceFiles, rowsPerRaceFile int, |
| 123 | +) csvTestFiles { |
| 124 | + if rewriteCSVTestData { |
| 125 | + dir := filepath.Join("testdata", "csv") |
| 126 | + if err := os.RemoveAll(dir); err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + if err := os.Mkdir(dir, 0777); err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + |
| 133 | + makeFiles(t, numFiles, rowsPerFile, dir, false /* makeRaceFiles */) |
| 134 | + makeFiles(t, numRaceFiles, rowsPerRaceFile, dir, true) |
| 135 | + } |
| 136 | + |
| 137 | + if util.RaceEnabled { |
| 138 | + return getTestFiles(numRaceFiles) |
| 139 | + } |
| 140 | + return getTestFiles(numFiles) |
| 141 | +} |
| 142 | + |
| 143 | +func gzipFile(t testing.TB, in string) string { |
| 144 | + r, err := os.Open(in) |
| 145 | + if err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + defer r.Close() |
| 149 | + name := in + ".gz" |
| 150 | + f, err := os.Create(name) |
| 151 | + if err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + defer f.Close() |
| 155 | + w := gzip.NewWriter(f) |
| 156 | + if _, err := io.Copy(w, r); err != nil { |
| 157 | + t.Fatal(err) |
| 158 | + } |
| 159 | + if err := w.Close(); err != nil { |
| 160 | + t.Fatal(err) |
| 161 | + } |
| 162 | + return name |
| 163 | +} |
| 164 | + |
| 165 | +func bzipFile(t testing.TB, dir, in string) string { |
| 166 | + _, err := exec.Command("bzip2", "-k", filepath.Join(dir, in)).CombinedOutput() |
| 167 | + if err != nil { |
| 168 | + if strings.Contains(err.Error(), "executable file not found") { |
| 169 | + return "" |
| 170 | + } |
| 171 | + t.Fatal(err) |
| 172 | + } |
| 173 | + return in + ".bz2" |
| 174 | +} |
0 commit comments