Skip to content

Commit a3b76a7

Browse files
author
Bavneet Singh
committed
add pester tests for k8s-extension
1 parent 39197b0 commit a3b76a7

33 files changed

Lines changed: 2239 additions & 0 deletions

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.4.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+
Write-Host "Installing connectedk8s..."
22+
az extension add -n connectedk8s
23+
if (!$?) {
24+
Write-Host "Unable to install connectedk8s, exiting..."
25+
exit 1
26+
}
27+
}
28+
29+
Write-Host "Onboard cluster to Azure...starting!"
30+
31+
az group show --name $envConfig.resourceGroup
32+
if (!$?) {
33+
Write-Host "Resource group does not exist, creating it now in region 'eastus2euap'"
34+
az group create --name $envConfig.resourceGroup --location eastus2euap
35+
36+
if (!$?) {
37+
Write-Host "Failed to create Resource Group - exiting!"
38+
Exit 1
39+
}
40+
}
41+
42+
# Skip creating the AKS Cluster if this is CI
43+
if (!$CI) {
44+
az aks show -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName
45+
if (!$?) {
46+
Write-Host "Cluster does not exist, creating it now"
47+
az aks create -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName --generate-ssh-keys
48+
} else {
49+
Write-Host "Cluster already exists, no need to create it."
50+
}
51+
52+
Write-Host "Retrieving credentials for your AKS cluster..."
53+
54+
az aks get-credentials -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName -f tmp/KUBECONFIG
55+
if (!$?)
56+
{
57+
Write-Host "Cluster did not create successfully, exiting!" -ForegroundColor Red
58+
Exit 1
59+
}
60+
Write-Host "Successfully retrieved the AKS kubectl credentials"
61+
} else {
62+
Copy-Item $HOME/.kube/config -Destination $PSScriptRoot/tmp/KUBECONFIG
63+
}
64+
65+
az connectedk8s show -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.arcClusterName
66+
if ($?)
67+
{
68+
Write-Host "Cluster is already connected, no need to re-connect"
69+
Exit 0
70+
}
71+
72+
Write-Host "Connecting the cluster to Arc with connectedk8s..."
73+
$Env:KUBECONFIG="$PSScriptRoot/tmp/KUBECONFIG"
74+
az connectedk8s connect -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.arcClusterName -l uksouth
75+
if (!$?)
76+
{
77+
kubectl get pods -A
78+
Exit 1
79+
}
80+
Write-Host "Successfully onboarded the cluster to Azure"

testing/Cleanup.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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
15+
if (!$?)
16+
{
17+
kubectl get pods -A
18+
kubectl logs -l app.kubernetes.io/component=cluster-metadata-operator -n azure-arc -c cluster-metadata-operator
19+
Exit 0
20+
}
21+
22+
# Skip deleting the AKS Cluster if this is CI
23+
if (!$CI) {
24+
Write-Host "Deleting the AKS cluster from Azure..."
25+
az aks delete -g $ENVCONFIG.resourceGroup -n $ENVCONFIG.aksClusterName
26+
if (Test-Path -Path $PSScriptRoot/tmp) {
27+
Write-Host "Deleting the tmp directory from the test directory"
28+
Remove-Item -Path $PSScriptRoot/tmp -Force -Confirm:$false
29+
}
30+
}

testing/README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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: Generate `k8s-extension` .whl package
41+
42+
You can perform the generation of the `k8s-extension` .whl package by running the following command
43+
44+
```bash
45+
azdev setup -r . -e k8s-extension
46+
azdev extension build k8s-extension
47+
```
48+
49+
Additional guidance for build the extension .whl package can be found [here](https://github.com/Azure/azure-cli/blob/master/doc/extensions/authoring.md#building)
50+
51+
### Step 4: Update the `k8s-extension`/`k8s-extension-private` .whl package
52+
53+
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.
54+
55+
56+
### Step 5: Create a `settings.json`
57+
58+
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.
59+
60+
### Step 6: Update the extension version value in `settings.json`
61+
62+
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
63+
```json
64+
{
65+
"k8s-extension": "0.1.0",
66+
"k8s-extension-private": "0.1.0.openservicemesh_5",
67+
"connectedk8s": "0.3.5"
68+
}
69+
70+
```
71+
72+
_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_
73+
74+
### Step 7: Run the Bootstrap Command
75+
To bootstrap the environment with AKS and Arc clusters, run
76+
```powershell
77+
.\Bootstrap.ps1
78+
```
79+
This script will provision the AKS and Arc clusters needed to run the integration test suite
80+
81+
## Testing
82+
83+
### Testing All Extension Suites
84+
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
85+
86+
| `-ExtensionType` | Installs `az extension` |
87+
| ---------------- | --------------------- |
88+
| `Public` | `k8s-extension` |
89+
| `Private` | `k8s-extension-private` |
90+
91+
For example, when calling
92+
```bash
93+
.\Test.ps1 -ExtensionType Public
94+
```
95+
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
96+
97+
### Testing Public Extensions Only
98+
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
99+
```bash
100+
.\Test.ps1 -ExtensionType Public -OnlyPublicTests
101+
```
102+
103+
### Testing Specific Extension Suite
104+
105+
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
106+
107+
```powershell
108+
.\Test.ps1 -Path <path\to\extensionsuite>
109+
```
110+
For example to call the `AzureMonitor.Tests.ps1` test suite, we run
111+
```powershell
112+
.\Test.ps1 -ExtensionType Public -Path .\test\extensions\public\AzureMonitor.Tests.ps1
113+
```
114+
115+
### Skipping Extension Re-Install
116+
117+
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.
118+
119+
```powershell
120+
.\Test.ps1 -ExtensionType Public -SkipInstall
121+
```
122+
123+
## Cleanup
124+
To cleanup the AKS and Arc clusters you have provisioned in testing, run
125+
```powershell
126+
.\Cleanup.ps1
127+
```
128+
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+
}
41.4 KB
Binary file not shown.
54.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)