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

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

namespace Drupal\Tests\aws_cloud\Functional\Ec2;

use Drupal\Tests\aws_cloud\Functional\AwsCloudTestCase;

/**
 * Tests AWS Cloud Network Interface.
 *
 * @group AWS Cloud
 */
class NetworkInterfaceTest extends AwsCloudTestCase {

  const AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT = 3;

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

  /**
   * {@inheritdoc}
   */
  protected function getMockDataTemplateVars() {
    return [
      'network_interface_id' => 'eni-' . $this->getRandomAwsId(),
      'vpc_id' => 'vpc-' . $this->getRandomAwsId(),
    ];
  }

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

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

    // Create security groups.
    $security_groups = $this->createSecurityGroupRandomTestFormData();
    $index = 0;
    foreach ($security_groups as $security_group) {
      $this->createSecurityGroupTestEntity($index++, $security_group['GroupId'], $security_group['Name'], '', $cloud_context);
    }

    // Add a new Network Interface.
    $add = $this->createNetworkInterfaceTestFormData(self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT);
    $subnets = $this->createRandomSubnets();
    $this->updateSubnetsToMockData($subnets);
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->reloadMockData();

      $this->addNetworkInterfaceMockData($add[$i]);

      unset($add[$i]['primary_private_ip']);

      $num = $i + 1;

      $subnet_index = array_rand($subnets);
      $add[$i]['subnet_id'] = $subnets[$subnet_index]['SubnetId'];
      $security_group_index = array_rand($security_groups);
      $add[$i]['security_groups[]'] = $security_groups[$security_group_index]['GroupId'];

      $this->drupalPostForm("/clouds/aws_cloud/$cloud_context/network_interface/add",
                            $add[$i],
                            t('Save'));
      $this->assertResponse(200, t('Add | HTTP 200: The new AWS Cloud Network Interface #@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(
        t('The AWS Cloud Network Interface "@name', [
          '@name' => $add[$i]['name'],
        ]),
        t('Confirm Message: Add | The AWS Cloud Network Interface "@name" has been created.', [
          '@name' => $add[$i]['name'],
        ])
      );
      $this->assertText(
        $add[$i]['name'],
        t('Network Interface: @name', [
          '@name' => $add[$i]['name'],
        ])
      );

      // Make sure listing.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
      $this->assertResponse(200, t('Add | List | HTTP 200: Network Interface #@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[$j]['name'],
                        t('Make sure w/ Listing: @name',
                        ['@name' => $add[$j]['name']]));
      }
    }

    // Edit an Network Interface information.
    $edit = $this->createNetworkInterfaceTestFormData(self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT);
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      unset($edit[$i]['subnet_id']);
      unset($edit[$i]['security_groups[]']);
      unset($edit[$i]['primary_private_ip']);

      $this->drupalPostForm("/clouds/aws_cloud/$cloud_context/network_interface/$num/edit",
                            $edit[$i],
                            t('Save'));
      $this->assertResponse(200, t('Edit | HTTP 200: The new AWS Cloud Network Interface #@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 Network Interface "@name" has been saved.', [
          '@name' => $edit[$i]['name'],
        ]),
        t('Confirm Message: The AWS Cloud Network Interface "@name" has been saved.', [
          '@name' => $edit[$i]['name'],
        ])
      );
      // Make sure the description.
      $this->assertFieldByName('description', $edit[$i]['description']);

      $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
      $this->assertResponse(200, t('Edit List | HTTP 200: Network Interface #@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->assertText($edit[$i]['name'], t('Network Interface: @name', ['@name' => $edit[$i]['name']]));

      // Make sure listing.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
      $this->assertResponse(200, t('Edit | List | HTTP 200: Network Interface #@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 Network Interface.
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $num = $i + 1;
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface/$num/delete");
      $this->assertResponse(200, t('HTTP 200: Delete | Network Interface #@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/network_interface/$num/delete",
                            [],
                            t('Delete'));

      $this->assertResponse(200, t('Delete | HTTP 200: The Cloud Network Interface #@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('Name: @name', ['@name' => $edit[$i]['name']]));
      $this->assertText(
        t('The AWS Cloud Network Interface "@name" has been deleted.', [
          '@name' => $edit[$i]['name'],
        ]),
        t('Confirm Message: Delete | The AWS Cloud Network Interface "@name" has been deleted.', [
          '@name' => $edit[$i]['name'],
        ])
      );

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

  /**
   * Tests deleting network interfaces with bulk operation.
   */
  public function testNetworkInterfaceBulk() {

    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      // Create network interface.
      $network_interfaces = $this->createNetworkInterfacesRandomTestFormData();
      $index = 0;
      $entities = [];
      foreach ($network_interfaces as $network_interface) {
        $entities[] = $this->createNetworkInterfaceTestEntity($index++, $network_interface['NetworkInterfaceId'], $network_interface['Name']);
      }

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

  /**
   * Test updating network interface.
   */
  public function testUpdateNetworkInterfaceList() {
    $cloud_context = $this->cloudContext;

    // Add a new Network Interface.
    $add = $this->createNetworkInterfaceTestFormData(self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT);
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->reloadMockData();

      $num = $i + 1;
      $this->addNetworkInterfaceMockData($add[$i]);
    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
    $this->assertResponse(200, t('Edit | List | HTTP 200: Network Interface #@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_NETWORK_INTERFACE_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 Network Interfaces.'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_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_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      // Confirm the detailed view.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface/$num");
      $this->assertLink(t('Edit'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/network_interface/$num/edit");
      $this->assertLink(t('Delete'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/network_interface/$num/delete");
      $this->assertLink(t('List AWS Cloud Network Interfaces'));
      // Click 'Refresh'.
      $this->clickLink(t('List AWS Cloud Network Interfaces'));
      $this->assertResponse(200, t('Edit | List | HTTP 200: Network Interface #@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'));

      // Confirm the edit view.
      $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface/$num/edit");
      $this->assertNoLink(t('Edit'));
      $this->assertLink(t('Delete'));
      $this->assertLinkByHref("/clouds/aws_cloud/$cloud_context/network_interface/$num/delete");
      $this->assertNoLink('Edit');
    }

    // Edit Network Interface information.
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {

      $num = $i + 1;

      // Change Network Interface Name in mock data.
      $add[$i]['name'] = 'eni-' . $this->getRandomAwsId();
      $this->updateNetworkInterfaceMockData($num - 1, $add[$i]['name']);

    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
    $this->assertResponse(200, t('Edit | List | HTTP 200: Network Interface #@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_NETWORK_INTERFACE_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 Network Interfaces.'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->assertText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }

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

      $num = $i + 1;

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

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_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_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->assertLink($add[$i]['tags_name']);
    }

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

      $num = $i + 1;

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

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_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_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->assertNoLink($add[$i]['tags_name']);
      $this->assertLink($add[$i]['name']);
    }

    // Delete Network Interface tags.
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {

      $num = $i + 1;

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

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_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_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->assertNoLink($add[$i]['tags_name']);
      $this->assertLink($add[$i]['name']);
    }

    // Delete Network Interface in mock data.
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->deleteFirstNetworkInterfaceMockData();
    }

    // Make sure listing.
    $this->drupalGet("/clouds/aws_cloud/$cloud_context/network_interface");
    $this->assertResponse(200, t('Edit | List | HTTP 200: Network Interface #@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_NETWORK_INTERFACE_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 Network Interfaces.'));
    // Make sure listing.
    for ($i = 0; $i < self::AWS_CLOUD_NETWORK_INTERFACE_REPEAT_COUNT; $i++) {
      $this->assertNoText($add[$i]['name'],
          t('Edit | List | Make sure w/ Listing: @name', [
            '@name' => $add[$i]['name'],
          ]));
    }

  }

  /**
   * Create random subnets.
   *
   * @return array
   *   Random subnets array.
   */
  private function createRandomSubnets() {

    $subnets = [];
    $count = rand(1, 10);
    for ($i = 0; $i < $count; $i++) {
      $subnets[] = [
        'SubnetId' => 'subnet-' . $this->getRandomAwsId(),
        '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;
  }

  /**
   * Update VPCs and subnets to mock data.
   *
   * @param array $subnets
   *   The subnets array.
   */
  private function updateSubnetsToMockData(array $subnets) {
    $mock_data = $this->getMockDataFromConfig();
    $mock_data['DescribeSubnets']['Subnets'] = $subnets;
    $this->updateMockDataToConfig($mock_data);
  }

}

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

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