-
Notifications
You must be signed in to change notification settings - Fork 1
/
initializer_test.go
118 lines (91 loc) · 3.38 KB
/
initializer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package config
import (
"net/http"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/getlantern/eventual"
"github.com/getlantern/flashlight/v7/common"
)
// TestInit tests initializing configs.
func TestInit(t *testing.T) {
t.Skip("flaky on CI")
defer deleteGlobalConfig()
flags := make(map[string]interface{})
flags["staging"] = true
gotProxies := eventual.NewValue()
gotGlobal := eventual.NewValue()
// Note these dispatch functions will receive multiple configs -- local ones,
// embedded ones, and remote ones.
globalDispatch := func(cfg interface{}, src Source) {
global := cfg.(*Global)
assert.True(t, len(global.Client.MasqueradeSets) > 1)
gotGlobal.Set(true)
}
stop := Init(
".", flags, newTestUserConfig(), globalDispatch, nil, &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
// the same token should also be configured on staging
// config-server, staging proxies and staging DDF distributions.
req.Header.Add(common.CfgSvrAuthTokenHeader, "staging-token")
return nil, nil
},
})
defer stop()
_, valid := gotProxies.Get(time.Second * 12)
assert.True(t, valid, "Should have got proxies config in a reasonable time")
_, valid = gotGlobal.Get(time.Second * 12)
assert.True(t, valid, "Should have got global config in a reasonable time")
}
// TestInitWithURLs tests that proxy and global configs are fetched at the
// correct polling intervals.
func TestInitWithURLs(t *testing.T) {
withTempDir(t, func(inTempDir func(string) string) {
globalConfig := newGlobalConfig(t)
globalConfig.GlobalConfigPollInterval = 3 * time.Second
globalConfig.ProxyConfigPollInterval = 1 * time.Second
// ensure a `global.yaml` exists in order to avoid fetching embedded config
writeObfuscatedConfig(t, globalConfig, inTempDir("global.yaml"))
// set up 2 servers:
// 1. one that serves up the global config and
// 2. one that serves up the proxy config
// each should track the number of requests made to it
// set up servers to serve global config and count number of requests
globalConfigURL, globalReqCount := startConfigServer(t, globalConfig)
// set up and call InitWithURLs
flags := make(map[string]interface{})
flags["staging"] = true
globalDispatch := func(interface{}, Source) {}
stop := InitWithURLs(
inTempDir("."), flags, newTestUserConfig(),
globalDispatch, nil,
globalConfigURL, &http.Transport{})
defer stop()
// sleep some amount
time.Sleep(7 * time.Second)
// in 7 sec, should have made:
// 1 + (7 / 3) = 3 global requests
// 1 + (7 / 1) = 8 proxy requests
// We provide a little leeway in the checks below to account for possible delays in CI.
// test that proxy & config servers were called the correct number of times
assert.GreaterOrEqual(t, 3, int(globalReqCount()), "should have fetched global config every %v", globalConfig.GlobalConfigPollInterval)
})
}
func TestStaging(t *testing.T) {
flags := make(map[string]interface{})
flags["staging"] = true
assert.True(t, isStaging(flags))
flags["staging"] = false
assert.False(t, isStaging(flags))
}
// TestOverrides tests url override flags
func TestOverrides(t *testing.T) {
url := "host"
flags := make(map[string]interface{})
out := checkOverrides(flags, url, "name")
assert.Equal(t, "host", out)
flags["cloudconfig"] = "test"
out = checkOverrides(flags, url, "name")
assert.Equal(t, "test/name", out)
}