-import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
+import { RgwMultisiteZoneFormComponent } from './rgw-multisite-zone-form.component'; // Adjust path as necessary
+import { of } from 'rxjs';
+import { RgwZoneService } from '~/app/shared/api/rgw-zone.service';
+import { HttpClientTestingModule } from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr';
import { SharedModule } from '~/app/shared/shared.module';
-
-import { RgwMultisiteZoneFormComponent } from './rgw-multisite-zone-form.component';
-import { configureTestBed } from '~/testing/unit-test-helper';
+import { RgwZone } from '../models/rgw-multisite';
describe('RgwMultisiteZoneFormComponent', () => {
let component: RgwMultisiteZoneFormComponent;
let fixture: ComponentFixture<RgwMultisiteZoneFormComponent>;
-
- configureTestBed({
- imports: [
- SharedModule,
- ReactiveFormsModule,
- RouterTestingModule,
- HttpClientTestingModule,
- ToastrModule.forRoot()
- ],
- providers: [NgbActiveModal],
- declarations: [RgwMultisiteZoneFormComponent]
- });
+ let rgwZoneService: RgwZoneService;
+ let rgwZoneServiceSpy: jasmine.Spy;
beforeEach(() => {
+ TestBed.configureTestingModule({
+ imports: [
+ SharedModule,
+ ReactiveFormsModule,
+ RouterTestingModule,
+ HttpClientTestingModule,
+ ToastrModule.forRoot()
+ ],
+ providers: [NgbActiveModal],
+ declarations: [RgwMultisiteZoneFormComponent]
+ }).compileComponents();
+
fixture = TestBed.createComponent(RgwMultisiteZoneFormComponent);
component = fixture.componentInstance;
+ rgwZoneService = TestBed.inject(RgwZoneService);
+
+ rgwZoneServiceSpy = spyOn(rgwZoneService, 'get');
+
+ rgwZoneServiceSpy.and.returnValue(
+ of({
+ placement_pools: [
+ {
+ key: 'default-placement',
+ val: {
+ storage_classes: {
+ STANDARD: {
+ data_pool: 'standard-data-pool',
+ compression_type: 'gzip'
+ }
+ },
+ index_pool: 'index-pool',
+ data_extra_pool: 'extra-data-pool'
+ }
+ }
+ ]
+ })
+ );
+
+ component.info = {
+ parent: {
+ data: {
+ name: 'zonegroup2',
+ placement_targets: [
+ { name: 'default-placement', tags: [], storage_classes: ['STANDARD'] }
+ ],
+ default_placement: 'default-placement'
+ }
+ },
+ data: {
+ name: 'zone2',
+ parent: 'zonegroup2',
+ is_default: true,
+ is_master: true,
+ endpoints: ['http://192.168.100.100:80'],
+ access_key: 'zxcftyuuhgg',
+ secret_key: 'Qwsdcfgghuiioklpoozsd'
+ }
+ };
+
+ component.zone = new RgwZone();
+ component.zone.name = component.info.data.name;
+ component.action = 'edit';
+
fixture.detectChanges();
+
+ component.getZonePlacementData('default-placement');
});
it('should create', () => {
expect(component).toBeTruthy();
});
+
+ it('should set correct values in the form on edit', () => {
+ expect(component.multisiteZoneForm.get('zoneName')?.value).toBe('zone2');
+ expect(component.multisiteZoneForm.get('selectedZonegroup')?.value).toBe('zonegroup2');
+ expect(component.multisiteZoneForm.get('default_zone')?.value).toBe(true);
+ expect(component.multisiteZoneForm.get('master_zone')?.value).toBe(true);
+ expect(component.multisiteZoneForm.get('zone_endpoints')?.value).toBe(
+ 'http://192.168.100.100:80'
+ );
+ expect(component.multisiteZoneForm.get('access_key')?.value).toBe('zxcftyuuhgg');
+ expect(component.multisiteZoneForm.get('secret_key')?.value).toBe('Qwsdcfgghuiioklpoozsd');
+ expect(component.multisiteZoneForm.get('placementTarget')?.value).toBe('default-placement');
+ expect(component.multisiteZoneForm.get('storageClass')?.value).toBe('STANDARD');
+ expect(component.multisiteZoneForm.get('storageDataPool')?.value).toBe('standard-data-pool');
+ expect(component.multisiteZoneForm.get('storageCompression')?.value).toBe('gzip');
+ });
+
+ it('should create a new zone', () => {
+ component.action = 'create';
+ const createSpy = spyOn(rgwZoneService, 'create').and.returnValue(of({}));
+ component.submit();
+ expect(createSpy).toHaveBeenCalledWith(
+ {
+ endpoints: 'http://192.168.100.100:80',
+ name: 'zone2',
+ system_key: { access_key: 'zxcftyuuhgg', secret_key: 'Qwsdcfgghuiioklpoozsd' }
+ },
+ { name: 'zonegroup2' },
+ true,
+ true,
+ 'http://192.168.100.100:80'
+ );
+ });
});
getZonePlacementData(placementTarget: string) {
this.zone = new RgwZone();
this.zone.name = this.info.data.name;
- if (this.placementTargets) {
- this.placementTargets.forEach((placement: any) => {
- if (placement.name === placementTarget) {
- let storageClasses = placement.storage_classes;
- this.storageClassList = Object.entries(storageClasses).map(([key, value]) => ({
- key,
- value
- }));
- }
- });
- }
this.rgwZoneService.get(this.zone).subscribe((zoneInfo: RgwZone) => {
this.zoneInfo = zoneInfo;
if (this.zoneInfo && this.zoneInfo['placement_pools']) {
+ const placementPoolKeys = this.zoneInfo['placement_pools'].map((plc_pool) => plc_pool.key);
+ this.placementTargets = this.placementTargets.filter((placement: { name: string }) =>
+ placementPoolKeys.includes(placement.name)
+ );
this.zoneInfo['placement_pools'].forEach((plc_pool) => {
if (plc_pool.key === placementTarget) {
let storageClasses = plc_pool.val.storage_classes;
+ this.storageClassList = Object.entries(storageClasses).map(([key, value]) => ({
+ key,
+ value
+ }));
let placementDataPool = storageClasses['STANDARD']
? storageClasses['STANDARD']['data_pool']
: '';
this.poolList.push({ poolname: placementDataPool });
this.poolList.push({ poolname: placementIndexPool });
this.poolList.push({ poolname: placementDataExtraPool });
- this.multisiteZoneForm.get('storageClass').setValue(this.storageClassList[0]['value']);
- this.multisiteZoneForm.get('storageDataPool').setValue(placementDataPool);
- this.multisiteZoneForm.get('storageCompression').setValue(this.compressionTypes[0]);
+ this.multisiteZoneForm.get('storageClass').setValue(this.storageClassList[0]['key']);
+ this.getStorageClassData(this.storageClassList[0]['key']);
this.multisiteZoneForm.get('placementDataPool').setValue(placementDataPool);
this.multisiteZoneForm.get('placementIndexPool').setValue(placementIndexPool);
this.multisiteZoneForm.get('placementDataExtraPool').setValue(placementDataExtraPool);
}
getStorageClassData(storageClass: string) {
- let storageClassSelected = this.storageClassList.find((x) => x['value'] == storageClass)[
- 'value'
- ];
- this.poolList.push({ poolname: storageClassSelected.data_pool });
- this.multisiteZoneForm.get('storageDataPool').setValue(storageClassSelected.data_pool);
+ let storageClassSelected = this.storageClassList.find((sc) => sc['key'] === storageClass);
+ this.poolList.push({ poolname: storageClassSelected['value']['data_pool'] });
+ this.multisiteZoneForm
+ .get('storageDataPool')
+ .setValue(storageClassSelected['value']['data_pool']);
this.multisiteZoneForm
.get('storageCompression')
- .setValue(storageClassSelected.compression_type);
+ .setValue(storageClassSelected['value']['compression_type']);
}
submit() {