Revision c89d5f34

b/kamaki/cli/argument/test.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
#from mock import patch, call
34
from mock import patch, call
35 35
from unittest import TestCase
36 36
from StringIO import StringIO
37
from sys import stdin, stdout
38 37
#from itertools import product
39 38

  
40 39
from kamaki.cli import argument
41 40
from kamaki.cli.config import Config
42 41

  
43 42

  
43
cnf_path = 'kamaki.cli.config.Config'
44

  
45

  
44 46
class Argument(TestCase):
45 47

  
46 48
    def test___init__(self):
......
89 91

  
90 92
class ConfigArgument(TestCase):
91 93

  
92
    #  A cloud name in config with a URL but no TOKEN
93
    SEMI_CLOUD = 'production'
94

  
95
    # A cloud name that is not configured in config
96
    INVALID_CLOUD = 'QWERTY_123456'
97

  
98 94
    def setUp(self):
99 95
        argument._config_arg = argument.ConfigArgument('Recovered Path')
100 96

  
......
110 106
    def test_get(self):
111 107
        c = argument._config_arg
112 108
        c.value = None
113
        self.assertEqual(c.value.get('global', 'config_cli'), 'config')
109
        with patch('%s.get' % cnf_path, return_value='config') as get:
110
            self.assertEqual(c.value.get('global', 'config_cli'), 'config')
111
            self.assertEqual(get.mock_calls[-1], call('global', 'config_cli'))
114 112

  
115
    def test_groups(self):
113
    @patch('%s.keys' % cnf_path, return_value=(
114
        'image_cli', 'config_cli', 'history_cli', 'file'))
115
    def test_groups(self, keys):
116 116
        c = argument._config_arg
117 117
        c.value = None
118
        self.assertTrue(set(c.groups).issuperset([
119
            'image', 'config', 'history']))
120

  
121
    def test_cli_specs(self):
118
        cset = set(c.groups)
119
        self.assertTrue(cset.issuperset(['image', 'config', 'history']))
120
        self.assertEqual(keys.mock_calls[-1], call('global'))
121
        self.assertFalse('file' in cset)
122
        self.assertEqual(keys.mock_calls[-1], call('global'))
123

  
124
    @patch('%s.items' % cnf_path, return_value=(
125
        ('image_cli', 'image'), ('file', 'pithos'),
126
        ('config_cli', 'config'), ('history_cli', 'history')))
127
    def test_cli_specs(self, items):
122 128
        c = argument._config_arg
123 129
        c.value = None
124
        self.assertTrue(set(c.cli_specs).issuperset([
130
        cset = set(c.cli_specs)
131
        self.assertTrue(cset.issuperset([
125 132
            ('image', 'image'), ('config', 'config'), ('history', 'history')]))
133
        self.assertEqual(items.mock_calls[-1], call('global'))
134
        self.assertFalse(cset.issuperset([('file', 'pithos'), ]))
135
        self.assertEqual(items.mock_calls[-1], call('global'))
126 136

  
127 137
    def test_get_global(self):
128 138
        c = argument._config_arg
......
131 141
                ('config_cli', 'config'),
132 142
                ('image_cli', 'image'),
133 143
                ('history_cli', 'history')):
134
            self.assertEqual(c.get_global(k), v)
144
            with patch('%s.get_global' % cnf_path, return_value=v) as gg:
145
                self.assertEqual(c.get_global(k), v)
146
                self.assertEqual(gg.mock_calls[-1], call(k))
135 147

  
136 148
    def test_get_cloud(self):
137
        """test_get_cloud (!! hard-set SEMI/INVALID_CLOUD to run this !!)"""
138 149
        c = argument._config_arg
139 150
        c.value = None
140
        if not self.SEMI_CLOUD:
141
            stdout.write(
142
                '\n\tA cloud name set in config file with URL but no TOKEN: ')
143
            self.SEMI_CLOUD = stdin.readline()[:-1]
144
        self.assertTrue(len(c.get_cloud(self.SEMI_CLOUD, 'url')) > 0)
145
        self.assertRaises(KeyError, c.get_cloud, self.SEMI_CLOUD, 'token')
146

  
147
        if not self.INVALID_CLOUD:
148
            stdout.write('\tok\n\tA cloud name NOT in your config file: ')
149
            self.INVALID_CLOUD = stdin.readline()[:-1]
150
        self.assertRaises(KeyError, c.get_cloud, self.INVALID_CLOUD, 'url')
151
        with patch(
152
                '%s.get_cloud' % cnf_path,
153
                return_value='http://cloud') as get_cloud:
154
            self.assertTrue(len(c.get_cloud('mycloud', 'url')) > 0)
155
            self.assertEqual(get_cloud.mock_calls[-1],  call('mycloud', 'url'))
156
        with patch(
157
                '%s.get_cloud' % cnf_path,
158
                side_effect=KeyError('no token')) as get_cloud:
159
            self.assertRaises(KeyError, c.get_cloud, 'mycloud', 'token')
160
        invalidcloud = 'PLEASE_DO_NOT_EVER_NAME_YOUR_CLOUD_LIKE_THIS111'
161
        self.assertRaises(KeyError, c.get_cloud, invalidcloud, 'url')
151 162

  
152 163

  
153 164
if __name__ == '__main__':

Also available in: Unified diff