Skip to content
Snippets Groups Projects
Commit c41fb2ca authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3201301 by Xiaohua Guan, yas: Add test cases for K8s Schedule (Pod)

parent f6f535a8
Branches 4.0.x
No related tags found
No related merge requests found
......@@ -115,7 +115,13 @@ class K8sScheduleEditForm extends K8sEditForm {
$this->copyFormItemValues($form);
$this->trimTextfields($form, $form_state);
$this->entity->save();
$entity = $this->entity;
$entity->save();
$this->processOperationStatus($entity, 'updated');
$form_state->setRedirect('view.k8s_schedule.list', [
'cloud_context' => $entity->getCloudContext(),
]);
}
}
<?php
namespace Drupal\Tests\k8s\Functional;
/**
* Tests K8s schedule.
*
* @group K8s
*/
class K8sScheduleTest extends K8sTestBase {
public const K8S_SCHEDULE_REPEAT_COUNT = 2;
/**
* {@inheritdoc}
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Exception
*/
protected function getPermissions(): array {
$namespaces = $this->createNamespacesRandomTestFormData();
$this->createNamespaceTestEntity($namespaces[0]);
$this->namespace = $namespaces[0]['name'];
return [
'view all cloud service providers',
'list k8s schedule',
'view any k8s schedule',
'edit any k8s schedule',
'add k8s schedule',
'delete any k8s schedule',
'list k8s pod',
'add k8s pod',
'view any k8s pod',
'edit any k8s pod',
'delete any k8s pod',
'view k8s namespace ' . $this->namespace,
];
}
/**
* Tests CRUD for Schedule.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testSchedule(): void {
$cloud_context = $this->cloudContext;
// List Schedule for K8s.
$this->drupalGet("/clouds/k8s/$cloud_context/schedule");
$this->assertNoErrorMessage();
// Add a new Schedule.
$add = $this->createPodTestFormData(self::K8S_SCHEDULE_REPEAT_COUNT, $this->namespace);
for ($i = 0; $i < self::K8S_SCHEDULE_REPEAT_COUNT; $i++) {
$this->reloadMockData();
$this->addPodMockData($add[$i]);
$this->drupalGet("/clouds/k8s/$cloud_context/pod/add");
$this->submitForm(
array_merge($add[$i]['post_data'], ['enable_time_scheduler' => TRUE]),
$this->t('Save')
);
$this->assertNoErrorMessage();
$t_args = ['@type' => 'Pod', '%label' => $add[$i]['name']];
$this->assertSession()->pageTextContains(strip_tags($this->t('The @type %label has been created.', $t_args)));
// Make sure listing.
$this->drupalGet("/clouds/k8s/$cloud_context/schedule");
$this->assertNoErrorMessage();
$this->assertSession()->pageTextContains($add[$i]['post_data']['namespace'] . '_' . $add[$i]['name']);
}
for ($i = 0, $num = 1; $i < self::K8S_SCHEDULE_REPEAT_COUNT; $i++, $num++) {
// Make sure the all schedule listing exists.
$this->drupalGet('/clouds/k8s/schedule');
$this->assertNoErrorMessage();
for ($j = 0; $j < $num; $j++) {
$this->assertSession()->pageTextContains($add[$j]['name']);
}
}
// Edit a Schedule.
$edit = $this->createScheduleTestFormData(self::K8S_SCHEDULE_REPEAT_COUNT, $this->namespace);
for ($i = 0, $num = 1; $i < self::K8S_SCHEDULE_REPEAT_COUNT; $i++, $num++) {
$this->drupalGet("/clouds/k8s/$cloud_context/schedule/$num/edit");
$this->submitForm(
$edit[$i]['post_data'],
$this->t('Save')
);
$this->assertNoErrorMessage();
$t_args = ['@type' => 'Schedule', '%label' => $add[$i]['post_data']['namespace'] . '_' . $add[$i]['name']];
$this->assertSession()->pageTextContains(strip_tags($this->t('The @type %label has been updated.', $t_args)));
}
// Delete Schedule.
for ($i = 0, $num = 1; $i < self::K8S_SCHEDULE_REPEAT_COUNT; $i++, $num++) {
$this->drupalGet("/clouds/k8s/$cloud_context/schedule/$num/delete");
$this->submitForm(
[],
$this->t('Delete')
);
$this->assertNoErrorMessage();
$t_args = ['@type' => 'Schedule', '@label' => $add[$i]['post_data']['namespace'] . '_' . $add[$i]['name']];
$this->assertSession()->pageTextContains(strip_tags($this->t('The @type @label has been deleted.', $t_args)));
// Make sure listing.
$this->drupalGet("/clouds/k8s/$cloud_context/schedule");
$this->assertNoErrorMessage();
$this->assertSession()->pageTextNotContains($add[$i]['name']);
}
}
/**
* Tests deleting schedules with bulk operation.
*
* @throws \Exception
*/
public function testScheduleBulk(): void {
for ($i = 0; $i < self::K8S_SCHEDULE_REPEAT_COUNT; $i++) {
// Create Schedules.
$schedules = $this->createSchedulesRandomTestFormData();
$entities = [];
foreach ($schedules ?: [] as $schedule) {
$entities[] = $this->createScheduleTestEntity($schedule);
}
// $this->deleteScheduleMockData($schedules[0]);
$this->runTestEntityBulk('schedule', $entities);
}
}
}
......@@ -27,6 +27,7 @@ use Drupal\k8s\Entity\K8sReplicaSet;
use Drupal\k8s\Entity\K8sResourceQuota;
use Drupal\k8s\Entity\K8sRole;
use Drupal\k8s\Entity\K8sRoleBinding;
use Drupal\k8s\Entity\K8sSchedule;
use Drupal\k8s\Entity\K8sSecret;
use Drupal\k8s\Entity\K8sServiceAccount;
use Drupal\k8s\Entity\K8sServiceEntity;
......@@ -479,6 +480,19 @@ trait K8sTestEntityTrait {
return parent::createTestEntity(CloudCostStorage::class, $cloud_cost_storage);
}
/**
* Create a K8s Schedule test entity.
*
* @param array $schedule
* The schedule data.
*
* @return \Drupal\k8s\Entity\K8sSchedule
* The secret entity.
*/
protected function createScheduleTestEntity(array $schedule): CloudContentEntityBase {
return $this->createTestEntity(K8sSchedule::class, $schedule);
}
/**
* Helper function to create a K8s test entity.
*
......
......@@ -1988,4 +1988,56 @@ EOS;
return $priority_classes;
}
/**
* Create test data for k8s schedule.
*
* @param int $repeat_count
* Repeat count.
* @param string $namespace
* The name of namespace.
*
* @return array
* Test data.
*/
protected function createScheduleTestFormData($repeat_count, $namespace): array {
$random = new Random();
// Input Fields.
$data = [];
for ($i = 0; $i < $repeat_count; $i++) {
$name = 'name-' . $random->name(8, TRUE);
$data[] = [
'name' => $name,
'post_data' => [
'start_hour' => rand(0, 23),
'start_minute' => rand(0, 59),
'stop_hour' => rand(0, 23),
'stop_minute' => rand(0, 59),
],
];
}
return $data;
}
/**
* Create random schedules.
*
* @return array
* Random schedules array.
*
* @throws \Exception
*/
protected function createSchedulesRandomTestFormData(): array {
$schedules = [];
$count = random_int(1, 10);
for ($i = 0, $num = 1; $i < $count; $i++, $num++) {
$schedules[] = [
'name' => sprintf('schedule-random-data #%d - %s - %s', $num, date('Y/m/d H:i:s'), $this->random->name(32, TRUE)),
];
}
return $schedules;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment