github.com/kubeshop/testkube@v1.17.23/pkg/filesystem/os_filesystem_test.go (about) 1 package filesystem 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/kubeshop/testkube/pkg/utils/test" 10 ) 11 12 func TestOSFileSystem_OpenFile_Integration(t *testing.T) { 13 test.IntegrationTest(t) 14 t.Parallel() 15 16 // Create a temporary file and write some data to it 17 f, err := os.CreateTemp("", "test.txt") 18 if err != nil { 19 t.Fatalf("failed to create temporary file: %v", err) 20 } 21 defer f.Close() 22 defer os.Remove(f.Name()) 23 24 data := []byte("hello, world") 25 if _, err := f.Write(data); err != nil { 26 t.Fatalf("failed to write data to file: %v", err) 27 } 28 29 // Create a new instance of OSFileSystem and use it to read the temporary file 30 fs := &OSFileSystem{} 31 reader, err := fs.OpenFileBuffered(f.Name()) 32 if err != nil { 33 t.Fatalf("failed to open file: %v", err) 34 } 35 36 // Read the content of the file 37 content, err := io.ReadAll(reader) 38 if err != nil { 39 t.Fatalf("failed to read file: %v", err) 40 } 41 42 // Check that the content matches what we wrote earlier 43 if string(content) != string(data) { 44 t.Errorf("expected content %q, but got %q", string(data), string(content)) 45 } 46 } 47 48 func TestOSFileSystem_Walk_Integration(t *testing.T) { 49 test.IntegrationTest(t) 50 t.Parallel() 51 // Create a temporary directory and some files 52 dir, err := os.MkdirTemp("", "test-dir") 53 if err != nil { 54 t.Fatalf("failed to create temporary directory: %v", err) 55 } 56 defer os.RemoveAll(dir) 57 58 files := []string{"file1.txt", "file2.txt", "file3.txt"} 59 for _, file := range files { 60 if err := os.WriteFile(filepath.Join(dir, file), []byte("hello, world"), 0644); err != nil { 61 t.Fatalf("failed to create file %q: %v", file, err) 62 } 63 } 64 65 // Create a new instance of OSFileSystem and use it to walk the temporary directory 66 fs := &OSFileSystem{} 67 var fileList []string 68 err = fs.Walk(dir, func(path string, info os.FileInfo, err error) error { 69 if err != nil { 70 return err 71 } 72 73 if !info.IsDir() { 74 fileList = append(fileList, path) 75 } 76 77 return nil 78 }) 79 if err != nil { 80 t.Fatalf("failed to walk directory: %v", err) 81 } 82 83 // Check that the list of files matches what we created earlier 84 expected := make(map[string]bool) 85 for _, file := range files { 86 expected[filepath.Join(dir, file)] = true 87 } 88 89 for _, file := range fileList { 90 if !expected[file] { 91 t.Errorf("unexpected file %q found in directory", file) 92 } 93 } 94 } 95 96 func TestOSFileSystem_OpenFileBuffered_Integration(t *testing.T) { 97 test.IntegrationTest(t) 98 t.Parallel() 99 100 // Create a temporary file and write some data to it 101 tempFile, err := os.CreateTemp("", "test_buffered_file.txt") 102 if err != nil { 103 t.Fatalf("failed to create temporary file: %v", err) 104 } 105 defer os.Remove(tempFile.Name()) 106 107 if _, err := tempFile.WriteString("hello, world"); err != nil { 108 t.Fatalf("failed to write to temporary file: %v", err) 109 } 110 if err := tempFile.Close(); err != nil { 111 t.Fatalf("failed to close temporary file: %v", err) 112 } 113 114 // Open the temporary file with OSFileSystem and read it using OpenFileBuffered 115 fs := &OSFileSystem{} 116 reader, err := fs.OpenFileBuffered(tempFile.Name()) 117 if err != nil { 118 t.Fatalf("failed to open file with OpenFileBuffered: %v", err) 119 } 120 121 // Check that the data read from the file matches what we wrote earlier 122 data, err := io.ReadAll(reader) 123 if err != nil { 124 t.Fatalf("failed to read data from file: %v", err) 125 } 126 127 if string(data) != "hello, world" { 128 t.Errorf("unexpected data read from file: got %q, expected %q", string(data), "hello, world") 129 } 130 } 131 132 func TestOSFileSystem_Integration_ReadDir(t *testing.T) { 133 test.IntegrationTest(t) 134 t.Parallel() 135 // Setup: Create a temporary directory 136 tempDir, err := os.MkdirTemp("", "readDirTest") 137 if err != nil { 138 t.Fatalf("Failed to create temporary directory: %s", err) 139 } 140 defer os.RemoveAll(tempDir) 141 142 // Create a few files in the directory 143 fileNames := []string{"file1.txt", "file2.txt", "file3.txt"} 144 for _, fName := range fileNames { 145 filePath := tempDir + "/" + fName 146 if _, err := os.Create(filePath); err != nil { 147 t.Fatalf("Failed to create file %s: %s", filePath, err) 148 } 149 } 150 151 // Execution: Call ReadDir 152 fs := NewOSFileSystem() 153 entries, err := fs.ReadDir(tempDir) 154 if err != nil { 155 t.Errorf("ReadDir returned error: %s", err) 156 } 157 158 // Verification: Check if the returned entries match the created files 159 if len(entries) != len(fileNames) { 160 t.Errorf("Expected %d entries, got %d", len(fileNames), len(entries)) 161 } 162 163 found := make(map[string]bool) 164 for _, entry := range entries { 165 found[entry.Name()] = true 166 } 167 168 for _, fName := range fileNames { 169 if !found[fName] { 170 t.Errorf("File %s was not found in directory entries", fName) 171 } 172 } 173 } 174 175 func TestOSFileSystem_Getwd(t *testing.T) { 176 t.Parallel() 177 178 // Create an instance of OSFileSystem 179 fs := NewOSFileSystem() 180 181 // Execution: Call Getwd 182 wd, err := fs.Getwd() 183 if err != nil { 184 t.Fatalf("Getwd returned error: %s", err) 185 } 186 187 // Verification: Check if the returned working directory matches os.Getwd 188 expectedWd, err := os.Getwd() 189 if err != nil { 190 t.Fatalf("os.Getwd returned error: %s", err) 191 } 192 193 if wd != expectedWd { 194 t.Errorf("Expected working directory '%s', got '%s'", expectedWd, wd) 195 } 196 }