Skip to content

Commit c0bba1e

Browse files
committed
add tests for error paths
1 parent 75c77bd commit c0bba1e

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

internal/metrics/yaml_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"runtime"
78
"sync"
89
"testing"
910
"time"
@@ -474,3 +475,50 @@ func TestConcurrentPresetUpdates(t *testing.T) {
474475
a.NoError(err)
475476
a.Equal(numGoroutines, len(finalMetrics.PresetDefs), "Some updates were lost due to race condition!")
476477
}
478+
479+
func TestGetMetricsNonExistentPath(t *testing.T) {
480+
nonExistent := filepath.Join(t.TempDir(), "does_not_exist", "metrics.yaml")
481+
fmr, err := metrics.NewYAMLMetricReaderWriter(ctx, nonExistent)
482+
assert.NoError(t, err)
483+
_, err = fmr.GetMetrics()
484+
assert.Error(t, err)
485+
}
486+
487+
func TestGetMetricsDirWithInvalidYAML(t *testing.T) {
488+
dir := t.TempDir()
489+
err := os.WriteFile(filepath.Join(dir, "bad.yaml"), []byte("invalid: yaml: {unclosed"), 0644)
490+
assert.NoError(t, err)
491+
fmr, err := metrics.NewYAMLMetricReaderWriter(ctx, dir)
492+
assert.NoError(t, err)
493+
_, err = fmr.GetMetrics()
494+
assert.Error(t, err)
495+
}
496+
497+
func TestMutationsGetMetricsError(t *testing.T) {
498+
nonExistent := filepath.Join(t.TempDir(), "does_not_exist", "metrics.yaml")
499+
fmr, err := metrics.NewYAMLMetricReaderWriter(ctx, nonExistent)
500+
assert.NoError(t, err)
501+
502+
assert.Error(t, fmr.DeleteMetric("x"))
503+
assert.Error(t, fmr.UpdateMetric("x", metrics.Metric{}))
504+
assert.Error(t, fmr.CreateMetric("x", metrics.Metric{}))
505+
assert.Error(t, fmr.DeletePreset("x"))
506+
assert.Error(t, fmr.UpdatePreset("x", metrics.Preset{}))
507+
assert.Error(t, fmr.CreatePreset("x", metrics.Preset{}))
508+
}
509+
510+
func TestLoadMetricsUnreadableFile(t *testing.T) {
511+
if runtime.GOOS == "windows" {
512+
t.Skip("cannot reliably test file permissions on Windows")
513+
}
514+
if os.Getuid() == 0 {
515+
t.Skip("running as root, permission checks do not apply")
516+
}
517+
f := filepath.Join(t.TempDir(), "metrics.yaml")
518+
err := os.WriteFile(f, []byte(""), 0000)
519+
assert.NoError(t, err)
520+
fmr, err := metrics.NewYAMLMetricReaderWriter(ctx, f)
521+
assert.NoError(t, err)
522+
_, err = fmr.GetMetrics()
523+
assert.Error(t, err)
524+
}

internal/sources/yaml_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,34 @@ func TestConcurrentSourceUpdates(t *testing.T) {
385385
a.NoError(err)
386386
a.Equal(numGoroutines, len(finalSources), "Some updates were lost due to race condition!")
387387
}
388+
389+
func TestGetSourcesDirWithInvalidYAML(t *testing.T) {
390+
dir := t.TempDir()
391+
err := os.WriteFile(filepath.Join(dir, "bad.yaml"), []byte("invalid: yaml: {unclosed"), 0644)
392+
assert.NoError(t, err)
393+
yamlrw, err := sources.NewYAMLSourcesReaderWriter(ctx, dir)
394+
assert.NoError(t, err)
395+
_, err = yamlrw.GetSources()
396+
assert.Error(t, err)
397+
}
398+
399+
func TestCreateSourceGetSourcesError(t *testing.T) {
400+
nonExistent := filepath.Join(t.TempDir(), "does_not_exist", "sources.yaml")
401+
yamlrw, err := sources.NewYAMLSourcesReaderWriter(ctx, nonExistent)
402+
assert.NoError(t, err)
403+
assert.Error(t, yamlrw.CreateSource(sources.Source{Name: "x"}))
404+
}
405+
406+
// TestMutationsWriteError verifies that UpdateSource, DeleteSource, and CreateSource
407+
// propagate write errors when the path is a directory (cannot be overwritten as a file).
408+
func TestMutationsWriteError(t *testing.T) {
409+
// Using a dir as the file path: getSources succeeds (reads empty dir),
410+
// but writeSources fails because os.WriteFile cannot write to a directory.
411+
dir := t.TempDir()
412+
yamlrw, err := sources.NewYAMLSourcesReaderWriter(ctx, dir)
413+
assert.NoError(t, err)
414+
415+
assert.Error(t, yamlrw.UpdateSource(sources.Source{Name: "x"}))
416+
assert.Error(t, yamlrw.DeleteSource("x"))
417+
assert.Error(t, yamlrw.CreateSource(sources.Source{Name: "x"}))
418+
}

0 commit comments

Comments
 (0)