summaryrefslogtreecommitdiff
path: root/pkg/systemd/generate/common_test.go
blob: 30e758127ef3f3af22894ab452dff6b784091b7e (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 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", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--conmon-pidfile=foo", "alpine"},
			[]string{"podman", "run", "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", "--conmon-pidfile", "foo", "--cidfile", "foo", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cgroups=foo", "--conmon-pidfile=foo", "--cidfile=foo", "alpine"},
			[]string{"podman", "run", "alpine"},
			1,
		},
		{
			[]string{"podman", "run", "--cgroups", "foo", "--conmon-pidfile", "foo", "--cidfile", "foo", "alpine", "--cgroups", "foo", "--conmon-pidfile", "foo", "--cidfile", "foo"},
			[]string{"podman", "run", "alpine", "--cgroups", "foo", "--conmon-pidfile", "foo", "--cidfile", "foo"},
			7,
		},
		{
			[]string{"podman", "run", "--cgroups=foo", "--conmon-pidfile=foo", "--cidfile=foo", "alpine", "--cgroups=foo", "--conmon-pidfile=foo", "--cidfile=foo"},
			[]string{"podman", "run", "alpine", "--cgroups=foo", "--conmon-pidfile=foo", "--cidfile=foo"},
			4,
		},
	}

	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 backslashs \\`},
			[]string{"foo", `"command with two backslashs \\\\"`},
		},
	}

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