|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | + |
| 3 | +import {ApiClient, Cluster} from ".."; |
| 4 | +import chai, {assert} from "chai"; |
| 5 | +import {mock, when, instance, deepEqual, verify, anything} from "ts-mockito"; |
| 6 | +import chaiAsPromised from "chai-as-promised"; |
| 7 | +import Time, {TimeUnits} from "../retries/Time"; |
| 8 | +import getMockTestCluster from "../test/fixtures/ClusterFixtures"; |
| 9 | +import {GetClusterResponse} from "../apis/cluster"; |
| 10 | +import TokenFixture from "../test/fixtures/TokenFixture"; |
| 11 | +import {RetryConfigs} from "../retries/retries"; |
| 12 | + |
| 13 | +chai.use(chaiAsPromised); |
| 14 | + |
| 15 | +describe(__filename, function () { |
| 16 | + this.timeout(new Time(10, TimeUnits.minutes).toMillSeconds().value); |
| 17 | + |
| 18 | + let mockedClient: ApiClient; |
| 19 | + let mockedCluster: Cluster; |
| 20 | + let testClusterDetails: GetClusterResponse; |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + ({mockedCluster, mockedClient, testClusterDetails} = |
| 24 | + await getMockTestCluster()); |
| 25 | + |
| 26 | + RetryConfigs.waitTime = (attempt) => { |
| 27 | + return new Time(0, TimeUnits.milliseconds); |
| 28 | + }; |
| 29 | + }); |
| 30 | + |
| 31 | + it("calling start on a non terminated state should not throw an error", async () => { |
| 32 | + when( |
| 33 | + mockedClient.request( |
| 34 | + "/api/2.0/clusters/get", |
| 35 | + "GET", |
| 36 | + deepEqual({ |
| 37 | + cluster_id: testClusterDetails.cluster_id, |
| 38 | + }) |
| 39 | + ) |
| 40 | + ).thenResolve( |
| 41 | + { |
| 42 | + ...testClusterDetails, |
| 43 | + state: "PENDING", |
| 44 | + }, |
| 45 | + { |
| 46 | + ...testClusterDetails, |
| 47 | + state: "PENDING", |
| 48 | + }, |
| 49 | + { |
| 50 | + ...testClusterDetails, |
| 51 | + state: "PENDING", |
| 52 | + }, |
| 53 | + { |
| 54 | + ...testClusterDetails, |
| 55 | + state: "PENDING", |
| 56 | + }, |
| 57 | + { |
| 58 | + ...testClusterDetails, |
| 59 | + state: "PENDING", |
| 60 | + }, |
| 61 | + { |
| 62 | + ...testClusterDetails, |
| 63 | + state: "RUNNING", |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + await mockedCluster.refresh(); |
| 68 | + assert.notEqual(mockedCluster.state, "RUNNING"); |
| 69 | + |
| 70 | + await mockedCluster.start(); |
| 71 | + assert.equal(mockedCluster.state, "RUNNING"); |
| 72 | + |
| 73 | + verify( |
| 74 | + mockedClient.request( |
| 75 | + "/api/2.0/clusters/get", |
| 76 | + anything(), |
| 77 | + anything() |
| 78 | + ) |
| 79 | + ).times(6); |
| 80 | + |
| 81 | + verify( |
| 82 | + mockedClient.request( |
| 83 | + "/api/2.0/clusters/start", |
| 84 | + anything(), |
| 85 | + anything() |
| 86 | + ) |
| 87 | + ).never(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should terminate cluster", async () => { |
| 91 | + when( |
| 92 | + mockedClient.request( |
| 93 | + "/api/2.0/clusters/get", |
| 94 | + "GET", |
| 95 | + deepEqual({ |
| 96 | + cluster_id: testClusterDetails.cluster_id, |
| 97 | + }) |
| 98 | + ) |
| 99 | + ).thenResolve( |
| 100 | + { |
| 101 | + ...testClusterDetails, |
| 102 | + state: "RUNNING", |
| 103 | + }, |
| 104 | + { |
| 105 | + ...testClusterDetails, |
| 106 | + state: "TERMINATING", |
| 107 | + }, |
| 108 | + { |
| 109 | + ...testClusterDetails, |
| 110 | + state: "TERMINATED", |
| 111 | + } |
| 112 | + ); |
| 113 | + |
| 114 | + when( |
| 115 | + mockedClient.request( |
| 116 | + "/api/2.0/clusters/delete", |
| 117 | + "POST", |
| 118 | + deepEqual({ |
| 119 | + cluster_id: testClusterDetails.cluster_id, |
| 120 | + }) |
| 121 | + ) |
| 122 | + ).thenResolve({}); |
| 123 | + |
| 124 | + assert.equal(mockedCluster.state, "RUNNING"); |
| 125 | + |
| 126 | + await mockedCluster.stop(); |
| 127 | + assert.equal(mockedCluster.state, "TERMINATED"); |
| 128 | + |
| 129 | + verify( |
| 130 | + mockedClient.request( |
| 131 | + "/api/2.0/clusters/get", |
| 132 | + anything(), |
| 133 | + anything() |
| 134 | + ) |
| 135 | + ).times(3); |
| 136 | + |
| 137 | + verify( |
| 138 | + mockedClient.request( |
| 139 | + "/api/2.0/clusters/delete", |
| 140 | + anything(), |
| 141 | + anything() |
| 142 | + ) |
| 143 | + ).once(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should terminate non running clusters", async () => { |
| 147 | + when( |
| 148 | + mockedClient.request( |
| 149 | + "/api/2.0/clusters/get", |
| 150 | + "GET", |
| 151 | + deepEqual({ |
| 152 | + cluster_id: testClusterDetails.cluster_id, |
| 153 | + }) |
| 154 | + ) |
| 155 | + ).thenResolve( |
| 156 | + { |
| 157 | + ...testClusterDetails, |
| 158 | + state: "PENDING", |
| 159 | + }, |
| 160 | + { |
| 161 | + ...testClusterDetails, |
| 162 | + state: "TERMINATING", |
| 163 | + }, |
| 164 | + { |
| 165 | + ...testClusterDetails, |
| 166 | + state: "TERMINATED", |
| 167 | + } |
| 168 | + ); |
| 169 | + |
| 170 | + await mockedCluster.refresh(); |
| 171 | + assert.notEqual(mockedCluster.state, "RUNNING"); |
| 172 | + |
| 173 | + await mockedCluster.stop(); |
| 174 | + assert.equal(mockedCluster.state, "TERMINATED"); |
| 175 | + |
| 176 | + verify( |
| 177 | + mockedClient.request( |
| 178 | + "/api/2.0/clusters/get", |
| 179 | + anything(), |
| 180 | + anything() |
| 181 | + ) |
| 182 | + ).times(3); |
| 183 | + |
| 184 | + verify( |
| 185 | + mockedClient.request( |
| 186 | + "/api/2.0/clusters/delete", |
| 187 | + anything(), |
| 188 | + anything() |
| 189 | + ) |
| 190 | + ).once(); |
| 191 | + }); |
| 192 | + |
| 193 | + it("should cancel cluster start", async () => { |
| 194 | + const whenMockGetCluster = when( |
| 195 | + mockedClient.request( |
| 196 | + "/api/2.0/clusters/get", |
| 197 | + "GET", |
| 198 | + deepEqual({ |
| 199 | + cluster_id: testClusterDetails.cluster_id, |
| 200 | + }) |
| 201 | + ) |
| 202 | + ); |
| 203 | + |
| 204 | + whenMockGetCluster.thenResolve({ |
| 205 | + ...testClusterDetails, |
| 206 | + state: "PENDING", |
| 207 | + }); |
| 208 | + |
| 209 | + when( |
| 210 | + mockedClient.request( |
| 211 | + "/api/2.0/clusters/delete", |
| 212 | + "POST", |
| 213 | + deepEqual({ |
| 214 | + cluster_id: testClusterDetails.cluster_id, |
| 215 | + }) |
| 216 | + ) |
| 217 | + ).thenCall(() => { |
| 218 | + whenMockGetCluster.thenResolve( |
| 219 | + { |
| 220 | + ...testClusterDetails, |
| 221 | + state: "TERMINATING", |
| 222 | + }, |
| 223 | + { |
| 224 | + ...testClusterDetails, |
| 225 | + state: "TERMINATED", |
| 226 | + } |
| 227 | + ); |
| 228 | + return {}; |
| 229 | + }); |
| 230 | + |
| 231 | + const token = mock(TokenFixture); |
| 232 | + when(token.isCancellationRequested).thenReturn(false, false, true); |
| 233 | + //mocked cluster is initially in running state, this gets it to pending state |
| 234 | + await mockedCluster.refresh(); |
| 235 | + |
| 236 | + assert.equal(mockedCluster.state, "PENDING"); |
| 237 | + await mockedCluster.start(instance(token)); |
| 238 | + |
| 239 | + verify(token.isCancellationRequested).thrice(); |
| 240 | + assert.equal(mockedCluster.state, "TERMINATED"); |
| 241 | + }); |
| 242 | +}); |
0 commit comments