Skip to content
Snippets Groups Projects
config_test.go 4.71 KiB
Newer Older
package runner_test
steeve.chailloux's avatar
steeve.chailloux committed
	"fmt"
	"github.com/go-yaml/yaml"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
Florent Aide's avatar
Florent Aide committed
	"orus.io/orus-io/beaver/runner"
	"orus.io/orus-io/beaver/testutils"
	fixtures    = "fixtures/f1"
	shaFixtures = "fixtures/f2"
Florent Aide's avatar
Florent Aide committed
	config, err := runner.NewConfig(filepath.Join(fixtures, "base"))
	require.NoError(t, err)
	// first config.spec.variables entry name should be VAULT_KV in our test file
	assert.Equal(t, "VAULT_KV", config.Variables[0].Name)
	assert.Equal(t, "orus.io", config.Variables[0].Value)
	assert.Equal(t, "../vendor/helm/postgresql", config.Charts["postgres"].Path)
	assert.Equal(t, "../vendor/ytt/odoo", config.Charts["odoo"].Path)

func TestYttBuildArgs(t *testing.T) {
	tl := testutils.NewTestLogger(t)
Florent Aide's avatar
Florent Aide committed
	testNS := "environments/ns1"
	absConfigDir, err := filepath.Abs(fixtures)
	require.NoError(t, err)
steeve.chailloux's avatar
steeve.chailloux committed
	c := runner.NewCmdConfig(tl.Logger(), absConfigDir, testNS, false, "")
	tmpDir, err := os.MkdirTemp(os.TempDir(), "beaver-")
	require.NoError(t, err)
Florent Aide's avatar
Florent Aide committed
	defer func() {
		assert.NoError(t, os.RemoveAll(tmpDir))
	}()
	require.NoError(t, c.Initialize(tmpDir))
	args := c.BuildYttArgs(c.Spec.Ytt, []string{"/tmp/postgres.1234.yaml", "/tmp/odoo.5678.yaml"})
steeve.chailloux's avatar
steeve.chailloux committed
	require.NoError(t, err)
steeve.chailloux's avatar
steeve.chailloux committed
		// []string{
		// 	"-f", "/tmp/postgres.1234.yaml", "--file-mark=postgres.1234.yaml:type=yaml-plain",
		// 	"-f", "/tmp/odoo.5678.yaml", "--file-mark=odoo.5678.yaml:type=yaml-plain",
		// 	"-f", filepath.Join(fixtures, "base/ytt"),
		// 	"-f", filepath.Join(fixtures, "base/ytt.yml"),
		// 	"-f", filepath.Join(fixtures, "environments/ns1/ytt"),
		// 	"-f", filepath.Join(fixtures, "environments/ns1/ytt.yaml"),
		// },
		14,
		len(args),

func TestCreateConfig(t *testing.T) {
	tl := testutils.NewTestLogger(t)
Florent Aide's avatar
Florent Aide committed
	testNS := "environments/ns1"
	absConfigDir, err := filepath.Abs(fixtures)
	require.NoError(t, err)
steeve.chailloux's avatar
steeve.chailloux committed
	c := runner.NewCmdConfig(tl.Logger(), absConfigDir, testNS, 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))
	require.Equal(t, 1, len(c.Spec.Creates))

	assert.True(t, filepath.IsAbs(c.Spec.Charts["postgres"].Path))
	crKey := runner.CmdCreateKey{
		Type: "configmap",
		Name: "xbus-pipelines",
	}
	cr, ok := c.Spec.Creates[crKey]
	assert.True(t, ok)
	require.Equal(t, 1, len(cr.Args))

	for k, create := range c.Spec.Creates {
		args := k.BuildArgs(c.Namespace, create.Args)
		assert.Equal(
			t,
			[]string{
				"-n", c.Namespace,
				"create",
				"configmap", "xbus-pipelines",
				"--dry-run=client", "-o", "yaml",
steeve.chailloux's avatar
steeve.chailloux committed
				"--from-file", "pipelines",
	shaValue := "2145bea9e32804c65d960e6d4af1c87f95ccc39fad7df5eec2f3925a193112ab"
steeve.chailloux's avatar
steeve.chailloux committed
	buildDir := filepath.Join(shaFixtures, "build", "example")
	defer func() {
		require.NoError(t, runner.CleanDir(buildDir))
	}()

	tl := testutils.NewTestLogger(t)
	absConfigDir, err := filepath.Abs(shaFixtures)
	require.NoError(t, err)
	c := runner.NewCmdConfig(tl.Logger(), absConfigDir, "base", 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))
	r := runner.NewRunner(c)
	require.NoError(t, r.Build(tmpDir))

	configMap := filepath.Join(buildDir, "ConfigMap.v1.demo.yaml")
steeve.chailloux's avatar
steeve.chailloux committed
	cm,  err := parseFile(configMap)
	require.NoError(t, err)
	sha, err := getLabel(cm, "mysha")
	require.NoError(t, err)
	require.Equal(t, shaValue, sha)

	deployment := filepath.Join(buildDir, "Deployment.apps_v1.nginx.yaml")
steeve.chailloux's avatar
steeve.chailloux committed
	deploy, err := parseFile(deployment)
	require.NoError(t, err)
	sha, err = getLabel(deploy, "config.sha")
	require.NoError(t, err)
	require.Equal(t, shaValue, sha)
steeve.chailloux's avatar
steeve.chailloux committed
func getLabel(resource map[string]interface{}, label string) (string, error) {
	metadata, ok := resource["metadata"].(map[interface{}]interface{})
steeve.chailloux's avatar
steeve.chailloux committed
	if !ok {
steeve.chailloux's avatar
steeve.chailloux committed
		return "", fmt.Errorf("fail to get label: metadata")
steeve.chailloux's avatar
steeve.chailloux committed
	}
	labels, ok := metadata["labels"].(map[interface{}]interface{})
steeve.chailloux's avatar
steeve.chailloux committed
	if !ok {
steeve.chailloux's avatar
steeve.chailloux committed
		return "", fmt.Errorf("fail to get label: labels")
steeve.chailloux's avatar
steeve.chailloux committed
	}
	result, ok := labels[label].(string)
steeve.chailloux's avatar
steeve.chailloux committed
	if !ok {
steeve.chailloux's avatar
steeve.chailloux committed
		return "", fmt.Errorf("fail to get label: result")
steeve.chailloux's avatar
steeve.chailloux committed
	}
	return result, nil
steeve.chailloux's avatar
steeve.chailloux committed
func parseFile(input string) (map[string]interface{}, error) {
	resource := make(map[string]interface{})

	content, err := os.ReadFile(input)
steeve.chailloux's avatar
steeve.chailloux committed
	if err != nil {
		return nil, fmt.Errorf("fail to parse: %w", err)
	}

	byteContent := bytes.NewReader(content)
	decoder := yaml.NewDecoder(byteContent)

	err = decoder.Decode(&resource)
steeve.chailloux's avatar
steeve.chailloux committed
	if err != nil {
		return nil, fmt.Errorf("fail to parse: %w", err)
	}
steeve.chailloux's avatar
steeve.chailloux committed
	return resource, nil