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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# Copyright 2014 Microsoft Corporation
#
# 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.
#
# Requires Python 2.4+ and Openssl 1.0+
#
import os.path
from azurelinuxagent.agent import *
from azurelinuxagent.common.conf import *
from tests.tools import *
EXPECTED_CONFIGURATION = \
"""AutoUpdate.Enabled = True
AutoUpdate.GAFamily = Prod
Autoupdate.Frequency = 3600
DVD.MountPoint = /mnt/cdrom/secure
DetectScvmmEnv = False
EnableOverProvisioning = False
Extension.LogDir = /var/log/azure
HttpProxy.Host = None
HttpProxy.Port = None
Lib.Dir = /var/lib/waagent
Logs.Verbose = False
OS.AllowHTTP = False
OS.CheckRdmaDriver = False
OS.EnableFIPS = True
OS.EnableFirewall = False
OS.EnableRDMA = False
OS.HomeDir = /home
OS.OpensslPath = /usr/bin/openssl
OS.PasswordPath = /etc/shadow
OS.RootDeviceScsiTimeout = 300
OS.SshClientAliveInterval = 42
OS.SshDir = /notareal/path
OS.SudoersDir = /etc/sudoers.d
OS.UpdateRdmaDriver = False
Pid.File = /var/run/waagent.pid
Provisioning.AllowResetSysUser = False
Provisioning.DecodeCustomData = False
Provisioning.DeleteRootPassword = True
Provisioning.Enabled = True
Provisioning.ExecuteCustomData = False
Provisioning.MonitorHostName = True
Provisioning.PasswordCryptId = 6
Provisioning.PasswordCryptSaltLength = 10
Provisioning.RegenerateSshHostKeyPair = True
Provisioning.SshHostKeyPairType = rsa
Provisioning.UseCloudInit = True
ResourceDisk.EnableSwap = False
ResourceDisk.Filesystem = ext4
ResourceDisk.Format = True
ResourceDisk.MountOptions = None
ResourceDisk.MountPoint = /mnt/resource
ResourceDisk.SwapSizeMB = 0""".split('\n')
class TestAgent(AgentTestCase):
def test_accepts_configuration_path(self):
conf_path = os.path.join(data_dir, "test_waagent.conf")
c, f, v, cfp = parse_args(["-configuration-path:" + conf_path])
self.assertEqual(cfp, conf_path)
@patch("os.path.exists", return_value=True)
def test_checks_configuration_path(self, mock_exists):
conf_path = "/foo/bar-baz/something.conf"
c, f, v, cfp = parse_args(["-configuration-path:"+conf_path])
self.assertEqual(cfp, conf_path)
self.assertEqual(mock_exists.call_count, 1)
@patch("sys.stderr")
@patch("os.path.exists", return_value=False)
@patch("sys.exit", side_effect=Exception)
def test_rejects_missing_configuration_path(self, mock_exit, mock_exists, mock_stderr):
try:
c, f, v, cfp = parse_args(["-configuration-path:/foo/bar.conf"])
self.assertTrue(False)
except Exception:
self.assertEqual(mock_exit.call_count, 1)
def test_configuration_path_defaults_to_none(self):
c, f, v, cfp = parse_args([])
self.assertEqual(cfp, None)
def test_agent_accepts_configuration_path(self):
Agent(False,
conf_file_path=os.path.join(data_dir, "test_waagent.conf"))
self.assertTrue(conf.get_fips_enabled())
@patch("azurelinuxagent.common.conf.load_conf_from_file")
def test_agent_uses_default_configuration_path(self, mock_load):
Agent(False)
mock_load.assert_called_once_with("/etc/waagent.conf")
@patch("azurelinuxagent.daemon.get_daemon_handler")
@patch("azurelinuxagent.common.conf.load_conf_from_file")
def test_agent_does_not_pass_configuration_path(self,
mock_load, mock_handler):
mock_daemon = Mock()
mock_daemon.run = Mock()
mock_handler.return_value = mock_daemon
agent = Agent(False)
agent.daemon()
mock_daemon.run.assert_called_once_with(child_args=None)
mock_load.assert_called_once()
@patch("azurelinuxagent.daemon.get_daemon_handler")
@patch("azurelinuxagent.common.conf.load_conf_from_file")
def test_agent_passes_configuration_path(self, mock_load, mock_handler):
mock_daemon = Mock()
mock_daemon.run = Mock()
mock_handler.return_value = mock_daemon
agent = Agent(False, conf_file_path="/foo/bar.conf")
agent.daemon()
mock_daemon.run.assert_called_once_with(child_args="-configuration-path:/foo/bar.conf")
mock_load.assert_called_once()
@patch("azurelinuxagent.common.conf.get_ext_log_dir")
def test_agent_ensures_extension_log_directory(self, mock_dir):
ext_log_dir = os.path.join(self.tmp_dir, "FauxLogDir")
mock_dir.return_value = ext_log_dir
self.assertFalse(os.path.isdir(ext_log_dir))
agent = Agent(False,
conf_file_path=os.path.join(data_dir, "test_waagent.conf"))
self.assertTrue(os.path.isdir(ext_log_dir))
@patch("azurelinuxagent.common.logger.error")
@patch("azurelinuxagent.common.conf.get_ext_log_dir")
def test_agent_logs_if_extension_log_directory_is_a_file(self, mock_dir, mock_log):
ext_log_dir = os.path.join(self.tmp_dir, "FauxLogDir")
mock_dir.return_value = ext_log_dir
fileutil.write_file(ext_log_dir, "Foo")
self.assertTrue(os.path.isfile(ext_log_dir))
self.assertFalse(os.path.isdir(ext_log_dir))
agent = Agent(False,
conf_file_path=os.path.join(data_dir, "test_waagent.conf"))
self.assertTrue(os.path.isfile(ext_log_dir))
self.assertFalse(os.path.isdir(ext_log_dir))
mock_log.assert_called_once()
def test_agent_get_configuration(self):
Agent(False, conf_file_path=os.path.join(data_dir, "test_waagent.conf"))
actual_configuration = []
configuration = conf.get_configuration()
for k in sorted(configuration.keys()):
actual_configuration.append("{0} = {1}".format(k, configuration[k]))
self.assertEqual(EXPECTED_CONFIGURATION, actual_configuration)
def test_agent_usage_message(self):
message = usage()
# Python 2.6 does not have assertIn()
self.assertTrue("-verbose" in message)
self.assertTrue("-force" in message)
self.assertTrue("-help" in message)
self.assertTrue("-configuration-path" in message)
self.assertTrue("-deprovision" in message)
self.assertTrue("-register-service" in message)
self.assertTrue("-version" in message)
self.assertTrue("-daemon" in message)
self.assertTrue("-start" in message)
self.assertTrue("-run-exthandlers" in message)
self.assertTrue("-show-configuration" in message)
# sanity check
self.assertFalse("-not-a-valid-option" in message)
|