One more CAL refactor change
[osm/SO.git] / rwlaunchpad / ra / pytest / conftest.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import pytest
19 import os
20 import subprocess
21 import sys
22
23 import rift.auto.log
24 import rift.auto.session
25 import rift.vcs.vcs
26 import rift.rwcal.openstack
27 import logging
28
29 import gi
30 gi.require_version('RwCloudYang', '1.0')
31
32 from gi.repository import RwCloudYang
33
34 @pytest.fixture(scope='session')
35 def cloud_name_prefix():
36 '''fixture which returns the prefix used in cloud account names'''
37 return 'cloud'
38
39 @pytest.fixture(scope='session')
40 def cloud_account_name(cloud_name_prefix):
41 '''fixture which returns the name used to identify the cloud account'''
42 return '{prefix}-0'.format(prefix=cloud_name_prefix)
43
44 @pytest.fixture(scope='session')
45 def sdn_account_name():
46 '''fixture which returns the name used to identify the sdn account'''
47 return 'sdn-0'
48
49 @pytest.fixture(scope='session')
50 def sdn_account_type():
51 '''fixture which returns the account type used by the sdn account'''
52 return 'odl'
53
54 @pytest.fixture(scope='session')
55 def cloud_module():
56 '''Fixture containing the module which defines cloud account
57 Returns:
58 module to be used when configuring a cloud account
59 '''
60 return RwCloudYang
61
62 @pytest.fixture(scope='session')
63 def cloud_xpath():
64 '''Fixture containing the xpath that should be used to configure a cloud account
65 Returns:
66 xpath to be used when configure a cloud account
67 '''
68 return '/cloud/account'
69
70 @pytest.fixture(scope='session')
71 def cloud_accounts(cloud_module, cloud_name_prefix, cloud_host, cloud_user, cloud_tenants, cloud_type):
72 '''fixture which returns a list of CloudAccounts. One per tenant provided
73
74 Arguments:
75 cloud_module - fixture: module defining cloud account
76 cloud_name_prefix - fixture: name prefix used for cloud account
77 cloud_host - fixture: cloud host address
78 cloud_user - fixture: cloud account user key
79 cloud_tenants - fixture: list of tenants to create cloud accounts on
80 cloud_type - fixture: cloud account type
81
82 Returns:
83 A list of CloudAccounts
84 '''
85 accounts = []
86 for idx, cloud_tenant in enumerate(cloud_tenants):
87 cloud_account_name = "{prefix}-{idx}".format(prefix=cloud_name_prefix, idx=idx)
88
89 if cloud_type == 'lxc':
90 accounts.append(
91 cloud_module.CloudAccount.from_dict({
92 "name": cloud_account_name,
93 "account_type": "cloudsim_proxy"})
94 )
95 elif cloud_type == 'openstack':
96 password = 'mypasswd'
97 auth_url = 'http://{cloud_host}:5000/v3/'.format(cloud_host=cloud_host)
98 mgmt_network = os.getenv('MGMT_NETWORK', 'private')
99 accounts.append(
100 cloud_module.CloudAccount.from_dict({
101 'name': cloud_account_name,
102 'account_type': 'openstack',
103 'openstack': {
104 'admin': True,
105 'key': cloud_user,
106 'secret': password,
107 'auth_url': auth_url,
108 'tenant': cloud_tenant,
109 'mgmt_network': mgmt_network}})
110 )
111 elif cloud_type == 'mock':
112 accounts.append(
113 cloud_module.CloudAccount.from_dict({
114 "name": cloud_account_name,
115 "account_type": "mock"})
116 )
117
118 return accounts
119
120
121 @pytest.fixture(scope='session', autouse=True)
122 def cloud_account(cloud_accounts):
123 '''fixture which returns an instance of CloudAccount
124
125 Arguments:
126 cloud_accounts - fixture: list of generated cloud accounts
127
128 Returns:
129 An instance of CloudAccount
130 '''
131 return cloud_accounts[0]
132
133 @pytest.fixture(scope='class')
134 def openstack_client(cloud_host, cloud_user, cloud_tenant):
135 """Fixture which returns a session to openstack host.
136
137 Returns:
138 Session to an openstack host.
139 """
140 password = 'mypasswd'
141 auth_url = 'http://{cloud_host}:5000/v3/'.format(cloud_host=cloud_host)
142 mgmt_network = os.getenv('MGMT_NETWORK', 'private')
143 return rift.rwcal.openstack.OpenstackDriver(**{'username': cloud_user,
144 'password': password,
145 'auth_url': auth_url,
146 'project' : cloud_tenant,
147 'mgmt_network': mgmt_network,
148 'cert_validate': False,
149 'user_domain': 'Default',
150 'project_domain':'Default',
151 'region': 'RegionOne'})