~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to smoketests/base.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-12-13 10:17:01 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20101213101701-txhhqbzsxw4avnxv
Tags: upstream-2011.1~bzr456
ImportĀ upstreamĀ versionĀ 2011.1~bzr456

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 boto
 
20
import commands
 
21
import httplib
 
22
import os
 
23
import paramiko
 
24
import random
 
25
import sys
 
26
import unittest
 
27
from boto.ec2.regioninfo import RegionInfo
 
28
 
 
29
from smoketests import flags
 
30
 
 
31
FLAGS = flags.FLAGS
 
32
 
 
33
 
 
34
class SmokeTestCase(unittest.TestCase):
 
35
    def connect_ssh(self, ip, key_name):
 
36
        # TODO(devcamcar): set a more reasonable connection timeout time
 
37
        key = paramiko.RSAKey.from_private_key_file('/tmp/%s.pem' % key_name)
 
38
        client = paramiko.SSHClient()
 
39
        client.set_missing_host_key_policy(paramiko.WarningPolicy())
 
40
        client.connect(ip, username='root', pkey=key)
 
41
        stdin, stdout, stderr = client.exec_command('uptime')
 
42
        print 'uptime: ', stdout.read()
 
43
        return client
 
44
 
 
45
    def can_ping(self, ip):
 
46
        """ Attempt to ping the specified IP, and give up after 1 second. """
 
47
 
 
48
        # NOTE(devcamcar): ping timeout flag is different in OSX.
 
49
        if sys.platform == 'darwin':
 
50
            timeout_flag = 't'
 
51
        else:
 
52
            timeout_flag = 'w'
 
53
 
 
54
        status, output = commands.getstatusoutput('ping -c1 -%s1 %s' %
 
55
                                                  (timeout_flag, ip))
 
56
        return status == 0
 
57
 
 
58
    def connection_for_env(self, **kwargs):
 
59
        """
 
60
        Returns a boto ec2 connection for the current environment.
 
61
        """
 
62
        access_key = os.getenv('EC2_ACCESS_KEY')
 
63
        secret_key = os.getenv('EC2_SECRET_KEY')
 
64
        clc_url = os.getenv('EC2_URL')
 
65
 
 
66
        if not access_key or not secret_key or not clc_url:
 
67
            raise Exception('Missing EC2 environment variables. Please source '
 
68
                            'the appropriate novarc file before running this '
 
69
                            'test.')
 
70
 
 
71
        parts = self.split_clc_url(clc_url)
 
72
        return boto.connect_ec2(aws_access_key_id=access_key,
 
73
                                aws_secret_access_key=secret_key,
 
74
                                is_secure=parts['is_secure'],
 
75
                                region=RegionInfo(None,
 
76
                                                  'nova',
 
77
                                                  parts['ip']),
 
78
                                port=parts['port'],
 
79
                                path='/services/Cloud',
 
80
                                **kwargs)
 
81
 
 
82
    def split_clc_url(self, clc_url):
 
83
        """
 
84
        Splits a cloud controller endpoint url.
 
85
        """
 
86
        parts = httplib.urlsplit(clc_url)
 
87
        is_secure = parts.scheme == 'https'
 
88
        ip, port = parts.netloc.split(':')
 
89
        return {'ip': ip, 'port': int(port), 'is_secure': is_secure}
 
90
 
 
91
    def create_key_pair(self, conn, key_name):
 
92
        try:
 
93
            os.remove('/tmp/%s.pem' % key_name)
 
94
        except:
 
95
            pass
 
96
        key = conn.create_key_pair(key_name)
 
97
        key.save('/tmp/')
 
98
        return key
 
99
 
 
100
    def delete_key_pair(self, conn, key_name):
 
101
        conn.delete_key_pair(key_name)
 
102
        try:
 
103
            os.remove('/tmp/%s.pem' % key_name)
 
104
        except:
 
105
            pass
 
106
 
 
107
    def bundle_image(self, image, kernel=False):
 
108
        cmd = 'euca-bundle-image -i %s' % image
 
109
        if kernel:
 
110
            cmd += ' --kernel true'
 
111
        status, output = commands.getstatusoutput(cmd)
 
112
        if status != 0:
 
113
            print '%s -> \n %s' % (cmd, output)
 
114
            raise Exception(output)
 
115
        return True
 
116
 
 
117
    def upload_image(self, bucket_name, image):
 
118
        cmd = 'euca-upload-bundle -b %s -m /tmp/%s.manifest.xml' % (bucket_name, image)
 
119
        status, output = commands.getstatusoutput(cmd)
 
120
        if status != 0:
 
121
            print '%s -> \n %s' % (cmd, output)
 
122
            raise Exception(output)
 
123
        return True
 
124
 
 
125
    def delete_bundle_bucket(self, bucket_name):
 
126
        cmd = 'euca-delete-bundle --clear -b %s' % (bucket_name)
 
127
        status, output = commands.getstatusoutput(cmd)
 
128
        if status != 0:
 
129
            print '%s -> \n%s' % (cmd, output)
 
130
            raise Exception(output)
 
131
        return True
 
132
 
 
133
def run_tests(suites):
 
134
    argv = FLAGS(sys.argv)
 
135
 
 
136
    if not os.getenv('EC2_ACCESS_KEY'):
 
137
        print >> sys.stderr, 'Missing EC2 environment variables. Please ' \
 
138
                             'source the appropriate novarc file before ' \
 
139
                             'running this test.'
 
140
        return 1
 
141
 
 
142
    if FLAGS.suite:
 
143
        try:
 
144
            suite = suites[FLAGS.suite]
 
145
        except KeyError:
 
146
            print >> sys.stderr, 'Available test suites:', \
 
147
                                 ', '.join(suites.keys())
 
148
            return 1
 
149
 
 
150
        unittest.TextTestRunner(verbosity=2).run(suite)
 
151
    else:
 
152
        for suite in suites.itervalues():
 
153
            unittest.TextTestRunner(verbosity=2).run(suite)
 
154