1
0

ssh_reload_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package nebula
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/slackhq/nebula/config"
  7. "github.com/slackhq/nebula/test"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestReloadCommandReportsTheResult(t *testing.T) {
  12. dir := t.TempDir()
  13. write := func(body string) {
  14. require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte(body), 0o600))
  15. }
  16. write("a: 1\nb: 1")
  17. l := test.NewLogger()
  18. c := config.NewC(l)
  19. require.NoError(t, c.Load(dir))
  20. fail := false
  21. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  22. r.RestartRequiredIfChanged("b")
  23. if fail {
  24. r.Failed()
  25. }
  26. })
  27. // reload reports what happened. A reload that failed is a refusal, exit 1 over ssh;
  28. // one that merely needs a restart is not.
  29. reload := func(failed bool) string {
  30. res := runReload(l, c)
  31. assert.Equal(t, !failed, res.OK)
  32. return res.Reason
  33. }
  34. write("a: 2\nb: 1")
  35. assert.Equal(t, "Config reload complete", reload(false))
  36. fail = true
  37. write("a: 3\nb: 1")
  38. assert.Equal(t, "Config reload completed with errors, review the logs", reload(true))
  39. fail = false
  40. write("a: 3\nb: 2")
  41. assert.Equal(t, "Config reload was completed but requires a restart to fully apply, keys [b]", reload(false))
  42. write("a: [")
  43. reason := reload(true)
  44. assert.Contains(t, reason, "Config reload failed, the new config did not load")
  45. assert.Contains(t, reason, dir, "names the config that did not load")
  46. }