Skip to content

Commit d477883

Browse files
munirsidddosaboy
authored andcommitted
Add packages option to default userdata for proxied environments
(cherry picked from commit 329a962)
1 parent 135a083 commit d477883

4 files changed

Lines changed: 97 additions & 10 deletions

File tree

unit_tests/test_configure_guest.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2026 Canonical Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unit_tests.utils as ut_utils
16+
17+
import zaza.openstack.configure.guest as guest
18+
19+
20+
class TestGetDefaultUserdata(ut_utils.BaseTestCase):
21+
22+
EXPECTED_NO_PACKAGES = """#cloud-config
23+
apt:
24+
http_proxy: http://proxy.example.com:3128
25+
26+
write_files:
27+
- path: /etc/environment
28+
content: |
29+
http_proxy=http://proxy.example.com:3128
30+
https_proxy=http://proxy.example.com:3128
31+
no_proxy=localhost,127.0.0.1
32+
append: true
33+
34+
"""
35+
36+
EXPECTED_PACKAGES = """#cloud-config
37+
apt:
38+
http_proxy: http://proxy.example.com:3128
39+
40+
write_files:
41+
- path: /etc/environment
42+
content: |
43+
http_proxy=http://proxy.example.com:3128
44+
https_proxy=http://proxy.example.com:3128
45+
no_proxy=localhost,127.0.0.1
46+
append: true
47+
48+
packages:
49+
- nfs-common
50+
- curl
51+
"""
52+
53+
def setUp(self):
54+
super().setUp()
55+
self.patch_object(
56+
guest.deployment_env, 'get_deployment_context',
57+
return_value={
58+
'TEST_HTTP_PROXY': 'http://proxy.example.com:3128',
59+
'TEST_NO_PROXY': 'localhost,127.0.0.1',
60+
})
61+
62+
def test_without_packages(self):
63+
"""Test get_default_userdata without packages."""
64+
result = guest.get_default_userdata()
65+
self.assertEqual(result, self.EXPECTED_NO_PACKAGES)
66+
67+
def test_with_packages(self):
68+
"""Test get_default_userdata with multiple packages."""
69+
result = guest.get_default_userdata(packages=['nfs-common', 'curl'])
70+
self.assertEqual(result, self.EXPECTED_PACKAGES)
71+
72+
def test_with_empty_packages(self):
73+
"""Test get_default_userdata with empty packages list."""
74+
result = guest.get_default_userdata(packages=[])
75+
self.assertEqual(result, self.EXPECTED_NO_PACKAGES)
76+
77+
def test_with_none_packages(self):
78+
"""Test get_default_userdata with packages=None."""
79+
result = guest.get_default_userdata(packages=None)
80+
self.assertEqual(result, self.EXPECTED_NO_PACKAGES)

zaza/openstack/charm_tests/manila/tests.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ class ManilaBaseTest(test_utils.OpenStackBaseTest):
121121

122122
RESOURCE_PREFIX = 'zaza-manilatests'
123123
INSTANCE_KEY = 'bionic'
124-
INSTANCE_USERDATA = """#cloud-config
125-
packages:
126-
- nfs-common
127-
"""
124+
INSTANCE_PACKAGES = ['nfs-common']
128125

129126
@classmethod
130127
def setUpClass(cls):
@@ -334,13 +331,14 @@ def test_manila_share(self):
334331
6. Profit
335332
"""
336333
# Spawn Servers
334+
userdata = guest.get_default_userdata(packages=self.INSTANCE_PACKAGES)
337335
instance_1 = self.launch_guest(
338336
guest_name='ins-1',
339-
userdata=self.INSTANCE_USERDATA,
337+
userdata=userdata,
340338
instance_key=self.INSTANCE_KEY)
341339
instance_2 = self.launch_guest(
342340
guest_name='ins-2',
343-
userdata=self.INSTANCE_USERDATA,
341+
userdata=userdata,
344342
instance_key=self.INSTANCE_KEY)
345343

346344
fip_1 = neutron_tests.floating_ips_from_instance(instance_1)[0]

zaza/openstack/charm_tests/octavia/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def create_loadbalancer(self, ensure_volume_backed=False):
476476
# Then we request two Ubuntu instances with the Apache web server
477477
# installed
478478
instance_1, instance_2 = self.launch_guests(
479-
userdata=get_default_userdata() + 'packages:\n - apache2\n')
479+
userdata=get_default_userdata(packages=['apache2']))
480480

481481
# Get IP of the prepared payload instances
482482
payload_ips = []

zaza/openstack/configure/guest.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
no_proxy={no_proxy}
4646
append: true
4747
48-
"""
48+
{packages_section}"""
4949

5050

5151
boot_tests = {
@@ -67,18 +67,27 @@
6767
'bootstring': 'finished at'}}
6868

6969

70-
def get_default_userdata():
70+
def get_default_userdata(packages=None):
7171
"""
7272
Get default guest vm userdata.
7373
7474
If http proxy settings are available create a userdata file to enable them
7575
inside launched guest vms.
76+
77+
:param packages: Optional list of packages to install via cloud-init.
78+
:type packages: Optional[list[str]]
7679
"""
7780
deploy_env = deployment_env.get_deployment_context()
81+
packages_section = ""
82+
if packages:
83+
packages_section = "packages:\n"
84+
packages_section += "\n".join("- {}".format(p) for p in packages)
85+
packages_section += "\n"
7886
return DEFAULT_USER_DATA.format(
7987
http_proxy=deploy_env.get('TEST_HTTP_PROXY'),
8088
https_proxy=deploy_env.get('TEST_HTTP_PROXY'),
81-
no_proxy=deploy_env.get('TEST_NO_PROXY'))
89+
no_proxy=deploy_env.get('TEST_NO_PROXY'),
90+
packages_section=packages_section)
8291

8392

8493
def launch_instance(instance_key, use_boot_volume=False, vm_name=None,

0 commit comments

Comments
 (0)