summaryrefslogtreecommitdiff
path: root/config/lives_test.go
blob: d6097b548171a0f13d65400135077c6d8f1a4bc1 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package config_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

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

func TestLivesConfig(t *testing.T) {
	cfg, err := config.ReadConfig([]byte(`---
    version: v1
    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: v1
    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.ReadConfig([]byte(`---
        version: v1
        lives:
          in: /foo`))

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

		t.Run("optional", func(t *testing.T) {
			_, err := config.ReadConfig([]byte(`---
        version: v1
        lives: {}`))

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

		t.Run("non-root", func(t *testing.T) {
			_, err := config.ReadConfig([]byte(`---
        version: v1
        lives:
          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.ReadConfig([]byte(`---
        version: v1
        lives:
          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.ReadConfig([]byte(`---
        version: v1
        lives:
          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)
			}
		})
	})

	t.Run("as", func(t *testing.T) {
		t.Run("ok", func(t *testing.T) {
			_, err := config.ReadConfig([]byte(`---
        version: v1
        lives:
          as: foo-bar.baz`))

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

		t.Run("optional", func(t *testing.T) {
			_, err := config.ReadConfig([]byte(`---
        version: v1
        lives: {}`))

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

		t.Run("no spaces", func(t *testing.T) {
			_, err := config.ReadConfig([]byte(`---
        version: v1
        lives:
          as: foo bar`))

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

				assert.Equal(t, `as: "foo bar" is not a valid user name`, msg)
			}
		})

		t.Run("long enough", func(t *testing.T) {
			_, err := config.ReadConfig([]byte(`---
        version: v1
        lives:
          as: fo`))

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

				assert.Equal(t, `as: "fo" is not a valid user name`, msg)
			}
		})

		t.Run("not root", func(t *testing.T) {
			_, err := config.ReadConfig([]byte(`---
        version: v1
        lives:
          as: root`))

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

				assert.Equal(t, `as: "root" is not a valid user name`, msg)
			}
		})
	})
}