import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { ComponentFixture, TestBed } from '@angular/core/testing';
+import {
+ ComponentFixture,
+ TestBed,
+ discardPeriodicTasks,
+ fakeAsync,
+ tick
+} from '@angular/core/testing';
import { CephfsSnapshotscheduleFormComponent } from './cephfs-snapshotschedule-form.component';
import {
import { ReactiveFormsModule } from '@angular/forms';
import { FormHelper, configureTestBed } from '~/testing/unit-test-helper';
import { CephfsSnapshotScheduleService } from '~/app/shared/api/cephfs-snapshot-schedule.service';
+import { of } from 'rxjs';
describe('CephfsSnapshotscheduleFormComponent', () => {
let component: CephfsSnapshotscheduleFormComponent;
let fixture: ComponentFixture<CephfsSnapshotscheduleFormComponent>;
let formHelper: FormHelper;
- let createSpy: jasmine.Spy;
configureTestBed({
declarations: [CephfsSnapshotscheduleFormComponent],
component.fsName = 'test_fs';
component.ngOnInit();
formHelper = new FormHelper(component.snapScheduleForm);
- createSpy = spyOn(TestBed.inject(CephfsSnapshotScheduleService), 'create').and.stub();
fixture.detectChanges();
});
expect(nativeEl.querySelector('cd-modal')).not.toBe(null);
});
- it('should submit the form', () => {
+ it('should submit the form', fakeAsync(() => {
+ const createSpy = spyOn(TestBed.inject(CephfsSnapshotScheduleService), 'create').and.stub();
+ const checkScheduleExistsSpy = spyOn(
+ TestBed.inject(CephfsSnapshotScheduleService),
+ 'checkScheduleExists'
+ ).and.returnValue(of(false));
const input = {
directory: '/test',
startDate: {
formHelper.setMultipleValues(input);
component.snapScheduleForm.get('directory').setValue('/test');
component.submit();
+ tick(400);
+ expect(checkScheduleExistsSpy).toHaveBeenCalled();
expect(createSpy).toHaveBeenCalled();
- });
+ discardPeriodicTasks();
+ }));
});
this.subvolume = value?.split?.('/')?.[3];
}),
filter(() => !!this.subvolume && !!this.subvolumeGroup),
+ tap(() => {
+ this.isSubvolume = !!this.subvolume && !!this.subvolumeGroup;
+ this.snapScheduleForm.get('repeatFrequency').setErrors(null);
+ }),
mergeMap(() =>
this.subvolumeService
.exists(
}
submit() {
- if (this.snapScheduleForm.invalid) {
- this.snapScheduleForm.setErrors({ cdSubmitButton: true });
- return;
- }
-
- const values = this.snapScheduleForm.value as SnapshotScheduleFormValue;
-
- if (this.isEdit) {
- const retentionPoliciesToAdd = (this.snapScheduleForm.get(
- 'retentionPolicies'
- ) as FormArray).controls
- ?.filter(
- (ctrl) =>
- !ctrl.get('retentionInterval').disabled && !ctrl.get('retentionFrequency').disabled
- )
- .map((ctrl) => ({
- retentionInterval: ctrl.get('retentionInterval').value,
- retentionFrequency: ctrl.get('retentionFrequency').value
- }));
-
- const updateObj = {
- fs: this.fsName,
- path: this.path,
- subvol: this.subvol,
- group: this.group,
- retention_to_add: this.parseRetentionPolicies(retentionPoliciesToAdd) || null,
- retention_to_remove: this.parseRetentionPolicies(this.retentionPoliciesToRemove) || null
- };
-
- this.taskWrapper
- .wrapTaskAroundCall({
- task: new FinishedTask('cephfs/snapshot/schedule/' + URLVerbs.EDIT, {
- path: this.path
- }),
- call: this.snapScheduleService.update(updateObj)
- })
- .subscribe({
- error: () => {
- this.snapScheduleForm.setErrors({ cdSubmitButton: true });
- },
- complete: () => {
- this.activeModal.close();
+ this.validateSchedule()(this.snapScheduleForm).subscribe({
+ next: () => {
+ if (this.snapScheduleForm.invalid) {
+ this.snapScheduleForm.setErrors({ cdSubmitButton: true });
+ return;
+ }
+
+ const values = this.snapScheduleForm.value as SnapshotScheduleFormValue;
+
+ if (this.isEdit) {
+ const retentionPoliciesToAdd = (this.snapScheduleForm.get(
+ 'retentionPolicies'
+ ) as FormArray).controls
+ ?.filter(
+ (ctrl) =>
+ !ctrl.get('retentionInterval').disabled && !ctrl.get('retentionFrequency').disabled
+ )
+ .map((ctrl) => ({
+ retentionInterval: ctrl.get('retentionInterval').value,
+ retentionFrequency: ctrl.get('retentionFrequency').value
+ }));
+
+ const updateObj = {
+ fs: this.fsName,
+ path: this.path,
+ subvol: this.subvol,
+ group: this.group,
+ retention_to_add: this.parseRetentionPolicies(retentionPoliciesToAdd) || null,
+ retention_to_remove: this.parseRetentionPolicies(this.retentionPoliciesToRemove) || null
+ };
+
+ this.taskWrapper
+ .wrapTaskAroundCall({
+ task: new FinishedTask('cephfs/snapshot/schedule/' + URLVerbs.EDIT, {
+ path: this.path
+ }),
+ call: this.snapScheduleService.update(updateObj)
+ })
+ .subscribe({
+ error: () => {
+ this.snapScheduleForm.setErrors({ cdSubmitButton: true });
+ },
+ complete: () => {
+ this.activeModal.close();
+ }
+ });
+ } else {
+ const snapScheduleObj = {
+ fs: this.fsName,
+ path: values.directory,
+ snap_schedule: this.parseSchedule(values?.repeatInterval, values?.repeatFrequency),
+ start: this.parseDatetime(values?.startDate, values?.startTime)
+ };
+
+ const retentionPoliciesValues = this.parseRetentionPolicies(values?.retentionPolicies);
+
+ if (retentionPoliciesValues) {
+ snapScheduleObj['retention_policy'] = retentionPoliciesValues;
}
- });
- } else {
- const snapScheduleObj = {
- fs: this.fsName,
- path: values.directory,
- snap_schedule: this.parseSchedule(values?.repeatInterval, values?.repeatFrequency),
- start: this.parseDatetime(values?.startDate, values?.startTime)
- };
- const retentionPoliciesValues = this.parseRetentionPolicies(values?.retentionPolicies);
-
- if (retentionPoliciesValues) snapScheduleObj['retention_policy'] = retentionPoliciesValues;
+ if (this.isSubvolume) {
+ snapScheduleObj['subvol'] = this.subvolume;
+ }
- if (this.isSubvolume) snapScheduleObj['subvol'] = this.subvolume;
+ if (this.isSubvolume && !this.isDefaultSubvolumeGroup) {
+ snapScheduleObj['group'] = this.subvolumeGroup;
+ }
- if (this.isSubvolume && !this.isDefaultSubvolumeGroup) {
- snapScheduleObj['group'] = this.subvolumeGroup;
+ this.taskWrapper
+ .wrapTaskAroundCall({
+ task: new FinishedTask('cephfs/snapshot/schedule/' + URLVerbs.CREATE, {
+ path: snapScheduleObj.path
+ }),
+ call: this.snapScheduleService.create(snapScheduleObj)
+ })
+ .subscribe({
+ error: () => {
+ this.snapScheduleForm.setErrors({ cdSubmitButton: true });
+ },
+ complete: () => {
+ this.activeModal.close();
+ }
+ });
+ }
}
-
- this.taskWrapper
- .wrapTaskAroundCall({
- task: new FinishedTask('cephfs/snapshot/schedule/' + URLVerbs.CREATE, {
- path: snapScheduleObj.path
- }),
- call: this.snapScheduleService.create(snapScheduleObj)
- })
- .subscribe({
- error: () => {
- this.snapScheduleForm.setErrors({ cdSubmitButton: true });
- },
- complete: () => {
- this.activeModal.close();
- }
- });
- }
+ });
}
validateSchedule() {
directory?.value,
this.fsName,
repeatInterval?.value,
- repeatFrequency?.value
+ repeatFrequency?.value,
+ this.isSubvolume
)
.pipe(
map((exists: boolean) => {
if (exists) {
+ repeatFrequency?.markAsDirty();
repeatFrequency?.setErrors({ notUnique: true }, { emitEvent: true });
} else {
repeatFrequency?.setErrors(null);