cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/tests/src/Traits/AwsCloudTestFormDataTrait.php

modules/cloud_service_providers/aws_cloud/tests/src/Traits/AwsCloudTestFormDataTrait.php
<?php

namespace Drupal\Tests\aws_cloud\Traits;

use Drupal\Component\Utility\Random;
use Drupal\Tests\aws_cloud\Functional\Utils;

/**
 * The trait creating form data for aws cloud testing.
 */
trait AwsCloudTestFormDataTrait {

  /**
   * AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT.
   *
   * @var int
   */
  public static $awsCloudSecurityGroupRepeatCount = 3;

  /**
   * AWS_CLOUD_SECURITY_GROUP_RULES_REPEAT_COUNT.
   *
   * @var int
   */
  public static $awsCloudSecurityGroupRulesRepeatCount = 10;

  /**
   * AWS_CLOUD_SECURITY_GROUP_RULES_INBOUND.
   *
   * @var int
   */
  public static $awsCloudSecurityGroupRulesInbound = 0;

  /**
   * AWS_CLOUD_SECURITY_GROUP_RULES_OUTBOUND.
   *
   * @var int
   */
  public static $awsCloudSecurityGroupRulesOutbound = 1;

  /**
   * AWS_CLOUD_SECURITY_GROUP_RULES_MIX.
   *
   * @var int
   */
  public static $awsCloudSecurityGroupRulesMix = 2;

  /**
   * Create test data for cloud service provider (CloudConfig).
   *
   * @return array
   *   Test data.
   */
  protected function createCloudConfigTestFormData() {
    $this->random = new Random();

    // Input Fields.
    $data = [];
    for ($i = 0, $num = 1; $i < self::AWS_CLOUD_CONFIG_REPEAT_COUNT; $i++, $num++) {

      $data[] = [
        'name[0][value]'              => sprintf('config-entity-%s - %s', $this->random->name(8, TRUE), date('Y/m/d H:i:s')),
        'field_description[0][value]' => "description #$num: " . date('Y/m/d H:i:s - D M j G:i:s T Y') . $this->random->string(64, TRUE),
        'field_api_version[0][value]' => 'latest',
        'field_region'                => "us-west-$num",
        'field_secret_key[0][value]'  => $this->random->name(20, TRUE),
        'field_access_key[0][value]'  => $this->random->name(40, TRUE),
        'field_account_id[0][value]'  => $this->random->name(16, TRUE),
        'regions[us-east-1]'          => 'us-east-1',
      ];
    }

    return $data;
  }

  /**
   * Create random cloud service provider (CloudConfig) data.
   *
   * @return array
   *   Random cloud service providers (CloudConfig).
   */
  protected function createCloudConfigRandomTestFormData() {
    $cloud_configs = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $cloud_configs[] = [
        'CloudContext' => "cloud_context-{$this->getRandomAwsId()}",
        'Name' => sprintf('config-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(4, TRUE)),
      ];
    }

    return $cloud_configs;
  }

  /**
   * Create Elastic IP test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return string[][]
   *   Elastic IP array.
   */
  protected function createElasticIpTestFormData($repeat_count) {
    $data = [];
    // 3 times.
    for ($i = 0; $i < $repeat_count; $i++) {
      // Input Fields.
      $data[$i] = [
        'name'   => sprintf('eip-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
        'domain' => 'standard',
      ];
    }
    return $data;
  }

  /**
   * Create network interface test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   * @param bool $attachment
   *   Whether attachment is needed.
   *
   * @return array
   *   Array of Network Interface test data.
   */
  protected function createNetworkInterfaceTestFormData($repeat_count, $attachment = FALSE) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {

      $num = $i + 1;

      // Input Fields.
      $data[$i] = [
        'name' => $this->random->name(32, TRUE),
        'description' => "Description #$num - {$this->random->name(64, TRUE)}",
        'subnet_id' => "subnet_id-{$this->getRandomAwsId()}",
        'security_groups[]' => "sg-{$this->getRandomAwsId()}",
        'primary_private_ip' => Utils::getRandomPrivateIp(),
      ];

      if ($attachment) {
        $data[$i]['attachment_id'] = "attachment-{$this->getRandomAwsId()}";
      }
    }
    return $data;
  }

  /**
   * Create image test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return string[][]
   *   test data array.
   */
  protected function createImageTestFormData($repeat_count) {
    $data = [];
    for ($i = 0; $i < $repeat_count; $i++) {
      $num = $i + 1;

      // Input Fields.
      $data[$i] = [
        'name'        => "Image #$num - " . date('Y/m/d - ') . $this->random->name(8, TRUE),
        'instance_id' => "i-{$this->getRandomAwsId()}",
        'description' => "description-{$this->random->name(64)}",
      ];
    }
    return $data;
  }

  /**
   * Create instance test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return array
   *   test data array.
   */
  protected function createInstanceTestFormData($repeat_count = 1) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {

      $num = $i + 1;
      $instance_name = sprintf('instance-entity #%d - %s - %s', $num, date('Y/m/d H:i:s'), $this->random->name(8, TRUE));

      // Input Fields.
      $data[$i] = [
        'name'                => $instance_name,
        'image_id'            => "ami-{$this->getRandomAwsId()}",
        'min_count'           => $num,
        'max_count'           => $num * 2,
        'key_pair_name'       => "key_pair-${num}-{$this->random->name(8, TRUE)}",
        'is_monitoring'       => 0,
        'availability_zone'   => "us-west-${num}",
        'security_groups[]'   => "security_group-$num-{$this->random->name(8, TRUE)}",
        'instance_type'       => "t${num}.micro",
        'kernel_id'           => "aki-{$this->getRandomAwsId()}",
        'ramdisk_id'          => "ari-{$this->getRandomAwsId()}",
        'user_data'           => "User Data #${num}: {$this->random->name(64, TRUE)}",
        'tags[0][tag_key]'    => 'Name',
        'tags[0][tag_value]'  => $instance_name,
      ];
    }

    return $data;
  }

  /**
   * Create KeyPair test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   */
  protected function createKeyPairTestFormData($repeat_count) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {
      // Input Fields.
      $data[$i] = [
        'key_pair_name' => $this->random->name(15, TRUE),
      ];
    }
    return $data;
  }

  /**
   * Create security group test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   * @param bool $is_edit
   *   Whether edit mode or not.
   *
   * @return array
   *   Security group test data.
   */
  protected function createSecurityGroupTestFormData($repeat_count = 1, $is_edit = FALSE) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {

      $num = $i + 1;

      // Input Fields.
      $data[$i] = [
        'description' => "Description #$num - {$this->random->name(64, TRUE)}",
      ];

      if ($is_edit) {
        $data[$i]['name'] = "group-name-#$num - {$this->random->name(15, TRUE)}";
      }
      else {
        $data[$i]['group_name'] = "group-name-#$num - {$this->random->name(15, TRUE)}";
      }
    }
    return $data;
  }

  /**
   * Create snapshot test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return string[][]
   *   test data array.
   */
  protected function createSnapshotTestFormData($repeat_count) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {

      $num = $i + 1;

      // Input Fields.
      $data[$i] = [
        'name'        => "Name #$num - {$this->random->name(32, TRUE)}",
        'volume_id'   => "vol-{$this->getRandomAwsId()}",
        'description' => "Description #$num - {$this->random->name(64, TRUE)}",
      ];
    }
    return $data;
  }

  /**
   * Create volume test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return string[][]|number[][]
   *   test data array.
   */
  protected function createVolumeTestFormData($repeat_count) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {

      $num = $i + 1;

      // Input Fields.
      $data[$i] = [
        'name'              => "volume-name #$num - {$this->random->name(32, TRUE)}",
        'snapshot_id'       => "snap-{$this->getRandomAwsId()}",
        'size'              => $num * 10,
        'availability_zone' => "us-west-${num}",
        'iops'              => $num * 1000,
        'encrypted'         => $num % 2,
        'volume_type'       => 'io1',
      ];
    }
    return $data;
  }

  /**
   * Create VPC test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return array
   *   Test data.
   */
  protected function createVpcTestFormData($repeat_count = 1) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {
      $num = $i + 1;

      // Input Fields.
      $data[] = [
        'name'        => "VPC #$num - {$this->random->name(32, TRUE)}",
        'cidr_block'   => Utils::getRandomCidr(),
      ];
    }
    return $data;
  }

  /**
   * Create subnet test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   * @param array $vpc_ids
   *   VPC IDs.
   *
   * @return array
   *   Test data.
   */
  protected function createSubnetTestFormData($repeat_count, array $vpc_ids) {
    $data = [];

    for ($i = 0; $i < $repeat_count; $i++) {
      $num = $i + 1;

      // Input Fields.
      $data[] = [
        'vpc_id'      => $vpc_ids[$i],
        'name'        => "Name #$num - {$this->random->name(32, TRUE)}",
        'cidr_block'  => Utils::getRandomCidr(),
      ];
    }
    return $data;
  }

  /**
   * Create cloud server template test data.
   *
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return array
   *   Test data.
   */
  protected function createServerTemplateTestFormData($repeat_count = 1) {
    $data = [];
    $random = $this->random;

    for ($i = 0; $i < $repeat_count; $i++) {

      $num = $i + 1;

      // Input Fields.
      $data[] = [
        'cloud_context[0][value]' => $this->cloudContext,
        'name[0][value]' => "ServerTemplate-{$random->name(16, TRUE)}",
        'field_description[0][value]' => "#$num: " . date('Y/m/d H:i:s - D M j G:i:s T Y')
        . ' - SimpleTest Cloud Server Template Description - '
        . $random->name(32, TRUE),
        'field_test_only[value]' => '1',
        'field_instance_type' => "m$num.xlarge",
        'field_availability_zone' => 'us-west-1',
        'field_monitoring[value]' => '1',
        'field_image_id' => "ami-{$this->getRandomAwsId()}",
        'field_min_count[0][value]' => 1,
        'field_max_count[0][value]' => 1,
        'field_kernel_id[0][value]' => "aki-{$this->getRandomAwsId()}",
        'field_ram[0][value]' => "ari-{$this->getRandomAwsId()}",
        'field_security_group' => 1,
        'field_ssh_key' => 1,
        'field_tags[0][tag_key]' => "key_{$random->name(8, TRUE)}",
        'field_tags[0][tag_value]' => "value_{$random->name(8, TRUE)}",
      ];
    }
    return $data;
  }

  /**
   * Create random snapshots data.
   *
   * @return array
   *   Random snapshots.
   */
  protected function createSnapshotsRandomTestFormData() {
    $snapshots = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $snapshots[] = [
        'SnapshotId' => "snap-{$this->getRandomAwsId()}",
        'Name' => sprintf('snapshot-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $snapshots;
  }

  /**
   * Create random volumes data.
   *
   * @return array
   *   Random volumes data.
   */
  protected function createVolumesRandomTestFormData() {
    $volumes = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $volumes[] = [
        'VolumeId' => "vol-{$this->getRandomAwsId()}",
        'Name' => sprintf('volume-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $volumes;
  }

  /**
   * Create random images data.
   *
   * @return array
   *   Random images data.
   */
  protected function createImagesRandomTestFormData() {
    $images = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $images[] = [
        'ImageId' => "ami-{$this->getRandomAwsId()}",
        'Name' => sprintf('image-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $images;
  }

  /**
   * Create random instances data.
   *
   * @return array
   *   Random instances data.
   */
  protected function createInstancesRandomTestFormData() {
    $instances = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $instances[] = [
        'InstanceId' => "i-{$this->getRandomAwsId()}",
        'Name' => sprintf('instance-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $instances;
  }

  /**
   * Create random VPCs data.
   *
   * @return array
   *   Random VPCs data.
   */
  protected function createVpcsRandomTestFormData() {
    $vpcs = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $vpcs[] = [
        'VpcId' => "vpc-{$this->getRandomAwsId()}",
        'Name' => sprintf('vpc-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
        'Tags' => [
          [
            'Key' => 'Name',
            'Value' => sprintf('vpc-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
          ],
        ],
      ];
    }

    return $vpcs;
  }

  /**
   * Create random Network Interfaces data.
   *
   * @return array
   *   Random network interfaces data.
   */
  protected function createNetworkInterfacesRandomTestFormData() {
    $network_interfaces = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $network_interfaces[] = [
        'NetworkInterfaceId' => "eni-{$this->getRandomAwsId()}",
        'Name' => sprintf('eni-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $network_interfaces;
  }

  /**
   * Create random security groups data.
   *
   * @return array
   *   Random security groups data.
   */
  protected function createSecurityGroupRandomTestFormData() {
    $security_groups = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $security_groups[] = [
        'GroupId' => "sg-{$this->getRandomAwsId()}",
        'GroupName' => sprintf('sg-random-data #%d-%s-%s', $i, date('Y/m/d-H:i:s'), $this->random->name(8, TRUE)),
        'Name' => sprintf('sg-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $security_groups;
  }

  /**
   * Create random Elastic IPs data.
   *
   * @return array
   *   Random Elastic IPs.
   */
  protected function createElasticIpRandomTestFormData() {
    $elastic_ips = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $elastic_ips[] = [
        'PublicIp' => Utils::getRandomPublicIp(),
        'Name' => sprintf('eip-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $elastic_ips;
  }

  /**
   * Create random key pairs data.
   *
   * @return array
   *   Random key pairs data.
   */
  protected function createKeyPairsRandomTestFormData() {

    $key_pairs = [];
    $count = rand(1, 10);

    for ($i = 0; $i < $count; $i++) {

      $key_fingerprint_parts = [];
      for ($part = 0; $part < 20; $part++) {
        $key_fingerprint_parts[] = sprintf('%02x', rand(0, 255));
      }

      $key_pairs[] = [
        'KeyFingerprint' => implode(':', $key_fingerprint_parts),
        'Name' => sprintf('key_pair-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
      ];
    }

    return $key_pairs;
  }

  /**
   * Create random IAM roles.
   *
   * @return array
   *   Random IAM roles.
   */
  protected function createIamRolesRandomTestFormData() {
    $random = $this->random;
    $iam_roles = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $arn_num = sprintf('%012s', rand(1, 999999999999));
      $arn_name = $random->name(16, TRUE);
      $name = $random->name(10, TRUE);
      $iam_roles[] = [
        'InstanceProfileName' => $name,
        'Arn' => "arn:aws:iam::$arn_num:instance-profile/$arn_name",
        'Roles' => [
          ['RoleName' => $name],
        ],
      ];

    }

    return $iam_roles;
  }

  /**
   * Create random subnets.
   *
   * @return array
   *   Random subnets array.
   */
  protected function createSubnetsRandomTestFormData() {
    $subnets = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $subnets[] = [
        'SubnetId' => "subnet-{$this->getRandomAwsId()}",
        'Name' => sprintf('subnet-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
        'Tags' => [
          [
            'Key' => 'Name',
            'Value' => sprintf('subnet-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
          ],
        ],
      ];
    }

    return $subnets;
  }

  /**
   * Create rules.
   *
   * @param int $rules_type
   *   The type of rules. Inbound | Outbound | Mixed.
   * @param string $edit_url
   *   The URL of security group edit form.
   * @param string $self_group_id
   *   The security group ID for rules.
   * @param int $security_group_rules_repeat_count
   *   The security group rules repeat count.
   *
   * @return array
   *   The rules created.
   */
  protected function createRulesTestFormData($rules_type, $edit_url, $self_group_id = NULL, $security_group_rules_repeat_count = 1) {
    $rules = [];
    $count = rand(1, $security_group_rules_repeat_count);
    for ($i = 0; $i < $count; $i++) {
      if (isset($self_group_id)) {
        $group_arr = [$self_group_id, "sg-{$this->getRandomAwsId()}"];
        $group_id = $group_arr[array_rand($group_arr)];
      }
      else {
        $group_id = "sg-{$this->getRandomAwsId()}";
      }
      $permissions = [
        [
          'source' => 'ip4',
          'cidr_ip' => Utils::getRandomCidr(),
          'from_port' => Utils::getRandomFromPort(),
          'to_port' => Utils::getRandomToPort(),
        ],
        [
          'source' => 'ip6',
          'cidr_ip_v6' => Utils::getRandomCidrV6(),
          'from_port' => Utils::getRandomFromPort(),
          'to_port' => Utils::getRandomToPort(),
        ],
        [
          'source' => 'group',
          'user_id' => $this->random->name(8, TRUE),
          'group_id' => $group_id,
          'vpc_id' => "vpc-{$this->getRandomAwsId()}",
          'peering_connection_id' => "pcx-{$this->getRandomAwsId()}",
          'peering_status' => 'active',
          'from_port' => Utils::getRandomFromPort(),
          'to_port' => Utils::getRandomToPort(),
        ],
      ];

      if ($rules_type == self::$awsCloudSecurityGroupRulesInbound
        || $rules_type == self::$awsCloudSecurityGroupRulesOutbound) {
        $type = $rules_type;
      }
      else {
        $types = [self::$awsCloudSecurityGroupRulesInbound, self::$awsCloudSecurityGroupRulesOutbound];
        $type = $types[array_rand($types)];
      }
      $rules[] = $permissions[array_rand($permissions)] + ['type' => $type];
    }

    // Post to form.
    $params = [];
    $inbound_index = 0;
    $outbound_index = 0;
    $rules_added = [];
    foreach ($rules as $rule) {
      if ($rule['type'] == self::$awsCloudSecurityGroupRulesInbound) {
        $index = $inbound_index++;
        $prefix = 'ip_permission';
      }
      else {
        $index = $outbound_index++;
        $prefix = 'outbound_permission';
      }

      foreach ($rule as $key => $value) {
        if ($key == 'type') {
          continue;
        }
        $params["${prefix}[${index}][${key}]"] = $value;
      }

      $rules_added[] = $rule;
      $this->updateRulesMockData($rules_added, self::$awsCloudSecurityGroupRulesOutbound);

      $this->drupalPostForm($edit_url, $params, t('Save'));
      $this->assertText($rule['from_port'], 'Create Rule');
    }

    // Confirm the values of edit form.
    $this->confirmRulesFormData($rules, $edit_url);

    return $rules;
  }

  /**
   * Revoke rules.
   *
   * @param array $rules
   *   The rules to be revoked.
   * @param string $view_url
   *   The URL of security group detailed view.
   * @param int $security_group_rules_repeat_count
   *   The Security Group rules repeat count.
   */
  protected function revokeRulesTestFormData(array $rules, $view_url, $security_group_rules_repeat_count = 1) {
    $this->drupalGet($view_url);
    $count = count($rules);
    $inbound_rules = array_filter($rules, function ($a) {
      return $a['type'] == self::$awsCloudSecurityGroupRulesInbound;
    });
    $inbound_rules_count = count($inbound_rules);
    for ($i = 0; $i < $count; $i++) {
      $rule = array_shift($rules);

      $index = 0;
      if ($rule['type'] == self::$awsCloudSecurityGroupRulesOutbound) {
        $index += $inbound_rules_count;
      }
      else {
        $inbound_rules_count--;
      }

      $this->clickLink(t('Revoke'), $index);
      $this->assertText($rule['from_port'], 'Revoke Rule');

      $this->updateRulesMockData($rules, self::$awsCloudSecurityGroupRulesOutbound);
      $this->submitForm([], t('Revoke'));
      $this->assertText('Permission revoked', 'Revoke Rule');
    }
  }

  /**
   * Confirm rule values of security group edit form.
   *
   * @param array $rules
   *   The array of rules.
   * @param string $edit_url
   *   The url of edit form.
   */
  protected function confirmRulesFormData(array $rules, $edit_url) {
    $this->drupalGet($edit_url);

    $inbound_index = 0;
    $outbound_index = 0;
    foreach ($rules as $rule) {
      if ($rule['type'] == self::$awsCloudSecurityGroupRulesInbound) {
        $index = $inbound_index++;
        $prefix = 'ip_permission';
      }
      else {
        $index = $outbound_index++;
        $prefix = 'outbound_permission';
      }
      foreach ($rule as $key => $value) {
        if ($key == 'type') {
          continue;
        }
        $name = "${prefix}[${index}][${key}]";
        $this->assertFieldByName($name, $value, 'Edit Rule');
      }
    }

    // Confirm the last raw is empty.
    $name = "ip_permission[${inbound_index}][from_port]";
    $this->assertFieldByName($name, '', 'Edit Rule');
    $name = "outbound_permission[${outbound_index}][from_port]";
    $this->assertFieldByName($name, '', 'Edit Rule');
  }

}

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

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