variables_test.go 4.23 KB
Newer Older
Kamil Trzciński's avatar
Kamil Trzciński committed
1 2 3
package common

import (
4
	"encoding/json"
Kamil Trzciński's avatar
Kamil Trzciński committed
5
	"testing"
6 7

	"github.com/stretchr/testify/assert"
8
	"github.com/stretchr/testify/require"
Kamil Trzciński's avatar
Kamil Trzciński committed
9 10
)

11
func TestVariablesJSON(t *testing.T) {
12
	var x JobVariable
13 14 15 16
	data := []byte(`{"key": "FOO", "value": "bar", "public": true, "internal": true, "file": true}`)

	err := json.Unmarshal(data, &x)
	assert.NoError(t, err)
17 18 19 20 21
	assert.Equal(t, "FOO", x.Key)
	assert.Equal(t, "bar", x.Value)
	assert.Equal(t, true, x.Public)
	assert.Equal(t, false, x.Internal) // cannot be set from the network
	assert.Equal(t, true, x.File)
22 23
}

Kamil Trzciński's avatar
Kamil Trzciński committed
24
func TestVariableString(t *testing.T) {
25
	v := JobVariable{"key", "value", false, false, false}
Kamil Trzciński's avatar
Kamil Trzciński committed
26 27 28
	assert.Equal(t, "key=value", v.String())
}

Kamil Trzciński's avatar
Kamil Trzciński committed
29
func TestPublicAndInternalVariables(t *testing.T) {
30 31 32 33
	v1 := JobVariable{"key", "value", false, false, false}
	v2 := JobVariable{"public", "value", true, false, false}
	v3 := JobVariable{"private", "value", false, true, false}
	all := JobVariables{v1, v2, v3}
Kamil Trzciński's avatar
Kamil Trzciński committed
34
	public := all.PublicOrInternal()
Kamil Trzciński's avatar
Kamil Trzciński committed
35
	assert.NotContains(t, public, v1)
Kamil Trzciński's avatar
Kamil Trzciński committed
36 37
	assert.Contains(t, public, v2)
	assert.Contains(t, public, v3)
Kamil Trzciński's avatar
Kamil Trzciński committed
38 39 40
}

func TestListVariables(t *testing.T) {
41
	v := JobVariables{{"key", "value", false, false, false}}
Kamil Trzciński's avatar
Kamil Trzciński committed
42 43 44 45
	assert.Equal(t, []string{"key=value"}, v.StringList())
}

func TestGetVariable(t *testing.T) {
46 47 48 49
	v1 := JobVariable{"key", "key_value", false, false, false}
	v2 := JobVariable{"public", "public_value", true, false, false}
	v3 := JobVariable{"private", "private_value", false, false, false}
	all := JobVariables{v1, v2, v3}
Kamil Trzciński's avatar
Kamil Trzciński committed
50 51 52 53 54 55 56 57

	assert.Equal(t, "public_value", all.Get("public"))
	assert.Empty(t, all.Get("other"))
}

func TestParseVariable(t *testing.T) {
	v, err := ParseVariable("key=value=value2")
	assert.NoError(t, err)
58
	assert.Equal(t, JobVariable{"key", "value=value2", false, false, false}, v)
Kamil Trzciński's avatar
Kamil Trzciński committed
59 60 61 62 63 64 65 66
}

func TestInvalidParseVariable(t *testing.T) {
	_, err := ParseVariable("some_other_key")
	assert.Error(t, err)
}

func TestVariablesExpansion(t *testing.T) {
67
	all := JobVariables{
Kamil Trzciński's avatar
Kamil Trzciński committed
68 69 70 71
		{"key", "value_of_$public", false, false, false},
		{"public", "some_value", true, false, false},
		{"private", "value_of_${public}", false, false, false},
		{"public", "value_of_$undefined", true, false, false},
Kamil Trzciński's avatar
Kamil Trzciński committed
72 73 74
	}

	expanded := all.Expand()
75
	assert.Len(t, expanded, 4)
76 77 78 79
	assert.Equal(t, "value_of_value_of_$undefined", expanded.Get("key"))
	assert.Equal(t, "value_of_", expanded.Get("public"))
	assert.Equal(t, "value_of_value_of_$undefined", expanded.Get("private"))
	assert.Equal(t, "value_of_ value_of_value_of_$undefined", expanded.ExpandValue("${public} ${private}"))
Kamil Trzciński's avatar
Kamil Trzciński committed
80
}
81 82

func TestSpecialVariablesExpansion(t *testing.T) {
83
	all := JobVariables{
84 85 86 87 88 89 90 91
		{"key", "$$", false, false, false},
		{"key2", "$/dsa", true, false, false},
		{"key3", "aa$@bb", false, false, false},
		{"key4", "aa${@}bb", false, false, false},
	}

	expanded := all.Expand()
	assert.Len(t, expanded, 4)
92 93 94 95
	assert.Equal(t, "$", expanded.Get("key"))
	assert.Equal(t, "/dsa", expanded.Get("key2"))
	assert.Equal(t, "aabb", expanded.Get("key3"))
	assert.Equal(t, "aabb", expanded.Get("key4"))
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

type multipleKeyUsagesTestCase struct {
	variables     JobVariables
	expectedValue string
}

func TestMultipleUsageOfAKey(t *testing.T) {
	getVariable := func(value string) JobVariable {
		return JobVariable{Key: "key", Value: value}
	}

	tests := map[string]multipleKeyUsagesTestCase{
		"defined at job level": {
			variables: JobVariables{
				getVariable("from-job"),
			},
			expectedValue: "from-job",
		},
		"defined at default and job level": {
			variables: JobVariables{
				getVariable("from-default"),
				getVariable("from-job"),
			},
			expectedValue: "from-job",
		},
		"defined at config, default and job level": {
			variables: JobVariables{
				getVariable("from-config"),
				getVariable("from-default"),
				getVariable("from-job"),
			},
			expectedValue: "from-job",
		},
		"defined at config and default level": {
			variables: JobVariables{
				getVariable("from-config"),
				getVariable("from-default"),
			},
			expectedValue: "from-default",
		},
		"defined at config level": {
			variables: JobVariables{
				getVariable("from-config"),
			},
			expectedValue: "from-config",
		},
	}

	for name, testCase := range tests {
		t.Run(name, func(t *testing.T) {
			for i := 0; i < 100; i++ {
				require.Equal(t, testCase.expectedValue, testCase.variables.Get("key"))
			}
		})
	}

}