Skip to content
This repository was archived by the owner on Apr 28, 2018. It is now read-only.

Commit 8bcf619

Browse files
committed
Adding test support for common modules.
1 parent 29186c8 commit 8bcf619

File tree

5 files changed

+441
-0
lines changed

5 files changed

+441
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<#
2+
.DESCRIPTION
3+
This script will test the basic build functionality.
4+
5+
#>
6+
7+
. .\Utils.ps1
8+
9+
function CreateDockerfile
10+
{
11+
param(
12+
[string]
13+
[ValidateNotNullOrEmpty()]
14+
$BasePath,
15+
16+
[string]
17+
[ValidateNotNullOrEmpty()]
18+
$ImageName
19+
)
20+
21+
$filepath = Join-Path $BasePath "Dockerfile"
22+
Write-Host "Creating dockerfile at path: '$filepath'."
23+
24+
"FROM $ImageName" | Out-File -FilePath $filePath -Encoding utf8 -Append
25+
"RUN echo test > test.txt" | Out-File -FilePath $filePath -Encoding utf8 -Append
26+
27+
Write-Host "Successfully created dockerfile."
28+
}
29+
30+
function TestImageBuild
31+
{
32+
param(
33+
[string]
34+
[ValidateNotNullOrEmpty()]
35+
$ImageName,
36+
37+
[bool]
38+
$IsIsolated,
39+
40+
[string]
41+
[ValidateNotNullOrEmpty()]
42+
$Tag
43+
)
44+
45+
# We need to module imported so we can create the types before invocation.
46+
Test-ImportedModule "Docker"
47+
48+
$basePath = New-TempTestPath
49+
CreateDockerfile $basePath $ImageName
50+
51+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
52+
if ($IsIsolated)
53+
{
54+
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
55+
}
56+
57+
Write-Host "Building image: '$Tag'"
58+
Build-ContainerImage -Path "$basePath" -Repository "$Tag" -SkipCache -Isolation $isolation
59+
}
60+
61+
function TestImageBuilds
62+
{
63+
param(
64+
[string]
65+
[ValidateNotNullOrEmpty()]
66+
$ImageName,
67+
68+
[bool]
69+
$IsIsolated
70+
)
71+
72+
$tag = "test"
73+
if ($IsIsolated)
74+
{
75+
$tag = "isolated" + $tag
76+
}
77+
78+
$firstTag = $tag + "1"
79+
$secondTag = $tag + "2"
80+
81+
try
82+
{
83+
# Test a level 1 build.
84+
$image = TestImageBuild "$ImageName" $IsIsolated "$firstTag"
85+
$image | Should Not Be $null
86+
87+
# Test a second build based on the first.
88+
$image2 = TestImageBuild "$firstTag" $IsIsolated "$secondTag"
89+
$image2 | Should Not Be $null
90+
}
91+
finally
92+
{
93+
# Cleanup
94+
if ($image2)
95+
{
96+
$image2 | Remove-ContainerImage
97+
}
98+
99+
if ($image)
100+
{
101+
$image | Remove-ContainerImage
102+
}
103+
}
104+
}
105+
106+
Describe "Build-ContainerImage - Test matrix of types and hosts." {
107+
It "WindowsServerCore_Image_Build" -Skip:$(Test-Client -or Test-Nano) {
108+
{ TestImageBuilds $global:WindowsServerCore $false } | Should Not Throw
109+
}
110+
111+
It "WindowsServerCore_Isolated_Image_Build" {
112+
{ TestImageBuilds $global:WindowsServerCore $true } | Should Not Throw
113+
}
114+
115+
It "NanoServer_Image_Build" -Skip:$(Test-Client) {
116+
{ TestImageBuilds $global:NanoServer $false } | Should Not Throw
117+
}
118+
119+
It "NanoServer_Isolated_Image_Build" {
120+
{ TestImageBuilds $global:NanoServer $true } | Should Not Throw
121+
}
122+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<#
2+
.DESCRIPTION
3+
This script will test the basic Invoke-ContainerImage cmdlet operations.
4+
#>
5+
6+
. .\Utils.ps1
7+
8+
function TestInvokeContainerImage
9+
{
10+
param(
11+
[string]
12+
[ValidateNotNullOrEmpty()]
13+
$ImageName,
14+
15+
[bool]
16+
$IsIsolated
17+
)
18+
19+
# We need to module imported so we can create the types before invocation.
20+
Test-ImportedModule "Docker"
21+
22+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
23+
if ($IsIsolated)
24+
{
25+
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
26+
}
27+
28+
try
29+
{
30+
$container = Invoke-ContainerImage -Id "$ImageName" -Isolation $isolation -Command @("cmd", "/c", "echo Worked") -PassThru
31+
$container | Should Not Be $null
32+
# TODO: How to test that output is "Worked"?
33+
}
34+
finally
35+
{
36+
# Cleanup
37+
if ($container)
38+
{
39+
$container | Remove-Container
40+
}
41+
}
42+
}
43+
44+
Describe "Invoke-ContainerImage - Test matrix of types and hosts." {
45+
It "Invoke_WindowsServerCore" -Skip:$(Test-Client -or Test-Nano) {
46+
{ TestInvokeContainerImage $global:WindowsServerCore $false } | Should Not Throw
47+
}
48+
49+
It "Invoke_WindowsServerCore_Isolated" {
50+
{ TestInvokeContainerImage $global:WindowsServerCore $true } | Should Not Throw
51+
}
52+
53+
It "Invoke_NanoServer" -Skip:$(Test-Client) {
54+
{ TestInvokeContainerImage $global:NanoServer $false } | Should Not Throw
55+
}
56+
57+
It "Invoke_NanoServer_Isolated" {
58+
{ TestInvokeContainerImage $global:NanoServer $true } | Should Not Throw
59+
}
60+
}
61+
62+
function TestInvokeWithDetach()
63+
{
64+
param(
65+
[bool]
66+
$IsIsolated
67+
)
68+
69+
$name = $global:DefaultContainerImageName
70+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process
71+
72+
if ($IsIsolated)
73+
{
74+
$name = $global:DefaultIsolatedContainerImageName
75+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process
76+
}
77+
78+
try
79+
{
80+
$container = Invoke-ContainerImage -Id "$name" -Isolation $isolation -Detach -Command @("cmd", "/c", "echo Worked") -PassThru
81+
$container | Should Not Be $null
82+
# TODO: Verify no output?
83+
84+
$container | Wait-Container
85+
}
86+
finally
87+
{
88+
if ($container)
89+
{
90+
$container | Remove-Container
91+
}
92+
}
93+
}
94+
95+
Describe "Invoke-ContainerImage -Detach does not wait." {
96+
It "Detach Default" -Skip:$(Test-Client) {
97+
{ TestInvokeWithDetach $false } | Should Not Throw
98+
}
99+
100+
It "Detach Default Isolated" -Skip:$(Test-Client) {
101+
{ TestInvokeWithDetach $true } | Should Not Throw
102+
}
103+
}
104+
105+
function TestRemoveAutomatically
106+
{
107+
param(
108+
[bool]
109+
$IsIsolated
110+
)
111+
112+
$name = $global:DefaultContainerImageName
113+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process
114+
115+
if ($IsIsolated)
116+
{
117+
$name = $global:DefaultIsolatedContainerImageName
118+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process
119+
}
120+
121+
$count = (Get-Container).Count
122+
Invoke-ContainerImage -Id "$name" -Isolation $isolation -RemoveAutomatically -Command @("cmd", "/c", "echo Worked") -PassThru
123+
$newCount = (Get-Container).Count
124+
125+
$newCount | Should Be $count
126+
}
127+
128+
Describe "Invoke-ContainerImage -RemoveAutomatically cleans up." {
129+
It "RemoveAutomatically Default" -Skip:$(Test-Client) {
130+
{ TestRemoveAutomatically $false } | Should Not Throw
131+
}
132+
133+
It "RemoveAutomatically Isolated" -Skip:$(Test-Client) {
134+
{ TestRemoveAutomatically $true } | Should Not Throw
135+
}
136+
}

test/pester/NewContainer.Tests.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<#
2+
.DESCRIPTION
3+
This script will test the basic container creation.
4+
5+
#>
6+
7+
. .\Utils.ps1
8+
9+
function TestNewContainer
10+
{
11+
param(
12+
[string]
13+
[ValidateNotNullOrEmpty()]
14+
$ImageName,
15+
16+
[bool]
17+
$IsIsolated
18+
)
19+
20+
# We need to module imported so we can create the types before invocation.
21+
Test-ImportedModule "Docker"
22+
23+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
24+
if ($IsIsolated)
25+
{
26+
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
27+
}
28+
29+
try
30+
{
31+
$container = New-Container -Id "$ImageName" -Isolation $isolation -Command @("cmd", "/c", "echo Worked")
32+
$container | Should Not Be $null
33+
}
34+
finally
35+
{
36+
# Cleanup
37+
if ($container)
38+
{
39+
$container | Remove-Container
40+
}
41+
}
42+
}
43+
44+
Describe "New-Container - Test matrix of types and hosts." {
45+
It "Create_WindowsServerCore" -Skip:$(Test-Client -or Test-Nano) {
46+
{ TestNewContainer $global:WindowsServerCore $false } | Should Not Throw
47+
}
48+
49+
It "Create_WindowsServerCore_Isolated" {
50+
{ TestNewContainer $global:WindowsServerCore $true } | Should Not Throw
51+
}
52+
53+
It "Create_NanoServer" -Skip:$(Test-Client) {
54+
{ TestNewContainer $global:NanoServer $false } | Should Not Throw
55+
}
56+
57+
It "Create_NanoServer_Isolated" {
58+
{ TestNewContainer $global:NanoServer $true } | Should Not Throw
59+
}
60+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<#
2+
.DESCRIPTION
3+
This script will test the basic container creation.
4+
5+
#>
6+
7+
. .\Utils.ps1
8+
9+
function TestStartContainer
10+
{
11+
param(
12+
[string]
13+
[ValidateNotNullOrEmpty()]
14+
$ImageName,
15+
16+
[bool]
17+
$IsIsolated
18+
)
19+
20+
# We need to module imported so we can create the types before invocation.
21+
Test-ImportedModule "Docker"
22+
23+
$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
24+
if ($IsIsolated)
25+
{
26+
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
27+
}
28+
29+
try
30+
{
31+
$container = New-Container -Id "$ImageName" -Isolation $isolation -Command @("cmd", "/c", "echo Worked")
32+
$container | Should Not Be $null
33+
34+
$container | Start-Container
35+
36+
$container | Wait-Container
37+
}
38+
finally
39+
{
40+
# Cleanup
41+
if ($container)
42+
{
43+
$container | Remove-Container
44+
}
45+
}
46+
}
47+
48+
Describe "Start-Container - Test matrix of types and hosts." {
49+
It "Start_WindowsServerCore" -Skip:$(Test-Client -or Test-Nano) {
50+
{ TestStartContainer $global:WindowsServerCore $false } | Should Not Throw
51+
}
52+
53+
It "Start_WindowsServerCore_Isolated" {
54+
{ TestStartContainer $global:WindowsServerCore $true } | Should Not Throw
55+
}
56+
57+
It "Start_NanoServer" -Skip:$(Test-Client) {
58+
{ TestStartContainer $global:NanoServer $false } | Should Not Throw
59+
}
60+
61+
It "Start_NanoServer_Isolated" {
62+
{ TestStartContainer $global:NanoServer $true } | Should Not Throw
63+
}
64+
}

0 commit comments

Comments
 (0)