From 0cfb8fef6bf00802a76c1e2304f1a1f8cac989ac Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 20 Aug 2024 13:54:32 +0200 Subject: [PATCH 1/7] test: use onlyImage() from cockpit test lib Use the onlyImage() decorator provided by the test lib of cockpit, replacing the usage of our own skipUnlessDistroFamily(). There is no behaviour change. --- test/check-subscriptions | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/test/check-subscriptions b/test/check-subscriptions index 2d7527a..ebc330f 100755 --- a/test/check-subscriptions +++ b/test/check-subscriptions @@ -17,12 +17,10 @@ # along with Cockpit; If not, see . import os -import unittest import uuid from packagelib import * from testlib import * -from machine import testvm # candlepin on the services image has a lot of demo data preloaded # useful info/commands: @@ -125,15 +123,6 @@ def machine_restorecon(machine, path, *args): return machine.execute(cmd) -def skipUnlessDistroFamily(distro, reason): - """ - Skip the current test function with the specified [reason] - unless the distribution of the machine is part of the specified - family [distro] (i.e. it starts as "distro-"). - """ - return unittest.skipUnless(testvm.DEFAULT_IMAGE.startswith(distro + "-"), reason) - - class SubscriptionsCase(MachineCase): # fmt: off provision = { @@ -324,7 +313,7 @@ class TestSubscriptions(SubscriptionsCase): ) self.allow_journal_messages("junior is not in the sudoers file. This incident will be reported.") - @skipUnlessDistroFamily("rhel", "Insights support is specific to RHEL") + @onlyImage("Insights support is specific to RHEL", "rhel-*") def testInsights(self): m = self.machine b = self.browser @@ -376,7 +365,7 @@ class TestSubscriptions(SubscriptionsCase): b.wait_visible("button:contains('Not connected')") - @skipUnlessDistroFamily("rhel", "Insights support is specific to RHEL") + @onlyImage("Insights support is specific to RHEL", "rhel-*") def testSubAndInAndFail(self): m = self.machine b = self.browser From 8f441c61483886a624c04e595d1a60b167da12e9 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 20 Aug 2024 13:59:06 +0200 Subject: [PATCH 2/7] test: upload product certificates to /etc/pki/product-default That is their canonical location. --- test/check-subscriptions | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/check-subscriptions b/test/check-subscriptions index ebc330f..aa929e8 100755 --- a/test/check-subscriptions +++ b/test/check-subscriptions @@ -148,9 +148,9 @@ class SubscriptionsCase(MachineCase): prod_id = product["id"] filename = os.path.join(self.tmpdir, "%s.pem" % prod_id) self.candlepin.download("/home/admin/candlepin/generated_certs/%s.pem" % prod_id, filename) - m.upload([filename], "/etc/pki/product") + m.upload([filename], "/etc/pki/product-default") - m.execute(["mkdir", "-p", "/etc/pki/product"]) + m.execute(["mkdir", "-p", "/etc/pki/product-default"]) download_product(PRODUCT_DONALDY) download_product(PRODUCT_SHARED) From 616c9add4f4964c90b527aadeb1ce63ab525df5b Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 3 Sep 2024 17:30:34 +0200 Subject: [PATCH 3/7] test: drop extra activation key code The Python snippet to add a pool to an activation key in practice does not work anymore for two reasons: 1) the test data in the self-deployed Candlepin changed 2) the test account used is based on SCA and the result of the last POST in the script is: { "displayMessage" : "Multi-entitlement not supported for pool \"Pool [id: 8a8082e491b0fb160191b0fb37e20233, type: NORMAL, product: sfs, productName: Shared File System, quantity: 5]\"", "requestUuid" : "8e43664e-e720-4cb8-8a99-bfe3cd49af2c" } Especially because of (2), it is possible to get rid of the script altogether, and simply use the available activation keys. There is no behaviour change. --- test/check-subscriptions | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/test/check-subscriptions b/test/check-subscriptions index aa929e8..b966d26 100755 --- a/test/check-subscriptions +++ b/test/check-subscriptions @@ -40,16 +40,6 @@ from testlib import * # # to use the candlepin image on a test machine, either add the certificate or # allow insecure connections (/etc/rhsm/rhsm.conf -> "insecure = 1") -# -# $IP is the ip of the candlepin machine -# -# add an activation key to a pool: -# curl --insecure --request POST --user huey:password \ -# https://$IP:8443/candlepin/activation_keys/ff80808155ca50b10155ca50cd280010/pools/ff80808155ca50b10155ca51f04607e5 -# register with: activation key "awesome_os_pool" and org "donaldduck" -# -# in order to get the right ids for the activation key and pool, see -# ACTIVATION_KEY_SCRIPT WAIT_SCRIPT = """ import sys @@ -72,29 +62,6 @@ for i in range(1, 200): time.sleep(1) """ -ACTIVATION_KEY_SCRIPT = """ -import sys -import json -import requests - -data = requests.get(sys.argv[1] + "/activation_keys", auth=("huey", "password")).json() -key = [ - e["id"] for e in data if e["name"] == "awesome_os_pool" and e["owner"]["displayName"] == "Admin Owner" -][0] - -data = requests.get(sys.argv[1] + "/pools", auth=("huey", "password")).json() -pool = [ - e["id"] - for e in data - if e["owner"]["key"] == "admin" - and e["contractNumber"] == "0" - and [p for p in e["providedProducts"] if p["productId"] == "88888"] -][0] - -key_url = sys.argv[1] + "/activation_keys/{key}/pools/{pool}".format(key=key, pool=pool) -requests.post(key_url, auth=("huey", "password")) -""" - # fmt: off CLIENT_ADDR = "10.111.112.1" CANDLEPIN_ADDR = "10.111.112.100" @@ -290,8 +257,6 @@ class TestSubscriptions(SubscriptionsCase): activation_key_checkbox = "#subscription-register-activation-key-method" b.click(activation_key_checkbox) - # make sure we have an activation key on the target machine - machine_python(self.machine, ACTIVATION_KEY_SCRIPT, CANDLEPIN_URL) b.set_input_text("#subscription-register-key", "awesome_os_pool") b.set_input_text("#subscription-register-org", "donaldduck") From 81c485b0aef153eeec1b8d47cee71ea7c553267c Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 3 Sep 2024 18:05:32 +0200 Subject: [PATCH 4/7] test: use curl directly to wait for Candlepin Instead of running a Python script to wait for Candlepin to start, use curl to do the busy-wait loop. Keep the initial delay as-is, performing it inline. There should be no behaviour changes. --- test/check-subscriptions | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/test/check-subscriptions b/test/check-subscriptions index b966d26..6bf41e7 100755 --- a/test/check-subscriptions +++ b/test/check-subscriptions @@ -17,6 +17,7 @@ # along with Cockpit; If not, see . import os +import time import uuid from packagelib import * @@ -41,27 +42,6 @@ from testlib import * # to use the candlepin image on a test machine, either add the certificate or # allow insecure connections (/etc/rhsm/rhsm.conf -> "insecure = 1") -WAIT_SCRIPT = """ -import sys -import time -import requests - -# minimum waiting time for Candlepin to answer (in seconds) -waiting_time = 10 - -sys.stderr.write("Waiting %s seconds for %s\\n" % (waiting_time, sys.argv[1])) -sys.stderr.flush() -time.sleep(waiting_time) -for i in range(1, 200): - try: - sys.stderr.write("Waiting for %s\\n" % sys.argv[1]) - sys.stderr.flush() - res = requests.get(sys.argv[1]) - break - except: - time.sleep(1) -""" - # fmt: off CLIENT_ADDR = "10.111.112.1" CANDLEPIN_ADDR = "10.111.112.100" @@ -133,8 +113,10 @@ class SubscriptionsCase(MachineCase): m.upload([candlepin_ca_tmpfile], f"/etc/rhsm/ca/{candlepin_ca_filename}") machine_restorecon(self.machine, "/etc/rhsm/ca/") - # Wait for the web service to be accessible - machine_python(self.machine, WAIT_SCRIPT, CANDLEPIN_URL) + # Wait for the web service to be accessible, with an initial delay + # to give more time to Candlepin to start + time.sleep(10) + m.execute(f"until curl --fail --silent --show-error {CANDLEPIN_URL}/status; do sleep 1; done") # Setup the repositories properly using the Candlepin RPM GPG key m.execute( From 1a462a502582dfb508b1a4313046b77c376aaa58 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 20 Aug 2024 14:19:24 +0200 Subject: [PATCH 5/7] test: use f-strings to replace other string formattings Use f-strings to replace C-like placeholders and format() calls. There is no behaviour change. --- test/check-subscriptions | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/check-subscriptions b/test/check-subscriptions index 6bf41e7..487378f 100755 --- a/test/check-subscriptions +++ b/test/check-subscriptions @@ -46,7 +46,7 @@ from testlib import * CLIENT_ADDR = "10.111.112.1" CANDLEPIN_ADDR = "10.111.112.100" CANDLEPIN_HOSTNAME = "services.cockpit.lan" -CANDLEPIN_URL = "https://%s:8443/candlepin" % CANDLEPIN_HOSTNAME +CANDLEPIN_URL = f"https://{CANDLEPIN_HOSTNAME}:8443/candlepin" PRODUCT_DONALDY = { "id": "7050", @@ -93,8 +93,8 @@ class SubscriptionsCase(MachineCase): # download product info from the candlepin machine def download_product(product): prod_id = product["id"] - filename = os.path.join(self.tmpdir, "%s.pem" % prod_id) - self.candlepin.download("/home/admin/candlepin/generated_certs/%s.pem" % prod_id, filename) + filename = os.path.join(self.tmpdir, f"{prod_id}.pem") + self.candlepin.download(f"/home/admin/candlepin/generated_certs/{prod_id}.pem", filename) m.upload([filename], "/etc/pki/product-default") m.execute(["mkdir", "-p", "/etc/pki/product-default"]) From 7e696796211ed7fefa43e8b894249867822f39ca Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 20 Aug 2024 14:21:07 +0200 Subject: [PATCH 6/7] test: use a simpler way to append content to a file Use the existing API of a cockpit Machine, rather than running a shell command manually. There is no behaviour change. --- test/check-subscriptions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/check-subscriptions b/test/check-subscriptions index 487378f..f8b00b9 100755 --- a/test/check-subscriptions +++ b/test/check-subscriptions @@ -88,7 +88,7 @@ class SubscriptionsCase(MachineCase): self.candlepin.execute(["/root/run-candlepin"]) # make sure the cockpit machine can resolve the service machine hostname - m.execute(f"echo '{CANDLEPIN_ADDR} {CANDLEPIN_HOSTNAME}' >> /etc/hosts") + m.write("/etc/hosts", f"{CANDLEPIN_ADDR} {CANDLEPIN_HOSTNAME}\n", append=True) # download product info from the candlepin machine def download_product(product): From 862c23c5cf01e64e0372c0f127e65f9d4616f056 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 3 Sep 2024 18:09:09 +0200 Subject: [PATCH 7/7] test: drop no more used machine_python() helper function There are no more Python scripts to run as part of the tests. --- test/check-subscriptions | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/check-subscriptions b/test/check-subscriptions index f8b00b9..5e241cd 100755 --- a/test/check-subscriptions +++ b/test/check-subscriptions @@ -60,11 +60,6 @@ PRODUCT_SHARED = { # fmt: on -def machine_python(machine, script, *args): - cmd = ["python3", "-c", script] + list(args) - return machine.execute(cmd) - - def machine_restorecon(machine, path, *args): cmd = ["restorecon", "-R", path] + list(args) return machine.execute(cmd)