Skip to content
Snippets Groups Projects
Commit b9769fe8 authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3018419 by baldwinlouie, yas, Xiaohua Guan: Add test case for Volume Attach and Detach

parent 92552428
No related branches found
No related tags found
No related merge requests found
......@@ -118,9 +118,11 @@ class VolumeAttachForm extends AwsDeleteForm {
$properties = [
'availability_zone' => $zone
];
if (!$account->hasPermission('view any aws cloud instance')) {
$properties[] = ['user_id', $account->id()];
$properties['user_id'] = $account->id();
}
return $this->entity_type_manager->getStorage('aws_cloud_instance')->loadByProperties($properties);
}
......
<?php
namespace Drupal\Tests\aws_cloud\Functional\Ec2;
use Drupal\aws_cloud\Entity\Ec2\Instance;
use Drupal\aws_cloud\Entity\Ec2\Volume;
use Drupal\Tests\aws_cloud\Functional\Utils;
/**
* Number of times to repeat the test
*/
const MAX_TEST_REPEAT_COUNT = 3;
/**
* Volume attach and detach test case.
*
* @group AWS Cloud
*/
class VolumeAttachDetachTest extends AwsCloudTestCase {
/**
* {@inheritdoc}
*/
protected function getPermissions() {
return [
'list aws cloud volume',
'add aws cloud volume',
'view aws cloud volume',
'edit aws cloud volume',
'delete aws cloud volume',
];
}
/**
* Test volume attach
*/
public function testVolumeAttachDetach() {
$this->_testVolumeAttachDetach($max_test_repeat_count = MAX_TEST_REPEAT_COUNT);
}
private function _testVolumeAttachDetach($max_test_repeat_count = 1) {
for ($i = 1; $i <= $max_test_repeat_count; $i++ ) {
// Setup for testing
$name = $this->random->name(8, TRUE);
$device_name = $this->random->name(8, TRUE);
// setup a test instance
$instance = $this->createTestInstance($name, $i);
$instance_id = $instance->instance_id();
// setup a test volume
$volume = $this->createTestVolume($i);
$volume_id = $volume->volume_id();
$attach_data = [
'device_name' => $device_name,
'instance_id' => $instance_id,
];
// Test attach
$this->updateVolumeInMockData('AttachVolume', $device_name, $volume_id, $instance_id, 'attaching');
$this->drupalPostForm("/clouds/aws_cloud/$this->cloud_context/volume/$i/attach",
$attach_data,
t('Attach'));
$this->assertResponse(200, t('Add | HTTP 200: A New AWS Cloud Volume #@num', ['@num' => $i]));
$this->assertNoText(t('Notice'), t('Add | Make sure w/o Notice'));
$this->assertNoText(t('warning'), t('Add | Make sure w/o Warnings'));
$this->assertText(t('Volume @volume is attaching to @instance', [
'@instance' => $instance_id,
'@volume' => $volume_id
]));
// Test detach
$this->updateVolumeInMockData('DetachVolume', $device_name, $volume_id, $instance_id, 'detaching');
$this->drupalPostForm("/clouds/aws_cloud/$this->cloud_context/volume/$i/detach",
[],
t('Detach'));
$this->assertResponse(200, t('Add | HTTP 200: A New AWS Cloud Volume #@num', ['@num' => $i]));
$this->assertNoText(t('Notice'), t('Add | Make sure w/o Notice'));
$this->assertNoText(t('warning'), t('Add | Make sure w/o Warnings'));
$this->assertText(t('Volume @volume is detaching from @instance', [
'@instance' => $instance_id,
'@volume' => $volume_id
]));
}
}
/**
* Update the volume state and instance
*/
private function updateVolumeInMockData($api, $device, $volume_id, $instance_id, $state) {
$mock_data = $this->getMockDataFromConfig();
$mock_data[$api] = [
'Device' => $device,
'InstanceId' => $instance_id,
'State' => $state,
'VolumeId' => $volume_id,
];
$this->updateMockDataToConfig($mock_data);
}
/**
* Create an AWS volume
*/
private function createTestVolume($num) {
$volume = Volume::create([
'name' => "volume-name #$num - " . $this->random->name(32, TRUE),
'size' => $num * 10,
'availability_zone' => "us-west-$num",
'iops' => $num * 1000,
'encrypted' => $num % 2,
'volume_type' => 'io1',
'volume_id' => 'vol-' . $this->random->name(17, TRUE),
'cloud_context' => $this->cloud_context,
'volume_status' => 'Available',
'uid' => $this->loggedInUser->id(),
]);
$volume->save();
return $volume;
}
/**
* Create an AWS cloud instance
*/
private function createTestInstance($name, $num) {
$public_ip = Utils::getRandomPublicIp();
$private_ip = Utils::getRandomPrivateIp();
$regions = ['us-west-1', 'us-west-2'];
$region = $regions[array_rand($regions)];
$instance = Instance::create([
'cloud_context' => $this->cloud_context,
'name' => "Instance #$name - " . date('Y/m/d - ') . $this->random->name(8, TRUE),
'image_id' => 'ami-' . $this->random->name(8, TRUE),
'key_pair_name' => "key_pair-$name-" . $this->random->name(8, TRUE),
'is_monitoring' => 0,
'availability_zone' => "us-west-$num",
'security_groups' => "security_group-$name-" . $this->random->name(8, TRUE),
'instance_type' => "t$num.small",
'kernel_id' => 'aki-' . $this->random->name(8, TRUE),
'ramdisk_id' => 'ari-' . $this->random->name(8, TRUE),
'user_data' => "User Data #$num: " . $this->random->string(64, TRUE),
'login_username' => "user-$name" . date('-Y/m/d-') . $this->random->name(8, TRUE),
'owner_id' => rand(100000000000, 999999999999),
'reservation_id' => 'r-' . $this->random->name(17, TRUE),
'group_name' => $this->random->name(8, TRUE),
'host_id' => $this->random->name(8, TRUE),
'affinity' => $this->random->name(8, TRUE),
'launch_time' => date('c'),
'security_group_id' => 'sg-' . $this->random->name(8, TRUE),
'security_group_name' => $this->random->name(10, TRUE),
'public_dns_name' => Utils::getPublicDns($region, $public_ip),
'public_ip_address' => $public_ip,
'private_dns_name' => Utils::getPrivateDns($region, $private_ip),
'private_ip_address' => $private_ip,
'vpc_id' => 'vpc-' . $this->random->name(8, TRUE),
'subnet_id' => 'subnet-' . $this->random->name(8, TRUE),
'image_id' => 'ami-' . $this->random->name(8, TRUE),
'reason' => $this->random->string(16, TRUE),
'instance_id' => 'i-' . $this->random->name(17, TRUE),
'uid' => $this->loggedInUser->id(),
]);
$instance->save();
return $instance;
}
}
\ No newline at end of file
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