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

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

namespace Drupal\Tests\aws_cloud\Functional\Ec2;

use Drupal\Tests\aws_cloud\Functional\AwsCloudTestCase;
use Drupal\Tests\aws_cloud\Functional\Utils;

/**
 * Tests AWS Cloud Volume.
 *
 * @group AWS Cloud
 */
class VolumeTest extends AwsCloudTestCase {

  const AWS_CLOUD_VOLUME_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',

      'add aws cloud snapshot',
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getMockDataTemplateVars() {
    return [
      'volume_id' => 'vol-' . $this->getRandomAwsId(),
      'create_time' => date('c'),
      'uid' => Utils::getRandomUid(),
    ];
  }

  /**
   * Tests CRUD for Volume information.
   */
  public function testVolume() {
    $cloud_context = $this->cloudContext;

    // List Volume for Amazon EC2.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
    $this->assertResponse(200, t('HTTP 200: List'));
    $this->assertNoText(t('Notice'), t('List | Make sure w/o Notice'));
    $this->assertNoText(t('warning'), t('List | Make sure w/o Warnings'));

    // Add a new Volume.
    $delete_count = 0;
    $add = $this->createVolumeTestFormData(self::AWS_CLOUD_VOLUME_REPEAT_COUNT);
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->reloadMockData();

      $state = $this->createRandomState();
      $volume_id = 'vol-' . $this->getRandomAwsId();
      $snapshot_name = 'snapshot-name' . $this->random->name(10, TRUE);
      $this->updateCreateVolumeMockData($state, $volume_id);
      $this->createSnapshotTestEntity($i, $add[$i]['snapshot_id'], $snapshot_name, $cloud_context);
      $this->updateDescribeSnapshotsMockData([['id' => $add[$i]['snapshot_id'], 'name' => $snapshot_name]]);
      if ($state != 'in-use') {
        $delete_count++;
      }

      $num = $i + 1;

      // Make sure checkbox Encrypted is checked.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume/add");
      $this->assertSession()->checkboxChecked('encrypted');

      $this->drupalPostForm("/clouds/aws_cloud/$cloud_context/volume/add",
                            $add[$i],
                            t('Save'));

      $this->assertResponse(200, t('Add | HTTP 200: The new AWS Cloud Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Add | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Add | Make sure w/o Warnings'));
      $this->assertText($add[$i]['name'], t('Add | Volume: @name', ['@name' => $add[$i]['name']]));
      $this->assertText(
        t('The AWS Cloud Volume "@name', ['@name' => $add[$i]['name']]),
        t('Confirm Message: Add | The AWS Cloud Volume "@name" has been created.', [
          '@name' => $add[$i]['name'],
        ])
      );

      // Make sure listing.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
      $this->assertResponse(200, t('HTTP 200: Add | List | Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Add | List | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Add | List | Make sure w/o Warnings'));
      for ($j = 0; $j < $i + 1; $j++) {
        $this->assertText($add[$i]['name'],
                        t('Add | List | Make sure w/ Listing: @name', [
                          '@name' => $add[$i]['name'],
                        ]));
      }

      // Assert delete link count.
      if ($delete_count > 0) {
        $this->assertLink(t('Delete'), $delete_count - 1);
      }

      // Make sure view.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume/$num");
      $this->assertResponse(200, t('HTTP 200: Add | View | Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Add | View | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Add | View | Make sure w/o Warnings'));
      $this->assertText($volume_id,
                        t('Add | View | Make sure volume id: @volume_id', [
                          '@volume_id' => $volume_id,
                        ]));
      $this->assertText($add[$i]['snapshot_id'],
                        t('Add | View | Make sure snapshot id: @snapshot_id', [
                          '@snapshot_id' => $add[$i]['snapshot_id'],
                        ]));
      $this->assertText($snapshot_name,
                        t('Add | View | Make sure snapshot name: @snapshot_name', [
                          '@snapshot_name' => $snapshot_name,
                        ]));
    }

    // Edit an Volume information.
    $edit = $this->createVolumeTestFormData(self::AWS_CLOUD_VOLUME_REPEAT_COUNT);
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      unset($edit[$i]['snapshot_id']);
      unset($edit[$i]['size']);
      unset($edit[$i]['availability_zone']);
      unset($edit[$i]['iops']);
      unset($edit[$i]['encrypted']);
      unset($edit[$i]['volume_type']);

      $this->drupalPostForm("/clouds/aws_cloud/$cloud_context/volume/$num/edit",
                            $edit[$i],
                            t('Save'));

      $this->assertResponse(200, t('HTTP 200: Edit | The new AWS Cloud Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Edit | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Edit | Make sure w/o Warnings'));
      $this->assertText(
        t('The AWS Cloud Volume "@name" has been saved.', ['@name' => $edit[$i]['name']]),
        t('Confirm Message: Edit | The AWS Cloud Volume "@name" has been saved.', [
          '@name' => $edit[$i]['name'],
        ])
      );

      // Make sure listing.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
      $this->assertResponse(200, t('Edit | List | HTTP 200: Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Edit | List | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Edit | List | Make sure w/o Warnings'));

      for ($j = 0; $j < $i + 1; $j++) {
        $this->assertText($edit[$i]['name'],
                        t('Edit | List | Make sure w/ Listing: @name', [
                          '@name' => $edit[$i]['name'],
                        ]));
      }
    }

    // Delete Volume.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume/$num/delete");
      $this->assertResponse(200, t('Delete | HTTP 200: Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Delete | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Delete | Make sure w/o Warnings'));
      $this->drupalPostForm("/clouds/aws_cloud/$cloud_context/volume/$num/delete",
                            [],
                            t('Delete'));

      $this->assertResponse(200, t('Delete | HTTP 200: The Cloud Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Delete | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Delete | Make sure w/o Warnings'));
      $this->assertText($edit[$i]['name'], t('Delete | Name: @name', ['@name' => $edit[$i]['name']]));
      $this->assertText(
        t('The AWS Cloud Volume "@name" has been deleted.', ['@name' => $edit[$i]['name']]),
        t('Confirm Message: Delete | The AWS Cloud Volume "@name" has been deleted.', [
          '@name' => $edit[$i]['name'],
        ])
      );

      // Make sure listing.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
      $this->assertResponse(200, t('Delete | HTTP 200: Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Delete | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Delete | Make sure w/o Warnings'));
    }
  }

  /**
   * Create random state.
   *
   * @return string
   *   random state.
   */
  private function createRandomState() {
    $states = ['creating', 'in-use'];
    return $states[array_rand($states)];
  }

  /**
   * Test updating volume list.
   */
  public function testUpdatingVolumeList() {

    $cloud_context = $this->cloudContext;

    // Add a new Volume.
    $add = $this->createVolumeTestFormData(self::AWS_CLOUD_VOLUME_REPEAT_COUNT);
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $num = $i + 1;
      $this->addVolumeMockData($add[$i]);
      $snapshot_name = 'snapshot-name' . $this->random->name(10, TRUE);
      $this->createSnapshotTestEntity($i, $add[$i]['snapshot_id'], $snapshot_name, $cloud_context);
    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
    $this->assertResponse(200, t('Edit | List | HTTP 200: Volume #@num', ['@num' => $num]));
    $this->assertNoText(t('Notice'), t('Edit | List | Make sure w/o Notice'));
    $this->assertNoText(t('warning'), t('Edit | List | Make sure w/o Warnings'));
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertNoText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }

    // Click 'Refresh'.
    $this->clickLink(t('Refresh'));
    $this->assertText(t('Updated Volumes.'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }

    // Make sure detailed and edit view.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume/$num");
      $this->assertLink(t('Edit'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/edit");
      $this->assertLink(t('Attach'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/attach");
      $this->assertLink(t('Delete'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/delete");
      $this->assertLink(t('List AWS Cloud Volumes'));
      // Click 'Refresh'.
      $this->clickLink(t('List AWS Cloud Volumes'));
      $this->assertResponse(200, t('Edit | List | HTTP 200: Volume #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Edit | List | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Edit | List | Make sure w/o Warnings'));

      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume/$num/edit");
      $this->assertNoLink('Edit');
      $this->assertLink(t('Attach'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/attach");
      $this->assertLink(t('Delete'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/delete");
    }

    // Edit Volume information.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + 1;

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

      // Change Volume Name in mock data.
      $add[$i]['name'] = "volume-name #$num - {$this->random->name(32, TRUE)}";

      $this->updateVolumeMockData($num - 1, $add[$i]['name'], $instance_id);

    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
    $this->assertResponse(200, t('Edit | List | HTTP 200: Volume #@num', ['@num' => $num]));
    $this->assertNoText(t('Notice'), t('Edit | List | Make sure w/o Notice'));
    $this->assertNoText(t('warning'), t('Edit | List | Make sure w/o Warnings'));
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertNoText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }

    // Click 'Refresh'.
    $this->clickLink(t('Refresh'));
    $this->assertText(t('Updated Volumes.'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }

    // Make sure detailed and edit view.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + self::AWS_CLOUD_VOLUME_REPEAT_COUNT + 1;

      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume/$num");
      $this->assertLink(t('Edit'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/edit");
      $this->assertLink(t('Detach'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/detach");
      $this->assertLink(t('Delete'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/volume/$num/delete");
    }

    // Update tags.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      // Update tags.
      $add[$i]['tags_name'] = $this->getRandomAwsId();
      $this->updateTagsInMockData($num - 1, 'Volumes', 'Name', $add[$i]['tags_name'], FALSE);
    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertNoLink($add[$i]['tags_name']);
    }

    // Click 'Refresh'.
    $this->clickLink(t('Refresh'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertLink($add[$i]['tags_name']);
    }

    // Update tags for empty.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      // Update tags.
      $this->updateTagsInMockData($num - 1, 'Volumes', 'Name', '', FALSE);
    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertLink($add[$i]['tags_name']);
    }

    // Click 'Refresh'.
    $this->clickLink(t('Refresh'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertNoLink($add[$i]['tags_name']);
      $this->assertLink($add[$i]['name']);
    }

    // Delete name tags.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      // Update tags.
      $this->updateTagsInMockData($num - 1, 'Volumes', 'Name', '', TRUE);
    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertNoLink($add[$i]['tags_name']);
      $this->assertLink($add[$i]['name']);
    }

    // Click 'Refresh'.
    $this->clickLink(t('Refresh'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertNoLink($add[$i]['tags_name']);
      $this->assertLink($add[$i]['name']);
    }

    // Delete Volume in mock data.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->deleteFirstVolumeMockData();
    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
    $this->assertResponse(200, t('Edit | List | HTTP 200: Volume #@num', ['@num' => $num + 1]));
    $this->assertNoText(t('Notice'), t('Edit | List | Make sure w/o Notice'));
    $this->assertNoText(t('warning'), t('Edit | List | Make sure w/o Warnings'));
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }

    // Click 'Refresh'.
    $this->clickLink(t('Refresh'));
    $this->assertText(t('Updated Volumes.'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      $this->assertNoText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }
  }

  /**
   * Test the operation of creating snapshot.
   */
  public function testCreateSnapshotOperation() {
    $this->repeatTestCreateSnapshotOperation(
      self::AWS_CLOUD_VOLUME_REPEAT_COUNT
    );
  }

  /**
   * Repeat testing the operation of creating snapshot.
   *
   * @param int $max_count
   *   Max test repeating count.
   */
  private function repeatTestCreateSnapshotOperation($max_count) {
    $cloud_context = $this->cloudContext;

    $add = $this->createVolumeTestFormData(self::AWS_CLOUD_VOLUME_REPEAT_COUNT);
    for ($i = 0; $i < $max_count; $i++) {
      $this->reloadMockData();

      $num = $i + 1;

      $snapshot_name = 'snapshot-name' . $this->random->name(10, TRUE);
      $this->updateDescribeSnapshotsMockData([['id' => $add[$i]['snapshot_id'], 'name' => $snapshot_name]]);
      $this->createSnapshotTestEntity($i, $add[$i]['snapshot_id'], $snapshot_name, $cloud_context);

      $this->drupalPostForm("/clouds/aws_cloud/$cloud_context/volume/add",
                            $add[$i],
                            t('Save'));
      $this->assertResponse(200);

      // Make sure listing.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
      $this->assertResponse(200, t('HTTP 200: List | Volume #@num', ['@num' => $num]));
      $this->assertText(t('Create Snapshot'));

      // Add a volume to DescribeVolumes.
      $volume_id = $this->latestTemplateVars['volume_id'];
      $add[$i]['name'] = $volume_id;
      $this->addVolumeMockData($add[$i]);

      // Click "Create Snapshot" link.
      $this->clickLink(t('Create Snapshot'), $i);

      // Make sure creating page.
      $this->assertResponse(200);
      $this->assertText(t('Add AWS Cloud Snapshot'));

      // Make sure the default value of field volume_id.
      $this->assertSession()->fieldValueEquals('volume_id', $volume_id);
    }
  }

  /**
   * Tests deleting volumes with bulk operation.
   */
  public function testVolumeBulk() {
    $cloud_context = $this->cloudContext;

    for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
      // Create volumes.
      $volumes = $this->createVolumesRandomTestFormData();
      $num = 0;
      $entities = [];
      foreach ($volumes as $volume) {
        $entities[] = $this->createVolumeTestEntity(
          $num++,
          $volume['VolumeId'],
          $volume['Name'],
          $cloud_context,
          Utils::getRandomUid()
        );
      }

      $this->doTestEntityBulk('volume', $entities);
    }
  }

}

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

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