Skip to content
Snippets Groups Projects
Commit 5a4d1854 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3016638 by Xiaohua Guan, yas, baldwinlouie: Multiple security groups in...

Issue #3016638 by Xiaohua Guan, yas, baldwinlouie: Multiple security groups in sever template don't work when launching instances
parent 7b46b3dd
No related branches found
No related tags found
No related merge requests found
......@@ -105,10 +105,10 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
$params['Placement']['AvailabilityZone'] = $cloud_server_template->get('field_availability_zone')->value;
}
$params['SecurityGroup'] = [];
$params['SecurityGroups'] = [];
foreach ($cloud_server_template->get('field_security_group') as $group) {
if (isset($group->entity)) {
$params['SecurityGroup'][] = $group->entity->get('group_name')->value;
$params['SecurityGroups'][] = $group->entity->get('group_name')->value;
}
}
......
<?php
namespace Drupal\Tests\aws_cloud\Unit\Plugin;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\Utility\Random;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\aws_cloud\Plugin\AwsCloudServerTemplatePlugin;
use Drupal\cloud_server_template\Entity\CloudServerTemplateInterface;
use Drupal\aws_cloud\Service\AwsEc2ServiceInterface;
use Drupal\aws_cloud\Aws\Ec2\ImageInterface;
use Drupal\aws_cloud\Aws\Ec2\KeyPairInterface;
use Drupal\aws_cloud\Aws\Ec2\SecurityGroupInterface;
use Drupal\aws_cloud\Aws\Ec2\NetworkInterfaceInterface;
// Amazon VPC Limits - Security groups per VPC (per region).
define('MAX_SECURITY_GROUPS_COUNT', 500);
/**
* Tests AWS Cloud Template Plugin.
*
* @group AWS Cloud
*/
class AwsCloudServerTemplatePluginTest extends UnitTestCase {
private $plugin;
private $mockAwsEc2Service;
private $random;
protected function setUp() {
$this->mockAwsEc2Service = $this->getMockBuilder(AwsEc2ServiceInterface::class)
->getMock();
$this->plugin = new AwsCloudServerTemplatePlugin(
[], '', [],
$this->mockAwsEc2Service,
$this->getMockBuilder(Messenger::class)
->disableOriginalConstructor()
->getMock(),
$this->getMock(EntityTypeManagerInterface::class)
);
$this->random = new Random();
}
/**
* Tests launching a instance.
*/
public function testLaunch() {
$random = $this->random;
// Mock object of Image.
$mock_image = $this->getMock(ImageInterface::class);
$image_value_map = [
['image_id', (object)['value' => 'ami-' . $random->name(8, TRUE)]],
['root_device_type', 'ebs'],
];
$mock_image->expects($this->any())
->method('get')
->will($this->returnValueMap($image_value_map));
// Mock object of KeyPair.
$mock_ssh_key = $this->getMock(KeyPairInterface::class);
$ssh_key_value_map = [
['key_pair_name', (object)['value' => $random->name(8, TRUE)]],
];
$mock_ssh_key->expects($this->any())
->method('get')
->will($this->returnValueMap($ssh_key_value_map));
// Mock object of NetworkInterface.
$mock_network = $this->getMock(NetworkInterfaceInterface::class);
$mock_network_value_map = [
['group_name', (object)['value' => $random->name(8, TRUE)]],
];
$mock_network->expects($this->any())
->method('subnet_id')
->will($this->returnValueMap($mock_network_value_map));
$field_security_groups_test_cases = $this->createFieldSecurityGroupsTestCases();
// Run test cases.
foreach ($field_security_groups_test_cases as $field_security_groups_test_case) {
$mock_template = $this->getMock(CloudServerTemplateInterface::class);
$template_value_map = [
['field_test_only', (object)['value' => '1']],
['field_image_id', (object)['entity' => $mock_image]],
['field_max_count', (object)['value' => 1]],
['field_min_count', (object)['value' => 1]],
['field_monitoring', (object)['value' => '1']],
['field_instance_type', (object)['value' => $random->name(5, TRUE)]],
['field_ssh_key', (object)['entity' => $mock_ssh_key]],
['field_kernel_id', (object)['value' => 'aki-' . $random->name(8, TRUE)]],
['field_ram', (object)['value' => 'ari-' . $random->name(8, TRUE)]],
['field_user_data', (object)['value' => $random->string(32, TRUE)]],
['field_availability_zone', (object)['value' => $random->name(7, TRUE)]],
['field_security_group', $this->extract_array_item($field_security_groups_test_case, 0)],
['field_network', (object)['entity' => $mock_network]],
['field_instance_shutdown_behavior', (object)['value' => '1']],
];
$mock_template->expects($this->any())
->method('get')
->will($this->returnValueMap($template_value_map));
// Assert followings for the first argument of method runInstances,
// 1. Has key SecurityGroups,
// 2. Doesn't have key SecurityGroup,
// 3. Contains the array of group1 and group2.
$this->mockAwsEc2Service
->expects($this->any())
->method('runInstances')
->with($this->logicalAnd(
$this->arrayHasKey('SecurityGroups'),
$this->logicalNot($this->arrayHasKey('SecurityGroup')),
$this->contains($this->extract_array_item($field_security_groups_test_case, 1))
))
->will($this->returnValue(TRUE));
$return = $this->plugin->launch(
$mock_template,
$this->getMock(FormStateInterface::class)
);
$this->assertNotNull($return);
}
}
private function extract_array_item($array, $index) {
$extracted = [];
foreach ($array as $item) {
$extracted[] = $item[$index];
}
return $extracted;
}
private function createFieldSecurityGroupsTestCases() {
$random = $this->random;
$mock_security_group_and_names = [];
for ($i = 0; $i < MAX_SECURITY_GROUPS_COUNT; $i++) {
// Mock object of SecurityGroup.
$mock_security_group = $this->getMock(SecurityGroupInterface::class);
$group_name = $random->name(8, TRUE);
$mock_security_group_value_map[] = [
['group_name', (object)['value' => $group_name]],
];
$mock_security_group->expects($this->any())
->method('get')
->will($this->returnValueMap($mock_security_group_value_map));
$mock_security_group_and_names[] = [$mock_security_group, $group_name];
}
$mock_security_group_and_names_exclude_first_last = array_slice(
$mock_security_group_and_names,
1,
count($mock_security_group_and_names) - 2
);
$field_security_groups_test_cases = [];
// 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),
];
if (MAX_SECURITY_GROUPS_COUNT > 3) {
// Two random items.
// Security Group A.
// ...
// Security Group M1 (* SELECTED).
// ...
// Security Group M2 (* SELECTED).
// ...
// Security Group N.
$array_indice = array_rand($mock_security_group_and_names_exclude_first_last, 2);
$field_security_groups_test_cases[] = [
$mock_security_group_and_names_exclude_first_last[
$array_indice[0]
],
$mock_security_group_and_names_exclude_first_last[
$array_indice[1]
],
];
}
}
}
return $field_security_groups_test_cases;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment