Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

import os 

 

import boto3 

import pytest 

from click.testing import CliRunner 

from moto import mock_autoscaling, mock_ec2, mock_elb, mock_emr, mock_rds 

 

 

@pytest.fixture 

def runner(): 

    """Define test runner""" 

    return CliRunner() 

 

 

@pytest.yield_fixture(scope='function') 

def asg(): 

    """AutoScaling mock service""" 

    mock = mock_autoscaling() 

    mock.start() 

 

    client = boto3.client('autoscaling') 

    groups = [] 

    lcs = [] 

    for i in range(3): 

        lc_config = dict( 

            LaunchConfigurationName='lc-{0}'.format(i), 

            ImageId='ami-xxxxxx', 

            KeyName='mykey', 

            InstanceType='c3.xlarge', 

        ) 

        client.create_launch_configuration(**lc_config) 

        lcs.append(lc_config) 

        asg_config = dict( 

            AutoScalingGroupName='asg-{0}'.format(i), 

            LaunchConfigurationName='lc-{0}'.format(i), 

            MaxSize=10, 

            MinSize=2, 

        ) 

        client.create_auto_scaling_group(**asg_config) 

        groups.append(asg_config) 

 

    yield {'lcs': lcs, 'groups': groups} 

    mock.stop() 

 

 

@pytest.yield_fixture(scope='function') 

def emr(): 

    """EMR mock service""" 

    # FIXME: moto can only support us-east-1 when EMR module is used 

    # https://github.com/spulec/moto/pull/456 

    # https://github.com/spulec/moto/pull/375 

    mock = mock_emr() 

    mock.start() 

    tmp_region = os.environ['AWS_DEFAULT_REGION'] 

    os.environ['AWS_DEFAULT_REGION'] = 'us-east-1' 

    client = boto3.client('emr') 

    clusters = [] 

    for i in range(2): 

        cluster = client.run_job_flow( 

            Name='cluster-{:02d}'.format(i), 

            Instances={ 

                'MasterInstanceType': 'c3.xlarge', 

                'SlaveInstanceType': 'c3.xlarge', 

                'InstanceCount': 3, 

                'Placement': {'AvailabilityZone': 'us-east-1'}, 

                'KeepJobFlowAliveWhenNoSteps': True, 

            }, 

            VisibleToAllUsers=True, 

        ) 

        clusters.append(cluster) 

    yield {'clusters': clusters} 

    mock.stop() 

    os.environ['AWS_DEFAULT_REGION'] = tmp_region 

 

 

@pytest.yield_fixture(scope='function') 

def elb(): 

    """ELB mock service""" 

    mock = mock_elb() 

    mock.start() 

 

    client = boto3.client('elb') 

    lbs = [] 

    for i in range(2): 

        lb = client.create_load_balancer( 

            LoadBalancerName='loadbalancer-{:02d}'.format(i), 

            Listeners=[ 

                { 

                    'Protocol': 'http', 

                    'LoadBalancerPort': 80, 

                    'InstanceProtocol': 'http', 

                    'InstancePort': 80, 

                } 

            ] 

        ) 

        lbs.append(lb) 

    yield {'lbs': lbs} 

    mock.stop() 

 

 

@pytest.yield_fixture(scope='function') 

def ec2(): 

    """EC2 mock service""" 

    mock = mock_ec2() 

    mock.start() 

 

    ec2 = boto3.resource('ec2') 

    ssh_server = ec2.create_instances(ImageId='ami-xxxxx', MinCount=1, MaxCount=1) 

    for s in ssh_server: 

        ec2.create_tags( 

            Resources=[s.id], 

            Tags=[{'Key': 'Name', 'Value': 'ssh_server'}]) 

 

    gateway_server = ec2.create_instances(ImageId='ami-xxxxx', MinCount=1, MaxCount=1) 

    for s in gateway_server: 

        ec2.create_tags( 

            Resources=[s.id], 

            Tags=[{'Key': 'Name', 'Value': 'gateway_server'}]) 

 

    server = ec2.create_instances(ImageId='ami-xxxxx', MinCount=1, MaxCount=1) 

    servers = ec2.create_instances(ImageId='ami-xxxxx', MinCount=2, MaxCount=2) 

    for i, s in enumerate(servers): 

        ec2.create_tags( 

            Resources=[s.id], 

            Tags=[{'Key': 'Name', 'Value': 'server{:0>2d}'.format(i)}]) 

 

    yield dict( 

        ec2=ec2, 

        servers=servers, 

        server=server[0], 

        ssh_target_server=ssh_server[0], 

        gateway_target_server=gateway_server[0], 

    ) 

 

    mock.stop() 

 

 

@pytest.yield_fixture(scope='function') 

def rds(): 

    """RDS mock service""" 

    mock = mock_rds() 

    mock.start() 

 

    rds = boto3.client('rds') 

 

    instances = rds.create_db_instance( 

        DBName='db-xxxx', 

        DBInstanceIdentifier='id1ewbkjqprhw13', 

        AllocatedStorage=64, 

        DBInstanceClass='db.t2.medium', 

        MasterUsername='toor', 

        MasterUserPassword='password', 

        AvailabilityZone='us-east-1a', 

        Port=3306, 

        MultiAZ=True, 

        Engine='mysql', 

        EngineVersion='5.7.11', 

        AutoMinorVersionUpgrade=False, 

        LicenseModel='general-public-license', 

        Iops=256, 

        PubliclyAccessible=True, 

        Tags=[ 

            { 

                'Key': 'Name', 

                'Value': 'id1ewbkjqprhw13' 

            }, 

        ], 

        DBClusterIdentifier='id1ewbkjqprhw13', 

        StorageType='standard', 

        StorageEncrypted=False, 

        CopyTagsToSnapshot=True, 

    ) 

 

    yield {'instances': instances} 

    mock.stop()