/* * @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 * as AWS from 'aws-sdk'; import { expect } from 'chai'; import 'mocha'; import * as sinon from 'sinon'; import * as ec2Calls from '../../src/aws/ec2-calls'; describe('ec2 calls module', () => { let sandbox: sinon.SinonSandbox; beforeEach(async () => { sandbox = sinon.sandbox.create(); }); afterEach(() => { sandbox.restore(); }); describe('getSecurityGroupsByNameTag', () => { let describeStub: sinon.SinonStub; beforeEach(() => { describeStub = sandbox.stub(); sandbox.stub(AWS, 'EC2').returns({ describeSecurityGroups: () => ({ promise: describeStub }) }); }); it('should return the list of security groups with the given name tag', async () => { describeStub.resolves({ SecurityGroups: [] }); const securityGroups = await ec2Calls.getSecurityGroupsByNameTag('FakeTag', 'FakeVpc'); expect(describeStub.callCount).to.equal(1); expect(securityGroups).to.deep.equal([]); }); it('should return an empty list when no security groups with the tag exist', async () => { const groupId = 'FakeId'; describeStub.resolves({ SecurityGroups: [{ GroupId: groupId }] }); const securityGroups = await ec2Calls.getSecurityGroupsByNameTag('FakeTag', 'FakeVpc'); expect(describeStub.callCount).to.equal(1); expect(securityGroups.length).to.equal(1); expect(securityGroups[0].GroupId).to.equal(groupId); }); }); describe('ingressRuleExists', () => { const port = 3306; const protocol = 'tcp'; const sourceSg: AWS.EC2.SecurityGroup = { GroupId: 'SourceId' }; it('should return the list of security groups with the given name tag', () => { const sg: AWS.EC2.SecurityGroup = { GroupId: 'DestId', IpPermissions: [ { FromPort: port, ToPort: port, IpProtocol: protocol, UserIdGroupPairs: [ { GroupId: sourceSg.GroupId } ] } ] }; const ruleExists = ec2Calls.ingressRuleExists(sg, port, port, protocol, sourceSg); expect(ruleExists).to.equal(true); }); it('should return an empty list when no security groups with the tag exist', async () => { const sg: AWS.EC2.SecurityGroup = { GroupId: 'DestId', IpPermissions: [] }; const ruleExists = ec2Calls.ingressRuleExists(sg, port, port, protocol, sourceSg); expect(ruleExists).to.equal(false); }); }); describe('addIngressRuleToSecurityGroup', () => { let describeStub: sinon.SinonStub; let authIngressStub: sinon.SinonStub; beforeEach(() => { describeStub = sandbox.stub(); authIngressStub = sandbox.stub(); sandbox.stub(AWS, 'EC2').returns({ describeSecurityGroups: () => ({ promise: describeStub }), authorizeSecurityGroupIngress: () => ({ promise: authIngressStub }) }); }); it('should add the rule and return the security group', async () => { authIngressStub.resolves({}); describeStub.resolves({ SecurityGroups: [{}] }); const securityGroup = await ec2Calls.addIngressRuleToSecurityGroup({}, {}, 'tcp', 3306, 3306, 'FakeVpc'); expect(authIngressStub.callCount).to.equal(1); expect(describeStub.callCount).to.equal(1); expect(securityGroup).to.deep.equal({}); }); it('should throw an error if it cant find the security group after adding the rule', async () => { authIngressStub.resolves({}); describeStub.resolves({ SecurityGroups: [] }); try { await ec2Calls.addIngressRuleToSecurityGroup({}, {}, 'tcp', 3306, 3306, 'FakeVpc'); expect(true).to.equal(false); // Should not get here } catch (err) { expect(authIngressStub.callCount).to.equal(1); expect(describeStub.callCount).to.equal(1); } }); }); });