|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/docker/docker-agent/pkg/paths" |
| 12 | +) |
| 13 | + |
| 14 | +func writeHookDropIn(t *testing.T, dir, name, content string) { |
| 15 | + t.Helper() |
| 16 | + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(content), 0o600)) |
| 17 | +} |
| 18 | + |
| 19 | +func TestLoadHookDropIns_MissingDir(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + assert.Nil(t, loadHookDropIns(filepath.Join(t.TempDir(), "hooks.d"))) |
| 22 | +} |
| 23 | + |
| 24 | +func TestLoadHookDropIns_LexicographicOrder(t *testing.T) { |
| 25 | + t.Parallel() |
| 26 | + dir := t.TempDir() |
| 27 | + writeHookDropIn(t, dir, "20-second.yaml", "stop:\n - type: command\n command: echo second\n") |
| 28 | + writeHookDropIn(t, dir, "10-first.yaml", "stop:\n - type: command\n command: echo first\n") |
| 29 | + |
| 30 | + hooks := loadHookDropIns(dir) |
| 31 | + require.NotNil(t, hooks) |
| 32 | + require.Len(t, hooks.Stop, 2) |
| 33 | + assert.Equal(t, "echo first", hooks.Stop[0].Command) |
| 34 | + assert.Equal(t, "echo second", hooks.Stop[1].Command) |
| 35 | +} |
| 36 | + |
| 37 | +func TestLoadHookDropIns_MergesAcrossEvents(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + dir := t.TempDir() |
| 40 | + writeHookDropIn(t, dir, "a.yaml", ` |
| 41 | +session_start: |
| 42 | + - type: command |
| 43 | + command: echo start |
| 44 | +pre_tool_use: |
| 45 | + - matcher: shell |
| 46 | + hooks: |
| 47 | + - type: command |
| 48 | + command: audit.sh |
| 49 | +`) |
| 50 | + writeHookDropIn(t, dir, "b.yml", "stop:\n - type: command\n command: echo stop\n") |
| 51 | + |
| 52 | + hooks := loadHookDropIns(dir) |
| 53 | + require.NotNil(t, hooks) |
| 54 | + require.Len(t, hooks.SessionStart, 1) |
| 55 | + assert.Equal(t, "echo start", hooks.SessionStart[0].Command) |
| 56 | + require.Len(t, hooks.PreToolUse, 1) |
| 57 | + assert.Equal(t, "shell", hooks.PreToolUse[0].Matcher) |
| 58 | + require.Len(t, hooks.Stop, 1) |
| 59 | + assert.Equal(t, "echo stop", hooks.Stop[0].Command) |
| 60 | +} |
| 61 | + |
| 62 | +func TestLoadHookDropIns_SkipsMalformedFiles(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + dir := t.TempDir() |
| 65 | + writeHookDropIn(t, dir, "10-broken-yaml.yaml", "stop: [unclosed\n") |
| 66 | + writeHookDropIn(t, dir, "20-unknown-key.yaml", "not_a_hook_event:\n - type: command\n command: echo hi\n") |
| 67 | + writeHookDropIn(t, dir, "30-invalid-hook-type.yaml", "stop:\n - type: teleport\n command: echo hi\n") |
| 68 | + writeHookDropIn(t, dir, "40-valid.yaml", "stop:\n - type: command\n command: echo ok\n") |
| 69 | + |
| 70 | + hooks := loadHookDropIns(dir) |
| 71 | + require.NotNil(t, hooks) |
| 72 | + require.Len(t, hooks.Stop, 1) |
| 73 | + assert.Equal(t, "echo ok", hooks.Stop[0].Command) |
| 74 | +} |
| 75 | + |
| 76 | +func TestLoadHookDropIns_IgnoresNonYAMLAndSubdirs(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + dir := t.TempDir() |
| 79 | + writeHookDropIn(t, dir, "README.md", "# not yaml") |
| 80 | + writeHookDropIn(t, dir, "50-disabled.yaml.bak", "stop:\n - type: command\n command: echo no\n") |
| 81 | + require.NoError(t, os.MkdirAll(filepath.Join(dir, "sub.yaml"), 0o700)) |
| 82 | + writeHookDropIn(t, filepath.Join(dir, "sub.yaml"), "inner.yaml", "stop:\n - type: command\n command: echo nested\n") |
| 83 | + |
| 84 | + assert.Nil(t, loadHookDropIns(dir)) |
| 85 | +} |
| 86 | + |
| 87 | +func TestLoadHookDropIns_EmptyAndCommentOnlyFiles(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + dir := t.TempDir() |
| 90 | + writeHookDropIn(t, dir, "a.yaml", "") |
| 91 | + writeHookDropIn(t, dir, "b.yaml", "# comment only\n") |
| 92 | + |
| 93 | + assert.Nil(t, loadHookDropIns(dir)) |
| 94 | +} |
| 95 | + |
| 96 | +func TestLoadHookDropIns_UsesConfigDir(t *testing.T) { |
| 97 | + // Not parallel: overrides the process-global config dir. |
| 98 | + configDir := t.TempDir() |
| 99 | + paths.SetConfigDir(configDir) |
| 100 | + t.Cleanup(func() { paths.SetConfigDir("") }) |
| 101 | + |
| 102 | + hooksDir := filepath.Join(configDir, "hooks.d") |
| 103 | + require.NoError(t, os.MkdirAll(hooksDir, 0o700)) |
| 104 | + writeHookDropIn(t, hooksDir, "a.yaml", "stop:\n - type: command\n command: echo hi\n") |
| 105 | + |
| 106 | + hooks := LoadHookDropIns() |
| 107 | + require.NotNil(t, hooks) |
| 108 | + require.Len(t, hooks.Stop, 1) |
| 109 | + assert.Equal(t, "echo hi", hooks.Stop[0].Command) |
| 110 | +} |
0 commit comments