Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jigish.addweb created an issue. See original summary.

jigish.addweb’s picture

jigish.addweb’s picture

Issue summary: View changes
yas’s picture

yas’s picture

Title: Fix a test case error: "Trying to access array offset on value of type null" in OpenStackEc2Service::describe () (OpenStackCloudConfigTest) » Fix a test case error: "Trying to access array offset on value of type null openstack_form_cloud_config_openstack_get_region()" (OpenStackCloudConfigTest)
Priority: Normal » Major
Issue summary: View changes
shobhit_juyal’s picture

FileSize
179.94 KB

Hi,

I have tried to fix the issue here. And as suggested, uploading the same patch file here to review.

diff -u b/modules/cloud_service_providers/openstack/openstack.module b/modules/cloud_service_providers/openstack/openstack.module
--- b/modules/cloud_service_providers/openstack/openstack.module
+++ b/modules/cloud_service_providers/openstack/openstack.module
@@ -293,8 +293,10 @@
   $response = OpenStackEc2Service::describeRegions([], $credentials);
 
   $region_name_response = [];
-  foreach ($response['Regions'] ?: [] as $regions) {
-    $region_name_response[] = $regions['RegionName'];
+  if (isset($response['Regions'])) {
+    foreach ($response['Regions'] ?: [] as $regions) {
+      $region_name_response[] = $regions['RegionName'];
+    }
   }

Note: The patch was as per the issue fixed for #3138080, I'd not recreated here.

yas’s picture

Status: Active » Needs work
Related issues: +#3138080: Refactor the status and log messages (2)

@shobhit_juyal

Thank you for uploading the patch. Could you please re-create the patch? We've merged the patch at #3138080.

shobhit_juyal’s picture

Working to upload patch

shobhit_juyal’s picture

FileSize
1.44 KB

Hi @yas,

Here is the patch..

shobhit_juyal’s picture

Status: Needs work » Needs review
shobhit_juyal’s picture

Assigned: shobhit_juyal » Unassigned

needs a review, unassigning myself.
Thanks

baldwinlouie’s picture

Status: Needs review » Needs work

Thank you for the patches.

@all, I think we should merge the contents of both patches.

@shobhit_juyal's patch is great for fixing the PHP warning. @jigish.addweb's patch fills in the mockdata for the actual test.

yas’s picture

@baldwinlouie

Thank you for your review. I also created the patch for the test case.

@shobhit_juyal

Thank you for re-creating the patch. The patch looks fixing the issue, and yes, actually the tests have been passed successfully however it might not fix the root cause. I guess the patch would only fix the issue by bypassing the test case. Our expectation is that OpenStack cloud service provider creation form validates if the parameter of the region is valid (= existing) or not by calling describeRegions() w/ the credential parameters if we pass the correct credentials.

@shobhit_juyal
@jigishaddweb

I added a test case for OpenStackCloudConfigTest::testCloudConfig() to make it error w/ a message 'Region is invalid. Please enter valid region.' by passing a wrong input parameter of the region while saving a new OpenStack cloud service provider. So could you please check the test w/ that patch?

jigish.addweb’s picture

@yas

Thank you for the patch.

Getting below error in OpenStackCloudConfigTest::testCloudConfig().

Drupal\Tests\openstack\Functional\cloud\config\OpenStackCloudConfigTest::testCloudConfig
Behat\Mink\Exception\ResponseTextException: The text "Creating cloud service provider was performed successfully." appears in the text of this page, but it should not.

Also, need to re-create the patch due to #3138473-7 patch merge.

yas’s picture

@jigishaddweb

Yes, that's what I recognized. If my additional test case is correct (please review it carefully), the current code have something wrong so it should be fixed.

I re-created the patch for the additional test case so you can use it.

jigish.addweb’s picture

jigish.addweb’s picture

jigish.addweb’s picture

jigish.addweb’s picture

@yas

I fixed below issues in the new patch

  1. "Cannot read credentials" in OpenStack Cloud service provider error
  1. OpenStackCloudConfigTest testcase error

Please review the new patch.

Thanks

jigish.addweb’s picture

Status: Needs work » Needs review
yas’s picture

Status: Needs review » Needs work

@jigishaddweb

Thank you for updating the patch. It looks good, so could you please change isset to !empty?

+++ b/modules/cloud_service_providers/openstack/openstack.module
@@ -121,19 +121,13 @@ function openstack_cloud_config_presave(CloudConfig $cloud_config) {
...
     if (isset($cloud_config->get('field_access_key')->value) && isset($cloud_config->get('field_secret_key')->value)) {
+++ b/modules/cloud_service_providers/openstack/openstack.module
@@ -293,8 +374,10 @@ function openstack_form_cloud_config_openstack_get_region(array $credentials = [
...
+  if (isset($response['Regions'])) {
shobhit_juyal’s picture

Hi @yas

Here, I guess, isset is fine because it check the existence of the array's key.

+++ b/modules/cloud_service_providers/openstack/openstack.module
@@ -293,8 +374,10 @@ function openstack_form_cloud_config_openstack_get_region(array $credentials = [
...
+  if (isset($response['Regions'])) {

Thanks

yas’s picture

@shobhit_juyal

Thank you for your review. We are trying to use !empty in any cases of the isset context in the entire Cloud family modules since isset and !empty is not exactly the same; and !empty will more fit to our expectation in most of cases.

Especially in the following case, we don't want to pass the variable $response['Regions'] at the if statement in case of $response['Regions'] = [].

+++ b/modules/cloud_service_providers/openstack/openstack.module
@@ -293,8 +374,10 @@ function openstack_form_cloud_config_openstack_get_region(array $credentials = [
...
+  if (isset($response['Regions'])) {

Moreover, we put ?: [] in foreach statement to avoid null pointer exception. e.g.

+    foreach ($response['Regions'] ?: [] as $regions) {

HTH

jigish.addweb’s picture

jigish.addweb’s picture

Status: Needs work » Needs review

@yas Thank you for reviewing the patch.

I updated the code. Please review the updated patch.

Thanks

yas’s picture

@jigishaddweb

Thank you for the update. It looks good to me now.

@baldwinlouie

What do you think?

baldwinlouie’s picture

Status: Needs review » Reviewed & tested by the community

@yas and @jigish, The patch looks good to me. I do have the following comment, but it can be another ticket for refactoring.

+++ b/modules/cloud_service_providers/openstack/openstack.module
@@ -280,6 +336,31 @@ function openstack_form_cloud_config_openstack_region_validate(array &$form, For
+function openstack_create_credential_file($cloud_context, $access_key, $secret_key) {
+  // Write the access key ID and secret out to an ini file.
+  $access_data = <<<EOF
+[default]
+aws_access_key_id = {$access_key}
+aws_secret_access_key = {$secret_key}
+EOF;
+  $credential_file = aws_cloud_ini_file_path($cloud_context);
+  $credential_directory = aws_cloud_ini_directory();
+  if (\Drupal::service('file_system')->prepareDirectory($credential_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
+    \Drupal::service('file_system')->saveData($access_data, $credential_file, FileSystemInterface::EXISTS_REPLACE);
+  }
+
+}
+

This can be another ticket, but I think this function can move up to aws_cloud module. aws_cloud has a similar function, and I think we can refactor this.

yas’s picture

@baldwinlouie

Thank you for your review. That's a good point, so let's address it in another issue. I'll merge the patch to 8.x-1.x and 8.x-2.x and close this issue as Fixed.

  • yas committed 590863b on 8.x-1.x authored by jigish.addweb
    Issue #3137586 by jigish.addweb, yas, shobhit_juyal, baldwinlouie: Fix a...

  • yas committed b8ebe7d on 8.x-2.x authored by jigish.addweb
    Issue #3137586 by jigish.addweb, yas, shobhit_juyal, baldwinlouie: Fix a...
yas’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.