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

Commit

Permalink
Adding test support for common modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
jterry75 committed Sep 29, 2016
1 parent 29186c8 commit 8bcf619
Show file tree
Hide file tree
Showing 5 changed files with 441 additions and 0 deletions.
122 changes: 122 additions & 0 deletions test/pester/BuildContainer.Tests.ps1
@@ -0,0 +1,122 @@
<#
.DESCRIPTION
This script will test the basic build functionality.
#>

. .\Utils.ps1

function CreateDockerfile
{
param(
[string]
[ValidateNotNullOrEmpty()]
$BasePath,

[string]
[ValidateNotNullOrEmpty()]
$ImageName
)

$filepath = Join-Path $BasePath "Dockerfile"
Write-Host "Creating dockerfile at path: '$filepath'."

"FROM $ImageName" | Out-File -FilePath $filePath -Encoding utf8 -Append
"RUN echo test > test.txt" | Out-File -FilePath $filePath -Encoding utf8 -Append

Write-Host "Successfully created dockerfile."
}

function TestImageBuild
{
param(
[string]
[ValidateNotNullOrEmpty()]
$ImageName,

[bool]
$IsIsolated,

[string]
[ValidateNotNullOrEmpty()]
$Tag
)

# We need to module imported so we can create the types before invocation.
Test-ImportedModule "Docker"

$basePath = New-TempTestPath
CreateDockerfile $basePath $ImageName

$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
if ($IsIsolated)
{
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
}

Write-Host "Building image: '$Tag'"
Build-ContainerImage -Path "$basePath" -Repository "$Tag" -SkipCache -Isolation $isolation
}

function TestImageBuilds
{
param(
[string]
[ValidateNotNullOrEmpty()]
$ImageName,

[bool]
$IsIsolated
)

$tag = "test"
if ($IsIsolated)
{
$tag = "isolated" + $tag
}

$firstTag = $tag + "1"
$secondTag = $tag + "2"

try
{
# Test a level 1 build.
$image = TestImageBuild "$ImageName" $IsIsolated "$firstTag"
$image | Should Not Be $null

# Test a second build based on the first.
$image2 = TestImageBuild "$firstTag" $IsIsolated "$secondTag"
$image2 | Should Not Be $null
}
finally
{
# Cleanup
if ($image2)
{
$image2 | Remove-ContainerImage
}

if ($image)
{
$image | Remove-ContainerImage
}
}
}

Describe "Build-ContainerImage - Test matrix of types and hosts." {
It "WindowsServerCore_Image_Build" -Skip:$(Test-Client -or Test-Nano) {
{ TestImageBuilds $global:WindowsServerCore $false } | Should Not Throw
}

It "WindowsServerCore_Isolated_Image_Build" {
{ TestImageBuilds $global:WindowsServerCore $true } | Should Not Throw
}

It "NanoServer_Image_Build" -Skip:$(Test-Client) {
{ TestImageBuilds $global:NanoServer $false } | Should Not Throw
}

It "NanoServer_Isolated_Image_Build" {
{ TestImageBuilds $global:NanoServer $true } | Should Not Throw
}
}
136 changes: 136 additions & 0 deletions test/pester/InvokeContainerImage.Tests.ps1
@@ -0,0 +1,136 @@
<#
.DESCRIPTION
This script will test the basic Invoke-ContainerImage cmdlet operations.
#>

. .\Utils.ps1

function TestInvokeContainerImage
{
param(
[string]
[ValidateNotNullOrEmpty()]
$ImageName,

[bool]
$IsIsolated
)

# We need to module imported so we can create the types before invocation.
Test-ImportedModule "Docker"

$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
if ($IsIsolated)
{
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
}

try
{
$container = Invoke-ContainerImage -Id "$ImageName" -Isolation $isolation -Command @("cmd", "/c", "echo Worked") -PassThru
$container | Should Not Be $null
# TODO: How to test that output is "Worked"?
}
finally
{
# Cleanup
if ($container)
{
$container | Remove-Container
}
}
}

Describe "Invoke-ContainerImage - Test matrix of types and hosts." {
It "Invoke_WindowsServerCore" -Skip:$(Test-Client -or Test-Nano) {
{ TestInvokeContainerImage $global:WindowsServerCore $false } | Should Not Throw
}

It "Invoke_WindowsServerCore_Isolated" {
{ TestInvokeContainerImage $global:WindowsServerCore $true } | Should Not Throw
}

It "Invoke_NanoServer" -Skip:$(Test-Client) {
{ TestInvokeContainerImage $global:NanoServer $false } | Should Not Throw
}

It "Invoke_NanoServer_Isolated" {
{ TestInvokeContainerImage $global:NanoServer $true } | Should Not Throw
}
}

function TestInvokeWithDetach()
{
param(
[bool]
$IsIsolated
)

$name = $global:DefaultContainerImageName
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process

if ($IsIsolated)
{
$name = $global:DefaultIsolatedContainerImageName
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process
}

try
{
$container = Invoke-ContainerImage -Id "$name" -Isolation $isolation -Detach -Command @("cmd", "/c", "echo Worked") -PassThru
$container | Should Not Be $null
# TODO: Verify no output?

$container | Wait-Container
}
finally
{
if ($container)
{
$container | Remove-Container
}
}
}

Describe "Invoke-ContainerImage -Detach does not wait." {
It "Detach Default" -Skip:$(Test-Client) {
{ TestInvokeWithDetach $false } | Should Not Throw
}

It "Detach Default Isolated" -Skip:$(Test-Client) {
{ TestInvokeWithDetach $true } | Should Not Throw
}
}

function TestRemoveAutomatically
{
param(
[bool]
$IsIsolated
)

$name = $global:DefaultContainerImageName
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process

if ($IsIsolated)
{
$name = $global:DefaultIsolatedContainerImageName
$isolation = [Docker.PowerShell.Objects.IsolationType]::Process
}

$count = (Get-Container).Count
Invoke-ContainerImage -Id "$name" -Isolation $isolation -RemoveAutomatically -Command @("cmd", "/c", "echo Worked") -PassThru
$newCount = (Get-Container).Count

$newCount | Should Be $count
}

Describe "Invoke-ContainerImage -RemoveAutomatically cleans up." {
It "RemoveAutomatically Default" -Skip:$(Test-Client) {
{ TestRemoveAutomatically $false } | Should Not Throw
}

It "RemoveAutomatically Isolated" -Skip:$(Test-Client) {
{ TestRemoveAutomatically $true } | Should Not Throw
}
}
60 changes: 60 additions & 0 deletions test/pester/NewContainer.Tests.ps1
@@ -0,0 +1,60 @@
<#
.DESCRIPTION
This script will test the basic container creation.
#>

. .\Utils.ps1

function TestNewContainer
{
param(
[string]
[ValidateNotNullOrEmpty()]
$ImageName,

[bool]
$IsIsolated
)

# We need to module imported so we can create the types before invocation.
Test-ImportedModule "Docker"

$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
if ($IsIsolated)
{
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
}

try
{
$container = New-Container -Id "$ImageName" -Isolation $isolation -Command @("cmd", "/c", "echo Worked")
$container | Should Not Be $null
}
finally
{
# Cleanup
if ($container)
{
$container | Remove-Container
}
}
}

Describe "New-Container - Test matrix of types and hosts." {
It "Create_WindowsServerCore" -Skip:$(Test-Client -or Test-Nano) {
{ TestNewContainer $global:WindowsServerCore $false } | Should Not Throw
}

It "Create_WindowsServerCore_Isolated" {
{ TestNewContainer $global:WindowsServerCore $true } | Should Not Throw
}

It "Create_NanoServer" -Skip:$(Test-Client) {
{ TestNewContainer $global:NanoServer $false } | Should Not Throw
}

It "Create_NanoServer_Isolated" {
{ TestNewContainer $global:NanoServer $true } | Should Not Throw
}
}
64 changes: 64 additions & 0 deletions test/pester/StartContainer.Tests.ps1
@@ -0,0 +1,64 @@
<#
.DESCRIPTION
This script will test the basic container creation.
#>

. .\Utils.ps1

function TestStartContainer
{
param(
[string]
[ValidateNotNullOrEmpty()]
$ImageName,

[bool]
$IsIsolated
)

# We need to module imported so we can create the types before invocation.
Test-ImportedModule "Docker"

$isolation = [Docker.PowerShell.Objects.IsolationType]::Default
if ($IsIsolated)
{
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
}

try
{
$container = New-Container -Id "$ImageName" -Isolation $isolation -Command @("cmd", "/c", "echo Worked")
$container | Should Not Be $null

$container | Start-Container

$container | Wait-Container
}
finally
{
# Cleanup
if ($container)
{
$container | Remove-Container
}
}
}

Describe "Start-Container - Test matrix of types and hosts." {
It "Start_WindowsServerCore" -Skip:$(Test-Client -or Test-Nano) {
{ TestStartContainer $global:WindowsServerCore $false } | Should Not Throw
}

It "Start_WindowsServerCore_Isolated" {
{ TestStartContainer $global:WindowsServerCore $true } | Should Not Throw
}

It "Start_NanoServer" -Skip:$(Test-Client) {
{ TestStartContainer $global:NanoServer $false } | Should Not Throw
}

It "Start_NanoServer_Isolated" {
{ TestStartContainer $global:NanoServer $true } | Should Not Throw
}
}

0 comments on commit 8bcf619

Please sign in to comment.