Skip to content
Snippets Groups Projects
CloudServerTemplateTest.php 9.54 KiB
Newer Older
<?php

namespace Drupal\Tests\k8s\Functional;

/**
 * Test K8s cloud server templates (CloudServerTemplate).
 *
 * @group k8s
 */
class CloudServerTemplateTest extends K8sTestCase {

  const CLOUD_SERVER_TEMPLATES_REPEAT_COUNT = 3;

  /**
   * {@inheritdoc}
   */
  protected function getPermissions() {
    // Setup namespaces.
    $namespaces = $this->createNamespacesRandomTestFormData();
    $this->addNamespaceMockData($namespaces[0]);
    $this->createNamespaceTestEntity($namespaces[0]);
    $this->namespace = $namespaces[0]['name'];
    $this->getNamespaceMockData($namespaces[0]);

    return [
      'add cloud server templates',
      'list cloud server template',
      'view any published cloud server templates',
      'view any unpublished cloud server templates',
      'edit any cloud server templates',
      'delete any cloud server templates',
      'access cloud server template revisions',
      'revert all cloud server template revisions',
      'delete all cloud server template revisions',
      'list k8s pod',
      'view any k8s pod',
      'edit any k8s pod',
      'list k8s deployment',
      'view k8s deployment',
      'edit k8s deployment',
      'add k8s deployment',
      'delete k8s deployment',
      'launch cloud server template',
      'view k8s namespace ' . $this->namespace,
    ];

  }

  /**
   * CRUD test for k8s server template.
   */
  public function testK8sServerTemplate() {
    $objects = $this->getK8sObjects();
    foreach ($objects as $object) {
      $this->runServerTemplateCrudTest($object);
    }
  }

  /**
   * CRUD test for k8s server template launch.
   */
  public function testK8sServerTemplateLaunch() {
    $objects = $this->getK8sObjects();
    foreach ($objects as $object) {
      $this->runServerTemplateLaunchTest($object);
    }
  }

  private function runServerTemplateCrudTest($object) {
    $cloud_context = $this->cloudContext;

    // Create test.
    $pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
    $add = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
    for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
      // field_object is automatically calculated.
      unset($add[$i]['field_object']);
      $num = $i + 1;
      $this->drupalPostForm(
        "/clouds/design/server_template/$cloud_context/k8s/add",
        $add[$i],
        t('Save'));

      $this->assertResponse(200, t('HTTP 200: Add | The new cloud server template Form #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      $this->assertText(
        t('Created the @name cloud server template', [
          '@name' => $add[$i]['name[0][value]'],
        ]),
        t('Confirm Message: Created the @name cloud server template.', [
          '@name' => $add[$i]['name[0][value]'],
        ])
      );
    }

    // List test.
    $this->drupalGet("/clouds/design/server_template/$cloud_context");
    $this->assertResponse(200, t('HTTP 200: List | Cloud server template'));
    $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
    $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));

    // Edit test.
    $pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
    $edit = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
    for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
      // field_object is automatically calculated.
      unset($edit[$i]['field_object']);

      // Go to listing page.
      $this->drupalGet("/clouds/design/server_template/$cloud_context");
      $this->clickLink('Edit', $i);

        $edit[$i],
        t('Save')
      );

      $this->assertResponse(200, t('HTTP 200: Edit | The new cloud server template #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      $this->assertText(
        t('Saved the @name cloud server template.',
          [
            '@name' => $edit[$i]['name[0][value]'],
          ]
        ),
        t('Confirm Message: Saved the @name cloud server template.',
          [
            '@name' => $edit[$i]['name[0][value]'],
          ]
        )
      );
      $this->assertText($edit[$i]['name[0][value]']);
    }

    // Delete test.
    for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
      $num = $i + 1;

      // Go to listing page.
      $this->drupalGet("/clouds/design/server_template/$cloud_context");

      $this->clickLink('Delete', 0);
      $this->drupalPostForm(
        $this->getUrl(),
        [],
        t('Delete'));
      $this->assertResponse(200, t('HTTP 200: Delete | Cloud server template #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('warning'), t('Make sure w/o Warnings'));
    // Make sure the deleted templates are not listed anymore.
    $this->drupalGet("/clouds/design/server_template/$cloud_context");
    $this->assertResponse(200, t('HTTP 200: List | Cloud server te#@num', ['@num' => $num]));
    $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
    $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
    for ($j = 0; $j < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $j++) {
      $this->assertNoText($edit[$j]['name[0][value]'],
        t("Make sure w/ Listing @num: @name", [
          '@num' => $j + 1,
          '@name' => $edit[$j]['name[0][value]'],
        ]));
  private function runServerTemplateLaunchTest($object) {
    $cloud_context = $this->cloudContext;

    // Create templates.
    switch ($object) {
      case 'pod':
        $data = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
        break;

      case 'deployment':
        $data = $this->createDeploymentTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
        break;

      default:
        break;
    }

    $add = $this->createServerTemplateTestFormData($data, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);

    for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
      // field_object is automatically calculated.
      unset($add[$i]['field_object']);
      $num = $i + 1;
      $this->drupalPostForm(
        "/clouds/design/server_template/$cloud_context/k8s/add",
        $add[$i],
        t('Save'));

      $this->assertResponse(200, t('HTTP 200: Add | The new cloud server template Form #@num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      $this->assertText(
        t('Created the @name cloud server template', [
          '@name' => $add[$i]['name[0][value]'],
        ]),
        t('Confirm Message: Created the @name cloud server template.', [
          '@name' => $add[$i]['name[0][value]'],
        ])
      );
    }

    // Launch the templates.
    for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
      $this->drupalGet("/clouds/design/server_template/$cloud_context");
      switch ($object) {
        case 'pod':
          $this->getMetricsPodMockData([]);
          $this->addPodMockData($data[$i]);
          break;

        case 'deployment':
          $this->addDeploymentMockData($data[$i]);
          break;

        default;
          break;
      }

      // Navigate to the server template, and launch it.
      $this->clickLink($data[$i]['name']);
      $this->clickLink('Launch');

        [],
        t('Launch')
      );

      $this->assertResponse(200, t('HTTP 200 for @num', ['@num' => $num]));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      $this->assertText(
        t('@object @name launched.',
          [
            '@object' => ucfirst($object),
            '@name' => $add[$i]['name[0][value]'],
          ]
        ),
        t('@object launched', ['@object' => ucfirst($object)])
      );
  /**
   * Get K8bs objects to test.
   *
   * @return array
   *   Array of K8s objects.
   */
  protected function getK8sObjects() {
    return [
      'pod',
      'deployment',
    ];
  }

   * @param array $test_data
   *   Test data array.
   * @param string $object
   *   Object type.
   * @param int $repeat_count
   *   Repeat count.
   *
   * @return array
   *   Test data.
   */
  protected function createServerTemplateTestFormData(array $test_data, $object, $repeat_count = 1) {
    $data = [];
    $random = $this->random;

    for ($i = 0; $i < $repeat_count; $i++) {
      $data[] = [
        'cloud_context[0][value]' => $this->cloudContext,
        'name[0][value]' => $test_data[$i]['name'],
        'field_detail[0][value]' => $test_data[$i]['post_data']['detail[0][value]'],
        'field_object' => $object,