~0x44/nova/extdoc

462.1.1 by NTT PF Lab.
Support IPv6
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
5
# All Rights Reserved.
6
#
7
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
8
#    not use this file except in compliance with the License. You may obtain
9
#    a copy of the License at
10
#
11
#         http://www.apache.org/licenses/LICENSE-2.0
12
#
13
#    Unless required by applicable law or agreed to in writing, software
14
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
#    License for the specific language governing permissions and limitations
17
#    under the License.
18
19
import commands
20
import os
21
import random
22
import sys
23
import time
24
520.1.7 by Vishvananda Ishaya
separate out smoketests and add updated nova.sh
25
# If ../nova/__init__.py exists, add ../ to Python search path, so that
26
# it will override what happens to be installed in /usr/(local/)lib/python...
27
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
28
                                   os.pardir,
29
                                   os.pardir))
30
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
31
    sys.path.insert(0, possible_topdir)
32
462.1.1 by NTT PF Lab.
Support IPv6
33
from smoketests import flags
34
from smoketests import base
35
36
#Note that this test should run from
37
#public network (outside of private network segments)
38
#Please set EC2_URL correctly
39
#You should use admin account in this test
40
41
FLAGS = flags.FLAGS
42
43
TEST_PREFIX = 'test%s' % int(random.random() * 1000000)
44
TEST_BUCKET = '%s_bucket' % TEST_PREFIX
45
TEST_KEY = '%s_key' % TEST_PREFIX
46
TEST_KEY2 = '%s_key2' % TEST_PREFIX
47
TEST_DATA = {}
48
49
520.1.7 by Vishvananda Ishaya
separate out smoketests and add updated nova.sh
50
class InstanceTestsFromPublic(base.UserSmokeTestCase):
462.1.1 by NTT PF Lab.
Support IPv6
51
    def test_001_can_create_keypair(self):
52
        key = self.create_key_pair(self.conn, TEST_KEY)
53
        self.assertEqual(key.name, TEST_KEY)
54
55
    def test_002_security_group(self):
56
        security_group_name = "".join(random.choice("sdiuisudfsdcnpaqwertasd")
57
                                      for x in range(random.randint(4, 8)))
58
        group = self.conn.create_security_group(security_group_name,
59
                                               'test group')
60
        group.connection = self.conn
61
        group.authorize('tcp', 22, 22, '0.0.0.0/0')
62
        if FLAGS.use_ipv6:
63
            group.authorize('tcp', 22, 22, '::/0')
64
65
        reservation = self.conn.run_instances(FLAGS.test_image,
66
                                        key_name=TEST_KEY,
67
                                        security_groups=[security_group_name],
68
                                        instance_type='m1.tiny')
69
        self.data['security_group_name'] = security_group_name
70
        self.data['group'] = group
71
        self.data['instance_id'] = reservation.instances[0].id
72
73
    def test_003_instance_with_group_runs_within_60_seconds(self):
74
        reservations = self.conn.get_all_instances([self.data['instance_id']])
75
        instance = reservations[0].instances[0]
76
        # allow 60 seconds to exit pending with IP
77
        for x in xrange(60):
78
            instance.update()
79
            if instance.state == u'running':
80
                break
81
            time.sleep(1)
82
        else:
83
            self.fail('instance failed to start')
84
        ip = reservations[0].instances[0].private_dns_name
85
        self.failIf(ip == '0.0.0.0')
86
        self.data['private_ip'] = ip
87
        if FLAGS.use_ipv6:
88
            ipv6 = reservations[0].instances[0].dns_name_v6
89
            self.failIf(ipv6 is None)
90
            self.data['ip_v6'] = ipv6
91
92
    def test_004_can_ssh_to_ipv6(self):
93
        if FLAGS.use_ipv6:
94
            for x in xrange(20):
95
                try:
96
                    conn = self.connect_ssh(
97
                                        self.data['ip_v6'], TEST_KEY)
98
                    conn.close()
99
                except Exception as ex:
100
                    print ex
101
                    time.sleep(1)
102
                else:
103
                    break
104
            else:
105
                self.fail('could not ssh to instance')
106
107
    def test_012_can_create_instance_with_keypair(self):
108
        if 'instance_id' in self.data:
109
            self.conn.terminate_instances([self.data['instance_id']])
110
        reservation = self.conn.run_instances(FLAGS.test_image,
111
                                              key_name=TEST_KEY,
112
                                              instance_type='m1.tiny')
113
        self.assertEqual(len(reservation.instances), 1)
114
        self.data['instance_id'] = reservation.instances[0].id
115
116
    def test_013_instance_runs_within_60_seconds(self):
117
        reservations = self.conn.get_all_instances([self.data['instance_id']])
118
        instance = reservations[0].instances[0]
119
        # allow 60 seconds to exit pending with IP
120
        for x in xrange(60):
121
            instance.update()
122
            if instance.state == u'running':
123
                break
124
            time.sleep(1)
125
        else:
126
            self.fail('instance failed to start')
127
        ip = reservations[0].instances[0].private_dns_name
128
        self.failIf(ip == '0.0.0.0')
129
        self.data['private_ip'] = ip
130
        if FLAGS.use_ipv6:
131
            ipv6 = reservations[0].instances[0].dns_name_v6
132
            self.failIf(ipv6 is None)
133
            self.data['ip_v6'] = ipv6
134
135
    def test_014_can_not_ping_private_ip(self):
136
        for x in xrange(4):
137
            # ping waits for 1 second
138
            status, output = commands.getstatusoutput(
139
                'ping -c1 %s' % self.data['private_ip'])
140
            if status == 0:
141
                self.fail('can ping private ip from public network')
142
            if FLAGS.use_ipv6:
143
                status, output = commands.getstatusoutput(
144
                    'ping6 -c1 %s' % self.data['ip_v6'])
145
                if status == 0:
146
                    self.fail('can ping ipv6 from public network')
147
        else:
148
            pass
149
150
    def test_015_can_not_ssh_to_private_ip(self):
151
        for x in xrange(1):
152
            try:
153
                conn = self.connect_ssh(self.data['private_ip'], TEST_KEY)
154
                conn.close()
155
            except Exception:
156
                time.sleep(1)
157
            else:
158
                self.fail('can ssh for ipv4 address from public network')
159
160
        if FLAGS.use_ipv6:
161
            for x in xrange(1):
162
                try:
163
                    conn = self.connect_ssh(
164
                                        self.data['ip_v6'], TEST_KEY)
165
                    conn.close()
166
                except Exception:
167
                    time.sleep(1)
168
                else:
169
                    self.fail('can ssh for ipv6 address from public network')
170
171
    def test_999_tearDown(self):
172
        self.delete_key_pair(self.conn, TEST_KEY)
173
        security_group_name = self.data['security_group_name']
174
        group = self.data['group']
175
        if group:
176
            group.revoke('tcp', 22, 22, '0.0.0.0/0')
177
            if FLAGS.use_ipv6:
178
                group.revoke('tcp', 22, 22, '::/0')
179
        self.conn.delete_security_group(security_group_name)
180
        if 'instance_id' in self.data:
181
            self.conn.terminate_instances([self.data['instance_id']])