blob: 48f792e00d9a0aab5b5789dae558dda4ba552167 [file] [log] [blame]
gallardo90ba8032021-11-11 10:58:29 +00001# -*- 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"""
23This module contains unit tests for the OpenStack VIM connector
24Run this directly with python2 or python3.
25"""
26
sousaedu0fe84cb2022-01-17 13:48:22 +000027from datetime import datetime
gallardo90ba8032021-11-11 10:58:29 +000028import json
sousaedu0fe84cb2022-01-17 13:48:22 +000029import logging
gallardo90ba8032021-11-11 10:58:29 +000030
31from osm_rovim_gcp.vimconn_gcp import vimconnector
gallardo90ba8032021-11-11 10:58:29 +000032
33
34__author__ = "Sergio G.R."
35__date__ = "$05-nov-2021 12:00:00$"
36
37
sousaedu97508412021-11-29 09:28:15 +000038class TestGCPOperations:
gallardo90ba8032021-11-11 10:58:29 +000039
40 gcp_conn = None
sousaedu97508412021-11-29 09:28:15 +000041 time_id = datetime.today().strftime("%Y%m%d%H%M%S")
gallardo90ba8032021-11-11 10:58:29 +000042 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,
sousaedu97508412021-11-29 09:28:15 +000060 flavor_id,
gallardo90ba8032021-11-11 10:58:29 +000061 ):
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(
sousaedu97508412021-11-29 09:28:15 +000069 "Not possible to read credentials JSON file %s",
70 self.config["credentials"],
gallardo90ba8032021-11-11 10:58:29 +000071 )
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):
sousaedu97508412021-11-29 09:28:15 +000091 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 )
gallardo90ba8032021-11-11 10:58:29 +000097 _ = 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):
sousaedu97508412021-11-29 09:28:15 +0000113 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 )
gallardo90ba8032021-11-11 10:58:29 +0000119
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,
sousaedu97508412021-11-29 09:28:15 +0000126 net_list=[
127 {"net_id": net_id_1[0], "use": "mgmt"},
128 {"net_id": net_id_2[0], "use": "internal"},
129 ],
gallardo90ba8032021-11-11 10:58:29 +0000130 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
gallardo90ba8032021-11-11 10:58:29 +0000137 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(
sousaedu97508412021-11-29 09:28:15 +0000152 {
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 }
gallardo90ba8032021-11-11 10:58:29 +0000165 )
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
178if __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:
sousaedu97508412021-11-29 09:28:15 +0000194 var, value = line.replace("\n", "").split("=")
gallardo90ba8032021-11-11 10:58:29 +0000195 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:
sousaedu97508412021-11-29 09:28:15 +0000208 raise Exception("Wrong format of GCP test environment file")
gallardo90ba8032021-11-11 10:58:29 +0000209
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,
sousaedu97508412021-11-29 09:28:15 +0000230 flavor_id,
gallardo90ba8032021-11-11 10:58:29 +0000231 )
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()