-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ContainerHelpersTests.cs
148 lines (136 loc) · 7.04 KB
/
ContainerHelpersTests.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.NET.Build.Containers.UnitTests;
public class ContainerHelpersTests
{
private const string DefaultRegistry = "docker.io";
[Theory]
// Valid Tests
[InlineData("mcr.microsoft.com", true)]
[InlineData("mcr.microsoft.com:5001", true)] // Registries can have ports
[InlineData("docker.io", true)] // default docker registry is considered valid
// // Invalid tests
[InlineData("mcr.mi-=crosoft.com", false)] // invalid url
[InlineData("mcr.microsoft.com/", false)] // invalid url
public void IsValidRegistry(string registry, bool expectedReturn)
{
Console.WriteLine($"Domain pattern is '{ReferenceParser.AnchoredDomainRegexp.ToString()}'");
Assert.Equal(expectedReturn, ContainerHelpers.IsValidRegistry(registry));
}
[Theory]
[InlineData("mcr.microsoft.com/dotnet/runtime:6.0", true, "mcr.microsoft.com", "dotnet/runtime", "6.0", true)]
[InlineData("mcr.microsoft.com/dotnet/runtime", true, "mcr.microsoft.com", "dotnet/runtime", null, true)]
[InlineData("mcr.microsoft.com/", false, null, null, null, false)] // no image = nothing resolves
// Ports tag along
[InlineData("mcr.microsoft.com:54/dotnet/runtime", true, "mcr.microsoft.com:54", "dotnet/runtime", null, true)]
// Even if nonsensical
[InlineData("mcr.microsoft.com:0/dotnet/runtime", true, "mcr.microsoft.com:0", "dotnet/runtime", null, true)]
// We don't allow hosts with missing ports when a port is anticipated
[InlineData("mcr.microsoft.com:/dotnet/runtime", false, null, null, null, false)]
// Use default registry when no registry specified.
[InlineData("ubuntu:jammy", true, DefaultRegistry, "library/ubuntu", "jammy", false)]
[InlineData("ubuntu/runtime:jammy", true, DefaultRegistry, "ubuntu/runtime", "jammy", false)]
// Alias 'docker.io' to Docker registry.
[InlineData("docker.io/ubuntu:jammy", true, DefaultRegistry, "library/ubuntu", "jammy", true)]
[InlineData("docker.io/ubuntu/runtime:jammy", true, DefaultRegistry, "ubuntu/runtime", "jammy", true)]
// 'localhost' registry.
[InlineData("localhost/ubuntu:jammy", true, "localhost", "ubuntu", "jammy", true)]
public void TryParseFullyQualifiedContainerName(string fullyQualifiedName, bool expectedReturn, string? expectedRegistry, string? expectedImage, string? expectedTag, bool expectedIsRegistrySpecified)
{
Assert.Equal(expectedReturn, ContainerHelpers.TryParseFullyQualifiedContainerName(fullyQualifiedName, out string? containerReg, out string? containerName, out string? containerTag, out string? containerDigest, out bool isRegistrySpecified));
Assert.Equal(expectedRegistry, containerReg);
Assert.Equal(expectedImage, containerName);
Assert.Equal(expectedTag, containerTag);
Assert.Equal(expectedIsRegistrySpecified, isRegistrySpecified);
}
[Theory]
[InlineData("dotnet/runtime", true)]
[InlineData("foo/bar", true)]
[InlineData("registry", true)]
[InlineData("-foo/bar", false)]
[InlineData(".foo/bar", false)]
[InlineData("_foo/bar", false)]
[InlineData("foo/bar-", false)]
[InlineData("foo/bar.", false)]
[InlineData("foo/bar_", false)]
[InlineData("--------", false)]
public void IsValidImageName(string imageName, bool expectedReturn)
{
Assert.Equal(expectedReturn, ContainerHelpers.IsValidImageName(imageName));
}
[Theory]
[InlineData("0aa", "0aa", null, null)]
[InlineData("9zz", "9zz", null, null)]
[InlineData("aa0", "aa0", null, null)]
[InlineData("zz9", "zz9", null, null)]
[InlineData("runtime", "runtime", null, null)]
[InlineData("dotnet_runtime", "dotnet_runtime", null, null)]
[InlineData("dotnet-runtime", "dotnet-runtime", null, null)]
[InlineData("dotnet/runtime", "dotnet/runtime", null, null)]
[InlineData("dotnet runtime", "dotnet-runtime", "NormalizedContainerName", null)]
[InlineData("Api", "api", "NormalizedContainerName", null)]
[InlineData("API", "api", "NormalizedContainerName", null)]
[InlineData("$runtime", null, null, "InvalidImageName_NonAlphanumericStartCharacter")]
[InlineData("-%", null, null, "InvalidImageName_NonAlphanumericStartCharacter")]
public void IsValidRepositoryName(string containerRepository, string? expectedNormalized, string? expectedWarning, string? expectedError)
{
var actual = ContainerHelpers.NormalizeRepository(containerRepository);
Assert.Equal(expectedNormalized, actual.normalizedImageName);
Assert.Equal(expectedWarning, actual.normalizationWarning?.Item1);
Assert.Equal(expectedError, actual.normalizationError?.Item1);
}
[Theory]
[InlineData("6.0", true)] // baseline
[InlineData("5.2-asd123", true)] // with commit hash
[InlineData(".6.0", false)] // starts with .
[InlineData("-6.0", false)] // starts with -
[InlineData("---", false)] // malformed
public void IsValidImageTag(string imageTag, bool expectedReturn)
{
Assert.Equal(expectedReturn, ContainerHelpers.IsValidImageTag(imageTag));
}
[Fact]
public void IsValidImageTag_InvalidLength()
{
Assert.False(ContainerHelpers.IsValidImageTag(new string('a', 129)));
}
[Theory]
[InlineData("80/tcp", true, 80, PortType.tcp, null)]
[InlineData("80", true, 80, PortType.tcp, null)]
[InlineData("125/dup", false, 125, PortType.tcp, ContainerHelpers.ParsePortError.InvalidPortType)]
[InlineData("invalidNumber", false, null, null, ContainerHelpers.ParsePortError.InvalidPortNumber)]
[InlineData("welp/unknowntype", false, null, null, (ContainerHelpers.ParsePortError)6)]
[InlineData("a/b/c", false, null, null, ContainerHelpers.ParsePortError.UnknownPortFormat)]
[InlineData("/tcp", false, null, null, ContainerHelpers.ParsePortError.MissingPortNumber)]
public void CanParsePort(string input, bool shouldParse, int? expectedPortNumber, PortType? expectedType, ContainerHelpers.ParsePortError? expectedError)
{
var parseSuccess = ContainerHelpers.TryParsePort(input, out var port, out var errors);
Assert.Equal(shouldParse, parseSuccess);
if (shouldParse)
{
Assert.NotNull(port);
Assert.Equal(port.Value.Number, expectedPortNumber);
Assert.Equal(port.Value.Type, expectedType);
}
else
{
Assert.Null(port);
Assert.NotNull(errors);
Assert.Equal(expectedError, errors);
}
}
[Theory]
[InlineData("FOO", true)]
[InlineData("foo_bar", true)]
[InlineData("foo-bar", false)]
[InlineData("foo.bar", false)]
[InlineData("foo bar", false)]
[InlineData("1_NAME", false)]
[InlineData("ASPNETCORE_URLS", true)]
[InlineData("ASPNETCORE_URLS2", true)]
public void CanRecognizeEnvironmentVariableNames(string envVarName, bool isValid)
{
var success = ContainerHelpers.IsValidEnvironmentVariable(envVarName);
Assert.Equal(isValid, success);
}
}