File: util_test.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (113 lines) | stat: -rw-r--r-- 2,230 bytes parent folder | download
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
package util

import (
	"os"
	"strconv"
	"testing"

	"github.com/containers/common/pkg/config"
	specs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/stretchr/testify/assert"
)

func TestMergeEnv(t *testing.T) {
	tests := [][3][]string{
		{
			[]string{"A=B", "B=C", "C=D"},
			nil,
			[]string{"A=B", "B=C", "C=D"},
		},
		{
			nil,
			[]string{"A=B", "B=C", "C=D"},
			[]string{"A=B", "B=C", "C=D"},
		},
		{
			[]string{"A=B", "B=C", "C=D", "E=F"},
			[]string{"B=O", "F=G"},
			[]string{"A=B", "B=O", "C=D", "E=F", "F=G"},
		},
	}
	for i, test := range tests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			result := MergeEnv(test[0], test[1])
			if len(result) != len(test[2]) {
				t.Fatalf("expected %v, got %v", test[2], result)
			}
			for i := range result {
				if result[i] != test[2][i] {
					t.Fatalf("expected %v, got %v", test[2], result)
				}
			}
		})
	}
}

func TestRuntime(t *testing.T) {
	os.Setenv("CONTAINERS_CONF", "/dev/null")
	conf, _ := config.Default()
	defaultRuntime := conf.Engine.OCIRuntime
	runtime := Runtime()
	if runtime != defaultRuntime {
		t.Fatalf("expected %v, got %v", runtime, defaultRuntime)
	}
	defaultRuntime = "myoci"
	os.Setenv("BUILDAH_RUNTIME", defaultRuntime)
	runtime = Runtime()
	if runtime != defaultRuntime {
		t.Fatalf("expected %v, got %v", runtime, defaultRuntime)
	}
}

func TestMountsSort(t *testing.T) {
	mounts1a := []specs.Mount{
		{
			Source:      "/a/bb/c",
			Destination: "/a/bb/c",
		},
		{
			Source:      "/a/b/c",
			Destination: "/a/b/c",
		},
		{
			Source:      "/a",
			Destination: "/a",
		},
		{
			Source:      "/a/b",
			Destination: "/a/b",
		},
		{
			Source:      "/d/e",
			Destination: "/a/c",
		},
		{
			Source:      "/b",
			Destination: "/b",
		},
		{
			Source:      "/",
			Destination: "/",
		},
		{
			Source:      "/a/b/c",
			Destination: "/aa/b/c",
		},
	}
	mounts1b := []string{
		"/",
		"/a",
		"/b",
		"/a/b",
		"/a/c",
		"/a/b/c",
		"/a/bb/c",
		"/aa/b/c",
	}
	sorted := SortMounts(mounts1a)
	sortedDests := make([]string, len(mounts1a))
	for i := range sorted {
		sortedDests[i] = sorted[i].Destination
	}
	assert.Equalf(t, mounts1b, sortedDests, "sort returned results in unexpected by-destination order")
}