summaryrefslogtreecommitdiff
path: root/buildscripts/tests/test_aws_ec2.py
blob: 3886e5a5b681a13fbf218f05da5ba7c271f1f3b2 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
"""Unit test for buildscripts/aws_ec2.py."""

from __future__ import absolute_import

import datetime
import unittest

from buildscripts import aws_ec2

_AMI = "ami-ed6bec86"
_INSTANCE_TYPE = "t1.micro"


class AwsEc2TestCase(unittest.TestCase):
    def setUp(self):
        self.aws_ec2 = aws_ec2.AwsEc2()
        self.launched_instances = []
        self.ami = _AMI
        self.instance_type = _INSTANCE_TYPE
        self.key_name = None
        self.security_groups = None
        self.expire_dt = datetime.datetime.utcnow() + datetime.timedelta(hours=1)
        self.tags = [{"Key": "expire-on", "Value": self.expire_dt.strftime("%Y-%m-%d %H:%M:%S")},
                     {"Key": "Name",
                      "Value": "Unittest AWS EC2 Launcher"}, {"Key": "owner", "Value": ""}]

    def tearDown(self):
        for instance in self.launched_instances:
            self.aws_ec2.control_instance(mode="terminate", image_id=instance)


class AwsEc2Connect(AwsEc2TestCase):
    def runTest(self):
        self.assertIsNotNone(self.aws_ec2)


class AwsEc2Launch(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, block_devices={"xvde": 5, "xvdf": 10}, instance_type=self.instance_type,
            key_name=self.key_name, security_groups=self.security_groups, tags=self.tags)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags, DryRun=True)
        self.assertEqual(1, code, ret)

        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags, InvalidParam=True)
        self.assertEqual(1, code, ret)

        code, ret = self.aws_ec2.launch_instance(
            ami="ami-bad_ami", instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertNotEqual(0, code, ret)

        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type="bad_instance_type", key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertNotEqual(0, code, ret)

        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name="bad_key_name",
            security_groups=self.security_groups, tags=self.tags)
        self.assertNotEqual(0, code, ret)


class AwsEc2LaunchStatus(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)
        self.assertEqual(self.ami, ret.image_id, ret)
        self.assertEqual(self.instance_type, ret.instance_type, ret)
        self.assertTrue(ret.state["Name"] in ["pending", "running"], ret)
        for tag in ret.tags:
            self.assertTrue(tag in self.tags, ret)
        for tag in self.tags:
            self.assertTrue(tag in ret.tags, ret)

        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags, wait_time_secs=300,
            show_progress=True)
        self.assertEqual("running", ret.state["Name"], ret)
        self.assertIsNotNone(ret.public_ip_address, ret)
        self.assertIsNotNone(ret.private_ip_address, ret)
        self.launched_instances.append(ret.instance_id)


class AwsEc2ControlStatus(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        code, ret = self.aws_ec2.control_instance(mode="status", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertEqual(self.ami, ret.image_id, ret)
        self.assertEqual(self.instance_type, ret.instance_type, ret)
        self.assertTrue(ret.state["Name"] in ["pending", "running"], ret)
        for tag in ret.tags:
            self.assertTrue(tag in self.tags, ret)
        for tag in self.tags:
            self.assertTrue(tag in ret.tags, ret)
        self.assertIsNotNone(ret.instance_id, ret)
        self.assertIsNotNone(ret.image_id, ret)
        self.assertIsNotNone(ret.instance_type, ret)
        self.assertIsNotNone(ret.state["Name"], ret)
        self.assertIsNotNone(ret.tags, ret)

        self.assertRaises(ValueError, self.aws_ec2.control_instance, mode="bad_mode",
                          image_id=ret.instance_id)

        code, ret = self.aws_ec2.control_instance(mode="status", image_id="bad_id")
        self.assertNotEqual(0, code, ret)
        self.assertRegexpMatches(ret, "Invalid", ret)


class AwsEc2ControlStart(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        code, ret = self.aws_ec2.control_instance(mode="start", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertTrue(ret.state["Name"] in ["starting", "running"], ret)


class AwsEc2ControlStartReboot(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        code, ret = self.aws_ec2.control_instance(mode="start", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertTrue(ret.state["Name"] in ["running"], ret)

        code, ret = self.aws_ec2.control_instance(mode="reboot", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertTrue(ret.state["Name"] in ["rebooting", "running"], ret)


class AwsEc2ControlStop(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags, wait_time_secs=60)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        code, ret = self.aws_ec2.control_instance(mode="stop", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertTrue(ret.state["Name"] in ["stopping", "stopped"], ret)

        code, ret = self.aws_ec2.control_instance(mode="start", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertTrue(ret.state["Name"] in ["pending", "running"], ret)

        code, ret = self.aws_ec2.control_instance(mode="force-stop", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertTrue(ret.state["Name"] in ["stopping", "stopped"], ret)


class AwsEc2ControlTerminate(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(
            ami=self.ami, instance_type=self.instance_type, key_name=self.key_name,
            security_groups=self.security_groups, tags=self.tags)
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        code, ret = self.aws_ec2.control_instance(mode="terminate", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        self.assertTrue(ret.state["Name"] in ["shutting-down", "terminated"], ret)

        code, ret = self.aws_ec2.control_instance(mode="start", image_id=ret.instance_id)
        self.assertNotEqual(0, code, ret)


class AwsEc2TagInstance(AwsEc2TestCase):
    def runTest(self):
        code, ret = self.aws_ec2.launch_instance(ami=self.ami, instance_type=self.instance_type,
                                                 key_name=self.key_name,
                                                 security_groups=self.security_groups, tags=[])
        self.assertEqual(0, code, ret)
        self.launched_instances.append(ret.instance_id)

        self.aws_ec2.tag_instance(image_id=ret.instance_id, tags=self.tags)
        code, ret = self.aws_ec2.control_instance(mode="status", image_id=ret.instance_id)
        self.assertEqual(0, code, ret)
        for tag in ret.tags:
            self.assertTrue(tag in self.tags, ret)
        for tag in self.tags:
            self.assertTrue(tag in ret.tags, ret)


if __name__ == "__main__":
    unittest.main()