Skip to content

Commit

Permalink
Add model testing.
Browse files Browse the repository at this point in the history
As a result of direction in the following comment:

> ansible#1458 (comment)

And the good work and lead of ansible#1436 in:

> ansible#1436
  • Loading branch information
decentral1se committed Sep 9, 2018
1 parent 56e3a2d commit d3484c0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
4 changes: 4 additions & 0 deletions molecule/driver/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Linode(base.Base):
name: linode
platforms:
- name: instance
linode_id: 12345678
plan: 1
datacenter: 1
distribution: 129
.. code-block:: bash
Expand Down
32 changes: 32 additions & 0 deletions molecule/model/schema_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,36 @@ def pre_validate_base_schema(env, keep_string):
},
}

platforms_linode_schema = {
'platforms': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'name': {
'type': 'string',
},
'linode_id': {
'type': 'integer',
'required': True,
},
'plan': {
'type': 'integer',
'required': True,
},
'datacenter': {
'type': 'integer',
'required': True,
},
'distribution': {
'type': 'integer',
'required': True,
},
},
},
},
}

dependency_command_nullable_schema = {
'dependency': {
'type': 'dict',
Expand Down Expand Up @@ -870,6 +900,8 @@ def validate(c):
elif c['driver']['name'] == 'vagrant':
util.merge_dicts(schema, driver_vagrant_provider_section_schema)
util.merge_dicts(schema, platforms_vagrant_schema)
elif c['driver']['name'] == 'linode':
util.merge_dicts(schema, platforms_linode_schema)
else:
util.merge_dicts(schema, platforms_base_schema)

Expand Down
70 changes: 70 additions & 0 deletions test/unit/model/v2/test_platforms_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,73 @@ def test_platforms_driver_name_required(_config):
x = {'platforms': [{0: [{'name': ['required field']}]}]}

assert x == schema_v2.validate(_config)


@pytest.fixture
def _model_platform_linode_section_data():
return {
'driver': {
'name': 'linode',
},
'platforms': [{
'name': str(),
'linode_id': int(),
'plan': int(),
'datacenter': int(),
'distribution': int(),
}]
}

@pytest.mark.parametrize(
'_config', ['_model_platform_linode_section_data'], indirect=True)
def test_platforms_linode(_config):
assert {} == schema_v2.validate(_config)


@pytest.fixture
def _model_platforms_linode_errors_section_data():
return {
'driver': {
'name': 'linode',
},
'platforms': [{
'name': int(),
'linode_id': str(),
'plan': str(),
'datacenter': str(),
'distribution': str(),
}]
}


@pytest.mark.parametrize(
'_config', ['_model_platforms_linode_errors_section_data'], indirect=True)
def test_platforms_linode_has_errors(_config):
x = {
'platforms': [{
0: [{
'name': ['must be of string type'],
'linode_id': ['must be of integer type'],
'plan': ['must be of integer type'],
'datacenter': ['must be of integer type'],
'distribution': ['must be of integer type'],
}],
}],
}

assert x == schema_v2.validate(_config)


@pytest.mark.parametrize(
'_config', ['_model_platform_linode_section_data'], indirect=True)
@pytest.mark.parametrize('_required_field', (
'linode_id',
'distribution',
'plan',
'datacenter',
'distribution',
))
def test_platforms_linode_fields_required(_config, _required_field):
del _config['platforms'][0][_required_field]
x = {'platforms': [{0: [{_required_field: ['required field']}]}]}
assert x == schema_v2.validate(_config)

0 comments on commit d3484c0

Please sign in to comment.