Newer
Older
baldwinlouie
committed
<?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',
baldwinlouie
committed
'add k8s pod',
'delete any k8s pod',
baldwinlouie
committed
'list k8s deployment',
'view k8s deployment',
'edit k8s deployment',
'add k8s deployment',
'delete k8s deployment',
baldwinlouie
committed
'launch cloud server template',
'view k8s namespace ' . $this->namespace,
];
}
baldwinlouie
committed
/**
* 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);
}
}
baldwinlouie
committed
/**
* CRUD test for k8s pod server template.
baldwinlouie
committed
*
* @param string $object
* Object to test.
baldwinlouie
committed
*/
baldwinlouie
committed
private function runServerTemplateCrudTest($object) {
baldwinlouie
committed
$cloud_context = $this->cloudContext;
// Create test.
$pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
baldwinlouie
committed
$add = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
baldwinlouie
committed
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
baldwinlouie
committed
// field_object is automatically calculated.
unset($add[$i]['field_object']);
baldwinlouie
committed
$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]'],
])
);
}
baldwinlouie
committed
// 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'));
baldwinlouie
committed
// Edit test.
$pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
baldwinlouie
committed
$edit = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
baldwinlouie
committed
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
baldwinlouie
committed
// field_object is automatically calculated.
unset($edit[$i]['field_object']);
baldwinlouie
committed
$num = $i + 1;
baldwinlouie
committed
// Go to listing page.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->clickLink('Edit', $i);
baldwinlouie
committed
$this->drupalPostForm(
baldwinlouie
committed
$this->getUrl(),
baldwinlouie
committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
$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;
baldwinlouie
committed
// Go to listing page.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->clickLink('Delete', 0);
$this->drupalPostForm(
$this->getUrl(),
baldwinlouie
committed
[],
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'));
baldwinlouie
committed
}
baldwinlouie
committed
baldwinlouie
committed
// 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]'],
]));
baldwinlouie
committed
}
}
/**
* Launch test for k8s pod server template.
baldwinlouie
committed
*
* @param string $object
* Object to test.
baldwinlouie
committed
*/
baldwinlouie
committed
private function runServerTemplateLaunchTest($object) {
baldwinlouie
committed
$cloud_context = $this->cloudContext;
// Create templates.
baldwinlouie
committed
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;
}
baldwinlouie
committed
$add = $this->createServerTemplateTestFormData($data, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
baldwinlouie
committed
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
baldwinlouie
committed
// field_object is automatically calculated.
unset($add[$i]['field_object']);
baldwinlouie
committed
$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++) {
baldwinlouie
committed
$this->drupalGet("/clouds/design/server_template/$cloud_context");
baldwinlouie
committed
// Update the mock data.
baldwinlouie
committed
switch ($object) {
case 'pod':
$this->getMetricsPodMockData([]);
$this->addPodMockData($data[$i]);
break;
case 'deployment':
$this->addDeploymentMockData($data[$i]);
break;
default;
break;
}
baldwinlouie
committed
$num = $i + 1;
baldwinlouie
committed
// Navigate to the server template, and launch it.
$this->clickLink($data[$i]['name']);
$this->clickLink('Launch');
baldwinlouie
committed
$this->drupalPostForm(
baldwinlouie
committed
$this->getUrl(),
baldwinlouie
committed
[],
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'));
baldwinlouie
committed
$this->assertText(
t('@object @name launched.',
[
'@object' => ucfirst($object),
'@name' => $add[$i]['name[0][value]'],
]
),
t('@object launched', ['@object' => ucfirst($object)])
);
baldwinlouie
committed
}
}
baldwinlouie
committed
/**
* Get K8bs objects to test.
*
* @return array
* Array of K8s objects.
*/
protected function getK8sObjects() {
return [
'pod',
'deployment',
];
}
baldwinlouie
committed
/**
* Create cloud server template test data.
*
baldwinlouie
committed
* @param array $test_data
* Test data array.
* @param string $object
* Object type.
baldwinlouie
committed
* @param int $repeat_count
* Repeat count.
*
* @return array
* Test data.
*/
baldwinlouie
committed
protected function createServerTemplateTestFormData(array $test_data, $object, $repeat_count = 1) {
baldwinlouie
committed
$data = [];
$random = $this->random;
for ($i = 0; $i < $repeat_count; $i++) {
$data[] = [
'cloud_context[0][value]' => $this->cloudContext,
baldwinlouie
committed
'name[0][value]' => $test_data[$i]['name'],
baldwinlouie
committed
'field_namespace' => $this->namespace,
baldwinlouie
committed
'field_detail[0][value]' => $test_data[$i]['post_data']['detail[0][value]'],
'field_object' => $object,
baldwinlouie
committed
];
}
return $data;
}
}