Skip to content

Commit 0893197

Browse files
added test ps1 files
1 parent 2c4465e commit 0893197

File tree

9 files changed

+913
-0
lines changed

9 files changed

+913
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
16+
$global:SkippedTests = @(
17+
'TestListInvalidLocation',
18+
'TestCreateQuotaOnInvalidLocation'
19+
)
20+
21+
$global:Location = "northwest"
22+
$global:ResourceGroupName = "System.local"
23+
$global:Provider = "Microsoft.Compute.Admin"
24+
$global:VHDUri = "https://test.blob.local.azurestack.external/test/xenial-server-cloudimg-amd64-disk1.vhd"
25+
26+
27+
$global:Client = $null
28+
29+
if (-not $global:RunRaw) {
30+
$scriptBlock = {
31+
if ($null -eq $global:Client) {
32+
$global:Client = Get-MockClient -ClassName 'ComputeAdminClient' -TestName $global:TestName -Verbose
33+
}
34+
return $global:Client
35+
}
36+
Mock New-ServiceClient $scriptBlock -ModuleName $global:ModuleName
37+
}
38+
39+
if (Test-Path "$PSScriptRoot\Override.ps1") {
40+
. $PSScriptRoot\Override.ps1
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
$global:ModuleName = "Azs.Compute.Admin"
16+
17+
if ($global:UseInstalled) {
18+
Import-Module $global:ModuleName -Force
19+
} else {
20+
Import-Module ..\Module\$global:ModuleName -Force
21+
}
22+
23+
if (-not $global:RunRaw) {
24+
if (Test-Path bin\Debug) {
25+
Import-Module ".\bin\Debug\$global:ModuleName.Tests.dll" -Force
26+
} elseif (Test-Path bin\Release) {
27+
Import-Module ".\bin\Release\$global:ModuleName.Tests.dll" -Force
28+
} else {
29+
throw "Cannot load test dll: $global:ModuleName.Tests.dll"
30+
}
31+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
$TestRecordingFile = Join-Path $PSScriptRoot 'Get-AzsDisk.Recording.json'
2+
$currentPath = $PSScriptRoot
3+
while(-not $mockingPath) {
4+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
5+
$currentPath = Split-Path -Path $currentPath -Parent
6+
}
7+
. ($mockingPath | Select-Object -First 1).FullName
8+
9+
$Global:UseInstalled = $UseInstalled
10+
$global:RunRaw = $RunRaw
11+
12+
#. $PSScriptRoot\CommonModules.ps1
13+
14+
$global:Location = "northwest"
15+
$global:TestName = ""
16+
17+
Describe 'Get-AzsDisk' {
18+
BeforeEach {
19+
20+
. $PSScriptRoot\Common.ps1
21+
22+
function ValidateDisk {
23+
param(
24+
[Parameter(Mandatory = $true)]
25+
$Disk
26+
)
27+
28+
$Disk | Should Not Be $null
29+
$Disk.Id | Should Not Be $null
30+
$Disk.Type | Should Not Be $null
31+
$Disk.Name | Should Not Be $null
32+
$Disk.ActualSizeGB | Should Not Be $null
33+
$Disk.ProvisionSizeGB | Should Not Be $null
34+
$Disk.DiskSku | Should Not Be $null
35+
$Disk.DiskType | Should Not Be $null
36+
$Disk.SharePath | Should Not Be $null
37+
$Disk.Status | Should Not Be $null
38+
$Disk.UserResourceId | Should Not Be $null
39+
$Disk.Location | Should Not Be $null
40+
$Disk.DiskId | Should Not Be $null
41+
}
42+
43+
function ValidateDisksTheSame {
44+
param(
45+
[Parameter(Mandatory = $true)]
46+
$DisksRight,
47+
[Parameter(Mandatory = $true)]
48+
$DisksLeft
49+
)
50+
$DisksRight | Should Not Be $null
51+
$DisksLeft | Should Not Be $null
52+
$DisksRight.Count -eq $DisksLeft.Count | Should Be $true
53+
54+
$DisksRight | % {CheckDisksInList -List $DisksLeft -Disk $_ | Should Be $true}
55+
}
56+
57+
function CheckDisksInList {
58+
param(
59+
[Parameter(Mandatory = $true)]
60+
$List,
61+
[Parameter(Mandatory = $true)]
62+
$Disk
63+
)
64+
$List | Should Not Be $null
65+
$Disk | Should Not Be $null
66+
67+
$diskInList = $List | ?{$_.UserResourceId.Equals($Disk.UserResourceId)}
68+
if($diskInList)
69+
{
70+
$true
71+
}
72+
else
73+
{
74+
$false
75+
}
76+
}
77+
78+
function ValidateDiskTheSame {
79+
param(
80+
[Parameter(Mandatory = $true)]
81+
$DiskRight,
82+
[Parameter(Mandatory = $true)]
83+
$DiskLeft
84+
)
85+
$DiskRight | Should Not Be $null
86+
$DiskLeft | Should Not Be $null
87+
88+
$DiskLeft.Id -eq $DiskRight.Id | Should Be $true
89+
}
90+
}
91+
92+
It "TestListDisks" {
93+
$global:TestName = 'TestListDisks'
94+
95+
$disks = Get-AzsDisk -Location $global:Location
96+
97+
$disks | Should Not Be $null
98+
foreach ($disk in $disks) {
99+
ValidateDisk -Disk $disk
100+
}
101+
if($disks.Count -gt 0)
102+
{
103+
$firstDisk = $disks[0]
104+
$tenantSubscriptionId = $($firstDisk.UserResourceId.Split("/", [System.StringSplitOptions]::RemoveEmptyEntries))[1]
105+
$disksForSubscription = Get-AzsDisk -Location $global:Location -UserSubscriptionId $tenantSubscriptionId
106+
ValidateDisksTheSame -DisksRight $($disks | ?{$_.UserResourceId.Contains($tenantSubscriptionId)}) -DisksLeft $disksForSubscription
107+
108+
$disksForStatus = Get-AzsDisk -Location $global:Location -Status $firstDisk.Status
109+
ValidateDisksTheSame -DisksRight $($disks | ?{$_.Status.Equals($firstDisk.Status)}) -DisksLeft $disksForStatus
110+
111+
$disksForShare = Get-AzsDisk -Location $global:Location -SharePath $firstDisk.SharePath
112+
ValidateDisksTheSame -DisksRight $($disks | ?{$_.SharePath.Equals($firstDisk.SharePath)}) -DisksLeft $disksForShare
113+
114+
if ($disks.Count -ge 2)
115+
{
116+
$disksWithCountAndStart = Get-AzsDisk -Location $global:Location -Start 1 -Count 1
117+
ValidateDisksTheSame -DisksRight @($disks[1]) -DisksLeft @($disksWithCountAndStart)
118+
}
119+
}
120+
}
121+
122+
It "TestGetDisk" {
123+
$global:TestName = 'TestGetDisk'
124+
125+
$disks = Get-AzsDisk -Location $global:Location
126+
127+
$disks | Should Not Be $null
128+
foreach ($disk in $disks) {
129+
ValidateDisk -Disk $disk
130+
}
131+
if($disks.Count -gt 0)
132+
{
133+
$firstDisk = $disks[0]
134+
$diskFromServer = Get-AzsDisk -Location $global:Location -Name $firstDisk.DiskId
135+
ValidateDiskTheSame -DiskRight $firstDisk -DiskLeft $diskFromServer
136+
}
137+
}
138+
139+
It "TestGetDiskInvalid" {
140+
$global:TestName = 'TestGetDiskInvalid'
141+
142+
{Get-AzsDisk -Location $global:Location -Name "454E5E28-8D5E-41F9-929E-BFF6A7E1A253" -ErrorAction Stop} | Should Throw
143+
}
144+
}
145+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
$TestRecordingFile = Join-Path $PSScriptRoot 'Get-AzsDiskMigrationJob.Recording.json'
2+
$currentPath = $PSScriptRoot
3+
while(-not $mockingPath) {
4+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
5+
$currentPath = Split-Path -Path $currentPath -Parent
6+
}
7+
. ($mockingPath | Select-Object -First 1).FullName
8+
9+
$Global:UseInstalled = $UseInstalled
10+
$global:RunRaw = $RunRaw
11+
12+
$global:Location = "northwest"
13+
$global:TestName = ""
14+
$global:TargetShare = "\\SU1FileServer.azurestack.local\SU1_ObjStore\"
15+
16+
17+
Describe 'Get-AzsDiskMigrationJob' {
18+
BeforeEach {
19+
20+
. $PSScriptRoot\Common.ps1
21+
22+
function ValidateDiskMigration {
23+
param(
24+
[Parameter(Mandatory = $true)]
25+
$DiskMigration
26+
)
27+
28+
$DiskMigration | Should Not Be $null
29+
$DiskMigration.Id | Should Not Be $null
30+
$DiskMigration.Type | Should Not Be $null
31+
$DiskMigration.Name | Should Not Be $null
32+
$DiskMigration.CreationTime | Should Not Be $null
33+
$DiskMigration.TargetShare | Should Not Be $null
34+
$DiskMigration.Status | Should Not Be $null
35+
$DiskMigration.Location | Should Not Be $null
36+
$DiskMigration.MigrationId | Should Not Be $null
37+
}
38+
}
39+
40+
It "TestDiskMigration" {
41+
$global:TestName = 'TestDiskMigration'
42+
43+
$disks = Get-AzsDisk -Location $global:Location
44+
$disks | Should Not Be $null
45+
$toMigrationDisks = @()
46+
foreach($disk in $disks)
47+
{
48+
if ($toMigrationDisks.Count -lt 3)
49+
{
50+
$toMigrationDisks +=$disk;
51+
}
52+
else
53+
{
54+
break;
55+
}
56+
}
57+
$migrationId = "ba0644a4-c2ed-4e3c-a167-089a32865297"; # this should be the same as session Records
58+
59+
$migration = Start-AzsDiskMigrationJob -Location $global:Location -Name $migrationId -TargetShare $global:TargetShare -Disks $toMigrationDisks
60+
ValidateDiskMigration -DiskMigration $migration
61+
62+
$migration = Stop-AzsDiskMigrationJob -Location $global:Location -Name $migration.MigrationId
63+
ValidateDiskMigration -DiskMigration $migration
64+
65+
$migrationFromGet = Get-AzsDiskMigrationJob -Location $global:Location -Name $migrationId
66+
ValidateDiskMigration -DiskMigration $migrationFromGet
67+
68+
$migrationList = Get-AzsDiskMigrationJob -Location $global:Location
69+
$migrationList | %{ValidateDiskMigration -DiskMigration $_ }
70+
71+
$migrationSucceededList = Get-AzsDiskMigrationJob -Location $global:Location -Status "Succeeded"
72+
$migrationSucceededList | %{ValidateDiskMigration -DiskMigration $_ }
73+
}
74+
75+
It "TestDiskMigrationInvalidInput" {
76+
$global:TestName = 'TestDiskMigrationInvalidInput'
77+
78+
$InvalidtTargetShare = "\\SU1FileServer.azurestack.local\SU1_ObjStore_Invalid\"
79+
$disks = @()
80+
$disks += Get-AzsDisk -Location $global:Location
81+
$disks | Should Not Be $null
82+
if($disks.Count -gt 0)
83+
{
84+
$toMigrationDisks = @($disks[0])
85+
$migrationId = "A50E9E6B-CFC2-4BC7-956B-0F7C35035DF2"; # This guid should be the same as the ones in sessionRecord
86+
{Start-AzsDiskMigrationJob -Location $global:Location -Name $migrationId -TargetShare $InvalidtTargetShare -Disks $toMigrationDisks -ErrorAction Stop} | Should throw
87+
{Get-AzsDiskMigrationJob -Location $global:Location -Name $migrationId -ErrorAction Stop }| Should throw
88+
}
89+
}
90+
}
91+

src/Azs.Compute.Admin/tests/Helper.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.AzureStack.Management.Compute.Admin;
16+
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
17+
using System.Linq;
18+
using System.Management.Automation;
19+
using System.Management.Automation.Runspaces;
20+
21+
namespace Test
22+
{
23+
24+
[Cmdlet(VerbsCommon.Get, "MockClient"), OutputType(typeof(ComputeAdminClient))]
25+
public class Helper : PSCmdlet {
26+
27+
[Parameter(Mandatory = true, Position = 0, HelpMessage = "The name of your test class.")]
28+
public System.String ClassName { get; set; }
29+
30+
[Parameter(Mandatory = true, Position = 1, HelpMessage = "The name of your test.")]
31+
public System.String TestName { get; set; }
32+
33+
protected override void ProcessRecord() {
34+
System.IO.Directory.SetCurrentDirectory(this.SessionState.Path.CurrentFileSystemLocation.ToString());
35+
36+
var handler = new RecordedDelegatingHandler { StatusCodeToReturn = System.Net.HttpStatusCode.OK };
37+
handler.IsPassThrough = true;
38+
39+
var context = MockContext.Start(ClassName, TestName);
40+
var client = context.GetServiceClient<ComputeAdminClient>(handlers: handler);
41+
WriteObject(client);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)