Skip to content

Commit 4c06e4b

Browse files
author
Bavneet Singh
committed
add pester tests for k8s-configuration
1 parent 1734250 commit 4c06e4b

33 files changed

+2130
-0
lines changed

testing/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
settings.json
2+
tmp/
3+
bin/*
4+
!bin/connectedk8s-1.0.0-py3-none-any.whl
5+
!bin/k8s_extension-0.3.0-py3-none-any.whl
6+
!bin/k8s_extension_private-0.1.0-py3-none-any.whl
7+
!bin/k8s_configuration-1.0.0-py3-none-any.whl
8+
!bin/connectedk8s-values.yaml
9+
*.xml

testing/Bootstrap.ps1

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
param (
2+
[switch] $SkipInstall,
3+
[switch] $CI
4+
)
5+
6+
# Disable confirm prompt for script
7+
az config set core.disable_confirm_prompt=true
8+
9+
# Configuring the environment
10+
$ENVCONFIG = Get-Content -Path $PSScriptRoot/settings.json | ConvertFrom-Json
11+
12+
az account set --subscription $ENVCONFIG.subscriptionId
13+
14+
if (-not (Test-Path -Path $PSScriptRoot/tmp)) {
15+
New-Item -ItemType Directory -Path $PSScriptRoot/tmp
16+
}
17+
18+
if (!$SkipInstall) {
19+
Write-Host "Removing the old connnectedk8s extension..."
20+
az extension remove -n connectedk8s
21+
$connectedk8sVersion = $ENVCONFIG.extensionVersion.connectedk8s
22+
if (!$connectedk8sVersion) {
23+
Write-Host "connectedk8s extension version wasn't specified" -ForegroundColor Red
24+
Exit 1
25+
}
26+
Write-Host "Installing connectedk8s version $connectedk8sVersion..."
27+
az extension add --source ./bin/connectedk8s-$connectedk8sVersion-py3-none-any.whl
28+
if (!$?) {
29+
Write-Host "Unable to find connectedk8s version $connectedk8sVersion, exiting..."
30+
exit 1
31+
}
32+
}
33+
34+
Write-Host "Onboard cluster to Azure...starting!"
35+
36+
az group show --name $envConfig.resourceGroup
37+
if (!$?) {
38+
Write-Host "Resource group does not exist, creating it now in region 'eastus2euap'"
39+
az group create --name $envConfig.resourceGroup --location eastus2euap
40+
41+
if (!$?) {
42+
Write-Host "Failed to create Resource Group - exiting!"
43+
Exit 1
44+
}
45+
}
46+
47+
# Skip creating the AKS Cluster if this is CI
48+
if (!$CI) {
49+
az aks show -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName
50+
if (!$?) {
51+
Write-Host "Cluster does not exist, creating it now"
52+
az aks create -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName --generate-ssh-keys
53+
} else {
54+
Write-Host "Cluster already exists, no need to create it."
55+
}
56+
57+
Write-Host "Retrieving credentials for your AKS cluster..."
58+
59+
az aks get-credentials -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName -f tmp/KUBECONFIG
60+
if (!$?)
61+
{
62+
Write-Host "Cluster did not create successfully, exiting!" -ForegroundColor Red
63+
Exit 1
64+
}
65+
Write-Host "Successfully retrieved the AKS kubectl credentials"
66+
} else {
67+
Copy-Item $HOME/.kube/config -Destination $PSScriptRoot/tmp/KUBECONFIG
68+
}
69+
70+
az connectedk8s show -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.arcClusterName
71+
if ($?)
72+
{
73+
Write-Host "Cluster is already connected, no need to re-connect"
74+
Exit 0
75+
}
76+
77+
Write-Host "Connecting the cluster to Arc with connectedk8s..."
78+
$Env:KUBECONFIG="$PSScriptRoot/tmp/KUBECONFIG"
79+
$Env:HELMVALUESPATH="$PSScriptRoot/bin/connectedk8s-values.yaml"
80+
az connectedk8s connect -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.arcClusterName -l uksouth

testing/Cleanup.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
param (
2+
[switch] $CI
3+
)
4+
5+
# Disable confirm prompt for script
6+
az config set core.disable_confirm_prompt=true
7+
8+
$ENVCONFIG = Get-Content -Path $PSScriptRoot/settings.json | ConvertFrom-Json
9+
10+
az account set --subscription $ENVCONFIG.subscriptionId
11+
12+
$Env:KUBECONFIG="$PSScriptRoot/tmp/KUBECONFIG"
13+
Write-Host "Removing the connectedk8s arc agents from the cluster..."
14+
az connectedk8s delete -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.arcClusterName -y
15+
16+
# Skip deleting the AKS Cluster if this is CI
17+
if (!$CI) {
18+
Write-Host "Deleting the AKS cluster from Azure..."
19+
az aks delete -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName
20+
if (Test-Path -Path $PSScriptRoot/tmp) {
21+
Write-Host "Deleting the tmp directory from the test directory"
22+
Remove-Item -Path $PSScriptRoot/tmp -Force -Confirm:$false
23+
}
24+
}

testing/README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# K8s Partner Extension Test Suite
2+
3+
This repository serves as the integration testing suite for the `k8s-extension` Azure CLI module.
4+
5+
## Testing Requirements
6+
7+
All partners who wish to merge their __Custom Private Preview Release__ (owner: _Partner_) into the __Official Private Preview Release__ are required to author additional integration tests for their extension to ensure that their extension will continue to function correctly as more extensions are added into the __Official Private Preview Release__.
8+
9+
For more information on creating these tests, see [Authoring Tests](docs/test_authoring.md)
10+
11+
## Pre-Requisites
12+
13+
In order to properly test all regression tests within the test suite, you must onboard an AKS cluster which you will use to generate your Azure Arc resource to test the extensions. Ensure that you have a resource group where you can onboard this cluster.
14+
15+
### Required Installations
16+
17+
The following installations are required in your environment for the integration tests to run correctly:
18+
19+
1. [Helm 3](https://helm.sh/docs/intro/install/)
20+
2. [Kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
21+
3. [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
22+
23+
## Setup
24+
25+
### Step 1: Install Pester
26+
27+
This project contains [Pester](https://pester.dev/) test framework commands that are required for the integration tests to run. In an admin powershell terminal, run
28+
29+
```powershell
30+
Install-Module Pester -Force -SkipPublisherCheck
31+
Import-Module Pester -PassThru
32+
```
33+
34+
If you run into issues installing the framework, refer to the [Installation Guide](https://pester.dev/docs/introduction/installation) provided by the Pester docs.
35+
36+
### Step 2: Get Test suite files
37+
38+
You can either clone this repo (preferred option, since you will be adding your tests to this suite) or copy the files in this repo locally. Rest of the instructions here assume your working directory is k8spartner-extension-testing.
39+
40+
### Step 3: Update the `k8s-extension`/`k8s-extension-private` .whl package
41+
42+
This integration test suite references the .whl packages found in the `\bin` directory. After generating your `k8s-extension`/`k8s-extension-private` .whl package, copy your updated package into the `\bin` directory.
43+
44+
### Step 4: Create a `settings.json`
45+
46+
To onboard the AKS and Arc clusters correctly, you will need to create a `settings.json` configuration. Create a new `settings.json` file by copying the contents of the `settings.template.json` into this file. Update the subscription id, resource group, and AKS and Arc cluster name fields with your specific values.
47+
48+
### Step 5: Update the extension version value in `settings.json`
49+
50+
To ensure that the tests point to your `k8s-extension-private` `.whl` package, change the value of the `k8s-extension-private` to match your package versioning in the format (Major.Minor.Patch.Extension). For example, the `k8s_extension_private-0.1.0.openservicemesh_5-py3-none-any.whl` whl package would have extension versions set to
51+
```json
52+
{
53+
"k8s-extension": "0.1.0",
54+
"k8s-extension-private": "0.1.0.openservicemesh_5",
55+
"connectedk8s": "0.3.5"
56+
}
57+
58+
```
59+
60+
_Note: Updates to the `connectedk8s` version and `k8s-extension` version can also be made by adding a different version of the `connectedk8s` and `k8s-extension` whl packages and changing the `connectedk8s` and `k8s-extension` values to match the (Major.Minor.Patch) version format shown above_
61+
62+
### Step 6: Run the Bootstrap Command
63+
To bootstrap the environment with AKS and Arc clusters, run
64+
```powershell
65+
.\Bootstrap.ps1
66+
```
67+
This script will provision the AKS and Arc clusters needed to run the integration test suite
68+
69+
## Testing
70+
71+
### Testing All Extension Suites
72+
To test all extension test suites, you must call `.\Test.ps1` with the `-ExtensionType` parameter set to either `Public` or `Private`. Based on this flag, the test suite will install the extension type specified below
73+
74+
| `-ExtensionType` | Installs `az extension` |
75+
| ---------------- | --------------------- |
76+
| `Public` | `k8s-extension` |
77+
| `Private` | `k8s-extension-private` |
78+
79+
For example, when calling
80+
```bash
81+
.\Test.ps1 -ExtensionType Public
82+
```
83+
the script will install your `k8s-extension` whl package and run the full test suite of `*.Tests.ps1` files included in the `\test\extensions` directory
84+
85+
### Testing Public Extensions Only
86+
If you only want to run the test cases against public-preview or GA extension test cases, you can use the `-OnlyPublicTests` flag to specify this
87+
```bash
88+
.\Test.ps1 -ExtensionType Public -OnlyPublicTests
89+
```
90+
91+
### Testing Specific Extension Suite
92+
93+
If you only want to run the test script on your specific test file, you can do so by specifying path to your extension test suite in the execution call
94+
95+
```powershell
96+
.\Test.ps1 -Path <path\to\extensionsuite>
97+
```
98+
For example to call the `AzureMonitor.Tests.ps1` test suite, we run
99+
```powershell
100+
.\Test.ps1 -ExtensionType Public -Path .\test\extensions\public\AzureMonitor.Tests.ps1
101+
```
102+
103+
### Skipping Extension Re-Install
104+
105+
By default the `Test.ps1` script will uninstall any old versions of `k8s-extension`/'`k8s-extension-private` and re-install the version specified in `settings.json`. If you do not want this re-installation to occur, you can specify the `-SkipInstall` flag to skip this process.
106+
107+
```powershell
108+
.\Test.ps1 -ExtensionType Public -SkipInstall
109+
```
110+
111+
## Cleanup
112+
To cleanup the AKS and Arc clusters you have provisioned in testing, run
113+
```powershell
114+
.\Cleanup.ps1
115+
```
116+
This will remove the AKS and Arc clusters as well as the `\tmp` directory that were created by the bootstrapping script.

testing/Test.ps1

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
param (
2+
[string] $Path,
3+
[switch] $SkipInstall,
4+
[switch] $CI,
5+
[switch] $ParallelCI,
6+
[switch] $OnlyPublicTests,
7+
8+
[Parameter(Mandatory=$True)]
9+
[ValidateSet('k8s-extension','k8s-configuration', 'k8s-extension-private')]
10+
[string]$Type
11+
)
12+
13+
# Disable confirm prompt for script
14+
# Only show errors, don't show warnings
15+
az config set core.disable_confirm_prompt=true
16+
az config set core.only_show_errors=true
17+
18+
$ENVCONFIG = Get-Content -Path $PSScriptRoot/settings.json | ConvertFrom-Json
19+
20+
az account set --subscription $ENVCONFIG.subscriptionId
21+
22+
$Env:KUBECONFIG="$PSScriptRoot/tmp/KUBECONFIG"
23+
$TestFileDirectory="$PSScriptRoot/results"
24+
25+
if (-not (Test-Path -Path $TestFileDirectory)) {
26+
New-Item -ItemType Directory -Path $TestFileDirectory
27+
}
28+
29+
if ($Type -eq 'k8s-extension') {
30+
$k8sExtensionVersion = $ENVCONFIG.extensionVersion.'k8s-extension'
31+
$Env:K8sExtensionName = "k8s-extension"
32+
33+
if (!$SkipInstall) {
34+
Write-Host "Removing the old k8s-extension extension..."
35+
az extension remove -n k8s-extension
36+
Write-Host "Installing k8s-extension version $k8sExtensionVersion..."
37+
az extension add --source ./bin/k8s_extension-$k8sExtensionVersion-py3-none-any.whl
38+
if (!$?) {
39+
Write-Host "Unable to find k8s-extension version $k8sExtensionVersion, exiting..."
40+
exit 1
41+
}
42+
}
43+
if ($OnlyPublicTests) {
44+
$testFilePath = "$PSScriptRoot/test/extensions/public"
45+
} else {
46+
$testFilePath = "$PSScriptRoot/test/extensions"
47+
}
48+
} elseif ($Type -eq 'k8s-extension-private') {
49+
$k8sExtensionPrivateVersion = $ENVCONFIG.extensionVersion.'k8s-extension-private'
50+
$Env:K8sExtensionName = "k8s-extension-private"
51+
52+
if (!$SkipInstall) {
53+
Write-Host "Removing the old k8s-extension-private extension..."
54+
az extension remove -n k8s-extension-private
55+
Write-Host "Installing k8s-extension-private version $k8sExtensionPrivateVersion..."
56+
az extension add --source ./bin/k8s_extension_private-$k8sExtensionPrivateVersion-py3-none-any.whl
57+
if (!$?) {
58+
Write-Host "Unable to find k8s-extension-private version $k8sExtensionPrivateVersion, exiting..."
59+
exit 1
60+
}
61+
}
62+
if ($OnlyPublicTests) {
63+
$testFilePath = "$PSScriptRoot/test/extensions/public"
64+
} else {
65+
$testFilePath = "$PSScriptRoot/test/extensions"
66+
}
67+
} elseif ($Type -eq 'k8s-configuration') {
68+
$k8sConfigurationVersion = $ENVCONFIG.extensionVersion.'k8s-configuration'
69+
if (!$SkipInstall) {
70+
Write-Host "Removing the old k8s-configuration extension..."
71+
az extension remove -n k8s-configuration
72+
Write-Host "Installing k8s-configuration version $k8sConfigurationVersion..."
73+
az extension add --source ./bin/k8s_configuration-$k8sConfigurationVersion-py3-none-any.whl
74+
}
75+
$testFilePaths = "$PSScriptRoot/test/configurations"
76+
}
77+
78+
if ($ParallelCI) {
79+
# This runs the tests in parallel during the CI pipline to speed up testing
80+
81+
Write-Host "Invoking Pester to run tests from '$testFilePath'..."
82+
$testFiles = @()
83+
foreach ($paths in $testFilePaths)
84+
{
85+
$temp = Get-ChildItem $paths
86+
$testFiles += $temp
87+
}
88+
$resultFileNumber = 0
89+
foreach ($testFile in $testFiles)
90+
{
91+
$resultFileNumber++
92+
$testName = Split-Path $testFile –leaf
93+
Start-Job -ArgumentList $testName, $testFile, $resultFileNumber, $TestFileDirectory -Name $testName -ScriptBlock {
94+
param($name, $testFile, $resultFileNumber, $testFileDirectory)
95+
96+
Write-Host "$testFile to result file #$resultFileNumber"
97+
$testResult = Invoke-Pester $testFile -Passthru -Output Detailed
98+
$testResult | Export-JUnitReport -Path "$testFileDirectory/$name.xml"
99+
}
100+
}
101+
102+
do {
103+
Write-Host ">> Still running tests @ $(Get-Date –Format "HH:mm:ss")" –ForegroundColor Blue
104+
Get-Job | Where-Object { $_.State -eq "Running" } | Format-Table –AutoSize
105+
Start-Sleep –Seconds 30
106+
} while((Get-Job | Where-Object { $_.State -eq "Running" } | Measure-Object).Count -ge 1)
107+
108+
Get-Job | Wait-Job
109+
$failedJobs = Get-Job | Where-Object { -not ($_.State -eq "Completed")}
110+
Get-Job | Receive-Job –AutoRemoveJob –Wait –ErrorAction 'Continue'
111+
112+
if ($failedJobs.Count -gt 0) {
113+
Write-Host "Failed Jobs" –ForegroundColor Red
114+
$failedJobs
115+
throw "One or more tests failed"
116+
}
117+
} elseif ($CI) {
118+
if ($Path) {
119+
$testFilePath = "$PSScriptRoot/$Path"
120+
}
121+
Write-Host "Invoking Pester to run tests from '$testFilePath'..."
122+
$testResult = Invoke-Pester $testFilePath -Passthru -Output Detailed
123+
$testName = Split-Path $testFilePath –leaf
124+
$testResult | Export-JUnitReport -Path "$testFileDirectory/$testName.xml"
125+
} else {
126+
if ($Path) {
127+
Write-Host "Invoking Pester to run tests from '$PSScriptRoot/$Path'"
128+
Invoke-Pester -Output Detailed $PSScriptRoot/$Path
129+
} else {
130+
Write-Host "Invoking Pester to run tests from '$testFilePath'..."
131+
Invoke-Pester -Output Detailed $testFilePath
132+
}
133+
}
61.3 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
systemDefaultValues:
2+
extensionoperator:
3+
enabled: true
41.4 KB
Binary file not shown.
51.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)