Merge "SNAPS tests now support offline testing."
[functest-xtesting.git] / functest / tests / unit / openstack / refstack_client / test_refstack_client.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 # matthew.lijun@huawei.com wangwulin@huawei.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8
9 import logging
10 import mock
11 import pkg_resources
12 import unittest
13
14 from functest.core import testcase
15 from functest.opnfv_tests.openstack.refstack_client import refstack_client
16
17
18 class OSRefstackClientTesting(unittest.TestCase):
19
20     _config = pkg_resources.resource_filename(
21         'functest',
22         'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
23     _testlist = pkg_resources.resource_filename(
24             'functest', 'opnfv_tests/openstack/refstack_client/defcore.txt')
25
26     def setUp(self):
27         self.defaultargs = {'config': self._config,
28                             'testlist': self._testlist}
29         self.refstackclient = refstack_client.RefstackClient()
30
31     def test_run_defcore(self):
32         config = 'tempest.conf'
33         testlist = 'testlist'
34         with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
35                         'refstack_client.ft_utils.execute_command') as m:
36             cmd = ("refstack-client test -c {0} -v --test-list {1}"
37                    .format(config, testlist))
38             self.refstackclient.run_defcore(config, testlist)
39             m.assert_any_call(cmd)
40
41     def _get_main_kwargs(self, key=None):
42         kwargs = {'config': self._config,
43                   'testlist': self._testlist}
44         if key:
45             del kwargs[key]
46         return kwargs
47
48     def _test_main(self, status, *args):
49         kwargs = self._get_main_kwargs()
50         self.assertEqual(self.refstackclient.main(**kwargs), status)
51         if len(args) > 0:
52             args[0].assert_called_once_with(
53                 refstack_client.RefstackClient.result_dir)
54         if len(args) > 1:
55             args
56
57     def _test_main_missing_keyword(self, key):
58         kwargs = self._get_main_kwargs(key)
59         self.assertEqual(self.refstackclient.main(**kwargs),
60                          testcase.TestCase.EX_RUN_ERROR)
61
62     def test_main_missing_conf(self):
63         self._test_main_missing_keyword('config')
64
65     def test_main_missing_testlist(self):
66         self._test_main_missing_keyword('testlist')
67
68     def _test_argparser(self, arg, value):
69         self.defaultargs[arg] = value
70         parser = refstack_client.RefstackClientParser()
71         self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]),
72                          self.defaultargs)
73
74     def test_argparser_conf(self):
75         self._test_argparser('config', self._config)
76
77     def test_argparser_testlist(self):
78         self._test_argparser('testlist', self._testlist)
79
80     def test_argparser_multiple_args(self):
81         self.defaultargs['config'] = self._config
82         self.defaultargs['testlist'] = self._testlist
83         parser = refstack_client.RefstackClientParser()
84         self.assertEqual(parser.parse_args(
85             ["--config={}".format(self._config),
86              "--testlist={}".format(self._testlist)
87              ]), self.defaultargs)
88
89
90 if __name__ == "__main__":
91     logging.disable(logging.CRITICAL)
92     unittest.main(verbosity=2)