summaryrefslogtreecommitdiff
path: root/tests/unittests/test_vmware/test_guestcust_util.py
blob: c8b59d836f5ccc329a74c28378c545138a6cd3c5 (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
# Copyright (C) 2019 Canonical Ltd.
# Copyright (C) 2019 VMware INC.
#
# Author: Xiaofeng Wang <xiaofengw@vmware.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import subp
from cloudinit.sources.helpers.vmware.imc.config import Config
from cloudinit.sources.helpers.vmware.imc.config_file import ConfigFile
from cloudinit.sources.helpers.vmware.imc.guestcust_util import (
    get_tools_config,
    set_gc_status,
)
from cloudinit.tests.helpers import CiTestCase, mock


class TestGuestCustUtil(CiTestCase):
    def test_get_tools_config_not_installed(self):
        """
        This test is designed to verify the behavior if vmware-toolbox-cmd
        is not installed.
        """
        with mock.patch.object(subp, 'which', return_value=None):
            self.assertEqual(
                get_tools_config('section', 'key', 'defaultVal'), 'defaultVal')

    def test_get_tools_config_internal_exception(self):
        """
        This test is designed to verify the behavior if internal exception
        is raised.
        """
        with mock.patch.object(subp, 'which', return_value='/dummy/path'):
            with mock.patch.object(subp, 'subp',
                                   return_value=('key=value', b''),
                                   side_effect=subp.ProcessExecutionError(
                                       "subp failed", exit_code=99)):
                # verify return value is 'defaultVal', not 'value'.
                self.assertEqual(
                    get_tools_config('section', 'key', 'defaultVal'),
                    'defaultVal')

    def test_get_tools_config_normal(self):
        """
        This test is designed to verify the value could be parsed from
        key = value of the given [section]
        """
        with mock.patch.object(subp, 'which', return_value='/dummy/path'):
            # value is not blank
            with mock.patch.object(subp, 'subp',
                                   return_value=('key =   value  ', b'')):
                self.assertEqual(
                    get_tools_config('section', 'key', 'defaultVal'),
                    'value')
            # value is blank
            with mock.patch.object(subp, 'subp',
                                   return_value=('key = ', b'')):
                self.assertEqual(
                    get_tools_config('section', 'key', 'defaultVal'),
                    '')
            # value contains =
            with mock.patch.object(subp, 'subp',
                                   return_value=('key=Bar=Wark', b'')):
                self.assertEqual(
                    get_tools_config('section', 'key', 'defaultVal'),
                    'Bar=Wark')

            # value contains specific characters
            with mock.patch.object(subp, 'subp',
                                   return_value=('[a] b.c_d=e-f', b'')):
                self.assertEqual(
                    get_tools_config('section', 'key', 'defaultVal'),
                    'e-f')

    def test_set_gc_status(self):
        """
        This test is designed to verify the behavior of set_gc_status
        """
        # config is None, return None
        self.assertEqual(set_gc_status(None, 'Successful'), None)

        # post gc status is NO, return None
        cf = ConfigFile("tests/data/vmware/cust-dhcp-2nic.cfg")
        conf = Config(cf)
        self.assertEqual(set_gc_status(conf, 'Successful'), None)

        # post gc status is YES, subp is called to execute command
        cf._insertKey("MISC|POST-GC-STATUS", "YES")
        conf = Config(cf)
        with mock.patch.object(subp, 'subp',
                               return_value=('ok', b'')) as mockobj:
            self.assertEqual(
                set_gc_status(conf, 'Successful'), ('ok', b''))
            mockobj.assert_called_once_with(
                ['vmware-rpctool', 'info-set guestinfo.gc.status Successful'],
                rcs=[0])

# vi: ts=4 expandtab