summaryrefslogtreecommitdiff
path: root/tests/unittests/sources/vmware/test_custom_script.py
blob: fcbb9cd5d7dc746d6cfb65114a86abb2d4146aca (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
# Copyright (C) 2015 Canonical Ltd.
# Copyright (C) 2017-2019 VMware INC.
#
# Author: Maitreyee Saikia <msaikia@vmware.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

import os
import stat
from cloudinit import util
from cloudinit.sources.helpers.vmware.imc.config_custom_script import (
    CustomScriptConstant,
    CustomScriptNotFound,
    PreCustomScript,
    PostCustomScript,
)
from tests.unittests.helpers import CiTestCase, mock


class TestVmwareCustomScript(CiTestCase):
    def setUp(self):
        self.tmpDir = self.tmp_dir()
        # Mock the tmpDir as the root dir in VM.
        self.execDir = os.path.join(self.tmpDir, ".customization")
        self.execScript = os.path.join(self.execDir,
                                       ".customize.sh")

    def test_prepare_custom_script(self):
        """
        This test is designed to verify the behavior based on the presence of
        custom script. Mainly needed for scenario where a custom script is
        expected, but was not properly copied. "CustomScriptNotFound" exception
        is raised in such cases.
        """
        # Custom script does not exist.
        preCust = PreCustomScript("random-vmw-test", self.tmpDir)
        self.assertEqual("random-vmw-test", preCust.scriptname)
        self.assertEqual(self.tmpDir, preCust.directory)
        self.assertEqual(self.tmp_path("random-vmw-test", self.tmpDir),
                         preCust.scriptpath)
        with self.assertRaises(CustomScriptNotFound):
            preCust.prepare_script()

        # Custom script exists.
        custScript = self.tmp_path("test-cust", self.tmpDir)
        util.write_file(custScript, "test-CR-strip\r\r")
        with mock.patch.object(CustomScriptConstant,
                               "CUSTOM_TMP_DIR",
                               self.execDir):
            with mock.patch.object(CustomScriptConstant,
                                   "CUSTOM_SCRIPT",
                                   self.execScript):
                postCust = PostCustomScript("test-cust",
                                            self.tmpDir,
                                            self.tmpDir)
                self.assertEqual("test-cust", postCust.scriptname)
                self.assertEqual(self.tmpDir, postCust.directory)
                self.assertEqual(custScript, postCust.scriptpath)
                postCust.prepare_script()

                # Custom script is copied with exec privilege
                self.assertTrue(os.path.exists(self.execScript))
                st = os.stat(self.execScript)
                self.assertTrue(st.st_mode & stat.S_IEXEC)
                with open(self.execScript, "r") as f:
                    content = f.read()
                self.assertEqual(content, "test-CR-strip")
                # Check if all carraige returns are stripped from script.
                self.assertFalse("\r" in content)

    def test_execute_post_cust(self):
        """
        This test is designed to verify the behavior after execute post
        customization.
        """
        # Prepare the customize package
        postCustRun = self.tmp_path("post-customize-guest.sh", self.tmpDir)
        util.write_file(postCustRun, "This is the script to run post cust")
        userScript = self.tmp_path("test-cust", self.tmpDir)
        util.write_file(userScript, "This is the post cust script")

        # Mock the cc_scripts_per_instance dir and marker file.
        # Create another tmp dir for cc_scripts_per_instance.
        ccScriptDir = self.tmp_dir()
        ccScript = os.path.join(ccScriptDir, "post-customize-guest.sh")
        markerFile = os.path.join(self.tmpDir, ".markerFile")
        with mock.patch.object(CustomScriptConstant,
                               "CUSTOM_TMP_DIR",
                               self.execDir):
            with mock.patch.object(CustomScriptConstant,
                                   "CUSTOM_SCRIPT",
                                   self.execScript):
                with mock.patch.object(CustomScriptConstant,
                                       "POST_CUSTOM_PENDING_MARKER",
                                       markerFile):
                    postCust = PostCustomScript("test-cust",
                                                self.tmpDir,
                                                ccScriptDir)
                    postCust.execute()
                    # Check cc_scripts_per_instance and marker file
                    # are created.
                    self.assertTrue(os.path.exists(ccScript))
                    with open(ccScript, "r") as f:
                        content = f.read()
                    self.assertEqual(content,
                                     "This is the script to run post cust")
                    self.assertTrue(os.path.exists(markerFile))

# vi: ts=4 expandtab