github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/storage/temp_dir_test.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package storage 12 13 import ( 14 "bytes" 15 "context" 16 "io/ioutil" 17 "math/rand" 18 "os" 19 "path/filepath" 20 "testing" 21 22 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 23 "github.com/cockroachdb/cockroach/pkg/util/stop" 24 ) 25 26 func TestCreateTempDir(t *testing.T) { 27 defer leaktest.AfterTest(t)() 28 29 stopper := stop.NewStopper() 30 defer stopper.Stop(context.Background()) 31 // Temporary parent directory to test this. 32 dir, err := ioutil.TempDir("", "") 33 if err != nil { 34 t.Fatal(err) 35 } 36 defer func() { 37 if err := os.RemoveAll(dir); err != nil { 38 t.Fatal(err) 39 } 40 }() 41 42 tempDir, err := CreateTempDir(dir, "test-create-temp", stopper) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 if dir != filepath.Dir(tempDir) { 48 t.Fatalf("unexpected parent directory of temp subdirectory.\nexpected: %s\nactual: %s", dir, filepath.Dir(tempDir)) 49 } 50 51 _, err = os.Stat(tempDir) 52 if os.IsNotExist(err) { 53 t.Fatalf("expected %s temp subdirectory to exist", tempDir) 54 } 55 if err != nil { 56 t.Fatal(err) 57 } 58 } 59 60 func TestRecordTempDir(t *testing.T) { 61 defer leaktest.AfterTest(t)() 62 recordFile := "foobar" 63 64 f, err := ioutil.TempFile("", "record-file") 65 if err != nil { 66 t.Fatal(err) 67 } 68 defer func() { 69 if err := os.Remove(f.Name()); err != nil { 70 t.Fatal(err) 71 } 72 }() 73 74 // We should close this since RecordTempDir should open the file. 75 if err = f.Close(); err != nil { 76 t.Fatal(err) 77 } 78 79 if err = RecordTempDir(f.Name(), recordFile); err != nil { 80 t.Fatal(err) 81 } 82 83 actual, err := ioutil.ReadFile(f.Name()) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 expected := append([]byte(recordFile), '\n') 89 if !bytes.Equal(expected, actual) { 90 t.Fatalf("unexpected record file content after recording temp dir.\nexpected: %s\nactual: %s", expected, actual) 91 } 92 } 93 94 func TestCleanupTempDirs(t *testing.T) { 95 defer leaktest.AfterTest(t)() 96 97 recordFile, err := ioutil.TempFile("", "record-file") 98 if err != nil { 99 t.Fatal(err) 100 } 101 defer func() { 102 if err := os.Remove(recordFile.Name()); err != nil { 103 t.Fatal(err) 104 } 105 }() 106 107 // Generate some temporary directories. 108 var tempDirs []string 109 for i := 0; i < 5; i++ { 110 tempDir, err := ioutil.TempDir("", "temp-dir") 111 if err != nil { 112 t.Fatal(err) 113 } 114 // Not strictly necessary, but good form to clean up temporary 115 // directories independent of test case. 116 defer func() { 117 if err := os.RemoveAll(tempDir); err != nil { 118 t.Fatal(err) 119 } 120 }() 121 tempDirs = append(tempDirs, tempDir) 122 // Record the temporary directories to the file. 123 if _, err = recordFile.Write(append([]byte(tempDir), '\n')); err != nil { 124 t.Fatal(err) 125 } 126 } 127 128 if err = recordFile.Close(); err != nil { 129 t.Fatal(err) 130 } 131 132 // Generate some temporary files inside the temporary directories. 133 var tempFiles []string 134 content := []byte("whatisthemeaningoflife\n") 135 for i := 0; i < 10; i++ { 136 dir := tempDirs[rand.Intn(len(tempDirs))] 137 tempFile, err := ioutil.TempFile(dir, "temp-file") 138 if err != nil { 139 t.Fatal(err) 140 } 141 if _, err = tempFile.Write(content); err != nil { 142 t.Fatal(err) 143 } 144 if err = tempFile.Close(); err != nil { 145 t.Fatal(err) 146 } 147 } 148 149 if err = CleanupTempDirs(recordFile.Name()); err != nil { 150 t.Fatal(err) 151 } 152 153 // We check if all the temporary subdirectories and files were removed. 154 for _, fname := range append(tempDirs, tempFiles...) { 155 _, err = os.Stat(fname) 156 if !os.IsNotExist(err) { 157 t.Fatalf("file %s expected to be removed by cleanup", fname) 158 } 159 if err != nil { 160 // We expect the files to not exist anymore. 161 if os.IsNotExist(err) { 162 continue 163 } 164 165 t.Fatal(err) 166 } 167 } 168 }