1
0

commit_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "errors"
  9. "testing"
  10. )
  11. type requiresRestart struct{}
  12. func (requiresRestart) VerifyConfiguration(_, _ Configuration) error {
  13. return nil
  14. }
  15. func (requiresRestart) CommitConfiguration(_, _ Configuration) bool {
  16. return false
  17. }
  18. func (requiresRestart) String() string {
  19. return "requiresRestart"
  20. }
  21. type validationError struct{}
  22. func (validationError) VerifyConfiguration(_, _ Configuration) error {
  23. return errors.New("some error")
  24. }
  25. func (validationError) CommitConfiguration(_, _ Configuration) bool {
  26. return true
  27. }
  28. func (validationError) String() string {
  29. return "validationError"
  30. }
  31. func TestReplaceCommit(t *testing.T) {
  32. w := Wrap("/dev/null", Configuration{Version: 0})
  33. if w.Raw().Version != 0 {
  34. t.Fatal("Config incorrect")
  35. }
  36. // Replace config. We should get back a clean response and the config
  37. // should change.
  38. resp := w.Replace(Configuration{Version: 1})
  39. if resp.ValidationError != nil {
  40. t.Fatal("Should not have a validation error")
  41. }
  42. if resp.RequiresRestart {
  43. t.Fatal("Should not require restart")
  44. }
  45. if w.Raw().Version != 1 {
  46. t.Fatal("Config should have changed")
  47. }
  48. // Now with a subscriber requiring restart. We should get a clean response
  49. // but with the restart flag set, and the config should change.
  50. w.Subscribe(requiresRestart{})
  51. resp = w.Replace(Configuration{Version: 2})
  52. if resp.ValidationError != nil {
  53. t.Fatal("Should not have a validation error")
  54. }
  55. if !resp.RequiresRestart {
  56. t.Fatal("Should require restart")
  57. }
  58. if w.Raw().Version != 2 {
  59. t.Fatal("Config should have changed")
  60. }
  61. // Now with a subscriber that throws a validation error. The config should
  62. // not change.
  63. w.Subscribe(validationError{})
  64. resp = w.Replace(Configuration{Version: 3})
  65. if resp.ValidationError == nil {
  66. t.Fatal("Should have a validation error")
  67. }
  68. if resp.RequiresRestart {
  69. t.Fatal("Should not require restart")
  70. }
  71. if w.Raw().Version != 2 {
  72. t.Fatal("Config should not have changed")
  73. }
  74. }