blob: f97353819e389431728db80282d2535e0f3976f1 [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
27import logging
28import json
29
30from osm_rovim_gcp.vimconn_gcp import vimconnector
31from datetime import datetime
32
33
34__author__ = "Sergio G.R."
35__date__ = "$05-nov-2021 12:00:00$"
36
37
sousaedu89278b82021-11-19 01:01:32 +000038class TestGCPOperations:
gallardo71e66112021-11-11 10:58:29 +000039
40 gcp_conn = None
sousaedu89278b82021-11-19 01:01:32 +000041 time_id = datetime.today().strftime("%Y%m%d%H%M%S")
gallardo71e66112021-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,
sousaedu89278b82021-11-19 01:01:32 +000060 flavor_id,
gallardo71e66112021-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(
sousaedu89278b82021-11-19 01:01:32 +000069 "Not possible to read credentials JSON file %s",
70 self.config["credentials"],
gallardo71e66112021-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):
sousaedu89278b82021-11-19 01:01:32 +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 )
gallardo71e66112021-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):
sousaedu89278b82021-11-19 01:01:32 +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 )
gallardo71e66112021-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,
sousaedu89278b82021-11-19 01:01:32 +0000126 net_list=[
127 {"net_id": net_id_1[0], "use": "mgmt"},
128 {"net_id": net_id_2[0], "use": "internal"},
129 ],
gallardo71e66112021-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
gallardo71e66112021-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(
sousaedu89278b82021-11-19 01:01:32 +0000152 {
153 "disk": 10,
154 "ram": 2048,
155 "vcpus": 1,
156 "extended": {"mempage-size": "LARGE", "numas": [{"threads": 1}]},
157 }
gallardo71e66112021-11-11 10:58:29 +0000158 )
159 vm_id_1 = self.gcp_conn.new_vminstance(
160 name=self.vm_name,
161 description="testvm",
162 start=True,
163 image_id=self.image_id,
164 flavor_id=machine_type,
165 net_list=[{"name": "default", "use": "mgmt"}],
166 cloud_config=self.cloud_config,
167 )
168 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
169
170
171if __name__ == "__main__":
172 # Setting logging parameters:
173 log_format = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(funcName)s(): %(message)s"
174 log_formatter = logging.Formatter(log_format, datefmt="%Y-%m-%dT%H:%M:%S")
175 handler = logging.StreamHandler()
176 handler.setFormatter(log_formatter)
177 logger = logging.getLogger("ro.vim.gcp")
178 logger.setLevel(level=logging.DEBUG)
179 logger.addHandler(handler)
180
181 # Setting relevant values for the tests from environment file
182 gcp_env_file = "gcp.env"
183
184 try:
185 with open(gcp_env_file) as f:
186 for line in f:
sousaedu89278b82021-11-19 01:01:32 +0000187 var, value = line.replace("\n", "").split("=")
gallardo71e66112021-11-11 10:58:29 +0000188 if var == "GCP_PROJECT":
189 project_name = value
190 elif var == "GCP_REGION":
191 region_name = value
192 elif var == "GCP_CREDENTIALS":
193 credentials_file = value
194 elif var == "GCP_IMAGE":
195 image_id = value
196 elif var == "GCP_IMAGE_CONNECTOR_ID":
197 image_connector_id = value
198 elif var == "GCP_FLAVOR":
199 flavor_id = value
200 except ValueError:
sousaedu89278b82021-11-19 01:01:32 +0000201 raise Exception("Wrong format of GCP test environment file")
gallardo71e66112021-11-11 10:58:29 +0000202
203 if (
204 project_name is None
205 or region_name is None
206 or credentials_file is None
207 or image_id is None
208 or image_connector_id is None
209 or flavor_id is None
210 ):
211 raise Exception(
212 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
213 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
214 )
215
216 test_gcp = TestGCPOperations()
217 test_gcp.setUp(
218 project_name,
219 region_name,
220 credentials_file,
221 image_id,
222 image_connector_id,
sousaedu89278b82021-11-19 01:01:32 +0000223 flavor_id,
gallardo71e66112021-11-11 10:58:29 +0000224 )
225 test_gcp.test_networks()
226 test_gcp.test_vminstances_default()
227 test_gcp.test_vminstances_2_nets()
228 test_gcp.test_vminstances_connector_id()
229 test_gcp.test_vminstances_flavor()