summaryrefslogtreecommitdiff
path: root/pkg/systemd/generate/common_test.go
blob: 80abebb26b195a2210a7c6fd8fb4336523f8c86a (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
package generate

import (
	"testing"

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

func TestFilterPodFlags(t *testing.T) {
	tests := []struct {
		input    []string
		output   []string
		argCount int
	}{
		{
			[]string{"podman", "pod", "create"},
			[]string{"podman", "pod", "create"},
			0,
		},
		{
			[]string{"podman", "pod", "create", "--name", "foo"},
			[]string{"podman", "pod", "create", "--name", "foo"},
			0,
		},
		{
			[]string{"podman", "pod", "create", "--pod-id-file", "foo"},
			[]string{"podman", "pod", "create"},
			0,
		},
		{
			[]string{"podman", "pod", "create", "--pod-id-file=foo"},
			[]string{"podman", "pod", "create"},
			0,
		},
		{
			[]string{"podman", "pod", "create", "--pod-id-file", "foo", "--infra-conmon-pidfile", "foo"},
			[]string{"podman", "pod", "create"},
			0,
		},
		{
			[]string{"podman", "pod", "create", "--pod-id-file", "foo", "--infra-conmon-pidfile=foo"},
			[]string{"podman", "pod", "create"},
			0,
		},
		{
			[]string{"podman", "run", "--pod", "foo"},
			[]string{"podman", "run"},
			0,
		},
		{
			[]string{"podman", "run", "--pod=foo"},
			[]string{"podman", "run"},
			0,
		},
		{
			[]string{"podman", "run", "--pod=foo", "fedora", "podman", "run", "--pod=test", "alpine"},
			[]string{"podman", "run", "fedora", "podman", "run", "--pod=test", "alpine"},
			5,
		},
		{
			[]string{"podman", "run", "--pod", "foo", "fedora", "podman", "run", "--pod", "test", "alpine"},
			[]string{"podman", "run", "fedora", "podman", "run", "--pod", "test", "alpine"},
			6,
		},
		{
			[]string{"podman", "run", "--pod-id-file=foo", "fedora", "podman", "run", "--pod-id-file=test", "alpine"},
			[]string{"podman", "run", "fedora", "podman", "run", "--pod-id-file=test", "alpine"},
			5,
		},
		{
			[]string{"podman", "run", "--pod-id-file", "foo", "fedora", "podman", "run", "--pod-id-file", "test", "alpine"},
			[]string{"podman", "run", "fedora", "podman", "run", "--pod-id-file", "test", "alpine"},
			6,
		},
	}

	for _, test := range tests {
		processed := filterPodFlags(test.input, test.argCount)
		assert.Equal(t, test.output, processed)
	}
}

func TestFilterCommonContainerFlags(t *testing.T) {
	tests := []struct {
		input    []string
		output   []string
		argCount int
	}{
		{
			[]string{"podman", "run", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--conmon-pidfile", "foo", "alpine"},
			[]string{"podman", "run", "--conmon-pidfile", "foo", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--conmon-pidfile=foo", "alpine"},
			[]string{"podman", "run", "--conmon-pidfile=foo", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cidfile", "foo", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cidfile=foo", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cgroups", "foo", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cgroups=foo", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cgroups=foo", "--rm", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cgroups", "--rm=bogus", "alpine", "--cgroups", "foo", "--conmon-pidfile", "foo", "--cidfile", "foo", "--rm"},
			[]string{"podman", "run", "alpine", "--cgroups", "foo", "--conmon-pidfile", "foo", "--cidfile", "foo", "--rm"},
			7,
		},
	}

	for _, test := range tests {
		processed := filterCommonContainerFlags(test.input, test.argCount)
		assert.Equal(t, test.output, processed)
	}
}

func TestEscapeSystemdArguments(t *testing.T) {
	tests := []struct {
		input  []string
		output []string
	}{
		{
			[]string{"foo", "bar=\"arg\""},
			[]string{"foo", "bar=\"arg\""},
		},
		{
			[]string{"foo", "bar=\"arg with space\""},
			[]string{"foo", "\"bar=\\\"arg with space\\\"\""},
		},
		{
			[]string{"foo", "bar=\"arg with\ttab\""},
			[]string{"foo", "\"bar=\\\"arg with\\ttab\\\"\""},
		},
		{
			[]string{"$"},
			[]string{"$$"},
		},
		{
			[]string{"foo", "command with dollar sign $"},
			[]string{"foo", "\"command with dollar sign $$\""},
		},
		{
			[]string{"foo", "command with two dollar signs $$"},
			[]string{"foo", "\"command with two dollar signs $$$$\""},
		},
		{
			[]string{"%"},
			[]string{"%%"},
		},
		{
			[]string{"foo", "command with percent sign %"},
			[]string{"foo", "\"command with percent sign %%\""},
		},
		{
			[]string{"foo", "command with two percent signs %%"},
			[]string{"foo", "\"command with two percent signs %%%%\""},
		},
		{
			[]string{`\`},
			[]string{`\\`},
		},
		{
			[]string{"foo", `command with backslash \`},
			[]string{"foo", `"command with backslash \\"`},
		},
		{
			[]string{"foo", `command with two backslashes \\`},
			[]string{"foo", `"command with two backslashes \\\\"`},
		},
	}

	for _, test := range tests {
		quoted := escapeSystemdArguments(test.input)
		assert.Equal(t, test.output, quoted)
	}
}