198 lines
4.2 KiB
Go
198 lines
4.2 KiB
Go
package config_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"coopcloud.tech/abra/pkg/config"
|
|
"coopcloud.tech/abra/pkg/recipe"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewApp(t *testing.T) {
|
|
app, err := config.NewApp(ExpectedAppEnv, AppName, ExpectedAppFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(app, ExpectedApp) {
|
|
t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, ExpectedApp)
|
|
}
|
|
}
|
|
|
|
func TestReadAppEnvFile(t *testing.T) {
|
|
app, err := config.ReadAppEnvFile(ExpectedAppFile, AppName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(app, ExpectedApp) {
|
|
t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, ExpectedApp)
|
|
}
|
|
}
|
|
|
|
func TestGetApp(t *testing.T) {
|
|
app, err := config.GetApp(ExpectedAppFiles, AppName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(app, ExpectedApp) {
|
|
t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, ExpectedApp)
|
|
}
|
|
}
|
|
|
|
func TestGetComposeFiles(t *testing.T) {
|
|
offline := true
|
|
r, err := recipe.Get("abra-test-recipe", offline)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tests := []struct {
|
|
appEnv map[string]string
|
|
composeFiles []string
|
|
}{
|
|
{
|
|
map[string]string{},
|
|
[]string{
|
|
fmt.Sprintf("%s/%s/compose.yml", config.RECIPES_DIR, r.Name),
|
|
},
|
|
},
|
|
{
|
|
map[string]string{"COMPOSE_FILE": "compose.yml"},
|
|
[]string{
|
|
fmt.Sprintf("%s/%s/compose.yml", config.RECIPES_DIR, r.Name),
|
|
},
|
|
},
|
|
{
|
|
map[string]string{"COMPOSE_FILE": "compose.extra_secret.yml"},
|
|
[]string{
|
|
fmt.Sprintf("%s/%s/compose.extra_secret.yml", config.RECIPES_DIR, r.Name),
|
|
},
|
|
},
|
|
{
|
|
map[string]string{"COMPOSE_FILE": "compose.yml:compose.extra_secret.yml"},
|
|
[]string{
|
|
fmt.Sprintf("%s/%s/compose.yml", config.RECIPES_DIR, r.Name),
|
|
fmt.Sprintf("%s/%s/compose.extra_secret.yml", config.RECIPES_DIR, r.Name),
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
composeFiles, err := config.GetComposeFiles(r.Name, test.appEnv)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assert.Equal(t, composeFiles, test.composeFiles)
|
|
}
|
|
}
|
|
|
|
func TestGetComposeFilesError(t *testing.T) {
|
|
offline := true
|
|
r, err := recipe.Get("abra-test-recipe", offline)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tests := []struct{ appEnv map[string]string }{
|
|
{map[string]string{"COMPOSE_FILE": "compose.yml::compose.foo.yml"}},
|
|
{map[string]string{"COMPOSE_FILE": "doesnt.exist.yml"}},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
_, err := config.GetComposeFiles(r.Name, test.appEnv)
|
|
if err == nil {
|
|
t.Fatalf("should have failed: %v", test.appEnv)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFilters(t *testing.T) {
|
|
oldDir := config.RECIPES_DIR
|
|
config.RECIPES_DIR = "./testdir"
|
|
defer func() {
|
|
config.RECIPES_DIR = oldDir
|
|
}()
|
|
|
|
app, err := config.NewApp(config.AppEnv{
|
|
"DOMAIN": "test.example.com",
|
|
"RECIPE": "test-recipe",
|
|
}, "test_example_com", config.AppFile{
|
|
Path: "./testdir/filtertest.end",
|
|
Server: "local",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
f, err := app.Filters(false, false)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
compareFilter(t, f, map[string]map[string]bool{
|
|
"name": {
|
|
"test_example_com": true,
|
|
},
|
|
})
|
|
|
|
f2, err := app.Filters(false, true)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
compareFilter(t, f2, map[string]map[string]bool{
|
|
"name": {
|
|
"^test_example_com": true,
|
|
},
|
|
})
|
|
|
|
f3, err := app.Filters(true, false)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
compareFilter(t, f3, map[string]map[string]bool{
|
|
"name": {
|
|
"test_example_com_bar": true,
|
|
"test_example_com_foo": true,
|
|
},
|
|
})
|
|
|
|
f4, err := app.Filters(true, true)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
compareFilter(t, f4, map[string]map[string]bool{
|
|
"name": {
|
|
"^test_example_com_bar": true,
|
|
"^test_example_com_foo": true,
|
|
},
|
|
})
|
|
|
|
f5, err := app.Filters(false, false, "foo")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
compareFilter(t, f5, map[string]map[string]bool{
|
|
"name": {
|
|
"test_example_com_foo": true,
|
|
},
|
|
})
|
|
}
|
|
|
|
func compareFilter(t *testing.T, f1 filters.Args, f2 map[string]map[string]bool) {
|
|
t.Helper()
|
|
j1, err := f1.MarshalJSON()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
j2, err := json.Marshal(f2)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if diff := cmp.Diff(string(j2), string(j1)); diff != "" {
|
|
t.Errorf("filters mismatch (-want +got):\n%s", diff)
|
|
}
|
|
}
|