feat(agent): UPDATE_TIMEOUT and DOCKER_CONFIG settings (#7)
This commit is contained in:
@@ -2,7 +2,9 @@ package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -16,6 +18,8 @@ func setValid(t *testing.T) {
|
||||
t.Setenv("AGENT_TOKEN", "pua_abc")
|
||||
t.Setenv("SELF_ID", "")
|
||||
t.Setenv("LOG_LEVEL", "")
|
||||
t.Setenv("UPDATE_TIMEOUT", "")
|
||||
t.Setenv("DOCKER_CONFIG", "")
|
||||
}
|
||||
|
||||
func TestLoad_Valid(t *testing.T) {
|
||||
@@ -25,7 +29,7 @@ func TestLoad_Valid(t *testing.T) {
|
||||
host, _ := os.Hostname()
|
||||
assert.Equal(t, &Config{
|
||||
MasterURL: "wss://10.0.0.5:8090/agent", MasterKey: validKey, Token: "pua_abc",
|
||||
SelfID: host, LogLevel: "info",
|
||||
SelfID: host, LogLevel: "info", UpdateTimeout: 5 * time.Minute,
|
||||
}, cfg)
|
||||
}
|
||||
|
||||
@@ -48,3 +52,45 @@ func TestLoad_Invalid(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_JobSettings(t *testing.T) {
|
||||
withConfig := func(t *testing.T, body string) string {
|
||||
dir := t.TempDir()
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.json"), []byte(body), 0o600))
|
||||
return dir
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
timeout string
|
||||
dockerCfg func(t *testing.T) string
|
||||
wantTime time.Duration
|
||||
wantErr string
|
||||
}{
|
||||
{name: "defaults", wantTime: 5 * time.Minute},
|
||||
{name: "custom timeout", timeout: "90s", wantTime: 90 * time.Second},
|
||||
{name: "zero timeout", timeout: "0", wantErr: "UPDATE_TIMEOUT"},
|
||||
{name: "garbage timeout", timeout: "abc", wantErr: "UPDATE_TIMEOUT"},
|
||||
{name: "docker config without file", dockerCfg: func(t *testing.T) string { return t.TempDir() }, wantErr: "DOCKER_CONFIG"},
|
||||
{name: "docker config not json", dockerCfg: func(t *testing.T) string { return withConfig(t, "nope") }, wantErr: "not valid JSON"},
|
||||
{name: "docker config ok", dockerCfg: func(t *testing.T) string { return withConfig(t, `{"auths":{}}`) }, wantTime: 5 * time.Minute},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
setValid(t)
|
||||
t.Setenv("UPDATE_TIMEOUT", c.timeout)
|
||||
dir := ""
|
||||
if c.dockerCfg != nil {
|
||||
dir = c.dockerCfg(t)
|
||||
t.Setenv("DOCKER_CONFIG", dir)
|
||||
}
|
||||
cfg, err := Load()
|
||||
if c.wantErr != "" {
|
||||
assert.ErrorContains(t, err, c.wantErr)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, c.wantTime, cfg.UpdateTimeout)
|
||||
assert.Equal(t, dir, cfg.DockerConfig)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user