Skip to content

Commit

Permalink
pytests: Add test for raw_image option
Browse files Browse the repository at this point in the history
Add Configurations and Hardware section tests in tests_all_configs
  • Loading branch information
prashant1221 committed Aug 28, 2024
1 parent caed399 commit 6c942e0
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
16 changes: 16 additions & 0 deletions pytest/configs/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ hardware:
memory:
type: memory
size: 4096
scsi1:
type: scsi_controller
subtype: lsilogic
sata1:
type: sata_controller
cdrom1:
type: cd_drive
parent: sata1
connected: true
rootdisk:
type: hard_disk
parent: sata1
Expand All @@ -31,6 +35,7 @@ hardware:
type: ethernet
subtype: VmxNet3
network: vm_network
connected: false
ethernet2:
type: ethernet
subtype: VmxNet3
Expand Down Expand Up @@ -76,6 +81,17 @@ product_sections:
category: some
password: true

configurations:
tall:
label: Tall
description: too little for the money
grande:
label: Grande
description: just right
venti:
label: Venti
description: too much

annotation:
text: the password is top secret
info: Any information
1 change: 1 addition & 0 deletions pytest/configs/raw-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hardware:
type: hard_disk
parent: sata1
raw_image: dummy.img
file_id: file1
usb1:
type: usb_controller
ethernet1:
Expand Down
33 changes: 30 additions & 3 deletions pytest/test_all_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ def test_envelope_configs(get_configs):
j += 1


def test_virtual_hardware_configs():
# TODO
pass
def test_virtual_hardware_configs(get_configs):
#Hardware Section
config, ovf = get_configs

cfg_hardware_section = config['hardware']
cfg_vmw_ovf = ovf['Envelope']['VirtualSystem']['VirtualHardwareSection']['Item']

for idx, key in enumerate(cfg_hardware_section):
assert_values(key, cfg_vmw_ovf[idx]['rasd:ElementName'])
if key == 'cpus':
assert_values(cfg_hardware_section[key], int(cfg_vmw_ovf[idx]['rasd:VirtualQuantity']))
elif key == 'memory':
assert_values(cfg_hardware_section[key]['size'], int(cfg_vmw_ovf[idx]['rasd:VirtualQuantity']))
else:
assert_values(cfg_hardware_section[key].get('subtype'), cfg_vmw_ovf[idx].get('rasd:ResourceSubType'))
assert_values(cfg_hardware_section[key].get('connected'), bool(cfg_vmw_ovf[idx].get('rasd:AutomaticAllocation')))


def test_configuration_configs(get_configs):
#Deployment Option Section
config, ovf = get_configs

cfg_configuration_section = config['configurations']
cfg_vmw_ovf = ovf['Envelope']['DeploymentOptionSection']['Configuration']
cfg_vmw_ovf = cfg_vmw_ovf if isinstance(cfg_vmw_ovf, list) else [cfg_vmw_ovf]

for idx, key in enumerate(cfg_configuration_section):
assert_values(key, cfg_vmw_ovf[idx]['@ovf:id'])
assert_values(cfg_configuration_section[key]['label'], cfg_vmw_ovf[idx]['Label'])
assert_values(cfg_configuration_section[key]['description'], cfg_vmw_ovf[idx]['Description'])
80 changes: 80 additions & 0 deletions pytest/test_raw_image_option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2024 Broadcom. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the “License”); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an “AS IS” BASIS, without warranties or
# conditions of any kind, EITHER EXPRESS OR IMPLIED. See the License for the
# specific language governing permissions and limitations under the License.

import glob
import os
import pytest
import shutil
import subprocess
import yaml
import xmltodict
import test_all_configs

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
OVA_COMPOSE = os.path.join(THIS_DIR, "..", "ova-compose", "ova-compose.py")

VMDK_CONVERT=os.path.join(THIS_DIR, "..", "build", "vmdk", "vmdk-convert")

CONFIG_DIR=os.path.join(THIS_DIR, "configs")

WORK_DIR=os.path.join(os.getcwd(), "pytest-configs")


@pytest.fixture(scope='module', autouse=True)
def setup_test():
os.makedirs(WORK_DIR, exist_ok=True)

process = subprocess.run(["dd", "if=/dev/zero", "of=dummy.img", "bs=1024", "count=1024"], cwd=WORK_DIR)
assert process.returncode == 0

yield
shutil.rmtree(WORK_DIR)


def yaml_param(loader, node):
params = loader.app_params
default = None
key = node.value

assert type(key) is str, f"param name must be a string"

if '=' in key:
key, default = [t.strip() for t in key.split('=', maxsplit=1)]
default = yaml.safe_load(default)
value = params.get(key, default)

assert value is not None, f"no param set for '{key}', and there is no default"

return value


def test_raw_image_configs():
in_yaml = os.path.join(CONFIG_DIR, "raw-image.yaml")

basename = os.path.basename(in_yaml.rsplit(".", 1)[0])
out_ovf = os.path.join(WORK_DIR, f"{basename}.ovf")

process = subprocess.run([OVA_COMPOSE, "-i", in_yaml, "-o", out_ovf, "--vmdk-convert", VMDK_CONVERT], cwd=WORK_DIR)
assert process.returncode == 0

with open(in_yaml) as f:
yaml_loader = yaml.SafeLoader
yaml_loader.app_params = {}
yaml.add_constructor("!param", yaml_param, Loader=yaml_loader)
config = yaml.load(f, Loader=yaml_loader)

with open(out_ovf) as f:
ovf = xmltodict.parse(f.read())

test_all_configs.test_envelope_configs((config, ovf))

0 comments on commit 6c942e0

Please sign in to comment.