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

modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/InstanceExtraTest.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 Instance.
 *
 * @group AWS Cloud
 */
class InstanceExtraTest extends AwsCloudTestCase {

  /**
   * Create three Instances for a test case.
   */
  const AWS_CLOUD_INSTANCE_REPEAT_COUNT = 3;

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

      'list cloud server template',
      'view own published cloud server templates',
      'launch cloud server template',

      'add aws cloud image',
      'list aws cloud image',
      'view any aws cloud image',
      'edit any aws cloud image',
      'delete any aws cloud image',

      'administer aws_cloud',
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getMockDataTemplateVars() {
    $public_ip = Utils::getRandomPublicIp();
    $private_ip = Utils::getRandomPrivateIp();
    $regions = ['us-west-1', 'us-west-2'];
    $region = $regions[array_rand($regions)];

    return [
      // 12 digits.
      'account_id' => rand(100000000000, 999999999999),
      'reservation_id' => 'r-' . $this->getRandomAwsId(),
      '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->getRandomAwsId(),
      '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->getRandomAwsId(),
      'subnet_id' => 'subnet-' . $this->getRandomAwsId(),
      'image_id' => 'ami-' . $this->getRandomAwsId(),
      'reason' => $this->random->string(16, TRUE),
      'instance_id' => 'i-' . $this->getRandomAwsId(),
      'state' => 'running',
    ];
  }

  /**
   * Tests updating instance attributes.
   */
  public function testUpdateInstanceAttributes() {
    $this->repeatTestUpdateInstanceAttributes(self::AWS_CLOUD_INSTANCE_REPEAT_COUNT);
  }

  /**
   * Repeating test update instance attributes.
   *
   * @param int $max_test_repeat_count
   *   Max test repeating count.
   */
  private function repeatTestUpdateInstanceAttributes($max_test_repeat_count = 1) {
    $cloud_context = $this->cloudContext;

    // IAM Roles.
    $iam_roles = $this->createIamRolesRandomTestFormData();
    $this->updateIamRolesMockData($iam_roles);

    // Add an empty value to IAM roles.
    $iam_roles = array_merge([[]], $iam_roles);

    $add = $this->createInstanceTestFormData($max_test_repeat_count);
    $edit = $this->createInstanceTestFormData($max_test_repeat_count);
    for ($i = 0, $num = 1; $i < $max_test_repeat_count; $i++, $num++) {

      // Create image.
      $image = $this->createImageTestEntity($i, $add[$i]['image_id'], $cloud_context);

      // Make sure if the image entity is created or not.
      $this->drupalGet("/clouds/aws_cloud/${cloud_context}/image");
      $this->assertResponse(200, t('HTTP 200: Image | The new AWS Cloud Image #@num', ['@num' => $num]));
      $this->assertNoText(t('warning'), t('Image | Make sure w/o Warnings'));
      $this->assertText($image->getName(), t('Image | Make sure w/ Image Name (TestEntity): @name', ['@name' => $image->getName()]));
      $this->assertText($image->getImageId(), t('Image | Make sure w/ Image ID (TestEntity): @image_id', ['@image_id' => $image->getImageId()]));
      $this->assertText($add[$i]['image_id'], t('Image | Make sure w/ Image ID (TestFormData): @image_id', ['@image_id' => $add[$i]['image_id']]));

      // Setup cloud server template and instance.
      $server_template = $this->createServerTemplateTestEntity($iam_roles, $image, $cloud_context);

      // Make sure if the cloud_server_template entity is created or not.
      $this->drupalGet("/clouds/design/server_template/${cloud_context}");
      $this->assertResponse(200, t('HTTP 200: CloudServerTemplate | The new CloudServerTemplate #@num', ['@num' => $num]));
      $this->assertNoText(t('warning'), t('CloudServerTemplate | Make sure w/o Warnings'));
      $this->assertText($server_template->name->value,
        t('CloudServerTemplate | Make sure w/ CloudServerTemplate Name: @name', [
          '@name' => $server_template->name->value,
        ])
      );

      // Launch a stopped Instance.
      $this->addInstanceMockData($add[$i]['name'], $add[$i]['key_pair_name'], 'stopped');
      $this->drupalPostForm("/clouds/design/server_template/$cloud_context/$num/launch",
        [],
        t('Launch'));

      $this->assertResponse(200, t('HTTP 200: Launch | The new Cloud Instance @num', [
        '@num' => $num,
      ]));
      $this->assertNoText(t('Notice'), t('Launch | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Launch | Make sure w/o Warnings'));
      $this->assertText('stopped', t('Instance Type: stopped'));

      // Edit instance.
      unset(
        $edit[$i]['image_id'],
        $edit[$i]['image_name'],
        $edit[$i]['min_count'],
        $edit[$i]['max_count'],
        $edit[$i]['key_pair_name'],
        $edit[$i]['is_monitoring'],
        $edit[$i]['availability_zone'],
        $edit[$i]['kernel_id'],
        $edit[$i]['ramdisk_id']
      );

      $instance_type = 'm1.small';
      $edit[$i]['instance_type'] = $instance_type;

      // Change security groups.
      $security_groups = $this->createSecurityGroupRandomTestFormData();
      $this->updateDescribeSecurityGroupsMockData($security_groups);
      $edit[$i]['security_groups[]'] = array_column($security_groups, 'GroupName');

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

      $this->updateInstanceMockData($i, $edit[$i]['name']);

      $this->assertResponse(200, t('Edit | HTTP 200:  The new Instance #@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 Instance "@name" has been saved.', ['@name' => $edit[$i]['name']]),
        t('Confirm Message: Edit | The AWS Cloud Instance "@name" has been saved.', [
          '@name' => $edit[$i]['name'],
        ])
      );
      $this->assertText(t(
        'The User Data of Instance "@name" was updated. Please start the Instance to reflect the User Data.', [
          '@name' => $edit[$i]['name'],
        ])
      );

      // Verify instance attributes.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/instance/$num");
      $this->assertResponse(200, t('View | HTTP 200: The Cloud Instance @num', [
        '@num' => $num,
      ]));
      $this->assertNoText(t('Notice'), t('View | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('View | Make sure w/o Warnings'));
      $this->assertText($instance_type, t('Instance Type: @instance_types', ['@instance_types' => $instance_type]));
      $groups = implode(', ', $edit[$i]['security_groups[]']);
      $this->assertText(
        $groups,
        t('Security Groups: @groups', ['@groups' => $groups])
      );
      $this->assertText($edit[$i]['user_data']);
    }
  }

  /**
   * Tests setting the configuration of instance terminating.
   */
  public function testInstanceTerminateConfiguration() {
    $this->repeatTestInstanceTerminateConfiguration(self::AWS_CLOUD_INSTANCE_REPEAT_COUNT);
  }

  /**
   * Repeating test instance terminate configuration.
   *
   * @param int $max_test_repeat_count
   *   Max test repeating count.
   */
  private function repeatTestInstanceTerminateConfiguration($max_test_repeat_count = 1) {
    $cloud_context = $this->cloudContext;

    // IAM Roles.
    $iam_roles = $this->createIamRolesRandomTestFormData();
    $this->updateIamRolesMockData($iam_roles);

    // Add an empty value to IAM roles.
    $iam_roles = array_merge([[]], $iam_roles);

    $terminate_allowed_values = [TRUE, FALSE];
    $add = $this->createInstanceTestFormData($max_test_repeat_count);
    for ($i = 0; $i < $max_test_repeat_count; $i++) {
      $num = $i + 1;

      $terminate_value = $terminate_allowed_values[array_rand($terminate_allowed_values)];
      $this->drupalPostForm("admin/config/services/cloud/aws_cloud/settings",
        [
          'aws_cloud_instance_terminate' => $terminate_value,
          'google_credential' => json_encode([]),
        ],
        t('Save configuration')
      );
      $this->assertResponse(200);

      // Create image.
      $image = $this->createImageTestEntity($i, $add[$i]['image_id'], $cloud_context);

      // Make sure if the image entity is created or not.
      $this->drupalGet("/clouds/aws_cloud/${cloud_context}/image");
      $this->assertResponse(200, t('HTTP 200: Image | The new AWS Cloud Image #@num', ['@num' => $num]));
      $this->assertNoText(t('warning'), t('Image | Make sure w/o Warnings'));
      $this->assertText($image->getName(), t('Image | Make sure w/ Image Name (TestEntity): @name', ['@name' => $image->getName()]));
      $this->assertText($image->getImageId(), t('Image | Make sure w/ Image ID (TestEntity): @image_id', ['@image_id' => $image->getImageId()]));
      $this->assertText($add[$i]['image_id'], t('Image | Make sure w/ Image ID (TestFormData): @image_id', ['@image_id' => $add[$i]['image_id']]));

      // Setup cloud server template and instance.
      $server_template = $this->createServerTemplateTestEntity($iam_roles, $image, $cloud_context);

      // Make sure if the cloud_server_template entity is created or not.
      $this->drupalGet("/clouds/design/server_template/${cloud_context}");
      $this->assertResponse(200, t('HTTP 200: CloudServerTemplate | The new CloudServerTemplate #@num', ['@num' => $num]));
      $this->assertNoText(t('warning'), t('CloudServerTemplate | Make sure w/o Warnings'));
      $this->assertText($server_template->name->value,
        t('CloudServerTemplate | Make sure w/ CloudServerTemplate Name: @name', [
          '@name' => $server_template->name->value,
        ])
      );

      $this->drupalGet("/clouds/design/server_template/$cloud_context/$num/launch");
      if ($terminate_value) {
        $this->assertFieldChecked('edit-terminate');
      }
      else {
        $this->assertNoFieldChecked('edit-terminate');
      }
    }
  }

  /**
   * Test launching instances with schedule tags.
   */
  public function testCreateInstanceWithScheduleTag() {
    $this->repeatTestCreateInstanceWithScheduleTag(self::AWS_CLOUD_INSTANCE_REPEAT_COUNT);
  }

  /**
   * Repeat testing create instance with schedule tag.
   *
   * @param int $max_test_repeat_count
   *   Max test repeating count.
   */
  private function repeatTestCreateInstanceWithScheduleTag($max_test_repeat_count = 1) {
    $cloud_context = $this->cloudContext;

    // IAM Roles.
    $iam_roles = $this->createIamRolesRandomTestFormData();
    $this->updateIamRolesMockData($iam_roles);

    // Add an empty value to IAM roles.
    $iam_roles = array_merge([[]], $iam_roles);

    // Setup an arbitrary schedule in the configuration.
    // This is needed in the cloud server template launch confirmation form.
    $schedule_value = $this->random->name(8, TRUE);

    $config = \Drupal::configFactory()->getEditable('aws_cloud.settings');
    $config->set('aws_cloud_scheduler_periods', $schedule_value)
      ->save();

    // Launch a new Instance, with schedule information.
    $add = $this->createInstanceTestFormData($max_test_repeat_count);
    for ($i = 0, $num = 1; $i < $max_test_repeat_count; $i++, $num++) {

      // Create image.
      $image = $this->createImageTestEntity($i, $add[$i]['image_id'], $cloud_context);

      // Make sure if the image entity is created or not.
      $this->drupalGet("/clouds/aws_cloud/${cloud_context}/image");
      $this->assertResponse(200, t('HTTP 200: Image | The new AWS Cloud Image #@num', ['@num' => $num]));
      $this->assertNoText(t('warning'), t('Image | Make sure w/o Warnings'));
      $this->assertText($image->getName(), t('Image | Make sure w/ Image Name (TestEntity)'));
      $this->assertText($image->getImageId(), t('Image | Make sure w/ Image ID (TestEntity)'));
      $this->assertText($add[$i]['image_id'], t('Image | Make sure w/ Image ID (TestFormData)'));

      // Setup cloud server template and instance.
      $server_template = $this->createServerTemplateTestEntity($iam_roles, $image, $cloud_context);

      // Make sure if the cloud_server_template entity is created or not.
      $this->drupalGet("/clouds/design/server_template/${cloud_context}");
      $this->assertResponse(200, t('HTTP 200: CloudServerTemplate | The new CloudServerTemplate #@num', ['@num' => $num]));
      $this->assertNoText(t('warning'), t('CloudServerTemplate | Make sure w/o Warnings'));
      $this->assertText($server_template->name->value,
        t('CloudServerTemplate | Make sure w/ CloudServerTemplate Name: @name', [
          '@name' => $server_template->name->value,
        ])
      );

      $this->addInstanceMockData($add[$i]['name'], $add[$i]['key_pair_name'], 'running', $schedule_value);

      $this->drupalPostForm("/clouds/design/server_template/$cloud_context/$num/launch",
        ['schedule' => $schedule_value],
        t('Launch'));

      $this->assertResponse(200, t('HTTP 200: Launch | The new Cloud Instance @num', [
        '@num' => $num,
      ]));
      $this->assertNoText(t('Notice'), t('Launch | Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Launch | Make sure w/o Warnings'));

      // Go to the instance page.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/instance/$num");
      $this->assertText($schedule_value, t('Schedule'));
    }
  }

}

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

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