blob: 2285f216ca7e8a466cbdd14fbb02cfbac45085c8 [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 gcp_conn = None
sousaedu89278b82021-11-19 01:01:32 +000039 time_id = datetime.today().strftime("%Y%m%d%H%M%S")
gallardo71e66112021-11-11 10:58:29 +000040 vim_id = "gcp-test-" + time_id
41 vim_name = vim_id
42 vm_name = "gcp-test-vm-" + time_id
43 net_name = "gcp-test-net-" + time_id
44 cloud_config = None
45 config = {}
46 credentials_file = None
47 image_id = None
48 image_connector_id = None
49 flavor_id = None
50
51 def setUp(
52 self,
53 project_name,
54 region_name,
55 credentials_file,
56 image_id,
57 image_connector_id,
sousaedu89278b82021-11-19 01:01:32 +000058 flavor_id,
gallardo71e66112021-11-11 10:58:29 +000059 ):
60 self.config["project_name"] = project_name
61 self.config["region_name"] = region_name
62 try:
63 with open(credentials_file) as file:
64 self.config["credentials"] = json.load(file)
65 except ValueError:
66 raise Exception(
sousaedu89278b82021-11-19 01:01:32 +000067 "Not possible to read credentials JSON file %s",
68 self.config["credentials"],
gallardo71e66112021-11-11 10:58:29 +000069 )
70 self.image_id = image_id
71 self.image_connector_id = image_connector_id
72 self.flavor_id = flavor_id
73
74 # instantiate dummy VIM connector so we can test it
75 self.gcp_conn = vimconnector(
76 uuid=self.vim_id,
77 name=self.vim_name,
78 tenant_id=project_name,
79 tenant_name=project_name,
80 url=None,
81 url_admin=None,
82 user=None,
83 passwd=None,
84 log_level=None,
85 config=self.config,
86 )
87
88 def test_networks(self):
sousaedu89278b82021-11-19 01:01:32 +000089 net_id_1 = self.gcp_conn.new_network(
90 self.net_name, None, {"subnet_address": "10.0.0.0/25"}
91 )
92 net_id_2 = self.gcp_conn.new_network(
93 self.net_name, None, {"subnet_address": "10.9.0.0/25"}
94 )
gallardo71e66112021-11-11 10:58:29 +000095 _ = self.gcp_conn.delete_network(net_id_1[0])
96 _ = self.gcp_conn.delete_network(net_id_2[0])
97
98 def test_vminstances_default(self):
99 vm_id_1 = self.gcp_conn.new_vminstance(
100 name=self.vm_name,
101 description="testvm",
102 start=True,
103 image_id=self.image_id,
104 flavor_id=self.flavor_id,
105 net_list=[{"name": "default", "use": "mgmt"}],
106 cloud_config=self.cloud_config,
107 )
108 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
109
110 def test_vminstances_2_nets(self):
sousaedu89278b82021-11-19 01:01:32 +0000111 net_id_1 = self.gcp_conn.new_network(
112 self.net_name, None, {"subnet_address": "10.0.0.0/25"}
113 )
114 net_id_2 = self.gcp_conn.new_network(
115 self.net_name, None, {"subnet_address": "10.9.0.0/25"}
116 )
gallardo71e66112021-11-11 10:58:29 +0000117
118 vm_id_1 = self.gcp_conn.new_vminstance(
119 name=self.vm_name,
120 description="testvm",
121 start=True,
122 image_id=self.image_id,
123 flavor_id=self.flavor_id,
sousaedu89278b82021-11-19 01:01:32 +0000124 net_list=[
125 {"net_id": net_id_1[0], "use": "mgmt"},
126 {"net_id": net_id_2[0], "use": "internal"},
127 ],
gallardo71e66112021-11-11 10:58:29 +0000128 cloud_config=self.cloud_config,
129 )
130 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
131
132 _ = self.gcp_conn.delete_network(net_id_1[0])
133 _ = self.gcp_conn.delete_network(net_id_2[0])
134
gallardo71e66112021-11-11 10:58:29 +0000135 def test_vminstances_image_connector_id(self):
136 image_id = self.gcp_conn.get_image_list({"name": self.image_connector_id})
137 vm_id_1 = self.gcp_conn.new_vminstance(
138 name=self.vm_name,
139 description="testvm",
140 start=True,
141 image_id=image_id[0].get("id"),
142 flavor_id=self.flavor_id,
143 net_list=[{"name": "default", "use": "mgmt"}],
144 cloud_config=self.cloud_config,
145 )
146 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
147
148 def test_vminstances_flavor(self):
149 machine_type = self.gcp_conn.get_flavor_id_from_data(
sousaedu89278b82021-11-19 01:01:32 +0000150 {
151 "disk": 10,
152 "ram": 2048,
153 "vcpus": 1,
154 "extended": {"mempage-size": "LARGE", "numas": [{"threads": 1}]},
155 }
gallardo71e66112021-11-11 10:58:29 +0000156 )
157 vm_id_1 = self.gcp_conn.new_vminstance(
158 name=self.vm_name,
159 description="testvm",
160 start=True,
161 image_id=self.image_id,
162 flavor_id=machine_type,
163 net_list=[{"name": "default", "use": "mgmt"}],
164 cloud_config=self.cloud_config,
165 )
166 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
167
168
169if __name__ == "__main__":
170 # Setting logging parameters:
171 log_format = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(funcName)s(): %(message)s"
172 log_formatter = logging.Formatter(log_format, datefmt="%Y-%m-%dT%H:%M:%S")
173 handler = logging.StreamHandler()
174 handler.setFormatter(log_formatter)
175 logger = logging.getLogger("ro.vim.gcp")
176 logger.setLevel(level=logging.DEBUG)
177 logger.addHandler(handler)
178
179 # Setting relevant values for the tests from environment file
180 gcp_env_file = "gcp.env"
181
182 try:
183 with open(gcp_env_file) as f:
184 for line in f:
sousaedu89278b82021-11-19 01:01:32 +0000185 var, value = line.replace("\n", "").split("=")
gallardo71e66112021-11-11 10:58:29 +0000186 if var == "GCP_PROJECT":
187 project_name = value
188 elif var == "GCP_REGION":
189 region_name = value
190 elif var == "GCP_CREDENTIALS":
191 credentials_file = value
192 elif var == "GCP_IMAGE":
193 image_id = value
194 elif var == "GCP_IMAGE_CONNECTOR_ID":
195 image_connector_id = value
196 elif var == "GCP_FLAVOR":
197 flavor_id = value
198 except ValueError:
sousaedu89278b82021-11-19 01:01:32 +0000199 raise Exception("Wrong format of GCP test environment file")
gallardo71e66112021-11-11 10:58:29 +0000200
201 if (
202 project_name is None
203 or region_name is None
204 or credentials_file is None
205 or image_id is None
206 or image_connector_id is None
207 or flavor_id is None
208 ):
209 raise Exception(
210 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
211 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
212 )
213
214 test_gcp = TestGCPOperations()
215 test_gcp.setUp(
216 project_name,
217 region_name,
218 credentials_file,
219 image_id,
220 image_connector_id,
sousaedu89278b82021-11-19 01:01:32 +0000221 flavor_id,
gallardo71e66112021-11-11 10:58:29 +0000222 )
223 test_gcp.test_networks()
224 test_gcp.test_vminstances_default()
225 test_gcp.test_vminstances_2_nets()
226 test_gcp.test_vminstances_connector_id()
227 test_gcp.test_vminstances_flavor()