Merge ~adam-collard/maas-ci/+git/system-tests:proxy-env-for-ansible into ~maas-committers/maas-ci/+git/system-tests:master

Proposed by Adam Collard
Status: Merged
Approved by: Adam Collard
Approved revision: 71c9c20d2ed5fa398fe3b9728321536ecfeb3879
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~adam-collard/maas-ci/+git/system-tests:proxy-env-for-ansible
Merge into: ~maas-committers/maas-ci/+git/system-tests:master
Diff against target: 231 lines (+61/-39)
2 files modified
systemtests/ansible.py (+40/-28)
systemtests/fixtures.py (+21/-11)
Reviewer Review Type Date Requested Status
Jack Lloyd-Walters Approve
MAAS Lander Approve
Review via email: mp+437129@code.launchpad.net

Commit message

[ansible-tests] Add proxy support

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: SUCCESS
COMMIT: 2f9a556bc794d00bd22cf23093b83404af1eb0a6

review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: SUCCESS
COMMIT: 3bffcd81b25a420af3bea52b017f05cc57e0c014

review: Approve
968c895... by Adam Collard

[ansible-playbook-tests] pass proxy as an extravar

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/system-tests-tester/416/consoleText
COMMIT: 1745597e7bc9b71ddeee888266ab06b95ca7ae79

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/system-tests-tester/417/consoleText
COMMIT: 968c89585c32fc9af80923947a04b6afa002671f

review: Needs Fixing
25414bd... by Adam Collard

[ansible-tests] Show tip of ansible-playbooks branch

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/system-tests-tester/418/consoleText
COMMIT: 3bba6f6f75e088e040c3b01789910863333c30b3

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: SUCCESS
COMMIT: 25414bd796c7ac82f78a45b893660bc8cf3a3272

review: Approve
1addfb4... by Adam Collard

[ansible-tests] Set proxy_env as base config to reset back to

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: SUCCESS
COMMIT: 1addfb4c32b21ae3be88708dac976d81d5fc7659

review: Approve
71c9c20... by Adam Collard

[ansible-tests] Bump timeout to 10m

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b proxy-env-for-ansible lp:~adam-collard/maas-ci/+git/system-tests into -b master lp:~maas-committers/maas-ci/+git/system-tests

STATUS: SUCCESS
COMMIT: 71c9c20d2ed5fa398fe3b9728321536ecfeb3879

review: Approve
Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) wrote :

LGTM, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/systemtests/ansible.py b/systemtests/ansible.py
2index 2ca5b40..87b8ad7 100644
3--- a/systemtests/ansible.py
4+++ b/systemtests/ansible.py
5@@ -7,7 +7,7 @@ from datetime import timedelta
6 from functools import cached_property
7 from logging import getLogger
8 from subprocess import CalledProcessError
9-from typing import TYPE_CHECKING, Any, Iterator
10+from typing import TYPE_CHECKING, Any, Iterator, Optional
11
12 from retry import retry
13
14@@ -56,23 +56,31 @@ def add_ansible_header(headers: list[str], config: dict[str, Any]) -> None:
15 headers.append("ansible-playbooks: true")
16
17
18-def apt_update(instance: Instance) -> None:
19+def apt_update(
20+ instance: Instance, environment: Optional[dict[str, str]] = None
21+) -> None:
22 """Update APT indices, fix broken dpkg."""
23- instance.quietly_execute(["apt-get", "update", "-y"])
24- instance.quietly_execute(["dpkg", "--configure", "-a"])
25+ instance.quietly_execute(["apt-get", "update", "-y"], environment=environment)
26+ instance.quietly_execute(["dpkg", "--configure", "-a"], environment=environment)
27
28
29-def apt_install(instance: Instance, package: str) -> None:
30+def apt_install(
31+ instance: Instance, package: str, environment: Optional[dict[str, str]] = None
32+) -> None:
33 """Install given package from apt."""
34- instance.quietly_execute(["apt", "install", package, "-y"])
35+ instance.quietly_execute(["apt", "install", package, "-y"], environment=environment)
36
37
38-def pip_install(instance: Instance, package: str) -> None:
39+def pip_install(
40+ instance: Instance, package: str, environment: Optional[dict[str, str]] = None
41+) -> None:
42 """Ensure latest version of Python package is installed."""
43 if not pip_package_exists(instance, package):
44- instance.quietly_execute(["pip3", "install", package])
45+ instance.quietly_execute(["pip3", "install", package], environment=environment)
46 else:
47- instance.quietly_execute(["pip3", "install", package, "--upgrade"])
48+ instance.quietly_execute(
49+ ["pip3", "install", package, "--upgrade"], environment=environment
50+ )
51
52
53 def pip_package_exists(instance: Instance, package: str) -> bool:
54@@ -84,7 +92,13 @@ def pip_package_exists(instance: Instance, package: str) -> bool:
55 return True
56
57
58-def clone_repo(instance: Instance, repo: str, branch: str, clone_path: str) -> None:
59+def clone_repo(
60+ instance: Instance,
61+ repo: str,
62+ branch: str,
63+ clone_path: str,
64+ environment: Optional[dict[str, str]] = None,
65+) -> None:
66 clone_file = instance.files[clone_path]
67 if not clone_file.exists():
68 instance.execute(
69@@ -98,7 +112,9 @@ def clone_repo(instance: Instance, repo: str, branch: str, clone_path: str) -> N
70 repo,
71 clone_path,
72 ],
73+ environment=environment,
74 )
75+ instance.execute(["git", "-C", clone_path, "show", "-q"])
76 instance.logger.info(f"Cloned {branch} from {repo} to {clone_path}")
77
78
79@@ -157,14 +173,6 @@ class AnsibleHost:
80 raise MissingRoleOnHost(role)
81 self.roles[role].pop(config_key)
82
83- def clear_config(self, role: str = "") -> None:
84- if not role:
85- self.config.clear()
86- return
87- if role not in self.roles.keys():
88- raise MissingRoleOnHost(role)
89- self.roles[role].clear()
90-
91 def host_setup(self, main_cfg: dict[str, str], role: str = "") -> str:
92 cfg = [self.ip]
93 cfg_dict = main_cfg.copy()
94@@ -256,7 +264,7 @@ class AnsibleHost:
95
96 class AnsibleMain:
97 use_timeout = True
98- timeout = timedelta(minutes=5)
99+ timeout = timedelta(minutes=10)
100 ssh_key_file = "/home/ubuntu/.ssh/id_rsa.pub"
101
102 def __init__(
103@@ -265,31 +273,35 @@ class AnsibleMain:
104 instance: Instance,
105 playbooks_repo: str,
106 playbooks_branch: str,
107+ proxy_env: Optional[dict[str, str]],
108 ) -> None:
109 self._lxd = lxd
110 self.instance = instance
111 self._playbooks_repo = playbooks_repo
112 self._playbooks_branch = playbooks_branch
113+ self._proxy_env = proxy_env
114 self._hosts_file = self.instance.files["/home/ubuntu/hosts"]
115
116- self.config: dict[str, str] = {}
117+ self._base_config = self._proxy_env if self._proxy_env else {}
118+ self.config: dict[str, str] = self._base_config.copy()
119 self._inventory_: set[AnsibleHost] = set()
120
121 self.ansible_repo_path = "/home/ubuntu/ansible_repo"
122
123 def setup(self) -> None:
124 self.logger.info("Installing python3-pip")
125- apt_update(self.instance)
126- apt_install(self.instance, "python3-pip")
127+ apt_update(self.instance, environment=self._proxy_env)
128+ apt_install(self.instance, "python3-pip", environment=self._proxy_env)
129 self.logger.info("Installing ansible")
130- pip_install(self.instance, "ansible")
131+ pip_install(self.instance, "ansible", environment=self._proxy_env)
132 self.logger.info("Installing netaddr")
133- pip_install(self.instance, "netaddr")
134+ pip_install(self.instance, "netaddr", environment=self._proxy_env)
135 clone_repo(
136 self.instance,
137 self._playbooks_repo,
138 self._playbooks_branch,
139 self.ansible_repo_path,
140+ environment=self._proxy_env,
141 )
142
143 self.create_config_file()
144@@ -388,7 +400,7 @@ class AnsibleMain:
145 yield hosts
146 finally:
147 self.remove_hosts(hosts)
148- self.clear_config()
149+ self.reset_config()
150
151 def _make_host_name_(self) -> str:
152 """Determine the smallest number not yet used as a name"""
153@@ -428,8 +440,8 @@ class AnsibleMain:
154 def remove_config(self, config_key: str) -> None:
155 self.config.pop(config_key)
156
157- def clear_config(self) -> None:
158- self.config.clear()
159+ def reset_config(self) -> None:
160+ self.config = self._base_config.copy()
161
162 def fetch_region(
163 self, host: AnsibleHost, user: str = "admin"
164@@ -520,4 +532,4 @@ class AnsibleMain:
165 ]
166 if _debug := re.match(r"-(v)+", debug):
167 cmd.append(str(_debug.group()))
168- self.instance.execute(cmd)
169+ self.instance.execute(cmd, environment=self._proxy_env)
170diff --git a/systemtests/fixtures.py b/systemtests/fixtures.py
171index e552fed..1052736 100644
172--- a/systemtests/fixtures.py
173+++ b/systemtests/fixtures.py
174@@ -50,11 +50,13 @@ def ansible_main(config: dict[str, Any]) -> Optional[Iterator[AnsibleMain]]:
175 lxd = get_lxd(log)
176 instance = Instance(lxd, "ansible-main")
177 instance.create_container(config["containers-image"])
178+ proxy_env = get_proxy_env(config.get("proxy", {}))
179 main = AnsibleMain(
180 lxd,
181 instance,
182 playbooks_repo=playbooks_repo,
183 playbooks_branch=playbooks_branch,
184+ proxy_env=proxy_env,
185 )
186 main.setup()
187 yield main
188@@ -110,6 +112,24 @@ def build_container(config: dict[str, Any]) -> Optional[Iterator[Instance]]:
189 yield instance
190
191
192+# TODO: Move to property of LXD, and expose on Instance
193+def get_proxy_env(
194+ proxy_config: dict[str, str], no_proxy: Optional[str] = None
195+) -> Optional[dict[str, str]]:
196+ """Get proxy related environment variables from config.yaml"""
197+ http_proxy = proxy_config.get("http", "")
198+ if http_proxy:
199+ proxy_env = {
200+ "http_proxy": http_proxy,
201+ "https_proxy": http_proxy,
202+ }
203+ if no_proxy:
204+ proxy_env["no_proxy"] = no_proxy
205+ return proxy_env
206+ else:
207+ return None
208+
209+
210 @pytest.fixture(scope="session")
211 def maas_deb_repo(
212 build_container: Optional[Instance], config: dict[str, Any]
213@@ -120,17 +140,7 @@ def maas_deb_repo(
214 else:
215 build_container.logger = getLogger(f"{LOG_NAME}.maas_deb_repo")
216 build_ip = build_container.get_ip_address()
217- http_proxy = config.get("proxy", {}).get("http", "")
218- proxy_env: Optional[dict[str, str]]
219- if http_proxy:
220- proxy_env = {
221- "http_proxy": http_proxy,
222- "https_proxy": http_proxy,
223- "no_proxy": build_ip,
224- }
225- else:
226- proxy_env = None
227-
228+ proxy_env = get_proxy_env(config.get("proxy", {}), no_proxy=build_ip)
229 if not build_container.files["/var/www/html/repo/Packages.gz"].exists():
230 maas_ppas = config.get("deb", {}).get(
231 "ppa", ["ppa:maas-committers/latest-deps"]

Subscribers

People subscribed via source and target branches

to all changes: