/* * @license * Copyright 2018 Brigham Young University * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { expect } from 'chai'; import { ServiceContext, ServiceType } from 'handel-extension-api'; import 'mocha'; import * as sinon from 'sinon'; import * as s3Calls from '../../../src/aws/s3-calls'; import { SharedDbConfig } from '../../../src/services/shared-db/config-types'; import * as db from '../../../src/services/shared-db/db'; import * as migrations from '../../../src/services/shared-db/migrations'; import accountConfig from '../../fake-account-config'; describe('migrations module', () => { let sandbox: sinon.SinonSandbox; let serviceParams: SharedDbConfig; let serviceContext: ServiceContext; const appName = 'FakeApp'; const envName = 'FakeService'; beforeEach(async () => { sandbox = sinon.sandbox.create(); serviceParams = { type: 'shared-db', cluster_id: 'FakeClusterId', schema_migrations_dir: '.' }; serviceContext = new ServiceContext(appName, envName, 'FakeService', new ServiceType('byu', 'shared-db'), serviceParams, accountConfig); }); afterEach(() => { sandbox.restore(); }); describe('uploadMigrationsToS3', () => { it('should upload the migrations directory to s3', async () => { const s3Key = 'FakeKey'; const uploadDirStub = sandbox.stub(s3Calls, 'uploadDirectoryToS3').resolves({ Key: s3Key }); const retKey = await migrations.uploadMigrationsToS3('FakePath', serviceContext); expect(uploadDirStub.callCount).to.equal(1); expect(retKey).to.equal(s3Key); }); }); describe('runSchemaMigrations', () => { let invokeLambdaStub: sinon.SinonStub; beforeEach(() => { invokeLambdaStub = sandbox.stub(db, 'invokeSharedRdsLambda'); invokeLambdaStub.onCall(0).resolves({ status: 'success' }); }); it('should run the schema migrations and wait for them to finish', async () => { invokeLambdaStub.onCall(1).resolves({ status: 'success' }); await migrations.runSchemaMigrations('FakeCluster', 'FakeDb', 'FakePath'); expect(invokeLambdaStub.callCount).to.equal(2); }); it('should throw an error on migration failure', async () => { const errMsg = 'Some error'; invokeLambdaStub.onCall(1).resolves({ status: 'failure', message: errMsg }); try { await migrations.runSchemaMigrations('FakeCluster', 'FakeDb', 'FakePath'); } catch (err) { expect(err.message).to.include(errMsg); expect(invokeLambdaStub.callCount).to.equal(2); } }); it('should throw an error when an unknown status is returned from the lambda', async () => { const errMsg = 'Some error'; invokeLambdaStub.onCall(1).resolves({ status: 'unknownstatus', message: errMsg }); try { await migrations.runSchemaMigrations('FakeCluster', 'FakeDb', 'FakePath'); } catch (err) { expect(err.message).to.include('Unknown status returned'); expect(invokeLambdaStub.callCount).to.equal(2); } }); }); describe('deleteMigrationsFromS3', () => { it('should delete the migrations files in s3', async () => { const deleteFilesStub = sandbox.stub(s3Calls, 'deleteFilesByPrefix').resolves({}); await migrations.deleteMigrationsFromS3(serviceContext); expect(deleteFilesStub.callCount).to.equal(1); }); }); });