blob: 62918c2a859c4eb290aa93a6b825fda9f82cefa1 [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
Attila Fazekasa23f5002012-10-23 19:32:45 +020018import logging
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019import os
20import re
Attila Fazekasa23f5002012-10-23 19:32:45 +020021import urlparse
22
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023import boto.exception
24import keystoneclient.exceptions
25
Matthew Treinish481466b2012-12-20 17:16:01 -050026import tempest.clients
Matthew Treinisha83a16e2012-12-07 13:44:02 -050027from tempest.common.utils.file_utils import have_effective_read_access
28import tempest.config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050029
Attila Fazekasa23f5002012-10-23 19:32:45 +020030A_I_IMAGES_READY = False # ari,ami,aki
31S3_CAN_CONNECT_ERROR = "Unknown Error"
32EC2_CAN_CONNECT_ERROR = "Unknown Error"
33
34
35def setup_package():
36 global A_I_IMAGES_READY
37 global S3_CAN_CONNECT_ERROR
38 global EC2_CAN_CONNECT_ERROR
39 secret_matcher = re.compile("[A-Za-z0-9+/]{32,}") # 40 in other system
40 id_matcher = re.compile("[A-Za-z0-9]{20,}")
41
42 def all_read(*args):
43 return all(map(have_effective_read_access, args))
44
45 config = tempest.config.TempestConfig()
46 materials_path = config.boto.s3_materials_path
47 ami_path = materials_path + os.sep + config.boto.ami_manifest
48 aki_path = materials_path + os.sep + config.boto.aki_manifest
49 ari_path = materials_path + os.sep + config.boto.ari_manifest
50
51 A_I_IMAGES_READY = all_read(ami_path, aki_path, ari_path)
52 boto_logger = logging.getLogger('boto')
53 level = boto_logger.level
54 boto_logger.setLevel(logging.CRITICAL) # suppress logging for these
55
56 def _cred_sub_check(connection_data):
57 if not id_matcher.match(connection_data["aws_access_key_id"]):
58 raise Exception("Invalid AWS access Key")
59 if not secret_matcher.match(connection_data["aws_secret_access_key"]):
60 raise Exception("Invalid AWS secret Key")
61 raise Exception("Unknown (Authentication?) Error")
Matthew Treinish481466b2012-12-20 17:16:01 -050062 openstack = tempest.clients.Manager()
Attila Fazekasa23f5002012-10-23 19:32:45 +020063 try:
64 if urlparse.urlparse(config.boto.ec2_url).hostname is None:
65 raise Exception("Failed to get hostname from the ec2_url")
66 ec2client = openstack.ec2api_client
67 try:
68 ec2client.get_all_regions()
69 except boto.exception.BotoServerError as exc:
70 if exc.error_code is None:
71 raise Exception("EC2 target does not looks EC2 service")
72 _cred_sub_check(ec2client.connection_data)
73
74 except keystoneclient.exceptions.Unauthorized:
75 EC2_CAN_CONNECT_ERROR = "AWS credentials not set," +\
76 " faild to get them even by keystoneclient"
77 except Exception as exc:
Attila Fazekasa23f5002012-10-23 19:32:45 +020078 EC2_CAN_CONNECT_ERROR = str(exc)
79 else:
80 EC2_CAN_CONNECT_ERROR = None
81
82 try:
83 if urlparse.urlparse(config.boto.s3_url).hostname is None:
84 raise Exception("Failed to get hostname from the s3_url")
85 s3client = openstack.s3_client
86 try:
87 s3client.get_bucket("^INVALID*#()@INVALID.")
88 except boto.exception.BotoServerError as exc:
89 if exc.status == 403:
90 _cred_sub_check(s3client.connection_data)
91 except Exception as exc:
Attila Fazekasa23f5002012-10-23 19:32:45 +020092 S3_CAN_CONNECT_ERROR = str(exc)
93 except keystoneclient.exceptions.Unauthorized:
94 S3_CAN_CONNECT_ERROR = "AWS credentials not set," +\
95 " faild to get them even by keystoneclient"
96 else:
97 S3_CAN_CONNECT_ERROR = None
98 boto_logger.setLevel(level)