Update tests
This commit is contained in:
1 parent
dec1f07266
commit
982693d8f5
2 files changed
+85
-9
No files matched your search
@@ -55,10 +55,17 @@ listener "tcp" {
|
||||
|
||||
devWorkerExtraConfig = `
|
||||
listener "tcp" {
|
||||
purpose = "worker-alpn-tls"
|
||||
tls_disable = true
|
||||
proxy_protocol_behavior = "allow_authorized"
|
||||
proxy_protocol_authorized_addrs = "127.0.0.1"
|
||||
}
|
||||
|
||||
worker {
|
||||
name = "dev-worker"
|
||||
description = "A default worker created in dev mode"
|
||||
controllers = ["127.0.0.1"]
|
||||
}
|
||||
`
|
||||
)
|
||||
|
||||
@@ -66,9 +73,16 @@ listener "tcp" {
|
||||
type Config struct {
|
||||
*configutil.SharedConfig `hcl:"-"`
|
||||
|
||||
DevController bool `hcl:"-"`
|
||||
DefaultOrgId string `hcl:"default_org_id"`
|
||||
PassthroughDirectory string `hcl:"-"`
|
||||
DevController bool `hcl:"-"`
|
||||
DefaultOrgId string `hcl:"default_org_id"`
|
||||
PassthroughDirectory string `hcl:"-"`
|
||||
Worker *Worker `hcl:"worker"`
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
Name string `hcl:"name"`
|
||||
Description string `hcl:"description"`
|
||||
Controllers []string `hcl:"controllers"`
|
||||
}
|
||||
|
||||
// DevWorker is a Config that is used for dev mode of Watchtower
|
||||
@@ -81,22 +95,27 @@ func DevWorker() (*Config, error) {
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
// DevController is a Config that is used for dev mode of Watchtower
|
||||
// controllers
|
||||
func DevController() (*Config, error) {
|
||||
func devKeyGeneration() (string, string) {
|
||||
randBuf := new(bytes.Buffer)
|
||||
n, err := randBuf.ReadFrom(&io.LimitedReader{
|
||||
R: rand.Reader,
|
||||
N: 64,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
panic(err)
|
||||
}
|
||||
if n != 64 {
|
||||
return nil, fmt.Errorf("expected to read 64 bytes, read %d", n)
|
||||
panic(fmt.Errorf("expected to read 64 bytes, read %d", n))
|
||||
}
|
||||
controllerKey := base64.StdEncoding.EncodeToString(randBuf.Bytes()[0:32])
|
||||
workerAuthKey := base64.StdEncoding.EncodeToString(randBuf.Bytes()[32:64])
|
||||
return controllerKey, workerAuthKey
|
||||
}
|
||||
|
||||
// DevController is a Config that is used for dev mode of Watchtower
|
||||
// controllers
|
||||
func DevController() (*Config, error) {
|
||||
controllerKey, workerAuthKey := devKeyGeneration()
|
||||
|
||||
hclStr := fmt.Sprintf(devConfig+devControllerExtraConfig, controllerKey, workerAuthKey)
|
||||
parsed, err := Parse(hclStr)
|
||||
@@ -107,6 +126,17 @@ func DevController() (*Config, error) {
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func DevCombined() (*Config, error) {
|
||||
controllerKey, workerAuthKey := devKeyGeneration()
|
||||
hclStr := fmt.Sprintf(devConfig+devControllerExtraConfig+devWorkerExtraConfig, controllerKey, workerAuthKey)
|
||||
parsed, err := Parse(hclStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing dev config: %w", err)
|
||||
}
|
||||
parsed.DevController = true
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func New() *Config {
|
||||
return &Config{
|
||||
SharedConfig: new(configutil.SharedConfig),
|
||||
|
||||
@@ -64,7 +64,9 @@ func TestDevController(t *testing.T) {
|
||||
},
|
||||
Telemetry: &configutil.Telemetry{
|
||||
DisableHostname: true,
|
||||
PrometheusRetentionTime: time.Hour * 24,
|
||||
PrometheusRetentionTime: 24 * time.Hour,
|
||||
UsageGaugePeriod: 10 * time.Minute,
|
||||
MaximumGaugeCardinality: 500,
|
||||
},
|
||||
},
|
||||
DevController: true,
|
||||
@@ -78,6 +80,50 @@ func TestDevController(t *testing.T) {
|
||||
assert.Equal(t, exp, actual)
|
||||
}
|
||||
|
||||
func TestDevWorker(t *testing.T) {
|
||||
actual, err := DevWorker()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addr, err := sockaddr.NewIPAddr("127.0.0.1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
exp := &Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
DisableMlock: true,
|
||||
Listeners: []*configutil.Listener{
|
||||
{
|
||||
Type: "tcp",
|
||||
TLSDisable: true,
|
||||
Purpose: []string{"worker-alpn-tls"},
|
||||
ProxyProtocolBehavior: "allow_authorized",
|
||||
ProxyProtocolAuthorizedAddrs: []*sockaddr.SockAddrMarshaler{
|
||||
{SockAddr: addr},
|
||||
},
|
||||
},
|
||||
},
|
||||
Telemetry: &configutil.Telemetry{
|
||||
DisableHostname: true,
|
||||
PrometheusRetentionTime: 24 * time.Hour,
|
||||
UsageGaugePeriod: 10 * time.Minute,
|
||||
MaximumGaugeCardinality: 500,
|
||||
},
|
||||
},
|
||||
Worker: &Worker{
|
||||
Name: "dev-worker",
|
||||
Description: "A default worker created in dev mode",
|
||||
Controllers: []string{"127.0.0.1"},
|
||||
},
|
||||
}
|
||||
|
||||
exp.Listeners[0].RawConfig = actual.Listeners[0].RawConfig
|
||||
|
||||
assert.Equal(t, exp, actual)
|
||||
}
|
||||
|
||||
func TestConfigDecrypt(t *testing.T) {
|
||||
|
||||
const (
|
||||
|
||||
Reference in new issue
Block a user