105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package app_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
appPkg "coopcloud.tech/abra/pkg/app"
|
|
"coopcloud.tech/abra/pkg/test"
|
|
testPkg "coopcloud.tech/abra/pkg/test"
|
|
stack "coopcloud.tech/abra/pkg/upstream/stack"
|
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetTimeoutFromLabel(t *testing.T) {
|
|
test.Setup()
|
|
t.Cleanup(func() { test.Teardown() })
|
|
|
|
tests := []struct {
|
|
configuredTimeout string
|
|
expectedTimeout int
|
|
}{
|
|
{"0", 0},
|
|
{"DOESNTEXIST", 0}, // NOTE(d1): test when missing from .env
|
|
{"80", 80},
|
|
{"120", 120},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
app, err := appPkg.GetApp(expectedAppFiles, testPkg.AppName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if test.configuredTimeout != "DOESNTEXIST" {
|
|
app.Env["TIMEOUT"] = test.configuredTimeout
|
|
}
|
|
|
|
composeFiles, err := app.Recipe.GetComposeFiles(app.Env)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
deployOpts := stack.Deploy{
|
|
Composefiles: composeFiles,
|
|
Namespace: app.StackName(),
|
|
Prune: false,
|
|
ResolveImage: stack.ResolveImageAlways,
|
|
Detach: false,
|
|
}
|
|
|
|
compose, err := appPkg.GetAppComposeConfig(app.Name, deployOpts, app.Env)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
timeout, err := appPkg.GetTimeoutFromLabel(compose, app.StackName())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
assert.Equal(t, timeout, test.expectedTimeout)
|
|
}
|
|
}
|
|
|
|
func TestSetRecipeLabel(t *testing.T) {
|
|
// the app service brings labels along on both levels, the db service
|
|
// has none at all, so the pre-existing and the nil map case are covered
|
|
compose := &composetypes.Config{
|
|
Services: []composetypes.ServiceConfig{
|
|
{
|
|
Name: "app",
|
|
Labels: composetypes.Labels{"example.container.label": "keep me"},
|
|
Deploy: composetypes.DeployConfig{
|
|
Labels: composetypes.Labels{"coop-cloud.backupbot.enabled": "true"},
|
|
},
|
|
},
|
|
{Name: "db"},
|
|
},
|
|
}
|
|
|
|
appPkg.SetRecipeLabel(compose, "test_example_com", "test-recipe")
|
|
|
|
services := make(map[string]composetypes.ServiceConfig)
|
|
for _, service := range compose.Services {
|
|
services[service.Name] = service
|
|
}
|
|
|
|
// the stack scoped label stays on the app service object only
|
|
assert.Equal(t, "test-recipe",
|
|
services["app"].Deploy.Labels["coop-cloud.test_example_com.recipe"])
|
|
assert.NotContains(t, services["db"].Deploy.Labels,
|
|
"coop-cloud.test_example_com.recipe")
|
|
|
|
// the static label reaches every container of the stack
|
|
for name, service := range services {
|
|
assert.Equal(t, "test-recipe", service.Labels["coop-cloud.recipe"], name)
|
|
assert.Equal(t, "test-recipe", service.Deploy.Labels["coop-cloud.recipe"], name)
|
|
}
|
|
|
|
// labels the recipe brought along are left alone
|
|
assert.Equal(t, "keep me", services["app"].Labels["example.container.label"])
|
|
assert.Equal(t, "true", services["app"].Deploy.Labels["coop-cloud.backupbot.enabled"])
|
|
}
|