-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDockerExecutorTests.cs
117 lines (105 loc) · 5.75 KB
/
DockerExecutorTests.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Docker.DotNet;
using Docker.DotNet.Models;
using Moq;
using Tes.Runner.Authentication;
using Tes.Runner.Docker;
using Tes.Runner.Exceptions;
using Tes.Runner.Logs;
using Tes.Runner.Models;
namespace Tes.Runner.Test.Docker
{
[TestClass, TestCategory("Unit")]
public class DockerExecutorTests
{
private IDockerClient dockerClient = null!;
private Mock<IImageOperations> dockerImageMock = null!;
private IStreamLogReader streamLogReader = null!;
private ContainerRegistryAuthorizationManager containerRegistryAuthorizationManager = null!;
[TestInitialize]
public void SetUp()
{
streamLogReader = new Mock<IStreamLogReader>().Object;
dockerImageMock = new();
Mock<IDockerClient> dockerClientMock = new();
dockerClientMock.Setup(d => d.Images).Returns(dockerImageMock.Object);
dockerClient = dockerClientMock.Object;
var credentialsManager = new Mock<CredentialsManager>();
credentialsManager.Setup(m => m.GetTokenCredential(It.IsAny<RuntimeOptions>(), It.IsAny<string>()))
.Throws(new IdentityUnavailableException());
containerRegistryAuthorizationManager = new(credentialsManager.Object);
}
[DataTestMethod]
[DataRow(System.Net.HttpStatusCode.Forbidden, "")]
[DataRow(System.Net.HttpStatusCode.Unauthorized, "")]
[DataRow(System.Net.HttpStatusCode.InternalServerError, "{\"message\":\"Head \\\"https://msftsc022830.azurecr.io/v2/broadinstitute/gatk/manifests/4.5.0.0-squash\\\": unauthorized: authentication required, visit https://aka.ms/acr/authorization for more information.\"}")]
public async Task RunOnContainerAsync_DockerClientReturnsAuthNeeded_CallsContainerRegistryAuthorizationManager(System.Net.HttpStatusCode statusCode, string responseBody)
{
var exception = new DockerApiException(statusCode, responseBody);
dockerImageMock.Setup(d => d.CreateImageAsync(It.IsAny<ImagesCreateParameters>(), It.IsAny<AuthConfig>(), It.IsAny<IProgress<JSONMessage>>(), It.IsAny<CancellationToken>()))
.Throws(exception);
DockerExecutor executor = new(dockerClient, streamLogReader, containerRegistryAuthorizationManager);
Models.RuntimeOptions runtimeOptions = new();
try
{
var result = await executor.RunOnContainerAsync(new("msftsc022830.azurecr.io/broadinstitute/gatk", "4.5.0.0-squash", [""], default, default, runtimeOptions));
}
catch (IdentityUnavailableException) { } // Success
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
Assert.AreEqual(2, dockerImageMock.Invocations.Count);
}
[DataTestMethod]
[DataRow(System.Net.HttpStatusCode.BadRequest, "")]
[DataRow(System.Net.HttpStatusCode.InternalServerError, "{\"message\":\"Something went wrong: badrequest: something else happended.\"}")]
public async Task RunOnContainerAsync_DockerClientReturnsOtherError_DoesNotCallContainerRegistryAuthorizationManager(System.Net.HttpStatusCode statusCode, string responseBody)
{
DockerExecutor.dockerPullRetryPolicyOptions.ExponentialBackOffExponent = 1;
var exception = new DockerApiException(statusCode, responseBody);
dockerImageMock.Setup(d => d.CreateImageAsync(It.IsAny<ImagesCreateParameters>(), It.IsAny<AuthConfig>(), It.IsAny<IProgress<JSONMessage>>(), It.IsAny<CancellationToken>()))
.Throws(exception);
DockerExecutor executor = new(dockerClient, streamLogReader, containerRegistryAuthorizationManager);
Models.RuntimeOptions runtimeOptions = new();
try
{
var result = await executor.RunOnContainerAsync(new("msftsc022830.azurecr.io/broadinstitute/gatk", "4.5.0.0-squash", [""], default, default, runtimeOptions));
Assert.Fail();
}
catch (IdentityUnavailableException)
{
Assert.Fail();
}
catch (Exception ex)
{
Assert.AreSame(exception, ex);
}
Assert.AreEqual(2 + DockerExecutor.dockerPullRetryPolicyOptions.MaxRetryCount, dockerImageMock.Invocations.Count);
}
//[DataTestMethod]
//[DataRow(System.Net.HttpStatusCode.BadRequest, "")]
//[DataRow(System.Net.HttpStatusCode.InternalServerError, "{\"message\":\"Something went wrong: badrequest: something else happended.\"}")]
//public async Task RunOnContainerAsync_DockerClientReturnsSuccess_DoesNotCallContainerRegistryAuthorizationManager(System.Net.HttpStatusCode statusCode, string responseBody)
//{
// dockerImageMock.Setup(d => d.CreateImageAsync(It.IsAny<ImagesCreateParameters>(), It.IsAny<AuthConfig>(), It.IsAny<IProgress<JSONMessage>>(), It.IsAny<CancellationToken>()))
// .Returns(Task.CompletedTask);
// DockerExecutor executor = new(dockerClient, streamLogReader, containerRegistryAuthorizationManager);
// Models.RuntimeOptions runtimeOptions = new();
// try
// {
// var result = await executor.RunOnContainerAsync(new("msftsc022830.azurecr.io/broadinstitute/gatk", "4.5.0.0-squash", [""], default, default, runtimeOptions));
// }
// catch (IdentityUnavailableException)
// {
// Assert.Fail();
// }
// catch (Exception ex)
// {
// Assert.Fail(ex.Message);
// }
// Assert.AreEqual(1, dockerImageMock.Invocations.Count);
//}
}
}