|
1 | 1 | package runner |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
4 | 8 | "testing" |
5 | 9 | "time" |
6 | 10 | ) |
@@ -171,3 +175,118 @@ queries: |
171 | 175 | t.Fatal("LoadSuite accepted an invalid instant vector order direction") |
172 | 176 | } |
173 | 177 | } |
| 178 | + |
| 179 | +const plannerOutputSuite = `name: fixed-output |
| 180 | +planner_output_dir: %s |
| 181 | +queries: |
| 182 | + - name: up |
| 183 | + expr: up |
| 184 | + instant_offsets_seconds: [60] |
| 185 | +` |
| 186 | + |
| 187 | +// writePlannerOutputSuite writes a suite under root/suites whose |
| 188 | +// planner_output_dir is configured, plus the named files under |
| 189 | +// root/suites/fixed. |
| 190 | +func writePlannerOutputSuite(t *testing.T, configured string, files ...string) (string, string) { |
| 191 | + t.Helper() |
| 192 | + root := t.TempDir() |
| 193 | + suiteDirectory := filepath.Join(root, "suites") |
| 194 | + outputDirectory := filepath.Join(suiteDirectory, "fixed") |
| 195 | + if err := os.MkdirAll(outputDirectory, 0o755); err != nil { |
| 196 | + t.Fatalf("create planner output directory: %v", err) |
| 197 | + } |
| 198 | + for _, name := range files { |
| 199 | + if err := os.WriteFile(filepath.Join(outputDirectory, name), []byte("{}\n"), 0o600); err != nil { |
| 200 | + t.Fatalf("write %s: %v", name, err) |
| 201 | + } |
| 202 | + } |
| 203 | + suitePath := filepath.Join(suiteDirectory, "suite.yaml") |
| 204 | + if err := os.WriteFile(suitePath, []byte(fmt.Sprintf(plannerOutputSuite, configured)), 0o600); err != nil { |
| 205 | + t.Fatalf("write suite: %v", err) |
| 206 | + } |
| 207 | + return suitePath, outputDirectory |
| 208 | +} |
| 209 | + |
| 210 | +func TestLoadSuiteFileResolvesPlannerOutputDirAgainstSuiteFile(t *testing.T) { |
| 211 | + suitePath, outputDirectory := writePlannerOutputSuite(t, "fixed", plannerStreamingConfigFile, plannerInferenceConfigFile) |
| 212 | + |
| 213 | + // Load through a relative suite path so resolution cannot depend on the |
| 214 | + // caller's working directory matching the suite's directory. |
| 215 | + workingDirectory, err := os.Getwd() |
| 216 | + if err != nil { |
| 217 | + t.Fatalf("Getwd: %v", err) |
| 218 | + } |
| 219 | + relativeSuitePath, err := filepath.Rel(workingDirectory, suitePath) |
| 220 | + if err != nil { |
| 221 | + t.Fatalf("Rel: %v", err) |
| 222 | + } |
| 223 | + suite, err := LoadSuiteFile(relativeSuitePath) |
| 224 | + if err != nil { |
| 225 | + t.Fatalf("LoadSuiteFile: %v", err) |
| 226 | + } |
| 227 | + if got, want := suite.PlannerOutputDir, outputDirectory; got != want { |
| 228 | + t.Fatalf("planner output dir = %q, want %q", got, want) |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +func TestLoadSuiteFileAcceptsAbsolutePlannerOutputDir(t *testing.T) { |
| 233 | + outputDirectory := t.TempDir() |
| 234 | + for _, name := range []string{plannerStreamingConfigFile, plannerInferenceConfigFile} { |
| 235 | + if err := os.WriteFile(filepath.Join(outputDirectory, name), []byte("{}\n"), 0o600); err != nil { |
| 236 | + t.Fatalf("write %s: %v", name, err) |
| 237 | + } |
| 238 | + } |
| 239 | + suitePath, _ := writePlannerOutputSuite(t, outputDirectory) |
| 240 | + |
| 241 | + suite, err := LoadSuiteFile(suitePath) |
| 242 | + if err != nil { |
| 243 | + t.Fatalf("LoadSuiteFile: %v", err) |
| 244 | + } |
| 245 | + if got, want := suite.PlannerOutputDir, outputDirectory; got != want { |
| 246 | + t.Fatalf("planner output dir = %q, want %q", got, want) |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +func TestLoadSuiteFileLeavesPlannerOutputDirUnsetByDefault(t *testing.T) { |
| 251 | + suite, err := LoadSuiteFile("../suites/temporal.yaml") |
| 252 | + if err != nil { |
| 253 | + t.Fatalf("LoadSuiteFile: %v", err) |
| 254 | + } |
| 255 | + if suite.PlannerOutputDir != "" { |
| 256 | + t.Fatalf("planner output dir = %q, want empty", suite.PlannerOutputDir) |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +func TestLoadSuiteFileRejectsIncompletePlannerOutputDir(t *testing.T) { |
| 261 | + for _, test := range []struct { |
| 262 | + name string |
| 263 | + configured string |
| 264 | + files []string |
| 265 | + missing string |
| 266 | + }{ |
| 267 | + {"missing inference config", "fixed", []string{plannerStreamingConfigFile}, plannerInferenceConfigFile}, |
| 268 | + {"missing streaming config", "fixed", []string{plannerInferenceConfigFile}, plannerStreamingConfigFile}, |
| 269 | + {"missing directory", "absent", nil, plannerStreamingConfigFile}, |
| 270 | + } { |
| 271 | + t.Run(test.name, func(t *testing.T) { |
| 272 | + suitePath, _ := writePlannerOutputSuite(t, test.configured, test.files...) |
| 273 | + _, err := LoadSuiteFile(suitePath) |
| 274 | + if err == nil { |
| 275 | + t.Fatal("LoadSuiteFile accepted a planner_output_dir without both config files") |
| 276 | + } |
| 277 | + if !strings.Contains(err.Error(), test.missing) { |
| 278 | + t.Fatalf("error %q does not name the missing file %q", err, test.missing) |
| 279 | + } |
| 280 | + }) |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +func TestLoadSuiteFileRejectsPlannerOutputDirectoryInPlaceOfFile(t *testing.T) { |
| 285 | + suitePath, outputDirectory := writePlannerOutputSuite(t, "fixed", plannerStreamingConfigFile) |
| 286 | + if err := os.Mkdir(filepath.Join(outputDirectory, plannerInferenceConfigFile), 0o755); err != nil { |
| 287 | + t.Fatalf("create directory: %v", err) |
| 288 | + } |
| 289 | + if _, err := LoadSuiteFile(suitePath); err == nil { |
| 290 | + t.Fatal("LoadSuiteFile accepted a directory as a planner config file") |
| 291 | + } |
| 292 | +} |
0 commit comments