Matt Godbolt | f68198a | 2020-09-26 17:50:40 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017, Compiler Explorer Authors |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 2 | // 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 Morton | 044dcfb | 2020-09-26 16:59:26 -0400 | [diff] [blame] | 25 | import './utils'; |
| 26 | import AWS from 'aws-sdk-mock'; |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 27 | |
Austin Morton | 044dcfb | 2020-09-26 16:59:26 -0400 | [diff] [blame] | 28 | import * as aws from '../lib/aws'; |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 29 | |
| 30 | const instanceA = { |
| 31 | State: {Name: 'running'}, |
| 32 | Id: 'A', |
| 33 | Tags: [ |
| 34 | {Key: 'Name', Value: 'Alice'}, |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 35 | {Key: 'Moose', Value: 'Bob'}, |
| 36 | ], |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 37 | }; |
| 38 | const instanceB = { |
| 39 | State: {Name: 'sleeping'}, |
| 40 | Id: 'B', |
| 41 | Tags: [ |
| 42 | {Key: 'Name', Value: 'Alice'}, |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 43 | {Key: 'Moose', Value: 'Bob'}, |
| 44 | ], |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 45 | }; |
| 46 | const instanceC = { |
| 47 | State: {Name: 'running'}, |
| 48 | Id: 'C', |
| 49 | Tags: [ |
| 50 | {Key: 'Name', Value: 'Bob'}, |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 51 | {Key: 'Moose', Value: 'Bob'}, |
| 52 | ], |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 53 | }; |
| 54 | const instanceD = { |
| 55 | State: {Name: 'running'}, |
| 56 | Id: 'D', |
| 57 | Tags: [ |
| 58 | {Key: 'Name', Value: 'Alice'}, |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 59 | {Key: 'Moose', Value: 'Bob'}, |
| 60 | ], |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 61 | }; |
| 62 | |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 63 | function setup() { |
| 64 | beforeEach(() => { |
| 65 | AWS.mock('EC2', 'describeInstances', { |
| 66 | Reservations: [ |
| 67 | { |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 68 | Instances: [instanceA, instanceB, instanceC, instanceD], |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 69 | }, |
| 70 | ], |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 71 | }); |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 72 | |
Patrick Quist | d6083ea | 2021-06-30 07:09:19 +0200 | [diff] [blame] | 73 | AWS.mock('SSM', 'getParameters', { |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 74 | Parameters: [ |
| 75 | { |
| 76 | Name: '/compiler-explorer/configValue', |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 77 | Value: 'fromAws', |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 78 | }, |
| 79 | { |
| 80 | Name: '/compiler-explorer/onlyOnAws', |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 81 | Value: 'bibble', |
| 82 | }, |
| 83 | ], |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 84 | }); |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 85 | }); |
| 86 | afterEach(() => AWS.restore()); |
| 87 | } |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 88 | |
| 89 | describe('AWS instance fetcher tests', () => { |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 90 | setup(); |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 91 | it('Fetches Bob', () => { |
| 92 | const fakeProps = { |
| 93 | region: 'not-a-region', |
| 94 | tagKey: 'Name', |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 95 | tagValue: 'Bob', |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 96 | }; |
| 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 Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 105 | tagValue: 'Alice', |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 106 | }; |
| 107 | const fetcher = new aws.InstanceFetcher(prop => fakeProps[prop]); |
| 108 | return fetcher.getInstances().should.eventually.deep.equal([instanceA, instanceD]); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | describe('AWS config tests', () => { |
Matt Godbolt | d33afe1 | 2018-08-11 11:24:17 -0500 | [diff] [blame] | 113 | setup(); |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 114 | it("Doesn't fetch unless region is configured", () => { |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 115 | const fakeProps = { |
| 116 | region: '', |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 117 | configValue: 'fromConfigFile', |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 118 | }; |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 119 | return aws |
| 120 | .initConfig(prop => fakeProps[prop]) |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 121 | .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 Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 130 | notInAmazon: 'yay', |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 131 | }; |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 132 | return aws |
| 133 | .initConfig(prop => fakeProps[prop]) |
Matt Godbolt | aa0b3e6 | 2017-12-10 15:52:52 -0600 | [diff] [blame] | 134 | .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 | }); |