summaryrefslogtreecommitdiff
path: root/spec/frontend/create_cluster/eks_cluster/services/aws_services_facade_spec.js
blob: 0ef09b4b87e9a8edae339de4cf5e4cffe20736a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import AWS from 'aws-sdk/global';
import EC2 from 'aws-sdk/clients/ec2';
import {
  setAWSConfig,
  fetchRoles,
  fetchRegions,
  fetchKeyPairs,
  fetchVpcs,
  fetchSubnets,
  fetchSecurityGroups,
  DEFAULT_REGION,
} from '~/create_cluster/eks_cluster/services/aws_services_facade';

const mockListRolesPromise = jest.fn();
const mockDescribeRegionsPromise = jest.fn();
const mockDescribeKeyPairsPromise = jest.fn();
const mockDescribeVpcsPromise = jest.fn();
const mockDescribeSubnetsPromise = jest.fn();
const mockDescribeSecurityGroupsPromise = jest.fn();

jest.mock('aws-sdk/clients/iam', () =>
  jest.fn().mockImplementation(() => ({
    listRoles: jest.fn().mockReturnValue({ promise: mockListRolesPromise }),
  })),
);

jest.mock('aws-sdk/clients/ec2', () =>
  jest.fn().mockImplementation(() => ({
    describeRegions: jest.fn().mockReturnValue({ promise: mockDescribeRegionsPromise }),
    describeKeyPairs: jest.fn().mockReturnValue({ promise: mockDescribeKeyPairsPromise }),
    describeVpcs: jest.fn().mockReturnValue({ promise: mockDescribeVpcsPromise }),
    describeSubnets: jest.fn().mockReturnValue({ promise: mockDescribeSubnetsPromise }),
    describeSecurityGroups: jest
      .fn()
      .mockReturnValue({ promise: mockDescribeSecurityGroupsPromise }),
  })),
);

describe('awsServicesFacade', () => {
  let region;
  let vpc;

  beforeEach(() => {
    region = 'west-1';
    vpc = 'vpc-2';
  });

  it('setAWSConfig configures AWS SDK with provided credentials and default region', () => {
    const awsCredentials = {
      accessKeyId: 'access-key',
      secretAccessKey: 'secret-key',
      sessionToken: 'session-token',
    };

    setAWSConfig({ awsCredentials });

    expect(AWS.config).toEqual({
      ...awsCredentials,
      region: DEFAULT_REGION,
    });
  });

  describe('when fetchRoles succeeds', () => {
    let roles;
    let rolesOutput;

    beforeEach(() => {
      roles = [
        { RoleName: 'admin', Arn: 'aws::admin' },
        { RoleName: 'read-only', Arn: 'aws::read-only' },
      ];
      rolesOutput = roles.map(({ RoleName: name, Arn: value }) => ({ name, value }));

      mockListRolesPromise.mockResolvedValueOnce({ Roles: roles });
    });

    it('return list of regions where each item has a name and value', () => {
      return expect(fetchRoles()).resolves.toEqual(rolesOutput);
    });
  });

  describe('when fetchRegions succeeds', () => {
    let regions;
    let regionsOutput;

    beforeEach(() => {
      regions = [{ RegionName: 'east-1' }, { RegionName: 'west-2' }];
      regionsOutput = regions.map(({ RegionName: name }) => ({ name, value: name }));

      mockDescribeRegionsPromise.mockResolvedValueOnce({ Regions: regions });
    });

    it('return list of roles where each item has a name and value', () => {
      return expect(fetchRegions()).resolves.toEqual(regionsOutput);
    });
  });

  describe('when fetchKeyPairs succeeds', () => {
    let keyPairs;
    let keyPairsOutput;

    beforeEach(() => {
      keyPairs = [{ KeyName: 'key-pair' }, { KeyName: 'key-pair-2' }];
      keyPairsOutput = keyPairs.map(({ KeyName: name }) => ({ name, value: name }));

      mockDescribeKeyPairsPromise.mockResolvedValueOnce({ KeyPairs: keyPairs });
    });

    it('instantatiates ec2 service with provided region', () => {
      fetchKeyPairs({ region });
      expect(EC2).toHaveBeenCalledWith({ region });
    });

    it('return list of key pairs where each item has a name and value', () => {
      return expect(fetchKeyPairs({ region })).resolves.toEqual(keyPairsOutput);
    });
  });

  describe('when fetchVpcs succeeds', () => {
    let vpcs;
    let vpcsOutput;

    beforeEach(() => {
      vpcs = [{ VpcId: 'vpc-1', Tags: [] }, { VpcId: 'vpc-2', Tags: [] }];
      vpcsOutput = vpcs.map(({ VpcId: vpcId }) => ({ name: vpcId, value: vpcId }));

      mockDescribeVpcsPromise.mockResolvedValueOnce({ Vpcs: vpcs });
    });

    it('instantatiates ec2 service with provided region', () => {
      fetchVpcs({ region });
      expect(EC2).toHaveBeenCalledWith({ region });
    });

    it('return list of vpcs where each item has a name and value', () => {
      return expect(fetchVpcs({ region })).resolves.toEqual(vpcsOutput);
    });
  });

  describe('when vpcs has a Name tag', () => {
    const vpcName = 'vpc name';
    const vpcId = 'vpc id';
    let vpcs;
    let vpcsOutput;

    beforeEach(() => {
      vpcs = [{ VpcId: vpcId, Tags: [{ Key: 'Name', Value: vpcName }] }];
      vpcsOutput = [{ name: vpcName, value: vpcId }];

      mockDescribeVpcsPromise.mockResolvedValueOnce({ Vpcs: vpcs });
    });

    it('uses name tag value as the vpc name', () => {
      return expect(fetchVpcs({ region })).resolves.toEqual(vpcsOutput);
    });
  });

  describe('when fetchSubnets succeeds', () => {
    let subnets;
    let subnetsOutput;

    beforeEach(() => {
      subnets = [{ SubnetId: 'subnet-1' }, { SubnetId: 'subnet-2' }];
      subnetsOutput = subnets.map(({ SubnetId }) => ({ name: SubnetId, value: SubnetId }));

      mockDescribeSubnetsPromise.mockResolvedValueOnce({ Subnets: subnets });
    });

    it('return list of subnets where each item has a name and value', () => {
      return expect(fetchSubnets({ region, vpc })).resolves.toEqual(subnetsOutput);
    });
  });

  describe('when fetchSecurityGroups succeeds', () => {
    let securityGroups;
    let securityGroupsOutput;

    beforeEach(() => {
      securityGroups = [
        { GroupName: 'admin group', GroupId: 'group-1' },
        { GroupName: 'basic group', GroupId: 'group-2' },
      ];
      securityGroupsOutput = securityGroups.map(({ GroupId: value, GroupName: name }) => ({
        name,
        value,
      }));

      mockDescribeSecurityGroupsPromise.mockResolvedValueOnce({ SecurityGroups: securityGroups });
    });

    it('return list of security groups where each item has a name and value', () => {
      return expect(fetchSecurityGroups({ region, vpc })).resolves.toEqual(securityGroupsOutput);
    });
  });
});