forked from actions/runner
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerUtilL0.cs
More file actions
202 lines (185 loc) · 8.56 KB
/
DockerUtilL0.cs
File metadata and controls
202 lines (185 loc) · 8.56 KB
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
using System;
using System.Collections.Generic;
using GitHub.Runner.Worker.Container;
using Xunit;
namespace GitHub.Runner.Common.Tests.Worker.Container
{
public sealed class DockerUtilL0
{
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void RegexParsesDockerPort()
{
// Arrange
var dockerPortOutput0 = new List<string>();
var dockerPortOutput1 = new List<string>
{
"80/tcp -> 0.0.0.0:32881"
};
var dockerPortOutput1Empty = new List<string>
{
""
};
var dockerPortOutput2 = new List<string>
{
"80/tcp -> 0.0.0.0:32881",
"6379/tcp -> 0.0.0.0:32882"
};
// Act
var result0 = DockerUtil.ParseDockerPort(dockerPortOutput0);
var result1 = DockerUtil.ParseDockerPort(dockerPortOutput1);
var result1Empty = DockerUtil.ParseDockerPort(dockerPortOutput1Empty);
var result2 = DockerUtil.ParseDockerPort(dockerPortOutput2);
// Assert
Assert.NotNull(result0);
Assert.Equal(0, result0.Count);
Assert.NotNull(result1);
Assert.Equal(1, result1.Count);
var result1Port80Mapping = result1.Find(pm =>
string.Equals(pm.ContainerPort, "80") &&
string.Equals(pm.HostPort, "32881") &&
string.Equals(pm.Protocol, "tcp", StringComparison.OrdinalIgnoreCase)
);
Assert.NotNull(result1Port80Mapping);
Assert.NotNull(result1Empty);
Assert.Equal(0, result1Empty.Count);
Assert.NotNull(result2);
Assert.Equal(2, result2.Count);
var result2Port80Mapping = result2.Find(pm =>
string.Equals(pm.ContainerPort, "80") &&
string.Equals(pm.HostPort, "32881") &&
string.Equals(pm.Protocol, "tcp", StringComparison.OrdinalIgnoreCase)
);
Assert.NotNull(result2Port80Mapping);
var result2Port6379Mapping = result2.Find(pm =>
string.Equals(pm.ContainerPort, "6379") &&
string.Equals(pm.HostPort, "32882") &&
string.Equals(pm.Protocol, "tcp", StringComparison.OrdinalIgnoreCase)
);
Assert.NotNull(result2Port6379Mapping);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void RegexParsesPathFromDockerConfigEnv()
{
// Arrange
var configOutput0 = new List<string>
{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"MY_VAR=test"
};
var configOutput1 = new List<string>
{
"PATH=/bad idea:/really,bad,idea:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"MY_VAR=test"
};
var configOutput2 = new List<string>();
var configOutput3 = new List<string>
{
"NOT_A_PATH=/bad idea:/really,bad,idea:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"MY_VAR=test"
};
var configOutput4 = new List<string>
{
"PATH",
"PATH="
};
var configOutput5 = new List<string>
{
"PATH=/foo/bar:/baz",
"Path=/no/where"
};
// Act
var result0 = DockerUtil.ParsePathFromConfigEnv(configOutput0);
var result1 = DockerUtil.ParsePathFromConfigEnv(configOutput1);
var result2 = DockerUtil.ParsePathFromConfigEnv(configOutput2);
var result3 = DockerUtil.ParsePathFromConfigEnv(configOutput3);
var result4 = DockerUtil.ParsePathFromConfigEnv(configOutput4);
var result5 = DockerUtil.ParsePathFromConfigEnv(configOutput5);
// Assert
Assert.NotNull(result0);
Assert.Equal("/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", result0);
Assert.NotNull(result1);
Assert.Equal("/bad idea:/really,bad,idea:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", result1);
Assert.NotNull(result2);
Assert.Equal("", result2);
Assert.NotNull(result3);
Assert.Equal("", result3);
Assert.NotNull(result4);
Assert.Equal("", result4);
Assert.NotNull(result5);
Assert.Equal("/foo/bar:/baz", result5);
}
[Theory]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
[InlineData("dockerhub/repo", "")]
[InlineData("localhost/doesnt_work", "")]
[InlineData("localhost:port/works", "localhost:port")]
[InlineData("host.tld/works", "host.tld")]
[InlineData("ghcr.io/owner/image", "ghcr.io")]
[InlineData("gcr.io/project/image", "gcr.io")]
[InlineData("myregistry.azurecr.io/namespace/image", "myregistry.azurecr.io")]
[InlineData("account.dkr.ecr.region.amazonaws.com/image", "account.dkr.ecr.region.amazonaws.com")]
[InlineData("docker.pkg.github.com/owner/repo/image", "docker.pkg.github.com")]
public void ParseRegistryHostnameFromImageName(string input, string expected)
{
var actual = DockerUtil.ParseRegistryHostnameFromImageName(input);
Assert.Equal(expected, actual);
}
[Theory]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
[InlineData("", "")]
[InlineData("HOME alpine:3.8 sh -c id #", "HOME alpine:3.8 sh -c id #")]
[InlineData("HOME \"alpine:3.8 sh -c id #", "HOME \\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \\\"alpine:3.8 sh -c id #", "HOME \\\\\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \\\\\"alpine:3.8 sh -c id #", "HOME \\\\\\\\\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \"\"alpine:3.8 sh -c id #", "HOME \\\"\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \\\"\"alpine:3.8 sh -c id #", "HOME \\\\\\\"\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \"\\\"alpine:3.8 sh -c id #", "HOME \\\"\\\\\\\"alpine:3.8 sh -c id #")]
public void CreateEscapedOption_keyOnly(string input, string escaped)
{
var flag = "--example";
var actual = DockerUtil.CreateEscapedOption(flag, input);
string expected;
if (String.IsNullOrEmpty(input))
{
expected = "";
}
else
{
expected = $"{flag} \"{escaped}\"";
}
Assert.Equal(expected, actual);
}
[Theory]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
[InlineData("HOME", "", "HOME", "")]
[InlineData("HOME alpine:3.8 sh -c id #", "HOME alpine:3.8 sh -c id #", "HOME alpine:3.8 sh -c id #", "HOME alpine:3.8 sh -c id #")]
[InlineData("HOME \"alpine:3.8 sh -c id #", "HOME \"alpine:3.8 sh -c id #", "HOME \\\"alpine:3.8 sh -c id #", "HOME \\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \\\"alpine:3.8 sh -c id #", "HOME \\\"alpine:3.8 sh -c id #", "HOME \\\\\\\"alpine:3.8 sh -c id #", "HOME \\\\\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \\\\\"alpine:3.8 sh -c id #", "HOME \\\\\"alpine:3.8 sh -c id #", "HOME \\\\\\\\\\\"alpine:3.8 sh -c id #", "HOME \\\\\\\\\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \"\"alpine:3.8 sh -c id #", "HOME \"\"alpine:3.8 sh -c id #", "HOME \\\"\\\"alpine:3.8 sh -c id #", "HOME \\\"\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \\\"\"alpine:3.8 sh -c id #", "HOME \\\"\"alpine:3.8 sh -c id #", "HOME \\\\\\\"\\\"alpine:3.8 sh -c id #", "HOME \\\\\\\"\\\"alpine:3.8 sh -c id #")]
[InlineData("HOME \"\\\"alpine:3.8 sh -c id #", "HOME \"\\\"alpine:3.8 sh -c id #", "HOME \\\"\\\\\\\"alpine:3.8 sh -c id #", "HOME \\\"\\\\\\\"alpine:3.8 sh -c id #")]
public void CreateEscapedOption_keyValue(string keyInput, string valueInput, string escapedKey, string escapedValue)
{
var flag = "--example";
var actual = DockerUtil.CreateEscapedOption(flag, keyInput, valueInput);
string expected;
if (String.IsNullOrEmpty(keyInput))
{
expected = "";
}
else
{
expected = $"{flag} \"{escapedKey}={escapedValue}\"";
}
Assert.Equal(expected, actual);
}
}
}