|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/AlexsanderHamir/prof/engine/collector" |
| 10 | + "github.com/AlexsanderHamir/prof/internal" |
| 11 | +) |
| 12 | + |
| 13 | +// cleanupBenchDirectory removes the bench directory if it exists |
| 14 | +func cleanupBenchDirectory(t *testing.T) { |
| 15 | + if _, err := os.Stat(internal.MainDirOutput); err == nil { |
| 16 | + if err := os.RemoveAll(internal.MainDirOutput); err != nil { |
| 17 | + t.Errorf("Failed to clean up bench directory: %v", err) |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestGetProfileTextOutput(t *testing.T) { |
| 23 | + // Use one of the binary files from tests/assets |
| 24 | + binaryFile := "../../../tests/assets/cpu.out" |
| 25 | + |
| 26 | + // Check if the file exists |
| 27 | + _, err := os.Stat(binaryFile) |
| 28 | + if os.IsNotExist(err) { |
| 29 | + t.Skip("Binary file not found, skipping test") |
| 30 | + } |
| 31 | + |
| 32 | + // Create a temporary output file |
| 33 | + tempDir, err := os.MkdirTemp("", "test_profile_output") |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 36 | + } |
| 37 | + defer os.RemoveAll(tempDir) |
| 38 | + |
| 39 | + outputFile := filepath.Join(tempDir, "cpu_profile.txt") |
| 40 | + |
| 41 | + // Test the function |
| 42 | + err = collector.GetProfileTextOutput(binaryFile, outputFile) |
| 43 | + |
| 44 | + // The function might fail if go tool pprof is not available |
| 45 | + // or if the binary file is not a valid profile |
| 46 | + if err != nil { |
| 47 | + // Check if the error is due to missing go tool or invalid profile |
| 48 | + if strings.Contains(err.Error(), "exec: \"go\": executable file not found in PATH") { |
| 49 | + t.Skip("Go tool not available, skipping test") |
| 50 | + } |
| 51 | + t.Errorf("GetProfileTextOutput failed: %v", err) |
| 52 | + } else { |
| 53 | + // If successful, check if output file was created and has content |
| 54 | + if _, err := os.Stat(outputFile); os.IsNotExist(err) { |
| 55 | + t.Errorf("Output file was not created: %s", outputFile) |
| 56 | + } else { |
| 57 | + content, err := os.ReadFile(outputFile) |
| 58 | + if err != nil { |
| 59 | + t.Errorf("Failed to read output file: %v", err) |
| 60 | + } else if len(content) == 0 { |
| 61 | + t.Errorf("Output file is empty") |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestGetPNGOutput(t *testing.T) { |
| 68 | + // Use one of the binary files from tests/assets |
| 69 | + binaryFile := "../../../tests/assets/memory.out" |
| 70 | + |
| 71 | + // Check if the file exists |
| 72 | + _, err := os.Stat(binaryFile) |
| 73 | + if os.IsNotExist(err) { |
| 74 | + t.Skip("Binary file not found, skipping test") |
| 75 | + } |
| 76 | + |
| 77 | + // Create a temporary output file |
| 78 | + tempDir, err := os.MkdirTemp("", "test_png_output") |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 81 | + } |
| 82 | + defer os.RemoveAll(tempDir) |
| 83 | + |
| 84 | + outputFile := filepath.Join(tempDir, "memory_profile.png") |
| 85 | + |
| 86 | + // Test the function |
| 87 | + err = collector.GetPNGOutput(binaryFile, outputFile) |
| 88 | + |
| 89 | + // The function might fail if go tool pprof is not available |
| 90 | + // or if the binary file is not a valid profile |
| 91 | + if err != nil { |
| 92 | + // Check if the error is due to missing go tool or invalid profile |
| 93 | + if strings.Contains(err.Error(), "exec: \"go\": executable file not found in PATH") { |
| 94 | + t.Skip("Go tool not available, skipping test") |
| 95 | + } |
| 96 | + t.Errorf("GetPNGOutput failed: %v", err) |
| 97 | + } else { |
| 98 | + // If successful, check if output file was created and has content |
| 99 | + if _, err := os.Stat(outputFile); os.IsNotExist(err) { |
| 100 | + t.Errorf("Output file was not created: %s", outputFile) |
| 101 | + } else { |
| 102 | + content, err := os.ReadFile(outputFile) |
| 103 | + if err != nil { |
| 104 | + t.Errorf("Failed to read output file: %v", err) |
| 105 | + } else if len(content) == 0 { |
| 106 | + t.Errorf("Output file is empty") |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestGetFunctionsOutput(t *testing.T) { |
| 113 | + // Use one of the binary files from tests/assets |
| 114 | + binaryFile := "../../../tests/assets/cpu.out" |
| 115 | + |
| 116 | + // Check if the file exists |
| 117 | + _, err := os.Stat(binaryFile) |
| 118 | + if os.IsNotExist(err) { |
| 119 | + t.Skip("Binary file not found, skipping test") |
| 120 | + } |
| 121 | + |
| 122 | + // Create a temporary output directory |
| 123 | + tempDir, err := os.MkdirTemp("", "test_functions_output") |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 126 | + } |
| 127 | + defer os.RemoveAll(tempDir) |
| 128 | + |
| 129 | + // Test with sample function names |
| 130 | + functions := []string{"cpuIntensiveWorkload", "func1"} |
| 131 | + |
| 132 | + // Test the function |
| 133 | + err = collector.GetFunctionsOutput(functions, binaryFile, tempDir) |
| 134 | + |
| 135 | + // The function might fail if go tool pprof is not available |
| 136 | + // or if the binary file is not a valid profile |
| 137 | + if err != nil { |
| 138 | + // Check if the error is due to missing go tool or invalid profile |
| 139 | + if strings.Contains(err.Error(), "exec: \"go\": executable file not found in PATH") { |
| 140 | + t.Skip("Go tool not available, skipping test") |
| 141 | + } |
| 142 | + t.Errorf("GetFunctionsOutput failed: %v", err) |
| 143 | + } else { |
| 144 | + // If successful, check if output files were created |
| 145 | + for _, function := range functions { |
| 146 | + outputFile := filepath.Join(tempDir, function+"."+internal.TextExtension) |
| 147 | + if _, err := os.Stat(outputFile); os.IsNotExist(err) { |
| 148 | + t.Errorf("Output file was not created for function %s: %s", function, outputFile) |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func TestRunCollector(t *testing.T) { |
| 155 | + // Use binary files from tests/assets |
| 156 | + binaryFiles := []string{ |
| 157 | + "../../../tests/assets/cpu.out", |
| 158 | + "../../../tests/assets/memory.out", |
| 159 | + } |
| 160 | + |
| 161 | + // Check if the files exist |
| 162 | + for _, file := range binaryFiles { |
| 163 | + _, err := os.Stat(file) |
| 164 | + if os.IsNotExist(err) { |
| 165 | + t.Skip("Binary files not found, skipping test") |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Create a temporary directory for testing |
| 170 | + tempDir, err := os.MkdirTemp("", "test_run_collector") |
| 171 | + if err != nil { |
| 172 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 173 | + } |
| 174 | + defer os.RemoveAll(tempDir) |
| 175 | + |
| 176 | + // Ensure cleanup of any existing bench directory |
| 177 | + defer cleanupBenchDirectory(t) |
| 178 | + |
| 179 | + // Test the function |
| 180 | + err = collector.RunCollector(binaryFiles, "test_tag") |
| 181 | + |
| 182 | + // The function might fail if go tool pprof is not available |
| 183 | + // or if the binary files are not valid profiles |
| 184 | + if err != nil { |
| 185 | + // Check if the error is due to missing go tool or invalid profile |
| 186 | + if strings.Contains(err.Error(), "exec: \"go\": executable file not found in PATH") { |
| 187 | + t.Skip("Go tool not available, skipping test") |
| 188 | + } |
| 189 | + t.Errorf("RunCollector failed: %v", err) |
| 190 | + } else { |
| 191 | + // If successful, check if the expected directory structure was created |
| 192 | + tagDir := filepath.Join("bench", "test_tag") |
| 193 | + if _, err := os.Stat(tagDir); os.IsNotExist(err) { |
| 194 | + t.Errorf("Tag directory was not created: %s", tagDir) |
| 195 | + } |
| 196 | + |
| 197 | + // Check if profile directories were created for each binary file |
| 198 | + for _, binaryFile := range binaryFiles { |
| 199 | + fileName := filepath.Base(binaryFile) |
| 200 | + fileName = strings.TrimSuffix(fileName, filepath.Ext(fileName)) |
| 201 | + profileDir := filepath.Join(tagDir, fileName) |
| 202 | + if _, err := os.Stat(profileDir); os.IsNotExist(err) { |
| 203 | + t.Errorf("Profile directory was not created: %s", profileDir) |
| 204 | + } |
| 205 | + |
| 206 | + // Check if text profile file was created |
| 207 | + textProfileFile := filepath.Join(profileDir, fileName+"."+internal.TextExtension) |
| 208 | + if _, err := os.Stat(textProfileFile); os.IsNotExist(err) { |
| 209 | + t.Errorf("Text profile file was not created: %s", textProfileFile) |
| 210 | + } |
| 211 | + |
| 212 | + // Check if functions directory was created |
| 213 | + functionsDir := filepath.Join(profileDir, "functions") |
| 214 | + if _, err := os.Stat(functionsDir); os.IsNotExist(err) { |
| 215 | + t.Errorf("Functions directory was not created: %s", functionsDir) |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +func TestRunCollectorWithInvalidFiles(t *testing.T) { |
| 222 | + // Test with non-existent files |
| 223 | + invalidFiles := []string{"/non/existent/file.out", "/another/invalid/file.out"} |
| 224 | + |
| 225 | + // Create a temporary directory for testing |
| 226 | + tempDir, err := os.MkdirTemp("", "test_run_collector_invalid") |
| 227 | + if err != nil { |
| 228 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 229 | + } |
| 230 | + defer os.RemoveAll(tempDir) |
| 231 | + |
| 232 | + // Ensure cleanup of any existing bench directory |
| 233 | + defer cleanupBenchDirectory(t) |
| 234 | + |
| 235 | + // Test the function - it should fail |
| 236 | + err = collector.RunCollector(invalidFiles, "test_tag") |
| 237 | + if err == nil { |
| 238 | + t.Error("Expected error when running collector with invalid files, got nil") |
| 239 | + } else if !strings.Contains(err.Error(), "pprof command failed") { |
| 240 | + t.Errorf("Expected error to contain 'pprof command failed', got: %v", err) |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +func TestRunCollectorWithEmptyFileList(t *testing.T) { |
| 245 | + // Test with empty file list |
| 246 | + emptyFiles := []string{} |
| 247 | + |
| 248 | + // Create a temporary directory for testing |
| 249 | + tempDir, err := os.MkdirTemp("", "test_run_collector_empty") |
| 250 | + if err != nil { |
| 251 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 252 | + } |
| 253 | + defer os.RemoveAll(tempDir) |
| 254 | + |
| 255 | + // Ensure cleanup of any existing bench directory |
| 256 | + defer cleanupBenchDirectory(t) |
| 257 | + |
| 258 | + // Test the function - it should succeed with no files to process |
| 259 | + err = collector.RunCollector(emptyFiles, "test_tag") |
| 260 | + if err != nil { |
| 261 | + t.Errorf("Expected no error when running collector with empty file list, got: %v", err) |
| 262 | + } |
| 263 | + |
| 264 | + // Check if tag directory was created in the current working directory |
| 265 | + tagDir := filepath.Join("bench", "test_tag") |
| 266 | + if _, err := os.Stat(tagDir); os.IsNotExist(err) { |
| 267 | + t.Errorf("Tag directory was not created: %s", tagDir) |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +func TestRunCollectorWithMockFiles(t *testing.T) { |
| 272 | + // Create a temporary directory for testing |
| 273 | + tempDir, err := os.MkdirTemp("", "test_run_collector_mock") |
| 274 | + if err != nil { |
| 275 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 276 | + } |
| 277 | + defer os.RemoveAll(tempDir) |
| 278 | + |
| 279 | + // Ensure cleanup of any existing bench directory |
| 280 | + defer cleanupBenchDirectory(t) |
| 281 | + |
| 282 | + // Create mock binary files |
| 283 | + mockFiles := []string{ |
| 284 | + createMockBinaryFile(t, tempDir, "mock1.out"), |
| 285 | + createMockBinaryFile(t, tempDir, "mock2.out"), |
| 286 | + } |
| 287 | + |
| 288 | + // Test the function - it should fail due to invalid binary files |
| 289 | + err = collector.RunCollector(mockFiles, "test_tag") |
| 290 | + |
| 291 | + // The function should fail because the mock files are not valid Go profiles |
| 292 | + if err == nil { |
| 293 | + t.Error("Expected error when running collector with mock files, got nil") |
| 294 | + } else if !strings.Contains(err.Error(), "pprof command failed") { |
| 295 | + t.Errorf("Expected error to contain 'pprof command failed', got: %v", err) |
| 296 | + } |
| 297 | +} |
0 commit comments