|
1 | 1 | /* eslint-disable no-undef, no-unused-vars */
|
2 | 2 |
|
3 | 3 | import * as React from 'react';
|
| 4 | +import * as _ from 'lodash-es'; |
4 | 5 | import { shallow } from 'enzyme';
|
5 | 6 |
|
6 |
| -import { requireOperatorGroup, NoOperatorGroupMsg } from '../../../public/components/operator-lifecycle-manager/operator-group'; |
7 |
| -import { testOperatorGroup } from '../../../__mocks__/k8sResourcesMocks'; |
| 7 | +import { requireOperatorGroup, NoOperatorGroupMsg, supports, InstallModeSet, InstallModeType, installedFor } from '../../../public/components/operator-lifecycle-manager/operator-group'; |
| 8 | +import { OperatorGroupKind, SubscriptionKind } from '../../../public/components/operator-lifecycle-manager'; |
| 9 | +import { testOperatorGroup, testSubscription } from '../../../__mocks__/k8sResourcesMocks'; |
8 | 10 |
|
9 | 11 | describe('requireOperatorGroup', () => {
|
10 | 12 | const SomeComponent = () => <div>Requires OperatorGroup</div>;
|
@@ -33,3 +35,111 @@ describe('requireOperatorGroup', () => {
|
33 | 35 | expect(wrapper.find(NoOperatorGroupMsg).exists()).toBe(false);
|
34 | 36 | });
|
35 | 37 | });
|
| 38 | + |
| 39 | +describe('installedFor', () => { |
| 40 | + const pkgName = testSubscription.spec.name; |
| 41 | + const ns = testSubscription.metadata.namespace; |
| 42 | + let subscriptions: SubscriptionKind[]; |
| 43 | + let operatorGroups: OperatorGroupKind[]; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + subscriptions = []; |
| 47 | + operatorGroups = []; |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns false if no `Subscriptions` exist for the given package', () => { |
| 51 | + subscriptions = [testSubscription]; |
| 52 | + operatorGroups = [{...testOperatorGroup, status: {namespaces: [ns], lastUpdated: null}}]; |
| 53 | + |
| 54 | + expect(installedFor(subscriptions)(operatorGroups)('new-operator')(ns)).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns false if no `OperatorGroups` target the given namespace', () => { |
| 58 | + subscriptions = [testSubscription]; |
| 59 | + operatorGroups = [{...testOperatorGroup, status: {namespaces: ['prod-a', 'prod-b'], lastUpdated: null}}]; |
| 60 | + |
| 61 | + expect(installedFor(subscriptions)(operatorGroups)(pkgName)(ns)).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns false if checking for `all-namespaces`', () => { |
| 65 | + subscriptions = [testSubscription]; |
| 66 | + operatorGroups = [{...testOperatorGroup, status: {namespaces: [ns], lastUpdated: null}}]; |
| 67 | + |
| 68 | + expect(installedFor(subscriptions)(operatorGroups)(pkgName)('')).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns true if `Subscription` exists in the "global" `OperatorGroup`', () => { |
| 72 | + subscriptions = [testSubscription]; |
| 73 | + operatorGroups = [{...testOperatorGroup, status: {namespaces: [''], lastUpdated: null}}]; |
| 74 | + |
| 75 | + expect(installedFor(subscriptions)(operatorGroups)(pkgName)(ns)).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns true if `Subscription` exists in an `OperatorGroup` that targets given namespace', () => { |
| 79 | + subscriptions = [testSubscription]; |
| 80 | + operatorGroups = [{...testOperatorGroup, status: {namespaces: [ns], lastUpdated: null}}]; |
| 81 | + |
| 82 | + expect(installedFor(subscriptions)(operatorGroups)(pkgName)(ns)).toBe(true); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('supports', () => { |
| 87 | + let set: InstallModeSet; |
| 88 | + let ownNamespaceGroup: OperatorGroupKind; |
| 89 | + let singleNamespaceGroup: OperatorGroupKind; |
| 90 | + let multiNamespaceGroup: OperatorGroupKind; |
| 91 | + let allNamespacesGroup: OperatorGroupKind; |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + ownNamespaceGroup = _.cloneDeep(testOperatorGroup); |
| 95 | + ownNamespaceGroup.status = {namespaces: [ownNamespaceGroup.metadata.namespace], lastUpdated: null}; |
| 96 | + singleNamespaceGroup = _.cloneDeep(testOperatorGroup); |
| 97 | + singleNamespaceGroup.status = {namespaces: ['test-ns'], lastUpdated: null}; |
| 98 | + multiNamespaceGroup = _.cloneDeep(testOperatorGroup); |
| 99 | + multiNamespaceGroup.status = {namespaces: ['test-ns', 'default'], lastUpdated: null}; |
| 100 | + allNamespacesGroup = _.cloneDeep(testOperatorGroup); |
| 101 | + allNamespacesGroup.status = {namespaces: [''], lastUpdated: null}; |
| 102 | + }); |
| 103 | + |
| 104 | + it('correctly returns for an Operator that can only run in its own namespace', () => { |
| 105 | + set = [ |
| 106 | + {type: InstallModeType.InstallModeTypeOwnNamespace, supported: true}, |
| 107 | + {type: InstallModeType.InstallModeTypeSingleNamespace, supported: true}, |
| 108 | + {type: InstallModeType.InstallModeTypeMultiNamespace, supported: false}, |
| 109 | + {type: InstallModeType.InstallModeTypeAllNamespaces, supported: false}, |
| 110 | + ]; |
| 111 | + |
| 112 | + expect(supports(set)(ownNamespaceGroup)).toBe(true); |
| 113 | + expect(supports(set)(singleNamespaceGroup)).toBe(true); |
| 114 | + expect(supports(set)(multiNamespaceGroup)).toBe(false); |
| 115 | + expect(supports(set)(allNamespacesGroup)).toBe(false); |
| 116 | + }); |
| 117 | + |
| 118 | + it('correctly returns for an Operator which can run in several namespaces', () => { |
| 119 | + set = [ |
| 120 | + {type: InstallModeType.InstallModeTypeOwnNamespace, supported: true}, |
| 121 | + {type: InstallModeType.InstallModeTypeSingleNamespace, supported: true}, |
| 122 | + {type: InstallModeType.InstallModeTypeMultiNamespace, supported: true}, |
| 123 | + {type: InstallModeType.InstallModeTypeAllNamespaces, supported: false}, |
| 124 | + ]; |
| 125 | + |
| 126 | + expect(supports(set)(ownNamespaceGroup)).toBe(true); |
| 127 | + expect(supports(set)(singleNamespaceGroup)).toBe(true); |
| 128 | + expect(supports(set)(multiNamespaceGroup)).toBe(true); |
| 129 | + expect(supports(set)(allNamespacesGroup)).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + it('correctly returns for an Operator which can only run in all namespaces', () => { |
| 133 | + set = [ |
| 134 | + {type: InstallModeType.InstallModeTypeOwnNamespace, supported: true}, |
| 135 | + {type: InstallModeType.InstallModeTypeSingleNamespace, supported: false}, |
| 136 | + {type: InstallModeType.InstallModeTypeMultiNamespace, supported: false}, |
| 137 | + {type: InstallModeType.InstallModeTypeAllNamespaces, supported: true}, |
| 138 | + ]; |
| 139 | + |
| 140 | + expect(supports(set)(ownNamespaceGroup)).toBe(false); |
| 141 | + expect(supports(set)(singleNamespaceGroup)).toBe(false); |
| 142 | + expect(supports(set)(multiNamespaceGroup)).toBe(false); |
| 143 | + expect(supports(set)(allNamespacesGroup)).toBe(true); |
| 144 | + }); |
| 145 | +}); |
0 commit comments