2020-04-13 17:22:00 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/watcher"
|
2020-04-13 17:22:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockNotifier struct {
|
|
|
|
configs []Root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *mockNotifier) ConfigDidUpdate(c Root) {
|
|
|
|
n.configs = append(n.configs, c)
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:30:50 +00:00
|
|
|
type mockFileWatcher struct {
|
|
|
|
path string
|
|
|
|
notifier watcher.Notification
|
|
|
|
ready chan struct{}
|
|
|
|
}
|
2020-04-13 17:22:00 +00:00
|
|
|
|
2020-05-01 15:30:50 +00:00
|
|
|
func (w *mockFileWatcher) Start(n watcher.Notification) {
|
|
|
|
w.notifier = n
|
|
|
|
w.ready <- struct{}{}
|
|
|
|
}
|
2020-04-13 17:22:00 +00:00
|
|
|
|
2020-05-01 15:30:50 +00:00
|
|
|
func (w *mockFileWatcher) Add(string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockFileWatcher) Shutdown() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockFileWatcher) TriggerChange() {
|
|
|
|
w.notifier.WatcherItemDidChange(w.path)
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigChanged(t *testing.T) {
|
|
|
|
filePath := "config.yaml"
|
|
|
|
f, err := os.Create(filePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() {
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = f.Close()
|
|
|
|
_ = os.Remove(filePath)
|
2020-04-13 17:22:00 +00:00
|
|
|
}()
|
|
|
|
c := &Root{
|
|
|
|
Forwarders: []Forwarder{
|
|
|
|
{
|
|
|
|
URL: "test.daltoniam.com",
|
|
|
|
Listener: "127.0.0.1:8080",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
configRead := func(configPath string, log *zerolog.Logger) (Root, error) {
|
2020-05-01 15:30:50 +00:00
|
|
|
return *c, nil
|
|
|
|
}
|
|
|
|
wait := make(chan struct{})
|
|
|
|
w := &mockFileWatcher{path: filePath, ready: wait}
|
2020-04-13 17:22:00 +00:00
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
log := zerolog.Nop()
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
service, err := NewFileManager(w, filePath, &log)
|
2020-05-01 15:30:50 +00:00
|
|
|
service.ReadConfig = configRead
|
2020-04-13 17:22:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
n := &mockNotifier{}
|
|
|
|
go service.Start(n)
|
|
|
|
|
2020-05-01 15:30:50 +00:00
|
|
|
<-wait
|
2020-04-13 17:22:00 +00:00
|
|
|
c.Forwarders = append(c.Forwarders, Forwarder{URL: "add.daltoniam.com", Listener: "127.0.0.1:8081"})
|
2020-05-01 15:30:50 +00:00
|
|
|
w.TriggerChange()
|
2020-04-13 17:22:00 +00:00
|
|
|
|
|
|
|
service.Shutdown()
|
|
|
|
|
|
|
|
assert.Len(t, n.configs, 2, "did not get 2 config updates as expected")
|
|
|
|
assert.Len(t, n.configs[0].Forwarders, 1, "not the amount of forwarders expected")
|
|
|
|
assert.Len(t, n.configs[1].Forwarders, 2, "not the amount of forwarders expected")
|
|
|
|
|
|
|
|
assert.Equal(t, n.configs[0].Forwarders[0].Hash(), c.Forwarders[0].Hash(), "forwarder hashes don't match")
|
|
|
|
assert.Equal(t, n.configs[1].Forwarders[0].Hash(), c.Forwarders[0].Hash(), "forwarder hashes don't match")
|
|
|
|
assert.Equal(t, n.configs[1].Forwarders[1].Hash(), c.Forwarders[1].Hash(), "forwarder hashes don't match")
|
|
|
|
}
|