blob: 11fa077ae35c65da3281132f27980810a81c569a [file] [log] [blame]
Attila Fazekasa23f5002012-10-23 19:32:45 +02001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18import tempest.config
19from tempest.common.utils.file_utils import have_effective_read_access
20import os
21import tempest.openstack
22import re
23import keystoneclient.exceptions
24import boto.exception
25import logging
26import urlparse
27
28A_I_IMAGES_READY = False # ari,ami,aki
29S3_CAN_CONNECT_ERROR = "Unknown Error"
30EC2_CAN_CONNECT_ERROR = "Unknown Error"
31
32
33def setup_package():
34 global A_I_IMAGES_READY
35 global S3_CAN_CONNECT_ERROR
36 global EC2_CAN_CONNECT_ERROR
37 secret_matcher = re.compile("[A-Za-z0-9+/]{32,}") # 40 in other system
38 id_matcher = re.compile("[A-Za-z0-9]{20,}")
39
40 def all_read(*args):
41 return all(map(have_effective_read_access, args))
42
43 config = tempest.config.TempestConfig()
44 materials_path = config.boto.s3_materials_path
45 ami_path = materials_path + os.sep + config.boto.ami_manifest
46 aki_path = materials_path + os.sep + config.boto.aki_manifest
47 ari_path = materials_path + os.sep + config.boto.ari_manifest
48
49 A_I_IMAGES_READY = all_read(ami_path, aki_path, ari_path)
50 boto_logger = logging.getLogger('boto')
51 level = boto_logger.level
52 boto_logger.setLevel(logging.CRITICAL) # suppress logging for these
53
54 def _cred_sub_check(connection_data):
55 if not id_matcher.match(connection_data["aws_access_key_id"]):
56 raise Exception("Invalid AWS access Key")
57 if not secret_matcher.match(connection_data["aws_secret_access_key"]):
58 raise Exception("Invalid AWS secret Key")
59 raise Exception("Unknown (Authentication?) Error")
60 openstack = tempest.openstack.Manager()
61 try:
62 if urlparse.urlparse(config.boto.ec2_url).hostname is None:
63 raise Exception("Failed to get hostname from the ec2_url")
64 ec2client = openstack.ec2api_client
65 try:
66 ec2client.get_all_regions()
67 except boto.exception.BotoServerError as exc:
68 if exc.error_code is None:
69 raise Exception("EC2 target does not looks EC2 service")
70 _cred_sub_check(ec2client.connection_data)
71
72 except keystoneclient.exceptions.Unauthorized:
73 EC2_CAN_CONNECT_ERROR = "AWS credentials not set," +\
74 " faild to get them even by keystoneclient"
75 except Exception as exc:
Attila Fazekasa23f5002012-10-23 19:32:45 +020076 EC2_CAN_CONNECT_ERROR = str(exc)
77 else:
78 EC2_CAN_CONNECT_ERROR = None
79
80 try:
81 if urlparse.urlparse(config.boto.s3_url).hostname is None:
82 raise Exception("Failed to get hostname from the s3_url")
83 s3client = openstack.s3_client
84 try:
85 s3client.get_bucket("^INVALID*#()@INVALID.")
86 except boto.exception.BotoServerError as exc:
87 if exc.status == 403:
88 _cred_sub_check(s3client.connection_data)
89 except Exception as exc:
Attila Fazekasa23f5002012-10-23 19:32:45 +020090 S3_CAN_CONNECT_ERROR = str(exc)
91 except keystoneclient.exceptions.Unauthorized:
92 S3_CAN_CONNECT_ERROR = "AWS credentials not set," +\
93 " faild to get them even by keystoneclient"
94 else:
95 S3_CAN_CONNECT_ERROR = None
96 boto_logger.setLevel(level)