Skip to content

Commit

Permalink
Merge pull request CentOS-PaaS-SIG#688 from 14rcole/validate-unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paas-bot committed Sep 19, 2018
2 parents 795d796 + 3cf4d1b commit 52cbebf
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/Dockerfiles/linchpin-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TESTS_DIR="./config/Dockerfiles/tests.d"


if [ -z "${PROVIDERS}" ]; then
PROVIDERS="dummy libvirt"
PROVIDERS="dummy libvirt beaker openstack"
fi

# this function checks the distros.exclude value within the test.
Expand Down
139 changes: 139 additions & 0 deletions linchpin/tests/mockdata/general/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"res_defs": {
"type": "list",
"schema": {
"anyof": [{
"type": "dict",
"schema": {
"role": {
"type": "string",
"required": true,
"allowed": ["libvirt_node"]
},
"name": { "type": "string", "required": true },
"vcpus": { "type": "integer", "required": true },
"memory": { "type": "integer", "required": true },
"count": { "type": "integer", "required": false },
"uri": { "type": "string", "required": false },
"driver": {
"type": "string",
"allowed": ["kvm", "qemu"],
"required": false },
"image_src": { "type": "string", "required": false },
"arch": { "type": "string", "required": false },
"remote_user": { "type": "string", "required": false },
"boot_dev": { "type": "string", "required": false },
"additional_storage": { "type": "string", "required": false },
"network_bridge": { "type": "string", "required": false },
"ssh_key": { "type": "string", "required": false },
"cloud_config": {
"type": "dict",
"required": false,
"schema": {
"virt_type": { "type": "string", "required": false },
"run_script": { "type": "string", "required": false },
"run_commands": { "type": "list", "required": false },
"users": {
"type": "list",
"required": false,
"schema": {
"type": "dict",
"required": false,
"schema": {
"name": { "type": "string", "required": true },
"gecos": { "type": "string", "required": false },
"primary_group": { "type": "string", "required": false },
"groups": { "type": "string", "required": false },
"sudo": { "type": "string", "required": false },
"passwd": { "type": "string", "required": false },
"lock_passwd": { "type": "boolean", "required": false },
"ssh_import_id": { "type": "string", "required": false },
"ssh_authorized_keys": { "type": "list", "required": false },
"inject_ssh_keys": { "type": "string", "required": false }
}
}
},
"root_password": { "type": "string", "required": false },
"packages": { "type": "list", "required": false }
}
},
"networks": {
"type": "list",
"required": false,
"schema": {
"type": "dict",
"schema": {
"name": { "type": "string", "required": true },
"ip": { "type": "string", "required": false },
"mac": { "type": "string", "required": false }
}
}
},
"storage": {
"type": "list",
"required": false,
"schema": {
"type": "dict",
"required": true,
"schema": {
"name": { "type": "string", "required": true },
"size": { "type": "integer", "required": true },
"units": { "type": "string", "required": false },
"disk_type": { "type": "string", "required": false,
"allowed": ["virtio_blk", "virtio_scsi"] },
"cache": { "type": "string", "required": false },
"format": { "type": "string", "required": false },
"shareable": { "type": "boolean", "required": false },
"device": { "type": "string", "required": false },
"source": {
"type": "dict",
"required": false,
"schema": {
"file": { "type": "string", "required": false },
"block": { "type": "string", "required": false },
"dir": { "type": "string", "required": false },
"network_protocol": { "type": "string", "required": false,
"allowed": ["nbd", "isci", "rbd", "sheepdog", "gluster", "vxhs"]},
"network_name": { "type": "string", "required": false },
"volume": { "type": "string", "required": false },
"pool": { "type": "string", "required": false }
}
}
}
}
}
}
},
{
"type": "dict",
"schema": {
"role": {
"type": "string",
"required": true,
"allowed": ["libvirt_network"] },
"name": { "type": "string", "required": true },
"uri": { "type": "string", "required": false },
"ip": { "type": "string", "required": true },
"prefix": { "type": "string", "required": false },
"dhcp_start": { "type": "string", "required": false },
"dhcp_end": { "type": "string", "required": false },
"bridge": { "type": "string", "required": false },
"domain": { "type": "string", "required": false }
}
},
{
"type": "dict",
"schema": {
"role": {
"type": "string",
"required": true,
"allowed": ["libvirt_storage"] },
"name": { "type": "string", "required": true },
"uri": { "type": "string", "required": false },
"size": { "type": "integer", "required": true },
"path": { "type": "integer", "required": true }
}
}]
}
}
}
26 changes: 26 additions & 0 deletions linchpin/tests/mockdata/libvirt/libvirt-new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
topology_name: libvirt-new
resource_groups:
- resource_group_name: libvirt-new
resource_group_type: libvirt
resource_definitions:
- role: libvirt_node
name: centos71
uri: qemu:///system
count: 1
image_src: http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1608.qcow2.xz
memory: 2048
vcpus: 1
arch: x86_64
ssh_key: libvirt
networks:
- name: default
additional_storage: 10G
cloud_config:
users:
- name: herlo
gecos: Clint Savage
groups: wheel
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_import_id: gh:herlo
lock_passwd: true
13 changes: 13 additions & 0 deletions linchpin/tests/test_init_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ def test_do_action():

assert not failed

@with_setup(setup_lp_api)
def test_do_validation():
return_code, results = lpa.do_validation(provision_data)

failed = False
if return_code:
failed = True
for target, data in results.iteritems():
if not data.startswith("valid"):
print("Validation for target '{0}': has failed with"
" error '{1}'".format(target, msg))

assert not failed

@with_setup(setup_lp_api)
def test_lp_journal():
Expand Down
60 changes: 60 additions & 0 deletions linchpin/tests/utils/test_validator_pass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import sys
import json
import yaml

from nose.tools import assert_true
from nose.tools import with_setup

from linchpin import LinchpinAPI
from linchpin.utils.dataparser import DataParser
from linchpin.context import LinchpinContext
from linchpin.rundb import RunDB

from linchpin.utils.validator import AnyofValidator

from linchpin.tests.mockdata.contextdata import ContextData


def setup_validator():

"""
Perform setup of AnyofValidator, definitions, field, and value
"""

global validator
global topo

lpc = LinchpinContext()
lpc.load_config()
lpc.load_global_evars()
lpa = LinchpinAPI(lpc)

schema_file = 'schema.json'
base_path = '{0}'.format(os.path.dirname(os.path.realpath(__file__)))\
.rstrip('/')
lib_path = os.path.realpath(os.path.join(base_path, os.pardir))
sp = '{0}/{1}/{2}'.format(lib_path, 'mockdata/general', schema_file)
schema = json.load(open(sp))

validator = AnyofValidator(schema)

topo_name = 'libvirt-new.yml'
topo_file = '{0}/{1}/{2}'.format(lib_path, 'mockdata/libvirt', topo_name)
topo = yaml.load(open(topo_file))



@with_setup(setup_validator)
def test_validate_anyof():
document = { 'res_defs': topo['resource_groups'][0]['resource_definitions']}
success = validator.validate(document)
assert_true(success)


def main():
pass


if __name__ == '__main__':
sys.exit(main())
1 change: 0 additions & 1 deletion linchpin/utils/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def __init__(self, *args, **kwargs):

def _validate_anyof(self, definitions, field, value):
""" {'type': 'list', 'logical': 'anyof'} """
# print "definitions:", definitions

if 'role' not in definitions[0]['schema'].keys():
return super(AnyofValidator, self)._validate_anyof(definitions,
Expand Down

0 comments on commit 52cbebf

Please sign in to comment.