From 3c12d3b535bff973961bfc2a2f989262368ea8b0 Mon Sep 17 00:00:00 2001 From: Nizamudeen A Date: Wed, 27 Sep 2023 15:13:34 +0530 Subject: [PATCH] mgr/dashboard: fix cephfs form validator Number is not allowed as the starting character of the mds service Fixes: https://tracker.ceph.com/issues/63005 Signed-off-by: Nizamudeen A (cherry picked from commit fe8fa180d323500303f449be537ec2b7eae1a64a) --- .../cephfs-form/cephfs-form.component.html | 2 +- .../cephfs-form/cephfs-form.component.spec.ts | 25 +++++++++++++++++-- .../cephfs-form/cephfs-form.component.ts | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.html b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.html index 8da9a74a506..f1bcd915571 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.html @@ -34,7 +34,7 @@ i18n>This field is required! File System name can only contain letters, numbers, '.', '-' or '_' + i18n>File System name should start with a letter and can only contain letters, numbers, '.', '-' or '_' diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.spec.ts index cf85a2128d7..5409131d97b 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.spec.ts @@ -1,15 +1,17 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { RouterTestingModule } from '@angular/router/testing'; import { CephfsVolumeFormComponent } from './cephfs-form.component'; -import { configureTestBed } from '~/testing/unit-test-helper'; +import { FormHelper, configureTestBed } from '~/testing/unit-test-helper'; import { SharedModule } from '~/app/shared/shared.module'; import { ToastrModule } from 'ngx-toastr'; import { ReactiveFormsModule } from '@angular/forms'; describe('CephfsVolumeFormComponent', () => { let component: CephfsVolumeFormComponent; let fixture: ComponentFixture; + let formHelper: FormHelper; + configureTestBed({ imports: [ BrowserAnimationsModule, @@ -24,9 +26,28 @@ describe('CephfsVolumeFormComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(CephfsVolumeFormComponent); component = fixture.componentInstance; + formHelper = new FormHelper(component.form); fixture.detectChanges(); }); + it('should create', () => { expect(component).toBeTruthy(); }); + + it('should validate proper names', fakeAsync(() => { + const validNames = ['test', 'test1234', 'test_1234', 'test-1234', 'test.1234', 'test12test']; + const invalidNames = ['1234', 'test@', 'test)']; + + for (const validName of validNames) { + formHelper.setValue('name', validName, true); + tick(); + formHelper.expectValid('name'); + } + + for (const invalidName of invalidNames) { + formHelper.setValue('name', invalidName, true); + tick(); + formHelper.expectError('name', 'pattern'); + } + })); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.ts index 0e607e9a02b..7c607fa33d8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-form/cephfs-form.component.ts @@ -80,7 +80,7 @@ export class CephfsVolumeFormComponent extends CdForm implements OnInit { }); this.form = this.formBuilder.group({ name: new FormControl('', { - validators: [Validators.pattern(/^[.A-Za-z0-9_-]+$/), Validators.required] + validators: [Validators.pattern(/^[a-zA-Z][.A-Za-z0-9_-]+$/), Validators.required] }), placement: ['hosts'], hosts: [[]], -- 2.39.5