summaryrefslogtreecommitdiff
path: root/docker/instructions_test.go
blob: 7124ff0a548c20f22a724c382319f852515df609 (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
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
154
package docker_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

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

func TestRun(t *testing.T) {
	i := build.Run{"echo", []string{"hello"}}
	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "RUN echo \"hello\"\n", di.Compile())
	}
}

func TestRunAll(t *testing.T) {
	i := build.RunAll{[]build.Run{
		{"echo", []string{"hello"}},
		{"echo", []string{"yo"}},
	}}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "RUN echo \"hello\" && echo \"yo\"\n", di.Compile())
	}
}

func TestCopy(t *testing.T) {
	i := build.Copy{[]string{"foo1", "foo2"}, "bar"}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "COPY [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
	}
}

func TestCopyAs(t *testing.T) {
	t.Run("with Copy", func(t *testing.T) {
		i := build.CopyAs{123, 124, build.Copy{[]string{"foo1", "foo2"}, "bar"}}

		di, err := docker.NewInstruction(i)

		if assert.NoError(t, err) {
			assert.Equal(t, "COPY --chown=123:124 [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
		}
	})

	t.Run("with CopyFrom", func(t *testing.T) {
		i := build.CopyAs{123, 124, build.CopyFrom{"foo", build.Copy{[]string{"foo1", "foo2"}, "bar"}}}

		di, err := docker.NewInstruction(i)

		if assert.NoError(t, err) {
			assert.Equal(t, "COPY --chown=123:124 --from=foo [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
		}
	})
}

func TestCopyFrom(t *testing.T) {
	i := build.CopyFrom{"foo", build.Copy{[]string{"foo1", "foo2"}, "bar"}}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "COPY --from=foo [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
	}
}

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

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "ENTRYPOINT [\"foo\", \"bar\"]\n", di.Compile())
	}
}

func TestEnv(t *testing.T) {
	i := build.Env{map[string]string{"foo": "bar", "bar": "foo"}}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "ENV bar=\"foo\" foo=\"bar\"\n", di.Compile())
	}
}

func TestLabel(t *testing.T) {
	i := build.Label{map[string]string{"foo": "bar", "bar": "foo"}}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "LABEL bar=\"foo\" foo=\"bar\"\n", di.Compile())
	}
}

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

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "USER \"foo\"\n", di.Compile())
	}
}

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

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "WORKDIR \"/foo/dir\"\n", di.Compile())
	}
}

func TestEscapeRun(t *testing.T) {
	i := build.Run{"/bin/true\nRUN echo HACKED!", []string{}}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "RUN /bin/true\\nRUN echo HACKED!\n", di.Compile())
	}
}

func TestEscapeCopy(t *testing.T) {
	i := build.Copy{[]string{"file.a", "file.b"}, "dest"}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "COPY [\"file.a\", \"file.b\", \"dest/\"]\n", di.Compile())
	}
}

func TestEscapeEnv(t *testing.T) {
	i := build.Env{map[string]string{"a": "b\nRUN echo HACKED!"}}

	di, err := docker.NewInstruction(i)

	if assert.NoError(t, err) {
		assert.Equal(t, "ENV a=\"b\\nRUN echo HACKED!\"\n", di.Compile())
	}
}