2015-06-11 06:46:57 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-06-11 06:46:57 +00:00
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-07-04 20:32:34 +00:00
|
|
|
type requiresRestart struct {
|
|
|
|
committed chan struct{}
|
|
|
|
}
|
2015-06-03 07:47:39 +00:00
|
|
|
|
|
|
|
func (requiresRestart) VerifyConfiguration(_, _ Configuration) error {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-04 20:32:34 +00:00
|
|
|
func (c requiresRestart) CommitConfiguration(_, _ Configuration) bool {
|
|
|
|
select {
|
|
|
|
case c.committed <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2015-06-03 07:47:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (requiresRestart) String() string {
|
|
|
|
return "requiresRestart"
|
|
|
|
}
|
|
|
|
|
|
|
|
type validationError struct{}
|
|
|
|
|
|
|
|
func (validationError) VerifyConfiguration(_, _ Configuration) error {
|
|
|
|
return errors.New("some error")
|
|
|
|
}
|
2016-07-04 20:32:34 +00:00
|
|
|
func (c validationError) CommitConfiguration(_, _ Configuration) bool {
|
2015-06-03 07:47:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
func (validationError) String() string {
|
|
|
|
return "validationError"
|
|
|
|
}
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
func replace(t testing.TB, w Wrapper, to Configuration) {
|
|
|
|
t.Helper()
|
|
|
|
waiter, err := w.Modify(func(cfg *Configuration) {
|
|
|
|
*cfg = to
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
waiter.Wait()
|
|
|
|
}
|
2017-01-01 12:27:18 +00:00
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
func TestReplaceCommit(t *testing.T) {
|
2020-11-20 13:21:54 +00:00
|
|
|
w := wrap("/dev/null", Configuration{Version: 0}, device1)
|
2021-01-15 14:43:34 +00:00
|
|
|
defer w.stop()
|
2016-11-12 09:34:18 +00:00
|
|
|
if w.RawCopy().Version != 0 {
|
2015-06-03 07:47:39 +00:00
|
|
|
t.Fatal("Config incorrect")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace config. We should get back a clean response and the config
|
|
|
|
// should change.
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
replace(t, w, Configuration{Version: 1})
|
2016-07-04 20:32:34 +00:00
|
|
|
if w.RequiresRestart() {
|
2015-06-03 07:47:39 +00:00
|
|
|
t.Fatal("Should not require restart")
|
|
|
|
}
|
2016-11-12 09:34:18 +00:00
|
|
|
if w.RawCopy().Version != CurrentVersion {
|
2015-06-03 07:47:39 +00:00
|
|
|
t.Fatal("Config should have changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now with a subscriber requiring restart. We should get a clean response
|
|
|
|
// but with the restart flag set, and the config should change.
|
|
|
|
|
2016-07-04 20:32:34 +00:00
|
|
|
sub0 := requiresRestart{committed: make(chan struct{}, 1)}
|
|
|
|
w.Subscribe(sub0)
|
2015-06-03 07:47:39 +00:00
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
replace(t, w, Configuration{Version: 1})
|
2016-07-04 20:32:34 +00:00
|
|
|
<-sub0.committed
|
|
|
|
if !w.RequiresRestart() {
|
2015-06-03 07:47:39 +00:00
|
|
|
t.Fatal("Should require restart")
|
|
|
|
}
|
2016-11-12 09:34:18 +00:00
|
|
|
if w.RawCopy().Version != CurrentVersion {
|
2015-06-03 07:47:39 +00:00
|
|
|
t.Fatal("Config should have changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now with a subscriber that throws a validation error. The config should
|
|
|
|
// not change.
|
|
|
|
|
|
|
|
w.Subscribe(validationError{})
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
_, err := w.Modify(func(cfg *Configuration) {
|
|
|
|
*cfg = Configuration{Version: 3}
|
|
|
|
})
|
2016-07-04 20:32:34 +00:00
|
|
|
if err == nil {
|
2015-06-03 07:47:39 +00:00
|
|
|
t.Fatal("Should have a validation error")
|
|
|
|
}
|
2016-07-04 20:32:34 +00:00
|
|
|
if !w.RequiresRestart() {
|
|
|
|
t.Fatal("Should still require restart")
|
2015-06-03 07:47:39 +00:00
|
|
|
}
|
2016-11-12 09:34:18 +00:00
|
|
|
if w.RawCopy().Version != CurrentVersion {
|
2015-06-03 07:47:39 +00:00
|
|
|
t.Fatal("Config should not have changed")
|
|
|
|
}
|
|
|
|
}
|