File: load_test.go

package info (click to toggle)
golang-github-containers-image 5.34.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,184 kB
  • sloc: sh: 194; makefile: 83
file content (58 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download | duplicates (4)
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
package sif

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestParseDefFile(t *testing.T) {
	for _, c := range []struct {
		name        string
		input       string
		environment []string
		runscript   []string
	}{
		{"Empty input", "", []string{}, []string{}},
		{
			name: "Basic smoke test",
			input: "Bootstrap: library\n" +
				"%environment\n" +
				"			export FOO=world\n" +
				"			export BAR=baz\n" +
				"%runscript\n" +
				`			echo "Hello $FOO"` + "\n" +
				"			sleep 5\n" +
				"%help\n" +
				"			Abandon all hope.\n",
			environment: []string{"export FOO=world", "export BAR=baz"},
			runscript:   []string{`echo "Hello $FOO"`, "sleep 5"},
		},
		{
			name: "Trailing section marker",
			input: "Bootstrap: library\n" +
				"%environment\n" +
				"			export FOO=world\n" +
				"%runscript",
			environment: []string{"export FOO=world"},
			runscript:   []string{},
		},
	} {
		env, rs, err := parseDefFile(bytes.NewReader([]byte(c.input)))
		require.NoError(t, err, c.name)
		assert.Equal(t, c.environment, env, c.name)
		assert.Equal(t, c.runscript, rs, c.name)
	}
}

func TestGenerateInjectedScript(t *testing.T) {
	res := generateInjectedScript([]string{"export FOO=world", "export BAR=baz"},
		[]string{`echo "Hello $FOO"`, "sleep 5"})
	assert.Equal(t, "#!/bin/bash\n"+
		"export FOO=world\n"+
		"export BAR=baz\n"+
		`echo "Hello $FOO"`+"\n"+
		"sleep 5\n", string(res))
}