Skip to content

Commit

Permalink
Improve Unit Testing (#152)
Browse files Browse the repository at this point in the history
* moved working tests to new folder structure; added empty tests for processors and build

* added empty generate cofnig tests; fixed and moved bicep,artifact and definitino folder builder tests

* added nexus arm tests; removed outdated vnf mocks

* added azure core tests for every function (including base)

* fixed up core and nexus arm processor

* fixed up arm processors; added logging to tests

* added arm template tests

* added test from copilot learning day

* changed assert

* removed build folder; added json dump to artifact and definition tests; improved arm processor testing

* removed mock open and replaced with new templates

* fixed linting

---------

Co-authored-by: Jordan <jordan.layton@metaswitch.com>
  • Loading branch information
jordlay and Jordan authored Mar 15, 2024
1 parent e774963 commit a73a246
Show file tree
Hide file tree
Showing 35 changed files with 820 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from azext_aosm.build_processors.arm_processor import AzureCoreArmBuildProcessor
from azext_aosm.build_processors.vhd_processor import VHDProcessor
from azext_aosm.build_processors.base_processor import BaseInputProcessor
from azext_aosm.common.constants import (
BASE_FOLDER_NAME,
VNF_CORE_BASE_TEMPLATE_FILENAME,
Expand Down Expand Up @@ -146,7 +145,7 @@ def _get_default_config(self, vhd) -> Dict[str, Any]:
default_config.update({"image_hyper_v_generation": "V1"})
if vhd.image_api_version:
default_config.update({"image_api_version": vhd.image_api_version})

# Add imageName
default_config["imageName"] = self.config.nf_name + 'Image'
return default_config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from azext_aosm.build_processors.arm_processor import NexusArmBuildProcessor
from azext_aosm.build_processors.nexus_image_processor import NexusImageProcessor
from azext_aosm.build_processors.base_processor import BaseInputProcessor
from azext_aosm.common.constants import (
BASE_FOLDER_NAME,
VNF_TEMPLATE_FOLDER_NAME,
Expand Down
7 changes: 3 additions & 4 deletions src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

from .onboarding_nfd_base_handler import OnboardingNFDBaseCLIHandler
from knack.log import get_logger
# from azext_aosm.configuration_models.onboarding_vnf_input_config import (
# OnboardingBaseVNFInputConfig,
# )
from azext_aosm.common.utils import render_bicep_contents_from_j2, get_template_path
from azext_aosm.configuration_models.onboarding_vnf_input_config import (OnboardingCoreVNFInputConfig, OnboardingNexusVNFInputConfig)
from azext_aosm.configuration_models.onboarding_vnf_input_config import (
OnboardingCoreVNFInputConfig, OnboardingNexusVNFInputConfig
)
from azext_aosm.definition_folder.builder.bicep_builder import (
BicepDefinitionElementBuilder,
)
Expand Down
3 changes: 2 additions & 1 deletion src/aosm/azext_aosm/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import os
import tarfile
from typing import Tuple
from pathlib import Path
from jinja2 import StrictUndefined, Template
import json
Expand Down Expand Up @@ -125,7 +126,7 @@ def check_tool_installed(tool_name: str) -> None:
raise MissingDependency(f"You must install {tool_name} to use this command.")


def split_image_path(image) -> "tuple[str, str, str]":
def split_image_path(image) -> Tuple[str, str, str]:
"""Split the image path into source acr registry, name and version."""
(source_acr_registry, name_and_version) = image.split("/", 2)
(name, version) = name_and_version.split(":", 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def create_artifact_object(artifact: dict) -> BaseArtifact:
"""
if "type" not in artifact or artifact["type"] not in ARTIFACT_TYPE_TO_CLASS:
raise ValueError(
"Artifact type is missing or invalid for artifact {artifact}"
f"Artifact type is missing or invalid for artifact {artifact}"
)
# Use reflection to get the required fields for the artifact class
class_sig = inspect.signature(ARTIFACT_TYPE_TO_CLASS[artifact["type"]].__init__)
Expand Down
2 changes: 1 addition & 1 deletion src/aosm/azext_aosm/inputs/vhd_file_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def get_schema(self) -> Dict[str, Any]:
schema["required"] += vhd_required

logger.debug("Schema for VHD file input: %s", json.dumps(schema, indent=4))
return copy.deepcopy(schema)
return copy.deepcopy(schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.15.31.15270",
"templateHash": "1656082395923655778"
}
},
"variables": {
"imageResourceGroup": "[resourceGroup().name]",
"subscriptionId": "[subscription().subscriptionId]",
"vmSizeSku": "Standard_D2s_v3"
},
"resources": [
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2021-05-01",
"name": "[format('{0}_nic', parameters('ubuntuVmName'))]",
"location": "[parameters('location')]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[format('{0}/subnets/{1}', parameters('virtualNetworkId'), parameters('subnetName'))]"
},
"primary": true,
"privateIPAddressVersion": "IPv4"
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-07-01",
"name": "[parameters('ubuntuVmName')]",
"location": "[parameters('location')]",
"properties": {
"hardwareProfile": {
"vmSize": "[variables('vmSizeSku')]"
},
"storageProfile": {
"imageReference": {
"id": "[extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', variables('subscriptionId'), variables('imageResourceGroup')), 'Microsoft.Compute/images', parameters('imageName'))]"
},
"osDisk": {
"osType": "Linux",
"name": "[format('{0}_disk', parameters('ubuntuVmName'))]",
"createOption": "FromImage",
"caching": "ReadWrite",
"writeAcceleratorEnabled": false,
"managedDisk": "[json('{\"storageAccountType\": \"Premium_LRS\"}')]",
"deleteOption": "Delete",
"diskSizeGB": 30
}
},
"osProfile": {
"computerName": "[parameters('ubuntuVmName')]",
"adminUsername": "azureuser",
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"path": "/home/azureuser/.ssh/authorized_keys",
"keyData": "[parameters('sshPublicKeyAdmin')]"
}
]
},
"provisionVMAgent": true,
"patchSettings": {
"patchMode": "ImageDefault",
"assessmentMode": "ImageDefault"
}
},
"secrets": [],
"allowExtensionOperations": true
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', format('{0}_nic', parameters('ubuntuVmName')))]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', format('{0}_nic', parameters('ubuntuVmName')))]"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.15.31.15270",
"templateHash": "1656082395923655778"
}
},
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"variables": {
"imageResourceGroup": "[resourceGroup().name]",
"subscriptionId": "[subscription().subscriptionId]",
"vmSizeSku": "Standard_D2s_v3"
},
"resources": [
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2021-05-01",
"name": "[format('{0}_nic', parameters('ubuntuVmName'))]",
"location": "[parameters('location')]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[format('{0}/subnets/{1}', parameters('virtualNetworkId'), parameters('subnetName'))]"
},
"primary": true,
"privateIPAddressVersion": "IPv4"
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-07-01",
"name": "[parameters('ubuntuVmName')]",
"location": "[parameters('location')]",
"properties": {
"hardwareProfile": {
"vmSize": "[variables('vmSizeSku')]"
},
"storageProfile": {
"imageReference": {
"id": "[extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', variables('subscriptionId'), variables('imageResourceGroup')), 'Microsoft.Compute/images', parameters('imageName'))]"
},
"osDisk": {
"osType": "Linux",
"name": "[format('{0}_disk', parameters('ubuntuVmName'))]",
"createOption": "FromImage",
"caching": "ReadWrite",
"writeAcceleratorEnabled": false,
"managedDisk": "[json('{\"storageAccountType\": \"Premium_LRS\"}')]",
"deleteOption": "Delete",
"diskSizeGB": 30
}
},
"osProfile": {
"computerName": "[parameters('ubuntuVmName')]",
"adminUsername": "azureuser",
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"path": "/home/azureuser/.ssh/authorized_keys",
"keyData": "[parameters('sshPublicKeyAdmin')]"
}
]
},
"provisionVMAgent": true,
"patchSettings": {
"patchMode": "ImageDefault",
"assessmentMode": "ImageDefault"
}
},
"secrets": [],
"allowExtensionOperations": true
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', format('{0}_nic', parameters('ubuntuVmName')))]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', format('{0}_nic', parameters('ubuntuVmName')))]"
]
}
]
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
# mock_read_text.return_value = """
# [
# {
# "artifact_type": "ArmTemplate",
# "artifact_name": "abc",
# "artifact_version": "1.0.0",
# "type": "MockType1",
# "artifact_manifest": "def",
# "file_path": "def"
# },
# {
# "artifact_type": "ArmTemplate",
# "artifact_name": "ghi",
# "artifact_version": "2.1.1",
# "type": "MockType2",
# "artifact_manifest": "jkl",
# "file_path": "jkl"
# }
# ]
Expand All @@ -52,7 +56,9 @@
# definition_element = ArtifactDefinitionElement(element_path, False)

# # Deploy the element.
# definition_element.deploy()
# mock_config = MagicMock()
# mock_context = MagicMock()
# definition_element.deploy(config=mock_config, command_context=mock_context)

# # Check results.
# mock_type_1.assert_has_calls(
Expand Down
Loading

0 comments on commit a73a246

Please sign in to comment.