From 2e3cbf7d89815e2915f77677388c49b48f8d20c3 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 12 Dec 2019 00:07:43 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../eks_cluster_configuration_form_spec.js | 16 -- .../services/aws_services_facade_spec.js | 193 +++++++++++++-------- .../eks_cluster/store/actions_spec.js | 21 +-- .../eks_cluster/store/mutations_spec.js | 12 -- 4 files changed, 124 insertions(+), 118 deletions(-) (limited to 'spec/frontend') diff --git a/spec/frontend/create_cluster/eks_cluster/components/eks_cluster_configuration_form_spec.js b/spec/frontend/create_cluster/eks_cluster/components/eks_cluster_configuration_form_spec.js index 25d613d64ed..d3992c6751c 100644 --- a/spec/frontend/create_cluster/eks_cluster/components/eks_cluster_configuration_form_spec.js +++ b/spec/frontend/create_cluster/eks_cluster/components/eks_cluster_configuration_form_spec.js @@ -27,13 +27,11 @@ describe('EksClusterConfigurationForm', () => { let subnetsActions; let keyPairsActions; let securityGroupsActions; - let instanceTypesActions; let vm; beforeEach(() => { state = eksClusterFormState(); actions = { - signOut: jest.fn(), createCluster: jest.fn(), setClusterName: jest.fn(), setEnvironmentScope: jest.fn(), @@ -66,9 +64,6 @@ describe('EksClusterConfigurationForm', () => { securityGroupsActions = { fetchItems: jest.fn(), }; - instanceTypesActions = { - fetchItems: jest.fn(), - }; rolesState = { ...clusterDropdownStoreState(), }; @@ -127,7 +122,6 @@ describe('EksClusterConfigurationForm', () => { instanceTypes: { namespaced: true, state: instanceTypesState, - actions: instanceTypesActions, }, }, }); @@ -164,7 +158,6 @@ describe('EksClusterConfigurationForm', () => { }); }; - const findSignOutButton = () => vm.find('.js-sign-out'); const findCreateClusterButton = () => vm.find('.js-create-cluster'); const findClusterNameInput = () => vm.find('[id=eks-cluster-name]'); const findEnvironmentScopeInput = () => vm.find('[id=eks-environment-scope]'); @@ -187,15 +180,6 @@ describe('EksClusterConfigurationForm', () => { it('fetches available roles', () => { expect(rolesActions.fetchItems).toHaveBeenCalled(); }); - - it('fetches available instance types', () => { - expect(instanceTypesActions.fetchItems).toHaveBeenCalled(); - }); - }); - - it('dispatches signOut action when sign out button is clicked', () => { - findSignOutButton().trigger('click'); - expect(actions.signOut).toHaveBeenCalled(); }); it('sets isLoadingRoles to RoleDropdown loading property', () => { diff --git a/spec/frontend/create_cluster/eks_cluster/services/aws_services_facade_spec.js b/spec/frontend/create_cluster/eks_cluster/services/aws_services_facade_spec.js index 146bcc04569..490a2775b67 100644 --- a/spec/frontend/create_cluster/eks_cluster/services/aws_services_facade_spec.js +++ b/spec/frontend/create_cluster/eks_cluster/services/aws_services_facade_spec.js @@ -1,42 +1,62 @@ -import AxiosMockAdapter from 'axios-mock-adapter'; -import awsServicesFacadeFactory from '~/create_cluster/eks_cluster/services/aws_services_facade'; -import axios from '~/lib/utils/axios_utils'; +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 apiPaths; - let axiosMock; - let awsServices; let region; let vpc; beforeEach(() => { - apiPaths = { - getKeyPairsPath: '/clusters/aws/api/key_pairs', - getRegionsPath: '/clusters/aws/api/regions', - getRolesPath: '/clusters/aws/api/roles', - getSecurityGroupsPath: '/clusters/aws/api/security_groups', - getSubnetsPath: '/clusters/aws/api/subnets', - getVpcsPath: '/clusters/aws/api/vpcs', - getInstanceTypesPath: '/clusters/aws/api/instance_types', - }; region = 'west-1'; vpc = 'vpc-2'; - awsServices = awsServicesFacadeFactory(apiPaths); - axiosMock = new AxiosMockAdapter(axios); }); - describe('when fetchRegions succeeds', () => { - let regions; - let regionsOutput; + it('setAWSConfig configures AWS SDK with provided credentials and default region', () => { + const awsCredentials = { + accessKeyId: 'access-key', + secretAccessKey: 'secret-key', + sessionToken: 'session-token', + }; - beforeEach(() => { - regions = [{ region_name: 'east-1' }, { region_name: 'west-2' }]; - regionsOutput = regions.map(({ region_name: name }) => ({ name, value: name })); - axiosMock.onGet(apiPaths.getRegionsPath).reply(200, { regions }); - }); + setAWSConfig({ awsCredentials }); - it('return list of roles where each item has a name and value', () => { - expect(awsServices.fetchRegions()).resolves.toEqual(regionsOutput); + expect(AWS.config).toEqual({ + ...awsCredentials, + region: DEFAULT_REGION, }); }); @@ -46,15 +66,32 @@ describe('awsServicesFacade', () => { beforeEach(() => { roles = [ - { role_name: 'admin', arn: 'aws::admin' }, - { role_name: 'read-only', arn: 'aws::read-only' }, + { RoleName: 'admin', Arn: 'aws::admin' }, + { RoleName: 'read-only', Arn: 'aws::read-only' }, ]; - rolesOutput = roles.map(({ role_name: name, arn: value }) => ({ name, value })); - axiosMock.onGet(apiPaths.getRolesPath).reply(200, { roles }); + 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', () => { - expect(awsServices.fetchRoles()).resolves.toEqual(rolesOutput); + 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', () => { + expect(fetchRegions()).resolves.toEqual(regionsOutput); }); }); @@ -63,15 +100,19 @@ describe('awsServicesFacade', () => { let keyPairsOutput; beforeEach(() => { - keyPairs = [{ key_pair: 'key-pair' }, { key_pair: 'key-pair-2' }]; - keyPairsOutput = keyPairs.map(({ key_name: name }) => ({ name, value: name })); - axiosMock - .onGet(apiPaths.getKeyPairsPath, { params: { region } }) - .reply(200, { key_pairs: keyPairs }); + 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', () => { - expect(awsServices.fetchKeyPairs({ region })).resolves.toEqual(keyPairsOutput); + expect(fetchKeyPairs({ region })).resolves.toEqual(keyPairsOutput); }); }); @@ -80,13 +121,37 @@ describe('awsServicesFacade', () => { let vpcsOutput; beforeEach(() => { - vpcs = [{ vpc_id: 'vpc-1' }, { vpc_id: 'vpc-2' }]; - vpcsOutput = vpcs.map(({ vpc_id: name }) => ({ name, value: name })); - axiosMock.onGet(apiPaths.getVpcsPath, { params: { region } }).reply(200, { vpcs }); + 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', () => { - expect(awsServices.fetchVpcs({ region })).resolves.toEqual(vpcsOutput); + 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', () => { + expect(fetchVpcs({ region })).resolves.toEqual(vpcsOutput); }); }); @@ -95,15 +160,14 @@ describe('awsServicesFacade', () => { let subnetsOutput; beforeEach(() => { - subnets = [{ subnet_id: 'vpc-1' }, { subnet_id: 'vpc-2' }]; - subnetsOutput = subnets.map(({ subnet_id }) => ({ name: subnet_id, value: subnet_id })); - axiosMock - .onGet(apiPaths.getSubnetsPath, { params: { region, vpc_id: vpc } }) - .reply(200, { subnets }); + 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', () => { - expect(awsServices.fetchSubnets({ region, vpc })).resolves.toEqual(subnetsOutput); + expect(fetchSubnets({ region, vpc })).resolves.toEqual(subnetsOutput); }); }); @@ -113,40 +177,19 @@ describe('awsServicesFacade', () => { beforeEach(() => { securityGroups = [ - { group_name: 'admin group', group_id: 'group-1' }, - { group_name: 'basic group', group_id: 'group-2' }, + { GroupName: 'admin group', GroupId: 'group-1' }, + { GroupName: 'basic group', GroupId: 'group-2' }, ]; - securityGroupsOutput = securityGroups.map(({ group_id: value, group_name: name }) => ({ + securityGroupsOutput = securityGroups.map(({ GroupId: value, GroupName: name }) => ({ name, value, })); - axiosMock - .onGet(apiPaths.getSecurityGroupsPath, { params: { region, vpc_id: vpc } }) - .reply(200, { security_groups: securityGroups }); - }); - it('return list of security groups where each item has a name and value', () => { - expect(awsServices.fetchSecurityGroups({ region, vpc })).resolves.toEqual( - securityGroupsOutput, - ); + mockDescribeSecurityGroupsPromise.mockResolvedValueOnce({ SecurityGroups: securityGroups }); }); - }); - - describe('when fetchInstanceTypes succeeds', () => { - let instanceTypes; - let instanceTypesOutput; - beforeEach(() => { - instanceTypes = [{ instance_type_name: 't2.small' }, { instance_type_name: 't2.medium' }]; - instanceTypesOutput = instanceTypes.map(({ instance_type_name }) => ({ - name: instance_type_name, - value: instance_type_name, - })); - axiosMock.onGet(apiPaths.getInstanceTypesPath).reply(200, { instance_types: instanceTypes }); - }); - - it('return list of instance types where each item has a name and value', () => { - expect(awsServices.fetchInstanceTypes()).resolves.toEqual(instanceTypesOutput); + it('return list of security groups where each item has a name and value', () => { + expect(fetchSecurityGroups({ region, vpc })).resolves.toEqual(securityGroupsOutput); }); }); }); diff --git a/spec/frontend/create_cluster/eks_cluster/store/actions_spec.js b/spec/frontend/create_cluster/eks_cluster/store/actions_spec.js index 578d82cfc26..fda1f71b1f9 100644 --- a/spec/frontend/create_cluster/eks_cluster/store/actions_spec.js +++ b/spec/frontend/create_cluster/eks_cluster/store/actions_spec.js @@ -1,5 +1,4 @@ import testAction from 'helpers/vuex_action_helper'; - import MockAdapter from 'axios-mock-adapter'; import createState from '~/create_cluster/eks_cluster/store/state'; import * as actions from '~/create_cluster/eks_cluster/store/actions'; @@ -21,7 +20,6 @@ import { CREATE_ROLE_ERROR, REQUEST_CREATE_CLUSTER, CREATE_CLUSTER_ERROR, - SIGN_OUT, } from '~/create_cluster/eks_cluster/store/mutation_types'; import axios from '~/lib/utils/axios_utils'; import createFlash from '~/flash'; @@ -64,7 +62,6 @@ describe('EKS Cluster Store Actions', () => { state = { ...createState(), createRolePath: '/clusters/roles/', - signOutPath: '/aws/signout', createClusterPath: '/clusters/', }; }); @@ -102,6 +99,10 @@ describe('EKS Cluster Store Actions', () => { roleArn: 'role_arn', externalId: 'externalId', }; + const response = { + accessKeyId: 'access-key-id', + secretAccessKey: 'secret-key-id', + }; describe('when request succeeds', () => { beforeEach(() => { @@ -110,7 +111,7 @@ describe('EKS Cluster Store Actions', () => { role_arn: payload.roleArn, role_external_id: payload.externalId, }) - .reply(201); + .reply(201, response); }); it('dispatches createRoleSuccess action', () => @@ -119,7 +120,7 @@ describe('EKS Cluster Store Actions', () => { payload, state, [], - [{ type: 'requestCreateRole' }, { type: 'createRoleSuccess' }], + [{ type: 'requestCreateRole' }, { type: 'createRoleSuccess', payload: response }], )); }); @@ -281,14 +282,4 @@ describe('EKS Cluster Store Actions', () => { expect(createFlash).toHaveBeenCalledWith(payload.name[0]); }); }); - - describe('signOut', () => { - beforeEach(() => { - mock.onDelete(state.signOutPath).reply(200, null); - }); - - it('commits signOut mutation', () => { - testAction(actions.signOut, null, state, [{ type: SIGN_OUT }]); - }); - }); }); diff --git a/spec/frontend/create_cluster/eks_cluster/store/mutations_spec.js b/spec/frontend/create_cluster/eks_cluster/store/mutations_spec.js index 0fb392f5eea..8bb014d4758 100644 --- a/spec/frontend/create_cluster/eks_cluster/store/mutations_spec.js +++ b/spec/frontend/create_cluster/eks_cluster/store/mutations_spec.js @@ -16,7 +16,6 @@ import { CREATE_ROLE_ERROR, REQUEST_CREATE_CLUSTER, CREATE_CLUSTER_ERROR, - SIGN_OUT, } from '~/create_cluster/eks_cluster/store/mutation_types'; import createState from '~/create_cluster/eks_cluster/store/state'; import mutations from '~/create_cluster/eks_cluster/store/mutations'; @@ -159,15 +158,4 @@ describe('Create EKS cluster store mutations', () => { expect(state.createClusterError).toBe(error); }); }); - - describe(`mutation ${SIGN_OUT}`, () => { - beforeEach(() => { - state.hasCredentials = true; - mutations[SIGN_OUT](state); - }); - - it('sets hasCredentials to false', () => { - expect(state.hasCredentials).toBe(false); - }); - }); }); -- cgit v1.2.1