Commit f767b407 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Unittest kamaki.cli.Config.__init__

Refs: #4058
parent 9f5cbafe
......@@ -31,17 +31,79 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from mock import patch, call, MagicMock
from mock import patch, call
from unittest import TestCase
from StringIO import StringIO
from datetime import datetime
#from itertools import product
from itertools import product
import os
from kamaki.cli import argument, errors
from kamaki.cli.config import Config
def _2steps_gen(limit=2):
counter, ret = 0, None
while True:
if counter >= limit:
ret = None if ret else 'something'
counter = 0
counter += 1
yield ret
_2value_gen = _2steps_gen()
class Config(TestCase):
"""Test Config methods"""
@patch('kamaki.cli.config.Config.remove_section')
@patch('kamaki.cli.config.Config.items', return_value=(
('k1', 'v1'), ('k2', 'v2')))
@patch('kamaki.cli.config.Config.sections', return_value=('a', 'b'))
@patch('kamaki.cli.config.Config.set_cloud')
@patch('kamaki.cli.config.Config.read')
@patch('kamaki.cli.config.Config._load_defaults')
def test___init__(
self, _ld, c_read, c_set_cloud, c_sections, c_items,
c_remove_section):
from kamaki.cli.config import (
Config, RawConfigParser, CONFIG_ENV, CONFIG_PATH)
_ld_num, c_sections_num, c_items_num, c_set_cloud_num = 0, 0, 0, 0
c_remove_section_num, gen_call = 0, [call('a'), call('b')]
for path, with_defaults in product((None, '/a/path'), (True, False)):
with patch(
'kamaki.cli.config.Config._cloud_name',
return_value=_2value_gen.next()) as _cloud_name:
cnf = Config(path=path, with_defaults=with_defaults)
self.assertTrue(isinstance(cnf, RawConfigParser))
cpath = path or os.environ.get(CONFIG_ENV, CONFIG_PATH)
self.assertEqual(cnf.path, cpath)
if with_defaults:
_ld_num += 1
self.assertEqual(_ld.mock_calls[-1], call())
self.assertEqual(len(_ld.mock_calls), _ld_num)
self.assertEqual(c_read.mock_calls[-1], call(cpath))
c_sections_num += 1
self.assertEqual(len(c_sections.mock_calls), c_sections_num)
self.assertEqual(c_sections.mock_calls[-1], call())
self.assertEqual(_cloud_name.mock_calls, gen_call)
r = _2value_gen.next()
if r:
c_items_num += 2
self.assertEqual(c_items.mock_calls[-2:], gen_call)
c_set_cloud_num += 4
self.assertEqual(c_set_cloud.mock_calls[-4:], [
call(r, 'k1', 'v1'), call(r, 'k2', 'v2'),
call(r, 'k1', 'v1'), call(r, 'k2', 'v2')])
c_remove_section_num += 2
self.assertEqual(
c_remove_section.mock_calls[-2:], gen_call)
self.assertEqual(len(c_items.mock_calls), c_items_num)
self.assertEqual(len(c_set_cloud.mock_calls), c_set_cloud_num)
self.assertEqual(
len(c_remove_section.mock_calls), c_remove_section_num)
if __name__ == '__main__':
from sys import argv
from kamaki.cli.test import runTestCase
#runTestCase(Argument, 'Argument', argv[1:])
runTestCase(Config, 'Config', argv[1:])
......@@ -38,6 +38,7 @@ from mock import patch, call
from itertools import product
from kamaki.cli.command_tree.test import Command, CommandTree
from kamaki.cli.config.test import Config
from kamaki.cli.argument.test import (
Argument, ConfigArgument, RuntimeConfigArgument, FlagArgument,
ValueArgument, IntArgument, DateArgument, VersionArgument,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment