Newer
Older
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"orus.io/orus-io/beaver/runner"
"orus.io/orus-io/beaver/testutils"
stdout, stderr, err := runner.RunCMD(c)
require.NoError(t, err)
for _, out := range stdout {
assert.Equal(t, "p00f", out)
fmt.Println(out)
}
for _, errMsg := range stderr {
fmt.Println(errMsg)
}
}
func TestCmdConfig(t *testing.T) {
tl := testutils.NewTestLogger(t)
require.NoError(t, err)
c := runner.NewCmdConfig(tl.Logger(), absConfigDir, testPath, false, "")
tmpDir, err := os.MkdirTemp(os.TempDir(), "beaver-")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tmpDir))
}()
require.NoError(t, c.Initialize(tmpDir))
t.Run("yttCharts", func(t *testing.T) {
odooYttChart, ok := c.Spec.Charts["odoo"]
require.True(t, ok, "we should have an odoo ytt chart in our cmdConfig")
assert.Equal(t, 2, len(odooYttChart.ValuesFileNames))
})
t.Run("yttPatches", func(t *testing.T) {
yttPatches := c.Spec.Ytt
l := tl.Logger()
logger := &l
logger.Debug().Str("patches", fmt.Sprintf("%+v", yttPatches)).Msg("found patches")
require.Equal(t, 4, len(yttPatches))
})
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
t.Run("helmCharts", func(t *testing.T) {
buildDir := filepath.Join(fixtures, "build", "ns1")
defer func() {
require.NoError(t, runner.CleanDir(buildDir))
}()
r := runner.NewRunner(c)
require.NoError(t, r.Build(tmpDir))
deployment := filepath.Join(buildDir, "Deployment.apps_v1.postgres.yaml")
deploy, err := parseFile(deployment)
require.NoError(t, err)
envVars, err := getEnvVars(deploy)
require.NoError(t, err)
pguser, ok := envVars["PGUSER"]
require.True(t, ok)
assert.Equal(t, "<path:k8s.orus.io/data/ns1/postgres#username>", pguser)
pgdatabase, ok := envVars["PGDATABASE"]
require.True(t, ok)
assert.Equal(t, "<path:k8s.orus.io/data/ns1/postgres#database>", pgdatabase)
})
}
func getEnvVars(resource map[string]interface{}) (map[string]string, error) {
result := make(map[string]string)
spec, ok := resource["spec"].(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("fail to env var: spec")
}
template, ok := spec["template"].(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("fail to env var: template")
}
containersSpec, ok := template["spec"].(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("fail to env var: containersSpec")
}
containers, ok := containersSpec["containers"].([]interface{})
if !ok {
return nil, fmt.Errorf("fail to env var: containers")
}
container, ok := containers[0].(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("fail to env var: container")
}
env, ok := container["env"].([]interface{})
if !ok {
return nil, fmt.Errorf("fail to env var: env")
}
for _, item := range env {
e, ok := item.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("fail to env var: env var: %v", item)
}
name, ok := e["name"].(string)
if !ok {
return nil, fmt.Errorf("fail to env var: env var name: %v", item)
}
value, ok := e["value"].(string)
if !ok {
return nil, fmt.Errorf("fail to env var: env var value: %v", item)
}
result[name] = value
}
return result, nil
func TestFindFiles(t *testing.T) {
Path: "postgres",
ValuesFileNames: nil,
},
}
layers := []string{
fmt.Sprintf("fixtures/f1/environments/%s", namespace),
"fixtures/f1/base",
}
require.Equal(t, 2, len(newCharts["postgres"].ValuesFileNames))
func TestYamlSplit(t *testing.T) {
namespace := "ns1"
compiled := "output.yaml"
buildDir := filepath.Join(fixtures, "build", namespace)
compiledFiles, err := runner.YamlSplit(buildDir, filepath.Join(fixtures, compiled))
require.NoError(t, err)
require.Equal(t, 3, len(compiledFiles))
for _, filePath := range compiledFiles {
fileName := filepath.Base(filePath)
tokens := strings.Split(fileName, ".")
require.Equal(t, 4, len(tokens))
content, err := os.ReadFile(filePath)
require.NoError(t, err)
assert.Equal(t, "---", string(content)[:3])
v := viper.New()
v.SetConfigName(strings.TrimSuffix(fileName, path.Ext(fileName)))
v.AddConfigPath(strings.TrimSuffix(filePath, fileName))
require.NoError(t, v.ReadInConfig())
resource := make(map[string]interface{})
require.NoError(t, v.Unmarshal(&resource))
kind, ok := resource["kind"].(string)
require.True(t, ok)
assert.Equal(t, tokens[0], kind)
apiVersion, ok := resource["apiversion"].(string)
require.True(t, ok)
assert.Equal(t, tokens[1], apiVersion)
metadata, ok := resource["metadata"].(map[string]interface{})
require.True(t, ok)
name, ok := metadata["name"].(string)
require.True(t, ok)
assert.Equal(t, tokens[2], name)
}
}