File: test_xapi_plugin.py

package info (click to toggle)
python-os-xenapi 0.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,012 kB
  • sloc: python: 8,137; sh: 2,154; makefile: 45
file content (100 lines) | stat: -rw-r--r-- 4,354 bytes parent folder | download | duplicates (2)
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
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock
import os
import shutil
import tempfile

from os_xenapi.tests import base
from os_xenapi.utils import common_function
from os_xenapi.utils import xapi_plugin


class XenapiPluginTestCase(base.TestCase):
    @mock.patch.object(tempfile, 'mkdtemp',
                       return_value='/tmp/tmp1VyeJn')
    @mock.patch.object(common_function, 'execute')
    def test_get_os_xenapi_dir_with_version(self, mock_exec, mock_tmp):
        VERSION = '0.3.1'
        is_tmp, dir = xapi_plugin.get_os_xenapi_dir(VERSION)

        self.assertEqual(2, mock_exec.call_count)
        self.assertEqual(dir, '/tmp/tmp1VyeJn')
        self.assertEqual(is_tmp, True)

    @mock.patch.object(tempfile, 'mkdtemp')
    @mock.patch.object(common_function, 'execute')
    def test_get_os_xenapi_dir_no_version(self, mock_exec, mock_tmp):
        fake_loc = '/fake/install/loc'
        mock_exec.return_value = 'Location: %s\n' % fake_loc

        is_tmp, dir = xapi_plugin.get_os_xenapi_dir()

        self.assertEqual(dir, fake_loc)
        self.assertEqual(is_tmp, False)
        mock_exec.assert_called_with('pip', 'show',
                                     xapi_plugin.OS_XENAPI_PKG)
        mock_tmp.assert_not_called()

    @mock.patch.object(xapi_plugin, 'get_os_xenapi_dir')
    @mock.patch.object(os, 'listdir')
    @mock.patch.object(shutil, 'rmtree')
    def test_install_plugins_to_dom0_no_version(self, mock_rm, mock_dir,
                                                mock_get):
        FAKE_PKG_PATH = '/fake/pkg/path'
        ssh_client = mock.Mock()
        mock_get.return_value = (False, FAKE_PKG_PATH)
        mock_dir.return_value = ['file1', 'file2']

        xapi_plugin.install_plugins_to_dom0(ssh_client)

        dom0_path = xapi_plugin.DOM0_PLUGIN_PATH
        local_path = '%s/%s' % (FAKE_PKG_PATH,
                                xapi_plugin.PKG_PLUGIN_PATH)
        scp_expect = [mock.call('%s/file1' % local_path,
                                '%s/file1' % dom0_path),
                      mock.call('%s/file2' % local_path,
                                '%s/file2' % dom0_path)]
        ssh_expect = [mock.call('chmod +x %s/file1' % dom0_path),
                      mock.call('chmod +x %s/file2' % dom0_path)]
        self.assertEqual(scp_expect, ssh_client.scp.call_args_list)
        self.assertEqual(ssh_expect, ssh_client.ssh.call_args_list)
        # Shouldn't invoke mock_rm to remove the package dir.
        mock_rm.assert_not_called()

    @mock.patch.object(xapi_plugin, 'get_os_xenapi_dir')
    @mock.patch.object(os, 'listdir')
    @mock.patch.object(shutil, 'rmtree')
    def test_install_plugins_to_dom0_with_version(self, mock_rm, mock_dir,
                                                  mock_get):
        VERSION = '0.3.1'
        FAKE_PKG_PATH = '/tmp/tmp1VyeJn'
        ssh_client = mock.Mock()
        mock_get.return_value = (True, FAKE_PKG_PATH)
        mock_dir.return_value = ['file1', 'file2']

        xapi_plugin.install_plugins_to_dom0(ssh_client, VERSION)

        dom0_path = xapi_plugin.DOM0_PLUGIN_PATH
        local_path = '%s/%s' % (FAKE_PKG_PATH,
                                xapi_plugin.PKG_PLUGIN_PATH)
        scp_expect = [mock.call('%s/file1' % local_path,
                                '%s/file1' % dom0_path),
                      mock.call('%s/file2' % local_path,
                                '%s/file2' % dom0_path)]
        ssh_expect = [mock.call('chmod +x %s/file1' % dom0_path),
                      mock.call('chmod +x %s/file2' % dom0_path)]
        self.assertEqual(scp_expect, ssh_client.scp.call_args_list)
        self.assertEqual(ssh_expect, ssh_client.ssh.call_args_list)
        # Should invoke mock_rm to remove the package dir.
        mock_rm.assert_called_with(FAKE_PKG_PATH)