summaryrefslogtreecommitdiff
path: root/config/lives_test.go
blob: 32916e56c4a79991bc73bb7afe9bff6e35894185 (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
package config_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"phabricator.wikimedia.org/source/blubber/build"
	"phabricator.wikimedia.org/source/blubber/config"
)

func TestLivesConfigYAML(t *testing.T) {
	cfg, err := config.ReadConfig([]byte(`---
    version: v2
    base: foo
    lives:
      in: /some/directory
      as: foouser
      uid: 123
      gid: 223
    variants:
      development: {}`))

	if assert.NoError(t, err) {
		assert.Equal(t, "/some/directory", cfg.Lives.In)
		assert.Equal(t, "foouser", cfg.Lives.As)
		assert.Equal(t, uint(123), cfg.Lives.UID)
		assert.Equal(t, uint(223), cfg.Lives.GID)

		variant, err := config.ExpandVariant(cfg, "development")

		if assert.NoError(t, err) {
			assert.Equal(t, "/some/directory", variant.Lives.In)
			assert.Equal(t, "foouser", variant.Lives.As)
			assert.Equal(t, uint(123), variant.Lives.UID)
			assert.Equal(t, uint(223), variant.Lives.GID)
		}
	}
}

func TestLivesConfigDefaults(t *testing.T) {
	cfg, err := config.ReadConfig([]byte(`---
    version: v2
    base: foo`))

	if assert.NoError(t, err) {
		assert.Equal(t, "somebody", cfg.Lives.As)
		assert.Equal(t, uint(65533), cfg.Lives.UID)
		assert.Equal(t, uint(65533), cfg.Lives.GID)
	}
}

func TestLivesConfigInstructions(t *testing.T) {
	cfg := config.LivesConfig{
		In: "/some/directory",
		UserConfig: config.UserConfig{
			As:  "foouser",
			UID: 123,
			GID: 223,
		},
	}

	t.Run("PhasePrivileged", func(t *testing.T) {
		assert.Equal(t,
			[]build.Instruction{build.RunAll{[]build.Run{
				{"groupadd -o -g %s -r", []string{"223", "foouser"}},
				{"useradd -o -m -d %s -r -g %s -u %s", []string{"/home/foouser", "foouser", "123", "foouser"}},
				{"mkdir -p", []string{"/some/directory"}},
				{"chown %s:%s", []string{"123", "223", "/some/directory"}},
				{"mkdir -p", []string{"/opt/lib"}},
				{"chown %s:%s", []string{"123", "223", "/opt/lib"}},
			}}},
			cfg.InstructionsForPhase(build.PhasePrivileged),
		)
	})

	t.Run("PhasePrivilegeDropped", func(t *testing.T) {
		assert.Equal(t,
			[]build.Instruction{
				build.WorkingDirectory{"/some/directory"},
			},
			cfg.InstructionsForPhase(build.PhasePrivilegeDropped),
		)
	})

	t.Run("PhasePreInstall", func(t *testing.T) {
		assert.Empty(t, cfg.InstructionsForPhase(build.PhasePreInstall))
	})

	t.Run("PhasePostInstall", func(t *testing.T) {
		assert.Empty(t, cfg.InstructionsForPhase(build.PhasePostInstall))
	})
}

func TestLivesConfigValidation(t *testing.T) {
	t.Run("in", func(t *testing.T) {
		t.Run("ok", func(t *testing.T) {
			err := config.Validate(config.LivesConfig{
				In: "/foo",
			})

			assert.False(t, config.IsValidationError(err))
		})

		t.Run("optional", func(t *testing.T) {
			err := config.Validate(config.LivesConfig{})

			assert.False(t, config.IsValidationError(err))
		})

		t.Run("non-root", func(t *testing.T) {
			err := config.Validate(config.LivesConfig{
				In: "/",
			})

			if assert.True(t, config.IsValidationError(err)) {
				msg := config.HumanizeValidationError(err)

				assert.Equal(t, `in: "/" is not a valid absolute non-root path`, msg)
			}
		})

		t.Run("non-root tricky", func(t *testing.T) {
			err := config.Validate(config.LivesConfig{
				In: "/foo/..",
			})

			if assert.True(t, config.IsValidationError(err)) {
				msg := config.HumanizeValidationError(err)

				assert.Equal(t, `in: "/foo/.." is not a valid absolute non-root path`, msg)
			}
		})

		t.Run("absolute", func(t *testing.T) {
			err := config.Validate(config.LivesConfig{
				In: "foo/bar",
			})

			if assert.True(t, config.IsValidationError(err)) {
				msg := config.HumanizeValidationError(err)

				assert.Equal(t, `in: "foo/bar" is not a valid absolute non-root path`, msg)
			}
		})
	})
}