Merge ~mitchburton/ubuntu/+source/landscape-client:2027613-2040189-service-start-fix-mantic into ubuntu/+source/landscape-client:ubuntu/noble-devel

Proposed by Mitch Burton
Status: Merged
Approved by: Andreas Hasenack
Approved revision: 9c04b8934e82f2f519ab93ebf1c0e754022c1b5f
Merged at revision: 9c04b8934e82f2f519ab93ebf1c0e754022c1b5f
Proposed branch: ~mitchburton/ubuntu/+source/landscape-client:2027613-2040189-service-start-fix-mantic
Merge into: ubuntu/+source/landscape-client:ubuntu/noble-devel
Diff against target: 488 lines (+382/-22)
9 files modified
debian/changelog (+16/-0)
debian/control (+3/-1)
debian/landscape-client.service (+1/-0)
debian/landscape-common.postinst (+0/-5)
debian/landscape-sysinfo.wrapper (+33/-15)
debian/patches/0001-start-service-during-config.patch (+269/-0)
debian/patches/0002-fix-broken-build-tests.patch (+54/-0)
debian/patches/series (+2/-0)
debian/rules (+4/-1)
Reviewer Review Type Date Requested Status
Andreas Hasenack Approve
Review via email: mp+454434@code.launchpad.net

Commit message

fix: landscape-config does not start landscape-client service (LP: #2040189)
fix: avoid stopping services on upgrade (LP: #2027613)

To post a comment you must log in.
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Thanks for adding the tests!

Comments inline about packaging practices. Besides those, it would help to have separate commits. Given the changes, I would expect three commits:
- one for the d/rules fix
- one for the added patch (so d/p/0001-.... and d/p/series)
- one for the d/changelog entry

In that way, I can easily compare the commits across different ubuntu releases using git range-diff.

review: Needs Fixing
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I'll refrain from reviewing the other branches, since the changes would be the same. Let's get noble in good shape, apply the same review comments to the other branches, and then we can proceed.

Revision history for this message
Mitch Burton (mitchburton) wrote :

Thanks for the comments. I've split the commit into three, as suggested, and addressed the inline comments in this revision.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

To re-enable the proper test suite, this is what I had to do:
Author: Andreas Hasenack <email address hidden>
Date: Wed Nov 8 14:30:55 2023 +0000

      * Re-enable build-time test suite (LP: #XXXXXX):
        - d/rules: use correct test command
        - d/control: build-depend on python3-pycurl which is used by the
          test suite

diff --git a/debian/control b/debian/control
index ec0471c..6c42510 100644
--- a/debian/control
+++ b/debian/control
@@ -6,7 +6,8 @@ XSBC-Original-Maintainer: Landscape Team <email address hidden>
 Build-Depends: debhelper-compat (= 12), po-debconf, libdistro-info-perl,
                dh-python, python3-dev, python3-distutils-extra,
                lsb-release, gawk, net-tools,
- python3-apt, python3-twisted, python3-configobj
+ python3-apt, python3-twisted, python3-configobj,
+ python3-pycurl
 Standards-Version: 4.4.0
 Homepage: https://github.com/CanonicalLtd/landscape-client

diff --git a/debian/rules b/debian/rules
index d6a156c..f41183a 100755
--- a/debian/rules
+++ b/debian/rules
@@ -24,3 +24,6 @@ override_dh_auto_install:

 override_dh_installsystemd:
        dh_installsystemd
+
+override_dh_auto_test:
+ make check3

There is a "check" makefile target already, but it calls out to check2 and check3. check2 is for python2, and doesn't work anymore. You might want to:
- remove check2 from upstream (if my reading of this is correct)
- maybe change setup.py to run the proper test suite with trial. If this is done, then I think we don't even need to change d/rules, as the dh_auto_test debhelper will likely do the right thing

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Hi Mitch, did you have a chance to look at my last comment yet?

Revision history for this message
Mitch Burton (mitchburton) wrote :

I've updated d/rules and d/changelog to enable the test suite. Do I need to create a corresponding LP bug?

Wasn't able to change setup.py to run the proper test suite, as setuptools appears to have deprecated the `test` command - they recommend using tox.

I've also created a PR to upstream to clean up the Makefile there, and after that's merged will also contribute the patch to get the test suite running on build.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

> I've updated d/rules and d/changelog to enable the test suite. Do I need to create a corresponding
> LP bug?

Yes please, and then updated the d/changelog entry about this with the LP bug number.

> Wasn't able to change setup.py to run the proper test suite, as setuptools appears to have deprecated
> the `test` command - they recommend using tox.

Ok

> I've also created a PR to upstream to clean up the Makefile there, and after that's merged will also
> contribute the patch to get the test suite running on build.

Cool

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I had to add a few extra build-dependencies, to cope with "module not found" errors in the test suite:

python3-netifaces
python3-yaml
ubuntu-advantage-tools

But the tests are now failing:
https://launchpadlibrarian.net/698874496/buildlog_ubuntu-noble-amd64.landscape-client_23.08-0ubuntu2~ppa4_BUILDING.txt.gz

Many of the errors are happening because HOME=/sbuild-nonexistent, and that directory really doesn't exist. I checked older build logs, before we enabled the test suite, and they also had this HOME setting.

Errors like:
FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.cache/ubuntu-pro'

This comes from within ubuntu-advantage-client. Have you seen this before? I suspect the workaround will be to set HOME to a valid path, but was wondering if you have seen this before in other test environments.

Revision history for this message
Mitch Burton (mitchburton) wrote :

Re: sbuild-nonexistent. I haven't seen this before in other test environments. Generally HOME is set to an existing directory. I agree that setting HOME to a valid path should work. What's the cleanest way to do that? Something like:

override_dh_auto_test:
    HOME=$(shell pwd) make check3

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I just tried this:

@@ -26,4 +26,4 @@ override_dh_installsystemd:
        dh_installsystemd

 override_dh_auto_test:
- make check3
+ HOME=$(shell mktemp -d) make check3

It moved much further[1], but now is failing elsewhere:

Traceback (most recent call last):
  File "/<<PKGBUILDDIR>>/landscape/client/manager/tests/test_scriptexecution.py", line 466, in test_user
    return self._run_script(username, uid, gid, path)
  File "/<<PKGBUILDDIR>>/landscape/client/manager/tests/test_scriptexecution.py", line 433, in _run_script
    self.assertEqual(spawn[4], path)
  File "/usr/lib/python3/dist-packages/twisted/trial/_synctest.py", line 422, in assertEqual
    super().assertEqual(first, second, msg)
  File "/usr/lib/python3.11/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.11/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
twisted.trial.unittest.FailTest: '/' != '/nonexistent'
- /
+ /nonexistent

and

Unable to list installed snaps: (7, "Failed to connect to localhost port 80 after 0 ms: Couldn't connect to server")

and one other type.

Maybe we need to export HOME.

1. https://launchpadlibrarian.net/698892672/buildlog_ubuntu-noble-amd64.landscape-client_23.08-0ubuntu2~ppa6_BUILDING.txt.gz

Revision history for this message
Mitch Burton (mitchburton) wrote :

Okay I see three categories of errors still:

  1. the one you mentioned above - I think exporting HOME will probably help, as this is a subprocess execution being tested
  2. communications with snapd's REST API. I take it the build environment does not have snapd? May need to have these tests conditionally skipped or mock the snapd side of things in all tests. It's mocked in one of them, which passes.
  3. locale.setlocale(locale.LC_CTYPE, ("C", "UTF-8")) is failing, probably because this locale hasn't been generated. Is it alright to locale-gen ahead of running the tests? Do we need that as a build-depends?

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

1. Trying exporting HOME:

 override_dh_auto_test:
- make check3
+ export HOME=$(shell mktemp -d) && make check3

Log: https://launchpadlibrarian.net/698896308/buildlog_ubuntu-noble-amd64.landscape-client_23.08-0ubuntu2~ppa8_BUILDING.txt.gz

In the last run, the locale and snap errors remain, but also this one:

[FAIL]
Traceback (most recent call last):
  File "/<<PKGBUILDDIR>>/landscape/client/manager/tests/test_scriptexecution.py", line 466, in test_user
    return self._run_script(username, uid, gid, path)
  File "/<<PKGBUILDDIR>>/landscape/client/manager/tests/test_scriptexecution.py", line 433, in _run_script
    self.assertEqual(spawn[4], path)
  File "/usr/lib/python3/dist-packages/twisted/trial/_synctest.py", line 422, in assertEqual
    super().assertEqual(first, second, msg)
  File "/usr/lib/python3.11/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.11/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
twisted.trial.unittest.FailTest: '/' != '/nonexistent'
- /
+ /nonexistent

This might still be about HOME.

2. I suspect snapd is not installed. If we need it for tests, we can build-depend on it, but to be honest I don't know if the test environment will restrict snapd somehow. We can try. Do you have to install it in the github runners?

3. I suppose it's allright to locale-gen before the test, but will that really fix it? I suspect it's another missing build-depends. Are these new tests? If not, we could go back to an older landscape-client and check what the build-depends was back then. But if this test is new, then it makes sense that a build-depends was missed.

Could you take it from here please? :) Use a PPA for these iterations, just like I was doing here.

Revision history for this message
Mitch Burton (mitchburton) wrote :

I'll look further into the HOME issue.

2. snapd does not have to be installed in the ubuntu github runners. It's already in there. I'd be interested in trying it as a build dependency.

3. Change and accompanying test are 3 1/2 years old: https://github.com/canonical/landscape-client/commit/25aed2015e3da424a185c1caaf38bada9705a9f4
   I can try an old revision and see.

Yup, I'll take it from here.

Revision history for this message
Mitch Burton (mitchburton) wrote :

Looks like for the above test, if the user's home directory (in this case /nonexistent) doesn't exist, the code falls back to /. I'll need to produce an additional patch to fix this, though it will be a very small one.

Revision history for this message
Mitch Burton (mitchburton) wrote (last edit ):

Including locales-all as a build dependency solved some of the errors. Including snapd did not solve the snapd-related issues.

https://launchpadlibrarian.net/698912164/buildlog_ubuntu-noble-amd64.landscape-client_23.08-0ubuntu2~ppa2_BUILDING.txt.gz

So it looks like snapd interaction is a bust, generally. I will have to patch the associated tests to mock it or be skipped.

https://launchpadlibrarian.net/698915314/buildlog_ubuntu-noble-amd64.landscape-client_23.08-0ubuntu2~ppa4_BUILDING.txt.gz

error: cannot list snaps: cannot communicate with server: Get "http://localhost/v2/snaps": dial unix /run/snapd.socket: connect: no such file or directory

Revision history for this message
Mitch Burton (mitchburton) wrote :

Pushed version with a passing build. Let me know if the commits or patches need rearranging.

buildlog: https://launchpadlibrarian.net/699090088/buildlog_ubuntu-noble-amd64.landscape-client_23.08-0ubuntu2~ppa6_BUILDING.txt.gz

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Please rebase on top of https://launchpad.net/ubuntu/+source/landscape-client/23.08-0ubuntu2, which was uploaded to noble

Revision history for this message
Mitch Burton (mitchburton) wrote :

Rebased on latest commit to noble-devel.

Revision history for this message
Andreas Hasenack (ahasenack) :
review: Needs Information
Revision history for this message
Andreas Hasenack (ahasenack) :
review: Needs Fixing
Revision history for this message
Andreas Hasenack (ahasenack) :
Revision history for this message
Mitch Burton (mitchburton) wrote :

Pushed latest changes. Summary of what I've changed
- added a revert of the fix for https://bugs.launchpad.net/ubuntu/+source/landscape-client/+bug/2040924
- added a new fix for above, updating d/landscape-sysinfo.wrapper, tested it by executing it as both a regular user and root as per my previous testing for the previous fix
- Added Bug-Ubuntu header items for both patches

re: inline comment about falling back to "/" instead of HOME in patch 0002:
Internally, landscape.lib.user uses pwd (https://docs.python.org/3/library/pwd.html#module-pwd) for script execution to run a script in the run-as user's pw_dir directory, which circumvents HOME. If pw_dir doesn't exist, it defaults to "/".

I'm open to changing that default to something less bad, though. Or maybe first trying to default to HOME, then something else? I was trying to restrict my changes to just test files, though.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Some comments inline, let me know what you think.,

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Ah, the cp will preserve the permissions if the target file (the cache in this case) already exists, but not if it doesn't, then it will use the permissions of the source file.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

It might be simpler to just make sure the perms are 0644 in the end.

Revision history for this message
Andreas Hasenack (ahasenack) :
Revision history for this message
Andreas Hasenack (ahasenack) :
Revision history for this message
Mitch Burton (mitchburton) :
Revision history for this message
Mitch Burton (mitchburton) :
Revision history for this message
Mitch Burton (mitchburton) wrote :

Sorry, Launchpad double-posted my previous comments. Also going to ensure perms are 0644.

Revision history for this message
Mitch Burton (mitchburton) wrote :

updated landscape-sysinfo.cache to use a shell variable instead of a temp file. This simplifies cleanup. Also changed permissions of the cache to 0644.

Revision history for this message
Andreas Hasenack (ahasenack) :
Revision history for this message
Mitch Burton (mitchburton) :
Revision history for this message
Andreas Hasenack (ahasenack) :
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

+1

review: Approve
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Sponsored:
Uploading landscape-client_23.08-0ubuntu3.dsc
Uploading landscape-client_23.08-0ubuntu3.debian.tar.xz
Uploading landscape-client_23.08-0ubuntu3_source.buildinfo
Uploading landscape-client_23.08-0ubuntu3_source.changes

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/debian/changelog b/debian/changelog
2index 4eaf917..1e04a38 100644
3--- a/debian/changelog
4+++ b/debian/changelog
5@@ -1,3 +1,19 @@
6+landscape-client (23.08-0ubuntu3) noble; urgency=medium
7+
8+ * d/p/0001-start-service-during-config.patch: fix landscape-config does not
9+ start landscape-client service (LP: #2040189)
10+ * d/rules: avoid stopping services on upgrade (LP: #2027613)
11+ * Re-enable build-time test suite (LP: #2044181)
12+ - d/rules: enable test suite during build
13+ - d/control: build-depend on python3-pycurl, python3-netifaces,
14+ python3-yaml, ubuntu-advantage-tools, locales-all
15+ - d/p/0002-fix-broken-build-tests.patch: add mocks to snapd-related tests;
16+ use existing directory if HOME does not exist
17+ * d/landscape-sysinfo.wrapper: fix handle using cache when permissions allow
18+ (LP: #2040924)
19+
20+ -- Mitch Burton <mitch.burton@canonical.com> Tue, 28 Nov 2023 11:40:17 -0800
21+
22 landscape-client (23.08-0ubuntu2) noble; urgency=medium
23
24 * d/landscape-common.postint: ensure sysinfo cache file exists and is globally
25diff --git a/debian/control b/debian/control
26index ec0471c..c70ceae 100644
27--- a/debian/control
28+++ b/debian/control
29@@ -6,7 +6,9 @@ XSBC-Original-Maintainer: Landscape Team <landscape-team@canonical.com>
30 Build-Depends: debhelper-compat (= 12), po-debconf, libdistro-info-perl,
31 dh-python, python3-dev, python3-distutils-extra,
32 lsb-release, gawk, net-tools,
33- python3-apt, python3-twisted, python3-configobj
34+ python3-apt, python3-twisted, python3-configobj,
35+ python3-pycurl, python3-netifaces, python3-yaml,
36+ ubuntu-advantage-tools, locales-all
37 Standards-Version: 4.4.0
38 Homepage: https://github.com/CanonicalLtd/landscape-client
39
40diff --git a/debian/landscape-client.service b/debian/landscape-client.service
41index 6dc721a..aa5c026 100644
42--- a/debian/landscape-client.service
43+++ b/debian/landscape-client.service
44@@ -11,6 +11,7 @@ WantedBy=multi-user.target
45 [Service]
46 Type=simple
47 Group=landscape
48+ExecCondition=/usr/bin/landscape-config --is-registered
49 ExecStart=/usr/bin/landscape-client
50 # Don't kill cgroup as child dpkg may restart the service during an upgrade.
51 KillMode=process
52diff --git a/debian/landscape-common.postinst b/debian/landscape-common.postinst
53index 9aeb7a5..e78c193 100755
54--- a/debian/landscape-common.postinst
55+++ b/debian/landscape-common.postinst
56@@ -90,11 +90,6 @@ case "$1" in
57 WRAPPER=/usr/share/landscape/landscape-sysinfo.wrapper
58 PROFILE_LOCATION=/etc/profile.d/50-landscape-sysinfo.sh
59 UPDATE_MOTD_LOCATION=/etc/update-motd.d/50-landscape-sysinfo
60- STAMP_LOCATION=/var/lib/landscape/landscape-sysinfo.cache
61-
62- touch $STAMP_LOCATION
63- chown landscape:root $STAMP_LOCATION
64- chmod 666 $STAMP_LOCATION
65
66 if [ "$RET" = "Cache sysinfo in /etc/motd" ]; then
67 rm -f $PROFILE_LOCATION 2>/dev/null || true
68diff --git a/debian/landscape-sysinfo.wrapper b/debian/landscape-sysinfo.wrapper
69index 54f139d..4460113 100644
70--- a/debian/landscape-sysinfo.wrapper
71+++ b/debian/landscape-sysinfo.wrapper
72@@ -2,28 +2,46 @@
73
74 # don't try refresh this more than once per minute
75 # Due to cpu consumption and login delays (LP: #1893716)
76-stamp="/var/lib/landscape/landscape-sysinfo.cache"
77-NEED_UPDATE="FALSE"
78-[ -z "$(find "$stamp" -newermt 'now-1 minutes' 2> /dev/null)" ] && NEED_UPDATE="TRUE"
79+CACHE="/var/lib/landscape/landscape-sysinfo.cache"
80+HAS_CACHE="FALSE"
81+CACHE_NEEDS_UPDATE="FALSE"
82+
83+[ -r "$CACHE" ] && HAS_CACHE="TRUE"
84+[ -z "$(find "$CACHE" -newermt 'now-1 minutes' 2> /dev/null)" ] && CACHE_NEEDS_UPDATE="TRUE"
85+
86+if [ "$HAS_CACHE" = "TRUE" ] && [ "$CACHE_NEEDS_UPDATE" = "FALSE" ]; then
87+ cat "$CACHE"
88+else
89+ SYSINFO=""
90
91-if [ "$NEED_UPDATE" = "TRUE" ]; then
92 # pam_motd does not carry the environment
93 [ -f /etc/default/locale ] && . /etc/default/locale
94 export LANG
95- cores=$(grep -c ^processor /proc/cpuinfo 2>/dev/null)
96- [ "$cores" -eq "0" ] && cores=1
97- threshold="${cores:-1}.0"
98- if [ $(echo "`cut -f1 -d ' ' /proc/loadavg` < $threshold" | bc) -eq 1 ]; then
99- printf "\n System information as of %s\n\n%s\n" \
100+ CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null)
101+ [ "$CORES" -eq "0" ] && CORES=1
102+ THRESHOLD="${cores:-1}.0"
103+
104+ if [ $(echo "`cut -f1 -d ' ' /proc/loadavg` < $THRESHOLD" | bc) -eq 1 ]; then
105+ SYSINFO=$(printf "\n System information as of %s\n\n%s\n" \
106 "$(/bin/date)" \
107- "$(/usr/bin/landscape-sysinfo)" \
108- > "$stamp"
109+ "$(/usr/bin/landscape-sysinfo)")
110+ echo "$SYSINFO" 2>/dev/null >"$CACHE" || true
111+ chmod 0644 "$CACHE" 2>/dev/null || true
112 else
113- # do not replace a formerly good result due to load
114- if ! grep -q "System information as of" $stamp 2> /dev/null; then
115- printf "\n System information disabled due to load higher than %s\n" "$threshold" > "$stamp"
116+ SYSINFO=$(printf "\n System information disabled due to load higher than %s\n" "$THRESHOLD")
117+
118+ if [ "$HAS_CACHE" = "TRUE" ]; then
119+ if ! grep -q "System information as of" $CACHE 2>/dev/null; then
120+ # do not replace a formerly good result due to load
121+ echo "$SYSINFO" 2>/dev/null >"$CACHE" || true
122+ chmod 0644 "$CACHE" 2>/dev/null || true
123+ else
124+ SYSINFO=$(cat "$CACHE")
125+ fi
126 fi
127 fi
128+
129+ printf "%s\n" "$SYSINFO"
130 fi
131
132-[ ! -r "$stamp" ] || cat "$stamp"
133+exit 0
134diff --git a/debian/patches/0001-start-service-during-config.patch b/debian/patches/0001-start-service-during-config.patch
135new file mode 100644
136index 0000000..d3fb5f9
137--- /dev/null
138+++ b/debian/patches/0001-start-service-during-config.patch
139@@ -0,0 +1,269 @@
140+Description: Allow landscape-config to start landscape-client systemd service
141+Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/landscape-client/+bug/2040189
142+Author: Mitch Burton <mitch.burton@canonical.com>
143+Origin: upstream, https://github.com/canonical/landscape-client/commit/0da6b4b64c7ca50c109279bd42633c537458fcf4
144+Reviewed-by: Kevin Nasto <kevin.nasto@canonical.com>
145+Applied-Upstream: 23.10
146+Last-Update: 2023-11-07
147+---
148+This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
149+--- a/landscape/client/configuration.py
150++++ b/landscape/client/configuration.py
151+@@ -624,8 +624,10 @@
152+ decode_base64_ssl_public_certificate(config)
153+ config.write()
154+ # Restart the client to ensure that it's using the new configuration.
155++
156+ if not config.no_start:
157+ try:
158++ set_secure_id(config, "registering")
159+ ServiceConfig.restart_landscape()
160+ except ServiceConfigException as exc:
161+ print_text(str(exc), error=True)
162+@@ -796,13 +798,14 @@
163+ # Results will be things like "success" or "ssl-error".
164+ result = results[0]
165+
166+- if isinstance(result, SystemExit):
167+- raise result
168+-
169+ # If there was an error and the caller requested that errors be reported
170+ # to the on_error callable, then do so.
171+ if result != "success" and on_error is not None:
172+ on_error(1)
173++
174++ if isinstance(result, SystemExit):
175++ raise result
176++
177+ return result
178+
179+
180+@@ -883,6 +886,21 @@
181+ return text
182+
183+
184++def set_secure_id(config, new_id):
185++ """Persists a secure id in the identity data file. This is used to indicate
186++ whether we are currently in the process of registering.
187++ """
188++ persist = Persist(
189++ filename=os.path.join(
190++ config.data_path,
191++ f"{BrokerService.service_name}.bpickle",
192++ ),
193++ )
194++ identity = Identity(config, persist)
195++ identity.secure_id = new_id
196++ persist.save()
197++
198++
199+ def main(args, print=print):
200+ """Interact with the user and the server to set up client configuration."""
201+
202+@@ -927,7 +945,11 @@
203+ # Attempt to register the client.
204+ reactor = LandscapeReactor()
205+ if config.silent:
206+- result = register(config, reactor)
207++ result = register(
208++ config,
209++ reactor,
210++ on_error=lambda _: set_secure_id(config, None),
211++ )
212+ report_registration_outcome(result, print=print)
213+ sys.exit(determine_exit_code(result))
214+ else:
215+@@ -937,6 +959,10 @@
216+ default=default_answer,
217+ )
218+ if answer:
219+- result = register(config, reactor)
220++ result = register(
221++ config,
222++ reactor,
223++ on_error=lambda _: set_secure_id(config, None),
224++ )
225+ report_registration_outcome(result, print=print)
226+ sys.exit(determine_exit_code(result))
227+--- a/landscape/client/tests/test_configuration.py
228++++ b/landscape/client/tests/test_configuration.py
229+@@ -34,6 +34,7 @@
230+ from landscape.client.configuration import register
231+ from landscape.client.configuration import registration_info_text
232+ from landscape.client.configuration import report_registration_outcome
233++from landscape.client.configuration import set_secure_id
234+ from landscape.client.configuration import setup
235+ from landscape.client.configuration import show_help
236+ from landscape.client.configuration import store_public_key_data
237+@@ -738,12 +739,17 @@
238+ bootstrap_tree_patcher = mock.patch(
239+ "landscape.client.configuration.bootstrap_tree",
240+ )
241++ set_secure_id_patch = mock.patch(
242++ "landscape.client.configuration.set_secure_id",
243++ )
244+ self.mock_getuid = getuid_patcher.start()
245+ self.mock_bootstrap_tree = bootstrap_tree_patcher.start()
246++ set_secure_id_patch.start()
247+
248+ def cleanup():
249+ getuid_patcher.stop()
250+ bootstrap_tree_patcher.stop()
251++ set_secure_id_patch.stop()
252+
253+ self.addCleanup(cleanup)
254+
255+@@ -1191,7 +1197,11 @@
256+ )
257+ self.assertEqual(0, exception.code)
258+ mock_setup.assert_called_once_with(mock.ANY)
259+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
260++ mock_register.assert_called_once_with(
261++ mock.ANY,
262++ mock.ANY,
263++ on_error=mock.ANY,
264++ )
265+
266+ @mock.patch("landscape.client.configuration.input", return_value="y")
267+ @mock.patch(
268+@@ -1219,7 +1229,11 @@
269+ )
270+ self.assertEqual(0, exception.code)
271+ mock_setup.assert_called_once_with(mock.ANY)
272+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
273++ mock_register.assert_called_once_with(
274++ mock.ANY,
275++ mock.ANY,
276++ on_error=mock.ANY,
277++ )
278+ mock_input.assert_called_once_with(
279+ "\nRequest a new registration for this computer now? [Y/n]: ",
280+ )
281+@@ -1256,7 +1270,11 @@
282+ )
283+ self.assertEqual(2, exception.code)
284+ mock_setup.assert_called_once_with(mock.ANY)
285+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
286++ mock_register.assert_called_once_with(
287++ mock.ANY,
288++ mock.ANY,
289++ on_error=mock.ANY,
290++ )
291+ mock_input.assert_called_once_with(
292+ "\nRequest a new registration for this computer now? [Y/n]: ",
293+ )
294+@@ -1295,7 +1313,11 @@
295+ )
296+ self.assertEqual(0, exception.code)
297+ mock_setup.assert_called_once_with(mock.ANY)
298+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
299++ mock_register.assert_called_once_with(
300++ mock.ANY,
301++ mock.ANY,
302++ on_error=mock.ANY,
303++ )
304+ mock_input.assert_not_called()
305+
306+ self.assertEqual(
307+@@ -1333,7 +1355,11 @@
308+ )
309+ self.assertEqual(2, exception.code)
310+ mock_setup.assert_called_once_with(mock.ANY)
311+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
312++ mock_register.assert_called_once_with(
313++ mock.ANY,
314++ mock.ANY,
315++ on_error=mock.ANY,
316++ )
317+ mock_input.assert_not_called()
318+ # Note that the error is output via sys.stderr.
319+ self.assertEqual(
320+@@ -1378,7 +1404,11 @@
321+ mock_serviceconfig.set_start_on_boot.assert_called_once_with(True)
322+ mock_serviceconfig.restart_landscape.assert_called_once_with()
323+ mock_setup_script().run.assert_called_once_with()
324+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
325++ mock_register.assert_called_once_with(
326++ mock.ANY,
327++ mock.ANY,
328++ on_error=mock.ANY,
329++ )
330+ mock_input.assert_called_with(
331+ "\nRequest a new registration for this computer now? [Y/n]: ",
332+ )
333+@@ -1457,7 +1487,11 @@
334+ print=noop_print,
335+ )
336+ mock_setup.assert_called_once_with(mock.ANY)
337+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
338++ mock_register.assert_called_once_with(
339++ mock.ANY,
340++ mock.ANY,
341++ on_error=mock.ANY,
342++ )
343+ mock_input.assert_called_once_with(
344+ "\nRequest a new registration for this computer now? [Y/n]: ",
345+ )
346+@@ -1477,7 +1511,11 @@
347+ print=noop_print,
348+ )
349+ mock_setup.assert_called_once_with(mock.ANY)
350+- mock_register.assert_called_once_with(mock.ANY, mock.ANY)
351++ mock_register.assert_called_once_with(
352++ mock.ANY,
353++ mock.ANY,
354++ on_error=mock.ANY,
355++ )
356+ mock_input.assert_not_called()
357+
358+ @mock.patch("landscape.client.configuration.input")
359+@@ -2290,6 +2328,27 @@
360+ # We ask for retries because networks aren't reliable.
361+ self.assertEqual(99, connector.max_retries)
362+
363++ def test_register_got_error(self):
364++ """If there is an error from the connection, raises `SystemExit`."""
365++ reactor = mock.Mock()
366++ connector_factory = mock.Mock()
367++ results = []
368++
369++ def add_error():
370++ results.append(SystemExit())
371++
372++ reactor.run.side_effect = add_error
373++
374++ self.assertRaises(
375++ SystemExit,
376++ register,
377++ self.config,
378++ reactor,
379++ connector_factory,
380++ max_retries=2,
381++ results=results,
382++ )
383++
384+ @mock.patch("landscape.client.configuration.LandscapeReactor")
385+ def test_register_without_reactor(self, mock_reactor):
386+ """If no reactor is passed, a LandscapeReactor will be instantiated.
387+@@ -2683,3 +2742,21 @@
388+ print=noop_print,
389+ )
390+ self.assertEqual(EXIT_NOT_REGISTERED, exception.code)
391++
392++
393++class SetSecureIdTest(LandscapeTest):
394++ """Tests for the `set_secure_id` function."""
395++
396++ @mock.patch("landscape.client.configuration.Persist")
397++ @mock.patch("landscape.client.configuration.Identity")
398++ def test_function(self, Identity, Persist):
399++ config = mock.Mock(data_path="/tmp/landscape")
400++
401++ set_secure_id(config, "fancysecureid")
402++
403++ Persist.assert_called_once_with(
404++ filename="/tmp/landscape/broker.bpickle",
405++ )
406++ Persist().save.assert_called_once_with()
407++ Identity.assert_called_once_with(config, Persist())
408++ self.assertEqual(Identity().secure_id, "fancysecureid")
409diff --git a/debian/patches/0002-fix-broken-build-tests.patch b/debian/patches/0002-fix-broken-build-tests.patch
410new file mode 100644
411index 0000000..8a118b3
412--- /dev/null
413+++ b/debian/patches/0002-fix-broken-build-tests.patch
414@@ -0,0 +1,54 @@
415+Description: Fix tests that do not pass in debian build environment
416+ In environments where the run-as user's home directory does not exist, such
417+ as sbuild, fall back to expecting the script path to be the root directory.
418+ Mock snapd, as it does not run in an accessible way in some environments.
419+Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/landscape-client/+bug/2044181
420+Author: Mitch Burton <mitch.burton@canonical.com>
421+Last-Update: 2023-11-22
422+---
423+This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
424+--- a/landscape/client/manager/tests/test_scriptexecution.py
425++++ b/landscape/client/manager/tests/test_scriptexecution.py
426+@@ -463,6 +463,9 @@
427+ gid = info.pw_gid
428+ path = info.pw_dir
429+
430++ if not os.path.exists(path):
431++ path = "/"
432++
433+ return self._run_script(username, uid, gid, path)
434+
435+ def test_user_no_home(self):
436+--- a/landscape/client/monitor/tests/test_snapmonitor.py
437++++ b/landscape/client/monitor/tests/test_snapmonitor.py
438+@@ -16,7 +16,12 @@
439+
440+ def test_get_data(self):
441+ """Tests getting installed snap data."""
442++ snap_http_mock = Mock(
443++ spec=SnapHttp,
444++ get_snaps=Mock(return_value={"result": []}),
445++ )
446+ plugin = SnapMonitor()
447++ plugin._snap_http = snap_http_mock
448+ self.monitor.add(plugin)
449+
450+ plugin.exchange()
451+--- a/landscape/client/snap/tests/test_http.py
452++++ b/landscape/client/snap/tests/test_http.py
453+@@ -19,6 +19,15 @@
454+ def test_get_snaps(self):
455+ """get_snaps() returns a dict with a list of installed snaps."""
456+ http = SnapHttp()
457++
458++ def fill_buff(curl, buff, **kwargs):
459++ buff.write(
460++ b'{"result": [{"id": "foo", "name": "bar", '
461++ b'"publisher": "baz"}]}'
462++ )
463++
464++ http._perform = Mock(side_effect=fill_buff)
465++
466+ result = http.get_snaps()["result"]
467+
468+ self.assertTrue(isinstance(result, list))
469diff --git a/debian/patches/series b/debian/patches/series
470index e69de29..ee1509d 100644
471--- a/debian/patches/series
472+++ b/debian/patches/series
473@@ -0,0 +1,2 @@
474+0001-start-service-during-config.patch
475+0002-fix-broken-build-tests.patch
476diff --git a/debian/rules b/debian/rules
477index ad30f5c..cc680cd 100755
478--- a/debian/rules
479+++ b/debian/rules
480@@ -23,4 +23,7 @@ override_dh_auto_install:
481 install -D -o root -g root -m 755 apt-update/apt-update $(CURDIR)/$(root_dir)$(LIBDIR)/apt-update
482
483 override_dh_installsystemd:
484- dh_installsystemd --no-enable --no-start --no-stop-on-upgrade
485+ dh_installsystemd
486+
487+override_dh_auto_test:
488+ HOME=$(shell mktemp -d) && make check3

Subscribers

People subscribed via source and target branches