21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / workbench2 / src / views / instance-types-panel / instance-types-panel.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { configure, mount } from "enzyme";
7 import { InstanceTypesPanel, calculateKeepBufferOverhead, discountRamByPercent } from './instance-types-panel';
8 import Adapter from "enzyme-adapter-react-16";
9 import { combineReducers, createStore } from "redux";
10 import { Provider } from "react-redux";
11 import { formatFileSize, formatCWLResourceSize } from 'common/formatters';
12
13 configure({ adapter: new Adapter() });
14
15 describe('<InstanceTypesPanel />', () => {
16
17     // let props;
18     let store;
19
20     const initialAuthState = {
21         config: {
22             clusterConfig: {
23                 InstanceTypes: {
24                     "normalType" : {
25                         ProviderType: "provider",
26                         Price: 0.123,
27                         VCPUs: 6,
28                         Preemptible: false,
29                         IncludedScratch: 1000,
30                         RAM: 5000,
31                     },
32                     "gpuType" : {
33                         ProviderType: "gpuProvider",
34                         Price: 0.456,
35                         VCPUs: 8,
36                         Preemptible: true,
37                         IncludedScratch: 500,
38                         RAM: 6000,
39                         CUDA: {
40                             DeviceCount: 1,
41                             HardwareCapability: '8.6',
42                             DriverVersion: '11.4',
43                         },
44                     },
45                 },
46                 Containers: {
47                     ReserveExtraRAM: 1000,
48                 }
49             }
50         }
51     }
52
53     beforeEach(() => {
54
55         store = createStore(combineReducers({
56             auth: (state: any = initialAuthState, action: any) => state,
57         }));
58     });
59
60     it('renders instance types', () => {
61         // when
62         const panel = mount(
63             <Provider store={store}>
64                 <InstanceTypesPanel />
65             </Provider>);
66
67         // then
68         Object.keys(initialAuthState.config.clusterConfig.InstanceTypes).forEach((instanceKey) => {
69             const instanceType = initialAuthState.config.clusterConfig.InstanceTypes[instanceKey];
70             const item = panel.find(`Grid[data-cy="${instanceKey}"]`)
71
72             expect(item.find('h6').text()).toContain(instanceKey);
73             expect(item.text()).toContain(`Provider type${instanceType.ProviderType}`);
74             expect(item.text()).toContain(`Price$${instanceType.Price}`);
75             expect(item.text()).toContain(`Cores${instanceType.VCPUs}`);
76             expect(item.text()).toContain(`Preemptible${instanceType.Preemptible.toString()}`);
77             expect(item.text()).toContain(`Max disk request${formatCWLResourceSize(instanceType.IncludedScratch)} (${formatFileSize(instanceType.IncludedScratch)})`);
78             if (instanceType.CUDA && instanceType.CUDA.DeviceCount > 0) {
79                 expect(item.text()).toContain(`CUDA GPUs${instanceType.CUDA.DeviceCount}`);
80                 expect(item.text()).toContain(`Hardware capability${instanceType.CUDA.HardwareCapability}`);
81                 expect(item.text()).toContain(`Driver version${instanceType.CUDA.DriverVersion}`);
82             }
83         });
84     });
85 });
86
87 describe('calculateKeepBufferOverhead', () => {
88     it('should calculate correct buffer size', () => {
89         const testCases = [
90             {input: 0, output: (220<<20)},
91             {input: 1, output: (220<<20) + ((1<<26) * (11/10))},
92             {input: 2, output: (220<<20) + 2*((1<<26) * (11/10))},
93         ];
94
95         for (const {input, output} of testCases) {
96             expect(calculateKeepBufferOverhead(input)).toBe(output);
97         }
98     });
99 });
100
101 describe('discountRamByPercent', () => {
102     it('should inflate ram requirement by 5% of final amount', () => {
103         const testCases = [
104             {input: 0, output: 0},
105             {input: 114, output: 120},
106         ];
107
108         for (const {input, output} of testCases) {
109             expect(discountRamByPercent(input)).toBe(output);
110         }
111     });
112 });