summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/crush-rule-form-modal/crush-rule-form-modal.component.spec.ts
blob: 2b8c9e5cfe5e76c16cdac41ab16a486463977e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr';
import { of } from 'rxjs';

import { CrushRuleService } from '~/app/shared/api/crush-rule.service';
import { CrushNode } from '~/app/shared/models/crush-node';
import { CrushRuleConfig } from '~/app/shared/models/crush-rule';
import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
import { configureTestBed, FixtureHelper, FormHelper, Mocks } from '~/testing/unit-test-helper';
import { PoolModule } from '../pool.module';
import { CrushRuleFormModalComponent } from './crush-rule-form-modal.component';

describe('CrushRuleFormComponent', () => {
  let component: CrushRuleFormModalComponent;
  let crushRuleService: CrushRuleService;
  let fixture: ComponentFixture<CrushRuleFormModalComponent>;
  let formHelper: FormHelper;
  let fixtureHelper: FixtureHelper;
  let data: { names: string[]; nodes: CrushNode[] };

  // Object contains functions to get something
  const get = {
    nodeByName: (name: string): CrushNode => data.nodes.find((node) => node.name === name),
    nodesByNames: (names: string[]): CrushNode[] => names.map(get.nodeByName)
  };

  // Expects that are used frequently
  const assert = {
    failureDomains: (nodes: CrushNode[], types: string[]) => {
      const expectation = {};
      types.forEach((type) => (expectation[type] = nodes.filter((node) => node.type === type)));
      const keys = component.failureDomainKeys;
      expect(keys).toEqual(types);
      keys.forEach((key) => {
        expect(component.failureDomains[key].length).toBe(expectation[key].length);
      });
    },
    formFieldValues: (root: CrushNode, failureDomain: string, device: string) => {
      expect(component.form.value).toEqual({
        name: '',
        root,
        failure_domain: failureDomain,
        device_class: device
      });
    },
    valuesOnRootChange: (
      rootName: string,
      expectedFailureDomain: string,
      expectedDevice: string
    ) => {
      const node = get.nodeByName(rootName);
      formHelper.setValue('root', node);
      assert.formFieldValues(node, expectedFailureDomain, expectedDevice);
    },
    creation: (rule: CrushRuleConfig) => {
      formHelper.setValue('name', rule.name);
      fixture.detectChanges();
      component.onSubmit();
      expect(crushRuleService.create).toHaveBeenCalledWith(rule);
    }
  };

  configureTestBed({
    imports: [HttpClientTestingModule, RouterTestingModule, ToastrModule.forRoot(), PoolModule],
    providers: [CrushRuleService, NgbActiveModal]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(CrushRuleFormModalComponent);
    fixtureHelper = new FixtureHelper(fixture);
    component = fixture.componentInstance;
    formHelper = new FormHelper(component.form);
    crushRuleService = TestBed.inject(CrushRuleService);
    data = {
      names: ['rule1', 'rule2'],
      /**
       * Create the following test crush map:
       * > default
       * --> ssd-host
       * ----> 3x osd with ssd
       * --> mix-host
       * ----> hdd-rack
       * ------> 2x osd-rack with hdd
       * ----> ssd-rack
       * ------> 2x osd-rack with ssd
       */
      nodes: Mocks.getCrushMap()
    };
    spyOn(crushRuleService, 'getInfo').and.callFake(() => of(data));
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('calls listing to get rules on ngInit', () => {
    expect(crushRuleService.getInfo).toHaveBeenCalled();
    expect(component.names.length).toBe(2);
    expect(component.buckets.length).toBe(5);
  });

  describe('lists', () => {
    afterEach(() => {
      // The available buckets should not change
      expect(component.buckets).toEqual(
        get.nodesByNames(['default', 'hdd-rack', 'mix-host', 'ssd-host', 'ssd-rack'])
      );
    });

    it('has the following lists after init', () => {
      assert.failureDomains(data.nodes, ['host', 'osd', 'osd-rack', 'rack']); // Not root as root only exist once
      expect(component.devices).toEqual(['hdd', 'ssd']);
    });

    it('has the following lists after selection of ssd-host', () => {
      formHelper.setValue('root', get.nodeByName('ssd-host'));
      assert.failureDomains(get.nodesByNames(['osd.0', 'osd.1', 'osd.2']), ['osd']); // Not host as it only exist once
      expect(component.devices).toEqual(['ssd']);
    });

    it('has the following lists after selection of mix-host', () => {
      formHelper.setValue('root', get.nodeByName('mix-host'));
      expect(component.devices).toEqual(['hdd', 'ssd']);
      assert.failureDomains(
        get.nodesByNames(['hdd-rack', 'ssd-rack', 'osd2.0', 'osd2.1', 'osd2.0', 'osd2.1']),
        ['osd-rack', 'rack']
      );
    });
  });

  describe('selection', () => {
    it('selects the first root after init automatically', () => {
      assert.formFieldValues(get.nodeByName('default'), 'osd-rack', '');
    });

    it('should select all values automatically by selecting "ssd-host" as root', () => {
      assert.valuesOnRootChange('ssd-host', 'osd', 'ssd');
    });

    it('selects automatically the most common failure domain', () => {
      // Select mix-host as mix-host has multiple failure domains (osd-rack and rack)
      assert.valuesOnRootChange('mix-host', 'osd-rack', '');
    });

    it('should override automatic selections', () => {
      assert.formFieldValues(get.nodeByName('default'), 'osd-rack', '');
      assert.valuesOnRootChange('ssd-host', 'osd', 'ssd');
      assert.valuesOnRootChange('mix-host', 'osd-rack', '');
    });

    it('should not override manual selections if possible', () => {
      formHelper.setValue('failure_domain', 'rack', true);
      formHelper.setValue('device_class', 'ssd', true);
      assert.valuesOnRootChange('mix-host', 'rack', 'ssd');
    });

    it('should preselect device by domain selection', () => {
      formHelper.setValue('failure_domain', 'osd', true);
      assert.formFieldValues(get.nodeByName('default'), 'osd', 'ssd');
    });
  });

  describe('form validation', () => {
    it(`isn't valid if name is not set`, () => {
      expect(component.form.invalid).toBeTruthy();
      formHelper.setValue('name', 'someProfileName');
      expect(component.form.valid).toBeTruthy();
    });

    it('sets name invalid', () => {
      component.names = ['awesomeProfileName'];
      formHelper.expectErrorChange('name', 'awesomeProfileName', 'uniqueName');
      formHelper.expectErrorChange('name', 'some invalid text', 'pattern');
      formHelper.expectErrorChange('name', null, 'required');
    });

    it(`should show all default form controls`, () => {
      // name
      // root (preselected(first root))
      // failure_domain (preselected=type that is most common)
      // device_class (preselected=any if multiple or some type if only one device type)
      fixtureHelper.expectIdElementsVisible(
        ['name', 'root', 'failure_domain', 'device_class'],
        true
      );
    });
  });

  describe('submission', () => {
    beforeEach(() => {
      const taskWrapper = TestBed.inject(TaskWrapperService);
      spyOn(taskWrapper, 'wrapTaskAroundCall').and.callThrough();
      spyOn(crushRuleService, 'create').and.stub();
    });

    it('creates a rule with only required fields', () => {
      assert.creation(Mocks.getCrushRuleConfig('default-rule', 'default', 'osd-rack'));
    });

    it('creates a rule with all fields', () => {
      assert.valuesOnRootChange('ssd-host', 'osd', 'ssd');
      assert.creation(Mocks.getCrushRuleConfig('ssd-host-rule', 'ssd-host', 'osd', 'ssd'));
    });
  });
});