|
|
|
@ -1,20 +1,18 @@
|
|
|
|
|
package flags
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestEnvConfig_Defaults(t *testing.T) {
|
|
|
|
|
// Unset testing environments own variables, since those are not what is under test
|
|
|
|
|
os.Unsetenv("DOCKER_TLS_VERIFY")
|
|
|
|
|
os.Unsetenv("DOCKER_HOST")
|
|
|
|
|
_ = os.Unsetenv("DOCKER_TLS_VERIFY")
|
|
|
|
|
_ = os.Unsetenv("DOCKER_HOST")
|
|
|
|
|
|
|
|
|
|
cmd := new(cobra.Command)
|
|
|
|
|
SetDefaults()
|
|
|
|
@ -48,10 +46,7 @@ func TestEnvConfig_Custom(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestGetSecretsFromFilesWithString(t *testing.T) {
|
|
|
|
|
value := "supersecretstring"
|
|
|
|
|
|
|
|
|
|
err := os.Setenv("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD", value)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer os.Unsetenv("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD")
|
|
|
|
|
t.Setenv("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD", value)
|
|
|
|
|
|
|
|
|
|
testGetSecretsFromFiles(t, "notification-email-server-password", value)
|
|
|
|
|
}
|
|
|
|
@ -60,18 +55,15 @@ func TestGetSecretsFromFilesWithFile(t *testing.T) {
|
|
|
|
|
value := "megasecretstring"
|
|
|
|
|
|
|
|
|
|
// Create the temporary file which will contain a secret.
|
|
|
|
|
file, err := ioutil.TempFile(os.TempDir(), "watchtower-")
|
|
|
|
|
file, err := os.CreateTemp(t.TempDir(), "watchtower-")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer os.Remove(file.Name()) // Make sure to remove the temporary file later.
|
|
|
|
|
|
|
|
|
|
// Write the secret to the temporary file.
|
|
|
|
|
secret := []byte(value)
|
|
|
|
|
_, err = file.Write(secret)
|
|
|
|
|
_, err = file.Write([]byte(value))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NoError(t, file.Close())
|
|
|
|
|
|
|
|
|
|
err = os.Setenv("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD", file.Name())
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer os.Unsetenv("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD")
|
|
|
|
|
t.Setenv("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD", file.Name())
|
|
|
|
|
|
|
|
|
|
testGetSecretsFromFiles(t, "notification-email-server-password", value)
|
|
|
|
|
}
|
|
|
|
@ -80,16 +72,15 @@ func TestGetSliceSecretsFromFiles(t *testing.T) {
|
|
|
|
|
values := []string{"entry2", "", "entry3"}
|
|
|
|
|
|
|
|
|
|
// Create the temporary file which will contain a secret.
|
|
|
|
|
file, err := ioutil.TempFile(os.TempDir(), "watchtower-")
|
|
|
|
|
file, err := os.CreateTemp(t.TempDir(), "watchtower-")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer os.Remove(file.Name()) // Make sure to remove the temporary file later.
|
|
|
|
|
|
|
|
|
|
// Write the secret to the temporary file.
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
_, err = file.WriteString("\n" + value)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
file.Close()
|
|
|
|
|
require.NoError(t, file.Close())
|
|
|
|
|
|
|
|
|
|
testGetSecretsFromFiles(t, "notification-url", `[entry1,entry2,entry3]`,
|
|
|
|
|
`--notification-url`, "entry1",
|
|
|
|
@ -166,9 +157,7 @@ func TestProcessFlagAliases(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestProcessFlagAliasesLogLevelFromEnvironment(t *testing.T) {
|
|
|
|
|
cmd := new(cobra.Command)
|
|
|
|
|
err := os.Setenv("WATCHTOWER_DEBUG", `true`)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer os.Unsetenv("WATCHTOWER_DEBUG")
|
|
|
|
|
t.Setenv("WATCHTOWER_DEBUG", `true`)
|
|
|
|
|
|
|
|
|
|
SetDefaults()
|
|
|
|
|
RegisterDockerFlags(cmd)
|
|
|
|
@ -202,9 +191,7 @@ func TestProcessFlagAliasesSchedAndInterval(t *testing.T) {
|
|
|
|
|
func TestProcessFlagAliasesScheduleFromEnvironment(t *testing.T) {
|
|
|
|
|
cmd := new(cobra.Command)
|
|
|
|
|
|
|
|
|
|
err := os.Setenv("WATCHTOWER_SCHEDULE", `@hourly`)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer os.Unsetenv("WATCHTOWER_SCHEDULE")
|
|
|
|
|
t.Setenv("WATCHTOWER_SCHEDULE", `@hourly`)
|
|
|
|
|
|
|
|
|
|
SetDefaults()
|
|
|
|
|
RegisterDockerFlags(cmd)
|
|
|
|
|