From e2adef487e7def58cbf1d2b3fb37a086a6f4b189 Mon Sep 17 00:00:00 2001 From: Sean Mooney Date: Tue, 11 Mar 2025 14:54:01 +0000 Subject: [PATCH] [tests] Add printing of sample and template paths This commit refactors _verify_response in ApiSampleTestBase to print the paths to the relevent template or sample on test failure. It also moves the validation that the sample and template agree after the verification that the template and response match. Functionally the test is the same but now it's simpler to read and debug as there is a cleaner separation between the two phases of the verification and less state and context to absorb as a result. Change-Id: Ifc2552b6c0f7b667d24639d8aa685028120432ec Signed-off-by: Sean Mooney --- .../tests/functional/api_samples_test_base.py | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/nova/tests/functional/api_samples_test_base.py b/nova/tests/functional/api_samples_test_base.py index a6e5dbbe07c9..addb72e3e816 100644 --- a/nova/tests/functional/api_samples_test_base.py +++ b/nova/tests/functional/api_samples_test_base.py @@ -376,24 +376,12 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): self.assertEqual(exp_code, response.status_code, message) response_data = response.content response_data = pretty_data(response_data) - if not os.path.exists(self._get_template(name, - self.microversion)): + template_path = self._get_template(name, self.microversion) + if not os.path.exists(template_path): self._write_template(name, response_data) template_data = response_data else: template_data = self._read_template(name) - if (self.generate_samples and - not os.path.exists(self._get_sample( - name, self.microversion))): - - self._write_sample(name, response_data) - sample_data = response_data - else: - with open(self._get_sample(name, - self.microversion)) as sample: - sample_data = sample.read() - if update_links: - sample_data = self._update_links(sample_data) try: template_data = objectify(template_data) @@ -403,11 +391,20 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): except NoMatch as e: raise NoMatch("\nFailed to match Template to Response: \n%s\n" "Template: %s\n\n" + "Template Path: %s\n\n" "Response: %s\n\n" % - (e, - pp.pformat(template_data), + (e, pp.pformat(template_data), template_path, pp.pformat(response_data))) + sample_path = self._get_sample(name, self.microversion) + if (self.generate_samples and not os.path.exists(sample_path)): + self._write_sample(name, response_data) + sample_data = response_data + else: + with open(sample_path) as sample: + sample_data = sample.read() + if update_links: + sample_data = self._update_links(sample_data) try: # NOTE(danms): replace some of the subs with patterns for the # doc/api_samples check, which won't have things like the @@ -426,12 +423,13 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): except NoMatch as e: raise NoMatch("\nFailed to match Template to Sample: \n%s\n" "Template: %s\n\n" + "Template Path: %s\n\n" "Sample: %s\n\n" + "sample Path: %s\n\n" "Hint: does your test need to override " "ApiSampleTestBase.generalize_subs()?" % - (e, - pp.pformat(template_data), - pp.pformat(sample_data))) + (e, pp.pformat(template_data), template_path, + pp.pformat(response_data), sample_path)) def _get_host(self): return 'http://openstack.example.com'