source_file_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package config_test
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/OliveTin/OliveTin/internal/config"
  7. "github.com/knadh/koanf/parsers/yaml"
  8. "github.com/knadh/koanf/providers/file"
  9. "github.com/knadh/koanf/v2"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestAppendSourceStampsActionSourceFiles(t *testing.T) {
  14. dir := t.TempDir()
  15. includeDir := filepath.Join(dir, "config.d")
  16. require.NoError(t, os.Mkdir(includeDir, 0o755))
  17. basePath := filepath.Join(dir, "config.yaml")
  18. require.NoError(t, os.WriteFile(basePath, []byte(`
  19. include: config.d
  20. actions:
  21. - title: From base
  22. shell: echo base
  23. `), 0o644))
  24. includePath := filepath.Join(includeDir, "10-extra.yaml")
  25. require.NoError(t, os.WriteFile(includePath, []byte(`
  26. actions:
  27. - title: From include
  28. shell: echo include
  29. entities:
  30. - name: host
  31. file: hosts.yaml
  32. `), 0o644))
  33. k := koanf.New(".")
  34. require.NoError(t, k.Load(file.Provider(basePath), yaml.Parser()))
  35. cfg := config.DefaultConfig()
  36. config.AppendSource(cfg, k, basePath)
  37. require.Len(t, cfg.Actions, 2)
  38. assert.Equal(t, basePath, cfg.Actions[0].SourceFile)
  39. assert.Equal(t, includePath, cfg.Actions[1].SourceFile)
  40. require.Len(t, cfg.Entities, 1)
  41. assert.Equal(t, includePath, cfg.Entities[0].SourceFile)
  42. }
  43. func TestAppendSourceIgnoresUserProvidedSourceFile(t *testing.T) {
  44. dir := t.TempDir()
  45. basePath := filepath.Join(dir, "config.yaml")
  46. require.NoError(t, os.WriteFile(basePath, []byte(`
  47. actions:
  48. - title: Spoofed
  49. shell: echo hi
  50. x-olivetin-source-file: /tmp/fake-user-path.yaml
  51. entities:
  52. - name: host
  53. file: hosts.yaml
  54. x-olivetin-source-file: /tmp/fake-entity-path.yaml
  55. `), 0o644))
  56. k := koanf.New(".")
  57. require.NoError(t, k.Load(file.Provider(basePath), yaml.Parser()))
  58. cfg := config.DefaultConfig()
  59. config.AppendSource(cfg, k, basePath)
  60. require.Len(t, cfg.Actions, 1)
  61. assert.Equal(t, basePath, cfg.Actions[0].SourceFile)
  62. assert.NotEqual(t, "/tmp/fake-user-path.yaml", cfg.Actions[0].SourceFile)
  63. require.Len(t, cfg.Entities, 1)
  64. assert.Equal(t, basePath, cfg.Entities[0].SourceFile)
  65. assert.NotEqual(t, "/tmp/fake-entity-path.yaml", cfg.Entities[0].SourceFile)
  66. }