When a user specify a single or multiple Security Groups, ServerTemplate can store the configuration; however the user tries to launch the instance, only the default Security Group is specified at the actual instance.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Xiaohua Guan created an issue. See original summary.

Xiaohua Guan’s picture

Issue summary: View changes
Xiaohua Guan’s picture

Xiaohua Guan’s picture

Status: Active » Needs review
Xiaohua Guan’s picture

@yas

Please review the patch file.

yas’s picture

@xiaohua-guan Thank you for the patch including the tests. I really appreciate it.

@baldwinlouie How do you think?

baldwinlouie’s picture

Status: Needs review » Reviewed & tested by the community

Looks good! setting to RTBC

yas’s picture

Status: Reviewed & tested by the community » Needs work

@xiaohua-guan In order to avoid hard code and "hard logic", could you please refactor your test code? My suggestion is the following pseudo code.


  // Amazon VPC Limits - Security groups per VPC (per region).
  define('MAX_SECURITY_GROUPS_COUNT', 500);

  public function testServerTemplateLaunchByServer() {
    ...
    for ($i = 0; $i < MAX_SECURITY_GROUPS_COUNT) {

      // Mock object of SecurityGroup.
      $mock_security_groups = $this->getMock(SecurityGroupInterface::class);
      $group_name = $random->name(8, TRUE);
      $mock_security_group_value_map[] = [
        ['group_name', (object)['value' => $group_name]],
      ];
      $mock_security_groups->expects($this->any())
        ->method('get')
        ->will($this->returnValueMap($mock_security_group_value_map));
    }
    ...
    // Test cases.
    // Test when Security Groups has the only one item, the first item only.
    $field_security_groups[] = ['field_security_group', [
        (object)['entity' => reset(mock_security_groups),
      ]],
    ];

    // Test when Security Groups has more than two items.
    if (MAX_SECURITY_GROUP_COUNT > 1) {

      // The last item only.
      $field_security_groups[] = ['field_security_group', [
          (object)['entity' => end(array_mock_security_groups),
        ]],
      ];

      // The first item and the last one only.
      $field_security_groups[] = ['field_security_group', [
          (object)['entity' => reset(array_mock_security_groups),
          (object)['entity' => end(array_mock_security_groups),
        ]],

      if (MAX_SECURITY_GROUP_COUNT > 2) {

        // Randomly picking up the only one item.

        $field_security_groups[] = ['field_security_group', [
            // Pickup one index randomly (array index: 1 to the (last index -2))
            (object)['entity' => array_mock_security_groups[...],
          ]],

        $field_security_groups[] = ['field_security_group', [
            (object)['entity' => reset(array_mock_security_groups),
            // Pickup one index randomly (array index: 1 to the (last index -2))
            (object)['entity' => array_mock_security_groups[...],
          ]],
        ];

        $field_security_groups[] = ['field_security_group', [
            // Pickup one index randomly (array index: 1 to the (last index -2))
            (object)['entity' => array_mock_security_groups[...],
            (object)['entity' => end(array_mock_security_groups),
          ]],
        ];

        $field_security_groups[] = ['field_security_group', [
            // Pickup one index randomly (array index: 1 to the (last index -2))
            (object)['entity' => array_mock_security_groups[...],
            // Pickup *another* one index randomly (array index: 1 to the (last index -2))
            (object)['entity' => array_mock_security_groups[...],
          ]],
        ];
      }

      // Run test cases.
      foreach ($field_security_groups as $security_groups) {

        $template_value_map = [
          ...
          ['field_security_group', $security_groups],
          ...
        ];

        // Test it.
      }
    }
  }

}

This test principle can be used in the other test codes.

Xiaohua Guan’s picture

Xiaohua Guan’s picture

Xiaohua Guan’s picture

Status: Needs work » Needs review
Xiaohua Guan’s picture

@yas

I change the test code as you said. Please review the patch file.

Status: Needs review » Needs work

The last submitted patch, 9: 3016638-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

yas’s picture

@xiaohua-guan

Thank you for your modification. It looks more sophisticated to me. The following debug code can be removed?

    var_dump(count($mock_security_group_and_names));

Also, could you please add the comments?

    // Test cases.
    //
    // Test when Security Groups has the only one item, the first item only.
    // Security Group A (* SELECTED).
    // ...
    // Security Group M.
    // ...
    // Security Group N.
    $field_security_groups_test_cases[] = [reset($mock_security_group_and_names)];

    // Test when Security Groups has more than two items.
    if (MAX_SECURITY_GROUPS_COUNT > 1) {

      // The last item only.
      // Security Group A.
      // ...
      // Security Group M.
      // ...
      // Security Group N (* SELECTED).
      $field_security_groups_test_cases[] = [end($mock_security_group_and_names)];

      // The first item and the last one only.
      // Security Group A (* SELECTED).
      // ...
      // Security Group M.
      // ...
      // Security Group N (* SELECTED).
      $field_security_groups_test_cases[] = [
        reset($mock_security_group_and_names),
        end($mock_security_group_and_names)
      ];

      if (MAX_SECURITY_GROUPS_COUNT > 2) {

        // Randomly picking up the only one item.

        // One random item.
        // Security Group A.
        // ...
        // Security Group M (* SELECTED).
        // ...
        // Security Group N.
        $field_security_groups_test_cases[] = [
          // Pickup one index randomly (array index: 1 to the (last index -2)).
          $mock_security_group_and_names_exclude_first_last[
            array_rand($mock_security_group_and_names_exclude_first_last)
          ]
        ];

        // The first item and one random one.
        // Security Group A (* SELECTED).
        // ...
        // Security Group M (* SELECTED).
        // ...
        // Security Group N.
        $field_security_groups_test_cases[] = [
           reset($mock_security_group_and_names),
           // Pickup one index randomly (array index: 1 to the (last index -2)).
           $mock_security_group_and_names_exclude_first_last[
             array_rand($mock_security_group_and_names_exclude_first_last)
           ],
         ];

        // One random item and the last one.
        // Security Group A.
        // ...
        // Security Group M (* SELECTED).
        // ...
        // Security Group N (* SELECTED).
        $field_security_groups_test_cases[] = [
          // Pickup one index randomly (array index: 1 to the (last index -2)).
          $mock_security_group_and_names_exclude_first_last[
            array_rand($mock_security_group_and_names_exclude_first_last)
          ],
          end($mock_security_group_and_names),
        ];
Xiaohua Guan’s picture

FileSize
10.45 KB
Xiaohua Guan’s picture

Status: Needs work » Needs review
Xiaohua Guan’s picture

@yas

I modified the code as you said. Please take a look. Thanks.

yas’s picture

@xiaohua-guan

Thanks, it looks good to me.

@baldwinlouie, how do you thank?

baldwinlouie’s picture

Status: Needs review » Reviewed & tested by the community

@yas and @xiaohua-guan, looks good! setting to RTBC

  • yas committed 5a4d185 on 8.x-1.x authored by Xiaohua Guan
    Issue #3016638 by Xiaohua Guan, yas, baldwinlouie: Multiple security...
yas’s picture

Status: Reviewed & tested by the community » Fixed

Merged it and closed this issue.

Status: Fixed » Closed (fixed)

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