blob: 8b31783c6da7c52d185792699b7bc0925a09dfe1 [file] [log] [blame] [raw]
Matt Godboltf68198a2020-09-26 17:50:40 -05001// Copyright (c) 2017, Compiler Explorer Authors
Matt Godboltaa0b3e62017-12-10 15:52:52 -06002// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23// POSSIBILITY OF SUCH DAMAGE.
24
Austin Morton044dcfb2020-09-26 16:59:26 -040025import './utils';
26import AWS from 'aws-sdk-mock';
Matt Godboltaa0b3e62017-12-10 15:52:52 -060027
Austin Morton044dcfb2020-09-26 16:59:26 -040028import * as aws from '../lib/aws';
Matt Godboltaa0b3e62017-12-10 15:52:52 -060029
30const instanceA = {
31 State: {Name: 'running'},
32 Id: 'A',
33 Tags: [
34 {Key: 'Name', Value: 'Alice'},
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020035 {Key: 'Moose', Value: 'Bob'},
36 ],
Matt Godboltaa0b3e62017-12-10 15:52:52 -060037};
38const instanceB = {
39 State: {Name: 'sleeping'},
40 Id: 'B',
41 Tags: [
42 {Key: 'Name', Value: 'Alice'},
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020043 {Key: 'Moose', Value: 'Bob'},
44 ],
Matt Godboltaa0b3e62017-12-10 15:52:52 -060045};
46const instanceC = {
47 State: {Name: 'running'},
48 Id: 'C',
49 Tags: [
50 {Key: 'Name', Value: 'Bob'},
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020051 {Key: 'Moose', Value: 'Bob'},
52 ],
Matt Godboltaa0b3e62017-12-10 15:52:52 -060053};
54const instanceD = {
55 State: {Name: 'running'},
56 Id: 'D',
57 Tags: [
58 {Key: 'Name', Value: 'Alice'},
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020059 {Key: 'Moose', Value: 'Bob'},
60 ],
Matt Godboltaa0b3e62017-12-10 15:52:52 -060061};
62
Matt Godboltd33afe12018-08-11 11:24:17 -050063function setup() {
64 beforeEach(() => {
65 AWS.mock('EC2', 'describeInstances', {
66 Reservations: [
67 {
Matt Godboltf2c1e0b2022-05-09 23:13:50 -050068 Instances: [instanceA, instanceB, instanceC, instanceD],
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020069 },
70 ],
Matt Godboltd33afe12018-08-11 11:24:17 -050071 });
Matt Godboltaa0b3e62017-12-10 15:52:52 -060072
Patrick Quistd6083ea2021-06-30 07:09:19 +020073 AWS.mock('SSM', 'getParameters', {
Matt Godboltd33afe12018-08-11 11:24:17 -050074 Parameters: [
75 {
76 Name: '/compiler-explorer/configValue',
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020077 Value: 'fromAws',
Matt Godboltd33afe12018-08-11 11:24:17 -050078 },
79 {
80 Name: '/compiler-explorer/onlyOnAws',
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020081 Value: 'bibble',
82 },
83 ],
Matt Godboltd33afe12018-08-11 11:24:17 -050084 });
Matt Godboltd33afe12018-08-11 11:24:17 -050085 });
86 afterEach(() => AWS.restore());
87}
Matt Godboltaa0b3e62017-12-10 15:52:52 -060088
89describe('AWS instance fetcher tests', () => {
Matt Godboltd33afe12018-08-11 11:24:17 -050090 setup();
Matt Godboltaa0b3e62017-12-10 15:52:52 -060091 it('Fetches Bob', () => {
92 const fakeProps = {
93 region: 'not-a-region',
94 tagKey: 'Name',
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020095 tagValue: 'Bob',
Matt Godboltaa0b3e62017-12-10 15:52:52 -060096 };
97 const fetcher = new aws.InstanceFetcher(prop => fakeProps[prop]);
98 return fetcher.getInstances().should.eventually.deep.equal([instanceC]);
99 });
100
101 it('Ignores sleeping nodes', () => {
102 const fakeProps = {
103 region: 'not-a-region',
104 tagKey: 'Name',
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200105 tagValue: 'Alice',
Matt Godboltaa0b3e62017-12-10 15:52:52 -0600106 };
107 const fetcher = new aws.InstanceFetcher(prop => fakeProps[prop]);
108 return fetcher.getInstances().should.eventually.deep.equal([instanceA, instanceD]);
109 });
110});
111
112describe('AWS config tests', () => {
Matt Godboltd33afe12018-08-11 11:24:17 -0500113 setup();
Matt Godboltf2c1e0b2022-05-09 23:13:50 -0500114 it("Doesn't fetch unless region is configured", () => {
Matt Godboltaa0b3e62017-12-10 15:52:52 -0600115 const fakeProps = {
116 region: '',
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200117 configValue: 'fromConfigFile',
Matt Godboltaa0b3e62017-12-10 15:52:52 -0600118 };
Matt Godboltf2c1e0b2022-05-09 23:13:50 -0500119 return aws
120 .initConfig(prop => fakeProps[prop])
Matt Godboltaa0b3e62017-12-10 15:52:52 -0600121 .then(() => {
122 aws.getConfig('configValue').should.equal('fromConfigFile');
123 });
124 });
125
126 it('Gets results from SSM, falling back to config if needed', () => {
127 const fakeProps = {
128 region: 'a non-empty region',
129 configValue: 'fromConfigFile',
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200130 notInAmazon: 'yay',
Matt Godboltaa0b3e62017-12-10 15:52:52 -0600131 };
Matt Godboltf2c1e0b2022-05-09 23:13:50 -0500132 return aws
133 .initConfig(prop => fakeProps[prop])
Matt Godboltaa0b3e62017-12-10 15:52:52 -0600134 .then(() => {
135 aws.getConfig('configValue').should.equal('fromAws');
136 aws.getConfig('onlyOnAws').should.equal('bibble');
137 aws.getConfig('notInAmazon').should.equal('yay');
138 });
139 });
140});