Skip to content
This repository has been archived by the owner on Jan 19, 2018. It is now read-only.

Commit

Permalink
Fixed unittests for refactored config. #524
Browse files Browse the repository at this point in the history
  • Loading branch information
rtnpro committed Jul 7, 2016
1 parent 8c0cf8c commit 37e8a81
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 72 deletions.
5 changes: 3 additions & 2 deletions tests/units/nulecule/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from atomicapp.nulecule.lib import NuleculeBase
from atomicapp.nulecule.exceptions import NuleculeException
from atomicapp.nulecule.config import Config


class TestNuleculeBaseGetProvider(unittest.TestCase):
Expand All @@ -16,7 +17,7 @@ def test_get_provider_success(self):
provider_key = u'openshift'
# method `get_provider` will read from this config, we give it here
# since we have neither provided it before nor it is auto-generated
nb.config = {u'general': {u'provider': provider_key}}
nb.config = Config(answers={u'general': {u'provider': provider_key}})

return_provider = mock.Mock()
# mocking return value of method plugin.getProvider,because it returns
Expand All @@ -35,6 +36,6 @@ def test_get_provider_failure(self):
nb = NuleculeBase(params = [], basepath = '', namespace = '')
# purposefully give the wrong provider key
provider_key = u'mesos'
nb.config = {u'general': {u'provider': provider_key}}
nb.config = Config(answers={u'general': {u'provider': provider_key}})
with self.assertRaises(NuleculeException):
nb.get_provider()
119 changes: 57 additions & 62 deletions tests/units/nulecule/test_nulecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from atomicapp.nulecule.base import Nulecule
from atomicapp.nulecule.exceptions import NuleculeException
from atomicapp.nulecule.config import Config


class TestNuleculeRun(unittest.TestCase):
Expand All @@ -15,8 +16,9 @@ def test_run(self):
dryrun = False
mock_component_1 = mock.Mock()
mock_component_2 = mock.Mock()
config = Config(answers={})

n = Nulecule('some-id', '0.0.2', [{}], 'some/path', {})
n = Nulecule('some-id', '0.0.2', [{}], 'some/path', {}, config=config)
n.components = [mock_component_1, mock_component_2]
n.run(provider)

Expand All @@ -34,7 +36,9 @@ def test_stop(self):
mock_component_1 = mock.Mock()
mock_component_2 = mock.Mock()

n = Nulecule('some-id', '0.0.2', {}, [], 'some/path')
config = Config(answers={})

n = Nulecule('some-id', '0.0.2', {}, [], 'some/path', config=config)
n.components = [mock_component_1, mock_component_2]
n.stop(provider)

Expand All @@ -50,70 +54,59 @@ def test_load_config_without_specified_provider(self):
"""
Test Nulecule load_config without specifying a provider.
"""
config = {'general': {}, 'group1': {'a': 'b'}}
mock_component_1 = mock.Mock()
mock_component_1.config = {
'group1': {'a': 'c', 'k': 'v'},
'group2': {'1': '2'}
}
config = Config(answers={})

n = Nulecule(id='some-id', specversion='0.0.2', metadata={}, graph=[], basepath='some/path')
n.components = [mock_component_1]
n.load_config(config)
params = [
{
"name": "key1",
"default": "val1",
},
{
"name": "key3",
"default": "val3"
}
]

self.assertEqual(n.config, {
'general': {'provider': 'kubernetes'},
'group1': {'a': 'b', 'k': 'v'},
'group2': {'1': '2'}
})
graph = [
{
"name": "component1",
"params": [
{
"name": "key1",
},
{
"name": "key2",
"default": "val2"
}
],
"artifacts": []
}
]

def test_load_config_with_defaultprovider(self):
"""
Test Nulecule load_config with default provider specified
in global params in Nulecule spec.
"""
config = {'general': {}, 'group1': {'a': 'b'}}
mock_component_1 = mock.Mock()
mock_component_1.config = {
'group1': {'a': 'c', 'k': 'v'},
'group2': {'1': '2'}
}

n = Nulecule(id='some-id', specversion='0.0.2', metadata={}, graph=[],
basepath='some/path',
params=[{'name': 'provider', 'default': 'some-provider'}])
n.components = [mock_component_1]
n = Nulecule(id='some-id', specversion='0.0.2', metadata={},
graph=graph, params=params, basepath='some/path',
config=config)
n.load_components()
n.load_config(config)

self.assertEqual(n.config, {
'general': {'provider': 'some-provider'},
'group1': {'a': 'b', 'k': 'v'},
'group2': {'1': '2'}
self.assertEqual(n.config.runtime_answers(), {
'general': {
'namespace': 'default',
'provider': 'kubernetes',
'key1': 'val1',
'key3': 'val3'
},
'component1': {
'key2': 'val2',
'key1': 'val1'
}
})

def test_load_config_with_defaultprovider_overridden_by_provider_in_answers(self):
"""
Test Nulecule load_config with default provider specified
in global params in Nulecule spec, but overridden in answers config.
"""
config = {'general': {'provider': 'new-provider'},
'group1': {'a': 'b'}}
mock_component_1 = mock.Mock()
mock_component_1.config = {
'group1': {'a': 'c', 'k': 'v'},
'group2': {'1': '2'}
}

n = Nulecule(id='some-id', specversion='0.0.2', metadata={}, graph=[],
basepath='some/path',
params=[{'name': 'provider', 'default': 'some-provider'}])
n.components = [mock_component_1]
n.load_config(config)

self.assertEqual(n.config, {
'general': {'provider': 'new-provider'},
'group1': {'a': 'b', 'k': 'v'},
'group2': {'1': '2'}
self.assertEqual(n.components[0].config.context(), {
'key3': 'val3',
'key2': 'val2',
'key1': 'val1',
'provider': 'kubernetes'
})


Expand All @@ -137,15 +130,17 @@ def test_load_components(self, MockNuleculeComponent):
}
]

n = Nulecule('some-id', '0.0.2', graph, 'some/path', {})
config = Config(answers={})

n = Nulecule('some-id', '0.0.2', graph, 'some/path', config=config)
n.load_components()

MockNuleculeComponent.assert_any_call(
graph[0]['name'], n.basepath, 'somecontainer',
graph[0]['params'], None, {})
graph[0]['params'], None, config)
MockNuleculeComponent.assert_any_call(
graph[1]['name'], n.basepath, None,
graph[1].get('params'), graph[1].get('artifacts'), {})
graph[1].get('params'), graph[1].get('artifacts'), config)


class TestNuleculeRender(unittest.TestCase):
Expand Down
15 changes: 7 additions & 8 deletions tests/units/nulecule/test_nulecule_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,18 @@ def test_load_config_local_app(self):
'general': {'a': 'b', 'key2': 'val2'},
'some-app': {'key1': 'val1'}
}
config = Config('some-app', answers=initial_config)
conf = Config('some-app', answers=initial_config)

nc = NuleculeComponent('some-app', 'some/path',
params=params)
nc.load_config(config=config)

self.assertEqual(nc.config.runtime_answers(), {
'general': {'a': 'b', 'key2': 'val2'},
params=params, config=conf)
nc.load_config()
runtime_answers = nc.config.runtime_answers()
self.assertEqual(runtime_answers, {
'general': {'a': 'b', 'key2': 'val2', 'provider': 'kubernetes', 'namespace': 'default'},
'some-app': {'key1': 'val1', 'key2': 'val2'}
})

@mock.patch('atomicapp.nulecule.base.NuleculeComponent.merge_config')
def test_load_config_external_app(self, mock_merge_config):
def test_load_config_external_app(self):
"""Test load config for external app"""
mock_nulecule = mock.Mock(
name='nulecule',
Expand Down

0 comments on commit 37e8a81

Please sign in to comment.