Skip to content

Commit 9d2d89e

Browse files
authored
Added Amazon Linux 2, reduced verbosity and removed instance/resource count (#847)
1 parent 49b7d9a commit 9d2d89e

File tree

5 files changed

+202
-100
lines changed

5 files changed

+202
-100
lines changed

devops/e2e/cloudtest/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ In addition to performing distro specific setup for [Preparing Linux for imaging
1818

1919
# Azure Helpers
2020

21-
The PowerShell module [AzureInfrastructureHelpers.psm1](./AzureInfrastructureHelpers.psm1) provides the `Create-AzureManagedDisk` PowerShell cmdlet which helps onboard a generalized Hyper-V Linux VHD into an Azure Compute Gallery. See the internal documentation for full onboarding instructions.
21+
The PowerShell module [AzureInfrastructureHelpers.psm1](./AzureInfrastructureHelpers.psm1) provides the `Create-AzureManagedDisk` PowerShell cmdlet which helps onboard a generalized Hyper-V Linux VHD into an Azure Compute Gallery. See the internal documentation for full onboarding instructions ([Onboarding new VM Images](https://eng.ms/docs/cloud-ai-platform/azure-edge-platform-aep/aep-security/ens-enterprise-and-security/commercial-fist/edge-security-platform-documentation/teams/est/osconfig/linux/readme#onboarding-new-vm-images)).

src/tests/universal-nrp-e2e/README.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ To perform tests on VMs the `StartVMTest.sh` allows you to perform tests on spec
1515
- cloud-image-utils
1616

1717
## Example
18-
The following example performs tests on an [Ubuntu Noble cloud image](https://cloud-images.ubuntu.com/noble/current/) on the AzureLinuxBaseline.zip (built in directory tree) which has 168 resources defined.
18+
The following example performs tests on an [Ubuntu Noble cloud image](https://cloud-images.ubuntu.com/noble/current/) on the AzureLinuxBaseline.zip (built in directory tree).
1919
```sh
2020
# Download image https://cloud-images.ubuntu.com/
2121
wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
2222
# Add 2Gb to filesystem in order to install all necessary dependencies
2323
qemu-img resize noble-server-cloudimg-amd64.img +2G
24-
./StartVMTest.sh -i noble-server-cloudimg-amd64.img -p ../../build/AzureLinuxBaseline.zip -c 168
24+
./StartVMTest.sh -i noble-server-cloudimg-amd64.img -p ../../build/AzureLinuxBaseline.zip
2525
```
2626
Once completed, log archives are created under `_<distro-image>` directory (in this case __focal-server-cloudimg-amd64.img_) which contain the JUnit test report along with the osconfig logs contained under `/var/log/osconfig*`
2727

@@ -35,7 +35,7 @@ You need to copy over the following files onto the target machine:
3535

3636
## Example
3737
```sh
38-
./StartLocalTest.sh -p AzureLinuxBaseline.zip -c 168
38+
./StartLocalTest.sh -p AzureLinuxBaseline.zip
3939
```
4040

4141
# Testing all supported distributions (Internal-Only)
@@ -63,7 +63,7 @@ Usage: ./StartTests.sh [-r run-id] [-m vm-memory-mb] [-j max-concurrent-jobs]
6363
## Testing locally/directly on target machine
6464
Although `StartLocalTest.sh` can be used to simply test a policy package, it can also invoke "Stages" which is used by the `StartVMTest.sh` to orchestrate tests on the VM through its ssh session and provide accurate exit codes for error reporting. It also provides the `-g` flag used to [generalize the image](#generalizing-a-vm-image) which is useful when preparing an image that will be reused/shared.
6565
```
66-
Usage: ./StartLocalTest.sh [-s stage-name] [-p policy-package.zip -c resource-count [-r]]
66+
Usage: ./StartLocalTest.sh [-s stage-name] [-p policy-package.zip [-r]]
6767
-s stage-name: Specify the stage name. Valid options are: dependency_check, run_tests, collect_logs.
6868
If no stage is specified, all stages will be executed in this order:
6969
dependency_check, run_tests, collect_logs
@@ -79,8 +79,6 @@ Usage: ./StartLocalTest.sh [-s stage-name] [-p policy-package.zip -c resource-co
7979
8080
-p policy-package.zip: The Azure Policy Package to test
8181
82-
-c resource-count: The number of resources to validate, tests will fail if this doesn't match (Default: 0)
83-
8482
-r remediate-flag: When the flag is enabled, performs remediation on the Policy Package (Default: No remediation performed)
8583
8684
-g generalize-flag: Generalize the current machine for tests. Performs the following:
@@ -91,18 +89,17 @@ Usage: ./StartLocalTest.sh [-s stage-name] [-p policy-package.zip -c resource-co
9189

9290
## Testing on VMs
9391
```
94-
Usage: ./StartVMTest.sh [-i /path/to/image.img -p /path/to/policypackage.zip -c resource-count [-g]] [-m 512] [-r] [-d]
95-
-i Image Path: Path to the image qcow2 format
92+
Usage: ./StartVMTest.sh [-i image.img -p policypackage.zip [-r]] [-i image.img -g] [-m 512] [-d]
93+
-i Image Path: Path to the image (raw or qcow2 format)
9694
-p Policy Package: Path to the policy package
97-
-c Resource Count: The number of resources to validate, tests will fail if this doesn't match (Default: 0)
9895
-m VM Memory (Megabytes): Size of VMs RAM (Default: 512)
9996
-r Remediation: Perform remediation flag (Default: false)
10097
-g Generalize Flag: Generalize the current machine for tests. Performs the following:
10198
- Remove logs and tmp directories
10299
- Clean package management cache
103100
- Clean cloud-init flags to reset cloud-init to initial-state
104101
-l Log Directory: Directory used to place output logs
105-
-d Debug Mode: VM stays up for debugging (Default: false)
102+
-d Debug Mode Flag: VM stays up for debugging (Default: false)
106103
```
107104
### VM Image Sources
108105
It's preferable for images to be "Cloud" images (contain cloud-init) as it makes tooling work out-of-box and does not require creating your own image of the distro and getting all the necessary packages/dependencies installed.
@@ -248,10 +245,9 @@ qemu-img convert -c -O qcow2 CentOS-7-x86_64-GenericCloud-2211.qcow2 CentOS-7-x8
248245
```
249246
Let's try the image and ensure it works before widely sharing...
250247
```
251-
./StartVMTest.sh -i CentOS-7-x86_64-GenericCloud-2211.qcow2 -p /mnt/c/Users/ahbenmes/Downloads/AzureLinuxBaseline.zip -c 168
248+
./StartVMTest.sh -i CentOS-7-x86_64-GenericCloud-2211.qcow2 -p /mnt/c/Users/ahbenmes/Downloads/AzureLinuxBaseline.zip
252249
Image path: CentOS-7-x86_64-GenericCloud-2211.qcow2.
253250
Policy package: /mnt/c/Users/ahbenmes/Downloads/AzureLinuxBaseline.zip.
254-
Resource count: 168.
255251
...
256252
...
257253
Tests Passed: 4, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0
@@ -288,5 +284,6 @@ az storage blob upload \
288284
--container-name diskimages \
289285
--name centos-7.qcow2 \
290286
--file CentOS-7-x86_64-GenericCloud-2211-Generalized.qcow2 \
287+
--content-md5 $(md5sum CentOS-7-x86_64-GenericCloud-2211-Generalized.qcow2 | awk '{ print $1 }') \
291288
--auth-mode login
292289
```

src/tests/universal-nrp-e2e/StartLocalTest.sh

+58-14
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
# Description: This script orchestrates tests on a local machine. Installs dependencies,
55
# runs tests, and collects logs/reports. Returns an error code if any stage fails.
66
#
7-
# Usage: ./StartLocalTest.sh [-s stage-name] [-p policy-package.zip -c resource-count [-r] [-g]]
7+
# Usage: ./StartLocalTest.sh [-s stage-name] [-p policy-package.zip [-r] [-g]] [-g]
88
# -s stage-name: Specify the stage name. Valid options are: dependency_check, run_tests, collect_logs.
99
# If no stage is specified, all stages will be executed in this order:
1010
# dependency_check, run_tests, collect_logs
1111
# -p policy-package.zip: The Azure Policy Package to test
12-
# -c resource-count: The number of resources to validate, tests will fail if this doesn't match (Default: 0)
1312
# -r remediate-flag: When the flag is enabled, performs remediation on the Policy Package (Default: No remediation performed)
1413
# -g generalize-flag: Generalize the current machine for tests. Performs the following:
1514
# - Remove logs and tmp directories
1615
# - Clean package management cache
1716
# - Clean cloud-init flags to reset cloud-init to initial-state
17+
# Dependencies: curl, wget, unzip
1818

19+
# Powershell and OMI are also required for the tests but they have different installation steps and do not use the distros package manager.
20+
dependencies=(curl wget unzip)
1921
powershell_version="7.4.6"
2022
powershell_uri="https://github.com/PowerShell/PowerShell/releases/download/v$powershell_version/powershell-$powershell_version-linux-x64.tar.gz"
2123
omi_base_uri="https://github.com/microsoft/omi/releases/download/v1.9.1-0/omi-1.9.1-0"
@@ -30,7 +32,7 @@ generalize=false
3032
use_sudo=false
3133

3234
usage() {
33-
echo "Usage: $0 [-s stage-name] [-p policy-package.zip -c resource-count [-r]]
35+
echo "Usage: $0 [-s stage-name] [-p policy-package.zip [-r]] [-g]
3436
-s stage-name: Specify the stage name. Valid options are: dependency_check, run_tests, collect_logs.
3537
If no stage is specified, all stages will be executed in this order:
3638
dependency_check, run_tests, collect_logs
@@ -39,15 +41,14 @@ usage() {
3941
Checks for and installs them if not present:
4042
- Powershell +modules: MachineConfiguration, Pester
4143
- OMI
44+
- unzip, curl, wget
4245
4346
- run_tests: Runs the tests (Powershell Pester Tests).
4447
4548
- collect_logs: Creates a tar.gz archive with the osconfig logs and JUnit Test Report
4649
4750
-p policy-package.zip: The Azure Policy Package to test
4851
49-
-c resource-count: The number of resources to validate, tests will fail if this doesn't match (Default: 0)
50-
5152
-r remediate-flag: When the flag is enabled, performs remediation on the Policy Package (Default: No remediation performed)
5253
5354
-g generalize-flag: Generalize the current machine for tests. Performs the following:
@@ -57,8 +58,40 @@ usage() {
5758

5859
exit 1;
5960
}
61+
62+
install_package() {
63+
if command -v apt &> /dev/null; then
64+
sudo apt update
65+
sudo apt install -y "$@"
66+
elif command -v yum &> /dev/null; then
67+
sudo yum update
68+
sudo yum install -y "$@"
69+
elif command -v dnf &> /dev/null; then
70+
sudo dnf update
71+
sudo dnf install -y "$@"
72+
elif command -v zypper &> /dev/null; then
73+
sudo zypper refresh
74+
sudo zypper install -y "$@"
75+
else
76+
echo "Unsupported Linux distribution." >&2
77+
exit 1
78+
fi
79+
}
80+
_unzip() {
81+
if ! command -v unzip &> /dev/null; then
82+
echo "unzip not found. Installing unzip..." >&2
83+
install_package unzip > /dev/null 2>&1
84+
fi
85+
unzip "$@"
86+
}
6087
dependency_check() {
6188
echo "Checking dependencies..."
89+
for dep in "${dependencies[@]}"; do
90+
if ! command -v $dep &> /dev/null; then
91+
echo -e "\n$dep not found. Installing $dep..."
92+
install_package $dep > /dev/null 2>&1
93+
fi
94+
done
6295
if ! pwsh --version > /dev/null 2>&1; then
6396
echo -e "\nPowershell not found. Installing Powershell..."
6497
# Download the powershell '.tar.gz' archive
@@ -125,6 +158,15 @@ dependency_check() {
125158
echo "done!"
126159
return 0
127160
}
161+
get_instance_count() {
162+
local package=$1
163+
local instanceCount=0
164+
tempDir=$(mktemp -d)
165+
_unzip -q $package -d $tempDir > /dev/null 2>&1
166+
instanceCount=$(find $tempDir -name "${package%.*}.mof" -exec grep -c "instance of OsConfigResource as \$OsConfigResource" {} \;)
167+
rm -rf $tempDir
168+
echo $instanceCount
169+
}
128170
run_tests() {
129171
echo "Running tests..."
130172
echo "Policy Package: $policypackage"
@@ -168,7 +210,10 @@ EOF
168210
}
169211
generalize() {
170212
echo "Generalizing machine..."
213+
# Clear bash history both for current user and root
214+
rm -f ~/.bash_history
171215
do_sudo su -c "
216+
rm -f ~/.bash_history
172217
# Clear system logs
173218
rm -rf /var/log/*
174219
# Clear authentication logs
@@ -212,7 +257,7 @@ do_sudo() {
212257
fi
213258
}
214259

215-
OPTSTRING=":s:p:c:rg"
260+
OPTSTRING=":s:p:rg"
216261

217262
while getopts ${OPTSTRING} opt; do
218263
case ${opt} in
@@ -227,9 +272,6 @@ while getopts ${OPTSTRING} opt; do
227272
p)
228273
policypackage=${OPTARG}
229274
;;
230-
c)
231-
resourcecount=${OPTARG}
232-
;;
233275
r)
234276
remediation=true
235277
;;
@@ -271,15 +313,17 @@ if [ $generalize = true ]; then
271313
fi
272314

273315
if [ -z "$policypackage" ]; then
274-
echo "Policy package not provided." 1>&2;
316+
echo "Policy package not provided." >&2
275317
usage
276318
fi
277-
if [ "$resourcecount" -eq 0 ]; then
278-
echo "Resource count not provided." 1>&2;
279-
usage
319+
320+
resourcecount=$(get_instance_count $policypackage)
321+
if [ -z "$resourcecount" ] || [ "$resourcecount" -eq 0 ]; then
322+
echo "Resource count invalid: $resourcecount" >&2
323+
exit 1
280324
fi
281325
if [ -z "$HOME/UniversalNRP.Tests.ps1" ]; then
282-
echo "UniversalNRP.Tests.ps1 not found. Copy Powershell script into $HOME directory" 1>&2;
326+
echo "UniversalNRP.Tests.ps1 not found. Copy Powershell script into $HOME directory" >&2
283327
fi
284328

285329
if [ "$stageName" = "run_tests" ]; then

0 commit comments

Comments
 (0)