1
0

ssh_reload_test.go 1.5 KB

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