1
0

ssh_reload_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package nebula
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/slackhq/nebula/config"
  9. "github.com/slackhq/nebula/test"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. type lineWriter struct {
  14. lines []string
  15. err error
  16. }
  17. func (w *lineWriter) WriteLine(s string) error {
  18. w.lines = append(w.lines, s)
  19. return w.err
  20. }
  21. func (w *lineWriter) Write(s string) error { return w.WriteLine(s) }
  22. func (w *lineWriter) WriteBytes(b []byte) error { return w.WriteLine(string(b)) }
  23. func (w *lineWriter) GetWriter() io.Writer { return io.Discard }
  24. func TestSSHReloadReportsTheResult(t *testing.T) {
  25. dir := t.TempDir()
  26. write := func(body string) {
  27. require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte(body), 0o600))
  28. }
  29. write("a: 1\nb: 1")
  30. l := test.NewLogger()
  31. c := config.NewC(l)
  32. require.NoError(t, c.Load(dir))
  33. fail := false
  34. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  35. r.RestartRequiredIfChanged("b")
  36. if fail {
  37. r.Failed()
  38. }
  39. })
  40. reload := func() []string {
  41. w := &lineWriter{}
  42. require.NoError(t, sshReload(l, c, w))
  43. return w.lines
  44. }
  45. write("a: 2\nb: 1")
  46. assert.Equal(t, []string{"Reloading config", "Config reload complete"}, reload())
  47. fail = true
  48. write("a: 3\nb: 1")
  49. assert.Equal(t, []string{"Reloading config", "Config reload completed with errors, review the logs"}, reload())
  50. fail = false
  51. write("a: 3\nb: 2")
  52. assert.Equal(t, []string{"Reloading config", "Config reload was completed but requires a restart to fully apply, keys [b]"}, reload())
  53. write("a: [")
  54. lines := reload()
  55. require.Len(t, lines, 2)
  56. assert.Contains(t, lines[1], "Config reload failed, the new config did not load")
  57. assert.Contains(t, lines[1], dir, "names the config that did not load")
  58. }
  59. // A client that went away still gets the reload it asked for
  60. func TestSSHReloadReloadsWhenTheClientIsGone(t *testing.T) {
  61. dir := t.TempDir()
  62. require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte("a: 1"), 0o600))
  63. l := test.NewLogger()
  64. c := config.NewC(l)
  65. require.NoError(t, c.Load(dir))
  66. reloaded := false
  67. c.RegisterReloadCallback(func(*config.C, *config.Reload) { reloaded = true })
  68. gone := errors.New("client is gone")
  69. require.ErrorIs(t, sshReload(l, c, &lineWriter{err: gone}), gone)
  70. assert.True(t, reloaded)
  71. }