blob: ea9783bc46b6c1b2d4ff867d8d1981cf9f815da5 [file] [log] [blame]
gallardo71e66112021-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
sousaedu049cbb12022-01-05 11:39:35 +000027from datetime import datetime
gallardo71e66112021-11-11 10:58:29 +000028import json
sousaedu049cbb12022-01-05 11:39:35 +000029import logging
gallardo71e66112021-11-11 10:58:29 +000030
31from osm_rovim_gcp.vimconn_gcp import vimconnector
gallardo71e66112021-11-11 10:58:29 +000032
33__author__ = "Sergio G.R."
34__date__ = "$05-nov-2021 12:00:00$"
35
36
sousaedu89278b82021-11-19 01:01:32 +000037class TestGCPOperations:
gallardo71e66112021-11-11 10:58:29 +000038
39 gcp_conn = None
sousaedu89278b82021-11-19 01:01:32 +000040 time_id = datetime.today().strftime("%Y%m%d%H%M%S")
gallardo71e66112021-11-11 10:58:29 +000041 vim_id = "gcp-test-" + time_id
42 vim_name = vim_id
43 vm_name = "gcp-test-vm-" + time_id
44 net_name = "gcp-test-net-" + time_id
45 cloud_config = None
46 config = {}
47 credentials_file = None
48 image_id = None
49 image_connector_id = None
50 flavor_id = None
51
52 def setUp(
53 self,
54 project_name,
55 region_name,
56 credentials_file,
57 image_id,
58 image_connector_id,
sousaedu89278b82021-11-19 01:01:32 +000059 flavor_id,
gallardo71e66112021-11-11 10:58:29 +000060 ):
61 self.config["project_name"] = project_name
62 self.config["region_name"] = region_name
63 try:
64 with open(credentials_file) as file:
65 self.config["credentials"] = json.load(file)
66 except ValueError:
67 raise Exception(
sousaedu89278b82021-11-19 01:01:32 +000068 "Not possible to read credentials JSON file %s",
69 self.config["credentials"],
gallardo71e66112021-11-11 10:58:29 +000070 )
71 self.image_id = image_id
72 self.image_connector_id = image_connector_id
73 self.flavor_id = flavor_id
74
75 # instantiate dummy VIM connector so we can test it
76 self.gcp_conn = vimconnector(
77 uuid=self.vim_id,
78 name=self.vim_name,
79 tenant_id=project_name,
80 tenant_name=project_name,
81 url=None,
82 url_admin=None,
83 user=None,
84 passwd=None,
85 log_level=None,
86 config=self.config,
87 )
88
89 def test_networks(self):
sousaedu89278b82021-11-19 01:01:32 +000090 net_id_1 = self.gcp_conn.new_network(
91 self.net_name, None, {"subnet_address": "10.0.0.0/25"}
92 )
93 net_id_2 = self.gcp_conn.new_network(
94 self.net_name, None, {"subnet_address": "10.9.0.0/25"}
95 )
gallardo71e66112021-11-11 10:58:29 +000096 _ = self.gcp_conn.delete_network(net_id_1[0])
97 _ = self.gcp_conn.delete_network(net_id_2[0])
98
99 def test_vminstances_default(self):
100 vm_id_1 = self.gcp_conn.new_vminstance(
101 name=self.vm_name,
102 description="testvm",
103 start=True,
104 image_id=self.image_id,
105 flavor_id=self.flavor_id,
106 net_list=[{"name": "default", "use": "mgmt"}],
107 cloud_config=self.cloud_config,
108 )
109 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
110
111 def test_vminstances_2_nets(self):
sousaedu89278b82021-11-19 01:01:32 +0000112 net_id_1 = self.gcp_conn.new_network(
113 self.net_name, None, {"subnet_address": "10.0.0.0/25"}
114 )
115 net_id_2 = self.gcp_conn.new_network(
116 self.net_name, None, {"subnet_address": "10.9.0.0/25"}
117 )
gallardo71e66112021-11-11 10:58:29 +0000118
119 vm_id_1 = self.gcp_conn.new_vminstance(
120 name=self.vm_name,
121 description="testvm",
122 start=True,
123 image_id=self.image_id,
124 flavor_id=self.flavor_id,
sousaedu89278b82021-11-19 01:01:32 +0000125 net_list=[
126 {"net_id": net_id_1[0], "use": "mgmt"},
127 {"net_id": net_id_2[0], "use": "internal"},
128 ],
gallardo71e66112021-11-11 10:58:29 +0000129 cloud_config=self.cloud_config,
130 )
131 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
132
133 _ = self.gcp_conn.delete_network(net_id_1[0])
134 _ = self.gcp_conn.delete_network(net_id_2[0])
135
gallardo71e66112021-11-11 10:58:29 +0000136 def test_vminstances_image_connector_id(self):
137 image_id = self.gcp_conn.get_image_list({"name": self.image_connector_id})
138 vm_id_1 = self.gcp_conn.new_vminstance(
139 name=self.vm_name,
140 description="testvm",
141 start=True,
142 image_id=image_id[0].get("id"),
143 flavor_id=self.flavor_id,
144 net_list=[{"name": "default", "use": "mgmt"}],
145 cloud_config=self.cloud_config,
146 )
147 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
148
149 def test_vminstances_flavor(self):
150 machine_type = self.gcp_conn.get_flavor_id_from_data(
sousaedu89278b82021-11-19 01:01:32 +0000151 {
152 "disk": 10,
153 "ram": 2048,
154 "vcpus": 1,
155 "extended": {"mempage-size": "LARGE", "numas": [{"threads": 1}]},
156 }
gallardo71e66112021-11-11 10:58:29 +0000157 )
158 vm_id_1 = self.gcp_conn.new_vminstance(
159 name=self.vm_name,
160 description="testvm",
161 start=True,
162 image_id=self.image_id,
163 flavor_id=machine_type,
164 net_list=[{"name": "default", "use": "mgmt"}],
165 cloud_config=self.cloud_config,
166 )
167 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
168
169
170if __name__ == "__main__":
171 # Setting logging parameters:
172 log_format = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(funcName)s(): %(message)s"
173 log_formatter = logging.Formatter(log_format, datefmt="%Y-%m-%dT%H:%M:%S")
174 handler = logging.StreamHandler()
175 handler.setFormatter(log_formatter)
176 logger = logging.getLogger("ro.vim.gcp")
177 logger.setLevel(level=logging.DEBUG)
178 logger.addHandler(handler)
179
180 # Setting relevant values for the tests from environment file
181 gcp_env_file = "gcp.env"
182
183 try:
184 with open(gcp_env_file) as f:
185 for line in f:
sousaedu89278b82021-11-19 01:01:32 +0000186 var, value = line.replace("\n", "").split("=")
gallardo71e66112021-11-11 10:58:29 +0000187 if var == "GCP_PROJECT":
188 project_name = value
189 elif var == "GCP_REGION":
190 region_name = value
191 elif var == "GCP_CREDENTIALS":
192 credentials_file = value
193 elif var == "GCP_IMAGE":
194 image_id = value
195 elif var == "GCP_IMAGE_CONNECTOR_ID":
196 image_connector_id = value
197 elif var == "GCP_FLAVOR":
198 flavor_id = value
199 except ValueError:
sousaedu89278b82021-11-19 01:01:32 +0000200 raise Exception("Wrong format of GCP test environment file")
gallardo71e66112021-11-11 10:58:29 +0000201
202 if (
203 project_name is None
204 or region_name is None
205 or credentials_file is None
206 or image_id is None
207 or image_connector_id is None
208 or flavor_id is None
209 ):
210 raise Exception(
211 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
212 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
213 )
214
215 test_gcp = TestGCPOperations()
216 test_gcp.setUp(
217 project_name,
218 region_name,
219 credentials_file,
220 image_id,
221 image_connector_id,
sousaedu89278b82021-11-19 01:01:32 +0000222 flavor_id,
gallardo71e66112021-11-11 10:58:29 +0000223 )
224 test_gcp.test_networks()
225 test_gcp.test_vminstances_default()
226 test_gcp.test_vminstances_2_nets()
227 test_gcp.test_vminstances_connector_id()
228 test_gcp.test_vminstances_flavor()