commit_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package config
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. type requiresRestart struct{}
  7. func (requiresRestart) VerifyConfiguration(_, _ Configuration) error {
  8. return nil
  9. }
  10. func (requiresRestart) CommitConfiguration(_, _ Configuration) bool {
  11. return false
  12. }
  13. func (requiresRestart) String() string {
  14. return "requiresRestart"
  15. }
  16. type validationError struct{}
  17. func (validationError) VerifyConfiguration(_, _ Configuration) error {
  18. return errors.New("some error")
  19. }
  20. func (validationError) CommitConfiguration(_, _ Configuration) bool {
  21. return true
  22. }
  23. func (validationError) String() string {
  24. return "validationError"
  25. }
  26. func TestReplaceCommit(t *testing.T) {
  27. w := Wrap("/dev/null", Configuration{Version: 0})
  28. if w.Raw().Version != 0 {
  29. t.Fatal("Config incorrect")
  30. }
  31. // Replace config. We should get back a clean response and the config
  32. // should change.
  33. resp := w.Replace(Configuration{Version: 1})
  34. if resp.ValidationError != nil {
  35. t.Fatal("Should not have a validation error")
  36. }
  37. if resp.RequiresRestart {
  38. t.Fatal("Should not require restart")
  39. }
  40. if w.Raw().Version != 1 {
  41. t.Fatal("Config should have changed")
  42. }
  43. // Now with a subscriber requiring restart. We should get a clean response
  44. // but with the restart flag set, and the config should change.
  45. w.Subscribe(requiresRestart{})
  46. resp = w.Replace(Configuration{Version: 2})
  47. if resp.ValidationError != nil {
  48. t.Fatal("Should not have a validation error")
  49. }
  50. if !resp.RequiresRestart {
  51. t.Fatal("Should require restart")
  52. }
  53. if w.Raw().Version != 2 {
  54. t.Fatal("Config should have changed")
  55. }
  56. // Now with a subscriber that throws a validation error. The config should
  57. // not change.
  58. w.Subscribe(validationError{})
  59. resp = w.Replace(Configuration{Version: 3})
  60. if resp.ValidationError == nil {
  61. t.Fatal("Should have a validation error")
  62. }
  63. if resp.RequiresRestart {
  64. t.Fatal("Should not require restart")
  65. }
  66. if w.Raw().Version != 2 {
  67. t.Fatal("Config should not have changed")
  68. }
  69. }