aboutsummaryrefslogtreecommitdiffstats
path: root/functest/tests/unit/cli/commands/test_cli_os.py
blob: 434370a5c16e362fdd9c2be6763dcb2561e42e35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
#
# jose.lausuch@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
#

import logging
import unittest
import os

import mock

from functest.cli.commands import cli_os


class CliOpenStackTesting(unittest.TestCase):

    def setUp(self):
        self.endpoint_ip = 'test_ip'
        self.os_auth_url = 'http://test_ip:test_port/v2.0'
        self.installer_type = 'test_installer_type'
        self.installer_ip = 'test_installer_ip'
        self.openstack_creds = 'test_openstack_creds'
        self.snapshot_file = 'test_snapshot_file'
        self.cli_os = cli_os.CliOpenStack()

    def test_ping_endpoint_default(self):
        self.cli_os.os_auth_url = self.os_auth_url
        self.cli_os.endpoint_ip = self.endpoint_ip
        with mock.patch('functest.cli.commands.cli_os.os.system',
                        return_value=0):
            self.assertEqual(self.cli_os.ping_endpoint(), 0)

    @mock.patch('functest.cli.commands.cli_os.exit', side_effect=Exception)
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_ping_endpoint_missing_auth_url(self, mock_click_echo,
                                            mock_exit):
        with self.assertRaises(Exception):
            self.cli_os.os_auth_url = None
            self.cli_os.ping_endpoint()
            mock_click_echo.assert_called_once_with("Source the OpenStack "
                                                    "credentials first '. "
                                                    "$creds'")

    @mock.patch('functest.cli.commands.cli_os.exit')
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_ping_endpoint_os_system_fails(self, mock_click_echo,
                                           mock_exit):
        self.cli_os.os_auth_url = self.os_auth_url
        self.cli_os.endpoint_ip = self.endpoint_ip
        with mock.patch('functest.cli.commands.cli_os.os.system',
                        return_value=1):
            self.cli_os.ping_endpoint()
            mock_click_echo.assert_called_once_with("Cannot talk to the "
                                                    "endpoint %s\n" %
                                                    self.endpoint_ip)
            mock_exit.assert_called_once_with(0)

    def test_check(self):
        with mock.patch.object(self.cli_os, 'ping_endpoint'), \
            mock.patch('functest.cli.commands.cli_os.check_deployment.'
                       'CheckDeployment') as mock_check_deployment:
            self.cli_os.check()
            self.assertTrue(mock_check_deployment.called)

    @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
                return_value=False)
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_snapshot_create(self, mock_click_echo, mock_os_path):
        with mock.patch.object(self.cli_os, 'ping_endpoint'), \
                mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
                as mock_os_snapshot:
            self.cli_os.snapshot_create()
            mock_click_echo.assert_called_once_with("Generating Openstack "
                                                    "snapshot...")
            self.assertTrue(mock_os_snapshot.called)

    @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
                return_value=True)
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path):
        with mock.patch('six.moves.input', return_value="y") \
                as mock_raw_input, \
                mock.patch.object(self.cli_os, 'ping_endpoint'), \
                mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
                as mock_os_snapshot:
            self.cli_os.snapshot_create()
            mock_click_echo.assert_called_once_with("Generating Openstack "
                                                    "snapshot...")
            mock_raw_input.assert_any_call("It seems there is already an "
                                           "OpenStack snapshot. Do you want "
                                           "to overwrite it with the current "
                                           "OpenStack status? [y|n]\n")
            self.assertTrue(mock_os_snapshot.called)

    @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
                return_value=False)
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path):
        self.cli_os.snapshot_show()
        mock_click_echo.assert_called_once_with("There is no OpenStack "
                                                "snapshot created. To create "
                                                "one run the command "
                                                "'functest openstack "
                                                "snapshot-create'")

    @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
                return_value=True)
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_snapshot_show_default(self, mock_click_echo, mock_os_path):
        with mock.patch('six.moves.builtins.open',
                        mock.mock_open(read_data='0')) \
                as m:
            self.cli_os.snapshot_file = self.snapshot_file
            self.cli_os.snapshot_show()
            m.assert_called_once_with(self.snapshot_file, 'r')
            mock_click_echo.assert_called_once_with("\n0")

    @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
                return_value=True)
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_clean(self, mock_click_echo, mock_os_path):
        with mock.patch.object(self.cli_os, 'ping_endpoint'), \
                mock.patch('functest.cli.commands.cli_os.os_clean.main') \
                as mock_os_clean:
            self.cli_os.clean()
            self.assertTrue(mock_os_clean.called)

    @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
                return_value=False)
    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_clean_missing_file(self, mock_click_echo, mock_os_path):
        with mock.patch.object(self.cli_os, 'ping_endpoint'):
            self.cli_os.clean()
            mock_click_echo.assert_called_once_with("Not possible to clean "
                                                    "OpenStack without a "
                                                    "snapshot. This could "
                                                    "cause problems. "
                                                    "Run first the command "
                                                    "'functest openstack "
                                                    "snapshot-create'")

    @mock.patch('functest.cli.commands.cli_os.click.echo')
    def test_show_credentials(self, mock_click_echo):
        key = 'OS_KEY'
        value = 'OS_VALUE'
        with mock.patch.dict(os.environ, {key: value}):
            self.cli_os.show_credentials()
            mock_click_echo.assert_any_call("{}={}".format(key, value))


if __name__ == "__main__":
    logging.disable(logging.CRITICAL)
    unittest.main(verbosity=2)