summaryrefslogtreecommitdiff
path: root/build/instructions_test.go
blob: 7cc2b207cf16f78a2b9fd734c81fd7fad3f4e873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
package build_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"gerrit.wikimedia.org/r/blubber/build"
)

func TestRun(t *testing.T) {
	i := build.Run{"echo %s %s", []string{"foo bar", "baz"}}

	assert.Equal(t, []string{`echo "foo bar" "baz"`}, i.Compile())
}

func TestRunWithInnerAndOuterArguments(t *testing.T) {
	i := build.Run{"useradd -d %s -u %s", []string{"/foo", "666", "bar"}}

	assert.Equal(t, []string{`useradd -d "/foo" -u "666" "bar"`}, i.Compile())
}

func TestRunAll(t *testing.T) {
	i := build.RunAll{[]build.Run{
		{"echo %s", []string{"foo"}},
		{"cat %s", []string{"/bar"}},
		{"baz", []string{}},
	}}

	assert.Equal(t, []string{`echo "foo" && cat "/bar" && baz`}, i.Compile())
}

func TestCopy(t *testing.T) {
	i := build.Copy{[]string{"source1", "source2"}, "dest"}

	assert.Equal(t, []string{`"source1"`, `"source2"`, `"dest/"`}, i.Compile())
}

func TestCopyAs(t *testing.T) {
	t.Run("wrapping Copy", func(t *testing.T) {
		i := build.CopyAs{
			123,
			124,
			build.Copy{[]string{"source1", "source2"}, "dest"},
		}

		assert.Equal(t, []string{"123:124", `"source1"`, `"source2"`, `"dest/"`}, i.Compile())
	})

	t.Run("wrapping CopyFrom", func(t *testing.T) {
		i := build.CopyAs{
			123,
			124,
			build.CopyFrom{"foo", build.Copy{[]string{"source1", "source2"}, "dest"}},
		}

		assert.Equal(t, []string{"123:124", "foo", `"source1"`, `"source2"`, `"dest/"`}, i.Compile())
	})
}

func TestCopyFrom(t *testing.T) {
	i := build.CopyFrom{"foo", build.Copy{[]string{"source1", "source2"}, "dest"}}

	assert.Equal(t, []string{"foo", `"source1"`, `"source2"`, `"dest/"`}, i.Compile())
}

func TestEntryPoint(t *testing.T) {
	i := build.EntryPoint{[]string{"/bin/foo", "bar", "baz"}}

	assert.Equal(t, []string{`"/bin/foo"`, `"bar"`, `"baz"`}, i.Compile())
}

func TestEnv(t *testing.T) {
	i := build.Env{map[string]string{
		"fooname": "foovalue",
		"barname": "barvalue",
		"quxname": "quxvalue",
	}}

	assert.Equal(t, []string{
		`barname="barvalue"`,
		`fooname="foovalue"`,
		`quxname="quxvalue"`,
	}, i.Compile())
}

func TestLabel(t *testing.T) {
	i := build.Label{map[string]string{
		"fooname": "foovalue",
		"barname": "barvalue",
		"quxname": "quxvalue",
	}}

	assert.Equal(t, []string{
		`barname="barvalue"`,
		`fooname="foovalue"`,
		`quxname="quxvalue"`,
	}, i.Compile())
}

func TestUser(t *testing.T) {
	i := build.User{"foo"}

	assert.Equal(t, []string{`"foo"`}, i.Compile())
}

func TestVolume(t *testing.T) {
	i := build.Volume{"/foo/dir"}

	assert.Equal(t, []string{`"/foo/dir"`}, i.Compile())
}

func TestWorkingDirectory(t *testing.T) {
	i := build.WorkingDirectory{"/foo/path"}

	assert.Equal(t, []string{`"/foo/path"`}, i.Compile())
}