summaryrefslogtreecommitdiff
path: root/build/macros_test.go
blob: 7b656b138bc2aab6dfc5972a4d562e0b30a17a4e (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
package build_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

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

func TestApplyUser(t *testing.T) {
	instructions := []build.Instruction{
		build.Copy{[]string{"foo"}, "bar"},
		build.Copy{[]string{"baz"}, "qux"},
		build.CopyFrom{"foo", build.Copy{[]string{"a"}, "b"}},
	}

	assert.Equal(t,
		[]build.Instruction{
			build.CopyAs{123, 223, build.Copy{[]string{"foo"}, "bar"}},
			build.CopyAs{123, 223, build.Copy{[]string{"baz"}, "qux"}},
			build.CopyAs{123, 223, build.CopyFrom{"foo", build.Copy{[]string{"a"}, "b"}}},
		},
		build.ApplyUser(123, 223, instructions),
	)
}

func TestChown(t *testing.T) {
	i := build.Chown(123, 124, "/foo")

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

func TestCreateDirectory(t *testing.T) {
	i := build.CreateDirectory("/foo")

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

func TestCreateUser(t *testing.T) {
	i := build.CreateUser("foo", 123, 124)

	if assert.Len(t, i, 2) {
		assert.Equal(t, []string{`groupadd -o -g "124" -r "foo"`}, i[0].Compile())
		assert.Equal(t, []string{`useradd -o -m -d "/home/foo" -r -g "foo" -u "123" "foo"`}, i[1].Compile())
	}
}

func TestHome(t *testing.T) {
	t.Run("root", func(t *testing.T) {
		assert.Equal(t,
			build.Env{map[string]string{"HOME": "/root"}},
			build.Home("root"),
		)
	})

	t.Run("non-root", func(t *testing.T) {
		assert.Equal(t,
			build.Env{map[string]string{"HOME": "/home/foo"}},
			build.Home("foo"),
		)
	})
}