summaryrefslogtreecommitdiff
path: root/libpod/container_internal_test.go
blob: 1654af9296e55fcbfc9c266ba5bd853b48079599 (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
package libpod

import (
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"

	rspec "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/stretchr/testify/assert"
)

// hookPath is the path to an example hook executable.
var hookPath string

func TestLocaleToLanguage(t *testing.T) {
	for _, testCase := range []struct {
		locale   string
		language string
	}{
		{
			locale:   "",
			language: "und-u-va-posix",
		},
		{
			locale:   "C",
			language: "und-u-va-posix",
		},
		{
			locale:   "POSIX",
			language: "und-u-va-posix",
		},
		{
			locale:   "c",
			language: "und-u-va-posix",
		},
		{
			locale:   "en",
			language: "en",
		},
		{
			locale:   "en_US",
			language: "en-US",
		},
		{
			locale:   "en.UTF-8",
			language: "en",
		},
		{
			locale:   "en_US.UTF-8",
			language: "en-US",
		},
		{
			locale:   "does-not-exist",
			language: "does-not-exist",
		},
	} {
		t.Run(testCase.locale, func(t *testing.T) {
			assert.Equal(t, testCase.language, localeToLanguage(testCase.locale))
		})
	}
}

func TestPostDeleteHooks(t *testing.T) {
	ctx := context.Background()
	dir, err := ioutil.TempDir("", "libpod_test_")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	statePath := filepath.Join(dir, "state")
	copyPath := filepath.Join(dir, "copy")
	c := Container{
		config: &ContainerConfig{
			ID: "123abc",
			Spec: &rspec.Spec{
				Annotations: map[string]string{
					"a": "b",
				},
			},
			StaticDir: dir, // not the bundle, but good enough for this test
		},
		state: &ContainerState{
			ExtensionStageHooks: map[string][]rspec.Hook{
				"poststop": {
					rspec.Hook{
						Path: hookPath,
						Args: []string{"sh", "-c", fmt.Sprintf("cat >%s", statePath)},
					},
					rspec.Hook{
						Path: "/does/not/exist",
					},
					rspec.Hook{
						Path: hookPath,
						Args: []string{"sh", "-c", fmt.Sprintf("cp %s %s", statePath, copyPath)},
					},
				},
			},
		},
	}
	err = c.postDeleteHooks(ctx)
	if err != nil {
		t.Fatal(err)
	}

	stateRegexp := `{"ociVersion":"1\.0\.1-dev","id":"123abc","status":"stopped","bundle":"` + strings.TrimSuffix(os.TempDir(), "/") + `/libpod_test_[0-9]*","annotations":{"a":"b"}}`
	for _, path := range []string{statePath, copyPath} {
		t.Run(path, func(t *testing.T) {
			content, err := ioutil.ReadFile(path)
			if err != nil {
				t.Fatal(err)
			}
			assert.Regexp(t, stateRegexp, string(content))
		})
	}
}

func init() {
	if runtime.GOOS != "windows" {
		hookPath = "/bin/sh"
	} else {
		panic("we need a reliable executable path on Windows")
	}
}