Skip to content

Commit

Permalink
instance pool tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkvanmeerten committed Apr 17, 2024
1 parent 7a00df0 commit 53a6745
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 49 deletions.
72 changes: 42 additions & 30 deletions src/oracle_instance_pool_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export default class OracleInstancePoolManager implements CloudInstanceManager {
this.launchInstances = this.launchInstances.bind(this);
}

setComputeManagementClient(client: core.ComputeManagementClient) {
this.computeManagementClient = client;
}

getComputeManagementClient() {
return this.computeManagementClient;
}

async detachInstance(ctx: Context, group: InstanceGroup, instance: string): Promise<void> {
ctx.logger.info(`[oraclepool] Detaching instance ${instance}`);
this.computeManagementClient.regionId = group.region;
Expand All @@ -67,6 +75,8 @@ export default class OracleInstancePoolManager implements CloudInstanceManager {
instancePoolId: group.instanceConfigurationId,
});

ctx.logger.debug(`[oraclepool] Instance Pool Details for group ${group.name}`, { poolDetails });

const poolInstances = await this.computeManagementClient.listInstancePoolInstances({
compartmentId: group.compartmentId,
instancePoolId: group.instanceConfigurationId,
Expand All @@ -92,14 +102,19 @@ export default class OracleInstancePoolManager implements CloudInstanceManager {
newSize,
});
}
const updateResult = await this.computeManagementClient.updateInstancePool({
instancePoolId: group.instanceConfigurationId,
updateInstancePoolDetails: {
size: newSize,
},
});

ctx.logger.info(`[oraclepool] Updated instance pool size for group ${group.name}`, { updateResult });
if (this.isDryRun) {
ctx.logger.info(`[oracle] Dry run enabled, instance pool size change skipped`, { newSize });
} else {
const updateResult = await this.computeManagementClient.updateInstancePool({
instancePoolId: group.instanceConfigurationId,
updateInstancePoolDetails: {
size: newSize,
},
});

ctx.logger.info(`[oraclepool] Updated instance pool size for group ${group.name}`, { updateResult });
}

this.workRequestClient.regionId = group.region;
const cwaiter = this.computeManagementClient.createWaiters(this.workRequestClient, waiterConfiguration);
Expand Down Expand Up @@ -140,29 +155,26 @@ export default class OracleInstancePoolManager implements CloudInstanceManager {
return result;
}

async getInstances(
ctx: Context,
group: InstanceGroup,
cloudRetryStrategy: CloudRetryStrategy,
): Promise<Array<CloudInstance>> {
const computeManagementClient = new core.ComputeManagementClient(
{
authenticationDetailsProvider: this.provider,
},
{
retryConfiguration: {
terminationStrategy: new common.MaxTimeTerminationStrategy(cloudRetryStrategy.maxTimeInSeconds),
delayStrategy: new common.ExponentialBackoffDelayStrategy(cloudRetryStrategy.maxDelayInSeconds),
retryCondition: (response) => {
return (
cloudRetryStrategy.retryableStatusCodes.filter((retryableStatusCode) => {
return response.statusCode === retryableStatusCode;
}).length > 0
);
},
},
},
);
async getInstances(ctx: Context, group: InstanceGroup, _: CloudRetryStrategy): Promise<Array<CloudInstance>> {
// const computeManagementClient = new core.ComputeManagementClient(
// {
// authenticationDetailsProvider: this.provider,
// },
// {
// retryConfiguration: {
// terminationStrategy: new common.MaxTimeTerminationStrategy(cloudRetryStrategy.maxTimeInSeconds),
// delayStrategy: new common.ExponentialBackoffDelayStrategy(cloudRetryStrategy.maxDelayInSeconds),
// retryCondition: (response) => {
// return (
// cloudRetryStrategy.retryableStatusCodes.filter((retryableStatusCode) => {
// return response.statusCode === retryableStatusCode;
// }).length > 0
// );
// },
// },
// },
// );
const computeManagementClient = this.computeManagementClient;
computeManagementClient.regionId = group.region;

const poolInstances = await computeManagementClient.listInstancePoolInstances({
Expand Down
145 changes: 126 additions & 19 deletions src/test/oracle_instance_pool_manager.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,158 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import core = require('oci-core');

import assert from 'node:assert';
import test, { afterEach, describe, mock } from 'node:test';

import OracleInstancePoolManager from '../oracle_instance_pool_manager';

function log(level, message, data) {
console.log(`${Date.now()} ${level}: ${message}`);
console.log(data);
console.log(`${new Date().toISOString()} ${level}: ${message}`);
if (data) {
console.log(JSON.stringify(data, null, 2));
}
}

const group = {
name: 'group',
provider: 'oraclepool',
region: 'testregion',
compartmentId: 'testcpt',
instanceConfigurationId: 'testpoolid',
};

describe('InstancePoolManager', () => {
const manager = new OracleInstancePoolManager({
isDryRun: true,
isDryRun: false,
ociConfigurationFilePath: process.env.OCI_CONFIGURATION_FILE_PATH,
ociConfigurationProfile: process.env.OCI_CONFIGURATION_PROFILE,
});

const mockWaiters = {
forInstancePool: mock.fn((request, _) => {
return {
instancePool: <core.models.InstancePool>{
id: request.instancePoolId,
name: group.name,
compartmentId: group.compartmentId,
instanceConfigurationId: 'testid',
size: 2,
},
};
}),
forDetachInstancePoolInstance: mock.fn((request) => {
return <core.responses.DetachInstancePoolInstanceResponse>{
instancePoolInstance: request.detachInstancePoolInstanceDetails,
workRequest: { id: 'testworkrequestid' },
};
}),
};

const mockComputeManagementClient = {
createWaiters: mock.fn(() => {
return mockWaiters;
}),
getInstancePool: mock.fn((request) => {
return {
instancePool: <core.models.InstancePool>{
id: request.instancePoolId,
name: group.name,
compartmentId: group.compartmentId,
instanceConfigurationId: 'testid',
size: 2,
},
};
}),
listInstancePoolInstances: mock.fn(() => {
return { items: [{ id: 'testinstanceid-1' }, { id: 'testinstanceid-2' }] };
}),
updateInstancePool: mock.fn((request) => {
return <core.models.InstancePool>{
id: request.instancePoolId,
name: group.name,
compartmentId: group.compartmentId,
instanceConfigurationId: 'testid',
size: request.size,
};
}),
};

manager.setComputeManagementClient(mockComputeManagementClient);

const context = {
logger: {
debug: mock.fn(log.bind('debug')),
info: mock.fn(log.bind('info')),
error: mock.fn(log.bind('error')),
debug: mock.fn((message, data) => {
log('DEBUG', message, data);
}),
info: mock.fn((message, data) => {
log('INFO', message, data);
}),
warn: mock.fn((message, data) => {
log('WARN', message, data);
}),
error: mock.fn((message, data) => {
log('ERROR', message, data);
}),
},
};

if (!process.env.OCI_CONFIGURATION_FILE_PATH || !process.env.OCI_CONFIGURATION_PROFILE) {
console.error('Please set OCI_CONFIGURATION_FILE_PATH and OCI_CONFIGURATION_PROFILE env variables');
process.exit(1);
}

if (!process.env.COMPARTMENT_OCID || !process.env.INSTANCE_POOL_ID || !process.env.REGION) {
console.error('Please set COMPARTMENT_OCID, INSTANCE_POOL_ID and REGION env variables');
process.exit(1);
}

afterEach(() => {
mock.restoreAll();
});

describe('getInstances', () => {
// This is a test for the getInstances method
test('will call the correct endpoint', async () => {
test('will list instances in a group', async () => {
console.log('Starting getInstances test');
const instances = await manager.getInstances(
context,
{
name: 'group',
region: process.env.REGION,
compartmentId: process.env.COMPARTMENT_OCID,
instanceConfigurationId: process.env.INSTANCE_POOL_ID,
},
{ maxAttempts: 1, maxTimeInSeconds: 60, maxDelayInSeconds: 30, retryableStatusCodes: [404, 429] },
);
console.log('ended getInstances test');
const instances = await manager.getInstances(context, group, {
maxAttempts: 1,
maxTimeInSeconds: 60,
maxDelayInSeconds: 30,
retryableStatusCodes: [404, 429],
});
log('TEST', 'ended getInstances test');
assert.ok(instances);
log('TEST', 'found instances', instances);
});
});

describe('launchInstances', () => {
// This is a test for the launchInstances method
test('will launch instances in a group', async () => {
console.log('Starting launchInstances test');
mockWaiters.forInstancePool.mock.mockImplementationOnce((_) => {
return {
instancePool: <core.models.InstancePool>{
id: group.instanceConfigurationId,
name: group.name,
compartmentId: group.compartmentId,
instanceConfigurationId: 'testid',
size: 3,
},
};
});
mockComputeManagementClient.listInstancePoolInstances.mock.mockImplementationOnce((_) => {
return { items: [{ id: 'testinstanceid-1' }, { id: 'testinstanceid-2' }, { id: 'new-instance-id' }] };
}, 2);
const instances = await manager.launchInstances(context, group, 2, 1);
console.log(mockComputeManagementClient.updateInstancePool.mock.callCount());
assert.equal(mockComputeManagementClient.updateInstancePool.mock.callCount(), 1);
assert.ok(instances);
console.log(instances);
assert.equal(instances[0], 'new-instance-id');
assert.equal(instances.length, 1);
log('TEST', 'ended launchInstances test');
log('TEST', 'launched instances', instances);
});
});
});

0 comments on commit 53a6745

Please sign in to comment.