cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/VolumeAttachDetachTest.php

modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/VolumeAttachDetachTest.php
<?php

namespace Drupal\Tests\aws_cloud\Functional\Ec2;

use Drupal\Tests\aws_cloud\Functional\AwsCloudTestCase;

/**
 * Tests AWS Cloud Volume for attach and detach operations.
 *
 * @group AWS Cloud
 */
class VolumeAttachDetachTest extends AwsCloudTestCase {

  /**
   * Number of times to repeat the test.
   */
  const MAX_TEST_REPEAT_COUNT = 3;

  /**
   * {@inheritdoc}
   */
  protected function getPermissions() {
    return [
      'list aws cloud volume',
      'add aws cloud volume',
      'view any aws cloud volume',
      'edit any aws cloud volume',
      'delete any aws cloud volume',
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getMockDataTemplateVars() {
    return [
      'create_time' => date('c'),
    ];
  }

  /**
   * Test volume attach.
   */
  public function testVolumeAttachDetach() {
    $this->repeatTestVolumeAttachDetach(self::MAX_TEST_REPEAT_COUNT);
  }

  /**
   * Repeating test volume attach detach.
   *
   * @param int $max_test_repeat_count
   *   Max test repeating count.
   */
  private function repeatTestVolumeAttachDetach($max_test_repeat_count = 1) {

    for ($i = 1; $i <= $max_test_repeat_count; $i++) {
      // Setup for testing.
      $device_name = $this->random->name(8, TRUE);

      // Setup a test instance.
      $instance = $this->createInstanceTestEntity($i);
      $instance_id = $instance->getInstanceId();

      // Setup a test volume.
      $volume = $this->createVolumeTestEntity(
        $i,
        'vol-' . $this->getRandomAwsId(),
        "volume-name #$i - {$this->random->name(32, TRUE)}",
        $this->cloudContext,
        $this->loggedInUser->id()
      );
      $volume_id = $volume->getVolumeId();

      $attach_data = [
        'device_name' => $device_name,
        'instance_id' => $instance_id,
      ];

      // Test attach.
      $this->updateAttachDetachVolumeMockData('AttachVolume', $device_name, $volume_id, $instance_id, 'attaching');
      $this->drupalPostForm("/clouds/aws_cloud/$this->cloudContext/volume/$i/attach",
        $attach_data,
        t('Attach'));

      $this->assertResponse(200, t('Add | HTTP 200: The 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('The volume @volume is attaching to @instance', [
        '@instance' => $instance_id,
        '@volume' => $volume_id,
      ]));

      // Test detach.
      $volume->setState('in-use');
      $volume->setAttachmentInformation($instance_id);
      $volume->save();
      $this->updateAttachDetachVolumeMockData('DetachVolume', $device_name, $volume_id, $instance_id, 'detaching');
      $this->drupalPostForm("/clouds/aws_cloud/$this->cloudContext/volume/$i/detach",
        [],
        t('Detach'));
      $this->assertResponse(200, t('Add | HTTP 200: The 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('The volume @volume is detaching from @instance', [
        '@instance' => $instance_id,
        '@volume' => $volume_id,
      ]));
    }

  }

  /**
   * Tests detaching Volumes with bulk operation.
   */
  public function testVolumeBulkDetach() {

    $cloud_context = $this->cloudContext;
    $num = 0;
    $total_count = 0;
    $total_volumes = [];

    for ($i = 0; $i < self::MAX_TEST_REPEAT_COUNT; $i++) {

      $volumes_count = rand(1, self::MAX_TEST_REPEAT_COUNT);
      // Create Volumes.
      $volumes = $this->createVolumeTestFormData($volumes_count);

      for ($j = 0; $j < $volumes_count; $j++) {
        ++$num;
        // Setup for testing.
        $device_name = $this->random->name(8, TRUE);

        // Setup a test instance.
        $instance = $this->createInstanceTestEntity($j);
        $instance_id = $instance->getInstanceId();
        $this->addInstanceMockData($instance->getName(), $instance->getKeyPairName());

        // Setup a test volume.
        $volume = $this->createVolumeTestEntity(
          $j,
          $volumes[$j]['name'],
          $volumes[$j]['name'],
          $this->cloudContext,
          $this->loggedInUser->id()
        );
        $volume_id = $volume->getVolumeId();
        $this->addVolumeMockData($volumes[$j]);

        $attach_data = [
          'device_name' => $device_name,
          'instance_id' => $instance_id,
        ];
        $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");

        // Test attach.
        $this->updateAttachDetachVolumeMockData('AttachVolume', $device_name, $volume_id, $instance_id, 'attaching');
        $this->drupalPostForm("/clouds/aws_cloud/$this->cloudContext/volume/$num/attach",
          $attach_data,
          t('Attach'));

        $volume->setState('in-use');
        $volume->setAttachmentInformation($instance_id);
        $volume->save();
        $volumes[$j]['instance_id'] = $instance_id;

      }

      $total_count += $volumes_count;
      $total_volumes = array_merge($total_volumes, $volumes);

      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");

      $data = [];
      $data['action'] = 'aws_cloud_volume_detach_action';

      $checkboxes = $this->cssSelect('input[type=checkbox]');
      foreach ($checkboxes as $checkbox) {
        if ($checkbox->getAttribute('name') == NULL) {
          continue;
        }

        $data[$checkbox->getAttribute('name')] = $checkbox->getAttribute('value');
      }

      // Confirm.
      $this->drupalPostForm(
        "/clouds/aws_cloud/$cloud_context/volume",
        $data,
        t('Apply to selected items')
      );
      $this->assertResponse(200);

      $message = 'Are you sure you want to detach these aws cloud volume entities?';
      if ($total_count == 1) {
        $message = 'Are you sure you want to detach this aws cloud volume?';
      }
      $this->assertText($message);

      foreach ($total_volumes as $volume) {
        $this->assertText($volume['name']);
      }

      // Disassociate.
      $this->drupalPostForm(
        "/clouds/aws_cloud/$cloud_context/volume/detach_multiple",
        [],
        t('Detach')
      );

      $this->assertResponse(200);
      if ($total_count == 1) {
        $this->assertText("Detached $volumes_count Volume.");
      }
      else {
        $this->assertText("Detached $total_count Volumes.");
      }

      for ($j = 0; $j < $total_count; $j++) {
        $volume = $total_volumes[$j];
        $name = $volume['name'];
        $this->assertText("The AWS Cloud Volume \"$name\" has been detached.");
        $this->updateVolumeMockData($j, $volume['name'], NULL);
      }

      // Click 'Refresh'.
      $this->clickLink(t('Refresh'));
      $this->assertText(t('Updated Volumes.'));

      // Make sure if disassociated from an instance.
      foreach ($total_volumes as $volume) {
        $this->assertNoText($volume['instance_id']);
      }
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc