21bdbc39b1709fdeb2e2de38c2a8088fe37b0a99
[osm/RO.git] / integration-tests / test_vimconn_gcp.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright ETSI Contributors and Others.
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 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
20 ##
21
22 """
23 This module contains unit tests for the OpenStack VIM connector
24 Run this directly with python2 or python3.
25 """
26
27 import logging
28 import json
29
30 from osm_rovim_gcp.vimconn_gcp import vimconnector
31 from datetime import datetime
32
33
34 __author__ = "Sergio G.R."
35 __date__ = "$05-nov-2021 12:00:00$"
36
37
38 class TestGCPOperations:
39
40 gcp_conn = None
41 time_id = datetime.today().strftime("%Y%m%d%H%M%S")
42 vim_id = "gcp-test-" + time_id
43 vim_name = vim_id
44 vm_name = "gcp-test-vm-" + time_id
45 net_name = "gcp-test-net-" + time_id
46 cloud_config = None
47 config = {}
48 credentials_file = None
49 image_id = None
50 image_connector_id = None
51 flavor_id = None
52
53 def setUp(
54 self,
55 project_name,
56 region_name,
57 credentials_file,
58 image_id,
59 image_connector_id,
60 flavor_id,
61 ):
62 self.config["project_name"] = project_name
63 self.config["region_name"] = region_name
64 try:
65 with open(credentials_file) as file:
66 self.config["credentials"] = json.load(file)
67 except ValueError:
68 raise Exception(
69 "Not possible to read credentials JSON file %s",
70 self.config["credentials"],
71 )
72 self.image_id = image_id
73 self.image_connector_id = image_connector_id
74 self.flavor_id = flavor_id
75
76 # instantiate dummy VIM connector so we can test it
77 self.gcp_conn = vimconnector(
78 uuid=self.vim_id,
79 name=self.vim_name,
80 tenant_id=project_name,
81 tenant_name=project_name,
82 url=None,
83 url_admin=None,
84 user=None,
85 passwd=None,
86 log_level=None,
87 config=self.config,
88 )
89
90 def test_networks(self):
91 net_id_1 = self.gcp_conn.new_network(
92 self.net_name, None, {"subnet_address": "10.0.0.0/25"}
93 )
94 net_id_2 = self.gcp_conn.new_network(
95 self.net_name, None, {"subnet_address": "10.9.0.0/25"}
96 )
97 _ = self.gcp_conn.delete_network(net_id_1[0])
98 _ = self.gcp_conn.delete_network(net_id_2[0])
99
100 def test_vminstances_default(self):
101 vm_id_1 = self.gcp_conn.new_vminstance(
102 name=self.vm_name,
103 description="testvm",
104 start=True,
105 image_id=self.image_id,
106 flavor_id=self.flavor_id,
107 net_list=[{"name": "default", "use": "mgmt"}],
108 cloud_config=self.cloud_config,
109 )
110 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
111
112 def test_vminstances_2_nets(self):
113 net_id_1 = self.gcp_conn.new_network(
114 self.net_name, None, {"subnet_address": "10.0.0.0/25"}
115 )
116 net_id_2 = self.gcp_conn.new_network(
117 self.net_name, None, {"subnet_address": "10.9.0.0/25"}
118 )
119
120 vm_id_1 = self.gcp_conn.new_vminstance(
121 name=self.vm_name,
122 description="testvm",
123 start=True,
124 image_id=self.image_id,
125 flavor_id=self.flavor_id,
126 net_list=[
127 {"net_id": net_id_1[0], "use": "mgmt"},
128 {"net_id": net_id_2[0], "use": "internal"},
129 ],
130 cloud_config=self.cloud_config,
131 )
132 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
133
134 _ = self.gcp_conn.delete_network(net_id_1[0])
135 _ = self.gcp_conn.delete_network(net_id_2[0])
136
137 def test_vminstances_image_connector_id(self):
138 image_id = self.gcp_conn.get_image_list({"name": self.image_connector_id})
139 vm_id_1 = self.gcp_conn.new_vminstance(
140 name=self.vm_name,
141 description="testvm",
142 start=True,
143 image_id=image_id[0].get("id"),
144 flavor_id=self.flavor_id,
145 net_list=[{"name": "default", "use": "mgmt"}],
146 cloud_config=self.cloud_config,
147 )
148 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
149
150 def test_vminstances_flavor(self):
151 machine_type = self.gcp_conn.get_flavor_id_from_data(
152 {
153 "disk": 10,
154 "ram": 2048,
155 "vcpus": 1,
156 "extended": {
157 "mempage-size": "LARGE",
158 "numas": [
159 {
160 "threads": 1,
161 }
162 ],
163 },
164 }
165 )
166 vm_id_1 = self.gcp_conn.new_vminstance(
167 name=self.vm_name,
168 description="testvm",
169 start=True,
170 image_id=self.image_id,
171 flavor_id=machine_type,
172 net_list=[{"name": "default", "use": "mgmt"}],
173 cloud_config=self.cloud_config,
174 )
175 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
176
177
178 if __name__ == "__main__":
179 # Setting logging parameters:
180 log_format = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(funcName)s(): %(message)s"
181 log_formatter = logging.Formatter(log_format, datefmt="%Y-%m-%dT%H:%M:%S")
182 handler = logging.StreamHandler()
183 handler.setFormatter(log_formatter)
184 logger = logging.getLogger("ro.vim.gcp")
185 logger.setLevel(level=logging.DEBUG)
186 logger.addHandler(handler)
187
188 # Setting relevant values for the tests from environment file
189 gcp_env_file = "gcp.env"
190
191 try:
192 with open(gcp_env_file) as f:
193 for line in f:
194 var, value = line.replace("\n", "").split("=")
195 if var == "GCP_PROJECT":
196 project_name = value
197 elif var == "GCP_REGION":
198 region_name = value
199 elif var == "GCP_CREDENTIALS":
200 credentials_file = value
201 elif var == "GCP_IMAGE":
202 image_id = value
203 elif var == "GCP_IMAGE_CONNECTOR_ID":
204 image_connector_id = value
205 elif var == "GCP_FLAVOR":
206 flavor_id = value
207 except ValueError:
208 raise Exception("Wrong format of GCP test environment file")
209
210 if (
211 project_name is None
212 or region_name is None
213 or credentials_file is None
214 or image_id is None
215 or image_connector_id is None
216 or flavor_id is None
217 ):
218 raise Exception(
219 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
220 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
221 )
222
223 test_gcp = TestGCPOperations()
224 test_gcp.setUp(
225 project_name,
226 region_name,
227 credentials_file,
228 image_id,
229 image_connector_id,
230 flavor_id,
231 )
232 test_gcp.test_networks()
233 test_gcp.test_vminstances_default()
234 test_gcp.test_vminstances_2_nets()
235 test_gcp.test_vminstances_connector_id()
236 test_gcp.test_vminstances_flavor()