1 # -*- coding: utf-8 -*-
4 # Copyright ETSI Contributors and Others.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
23 This module contains unit tests for the OpenStack VIM connector
24 Run this directly with python2 or python3.
27 from datetime
import datetime
31 from osm_rovim_gcp
.vimconn_gcp
import vimconnector
33 __author__
= "Sergio G.R."
34 __date__
= "$05-nov-2021 12:00:00$"
37 class TestGCPOperations
:
39 time_id
= datetime
.today().strftime("%Y%m%d%H%M%S")
40 vim_id
= "gcp-test-" + time_id
42 vm_name
= "gcp-test-vm-" + time_id
43 net_name
= "gcp-test-net-" + time_id
46 credentials_file
= None
48 image_connector_id
= None
60 self
.config
["project_name"] = project_name
61 self
.config
["region_name"] = region_name
63 with
open(credentials_file
) as file:
64 self
.config
["credentials"] = json
.load(file)
67 "Not possible to read credentials JSON file %s",
68 self
.config
["credentials"],
70 self
.image_id
= image_id
71 self
.image_connector_id
= image_connector_id
72 self
.flavor_id
= flavor_id
74 # instantiate dummy VIM connector so we can test it
75 self
.gcp_conn
= vimconnector(
78 tenant_id
=project_name
,
79 tenant_name
=project_name
,
88 def test_networks(self
):
89 net_id_1
= self
.gcp_conn
.new_network(
90 self
.net_name
, None, {"subnet_address": "10.0.0.0/25"}
92 net_id_2
= self
.gcp_conn
.new_network(
93 self
.net_name
, None, {"subnet_address": "10.9.0.0/25"}
95 _
= self
.gcp_conn
.delete_network(net_id_1
[0])
96 _
= self
.gcp_conn
.delete_network(net_id_2
[0])
98 def test_vminstances_default(self
):
99 vm_id_1
= self
.gcp_conn
.new_vminstance(
101 description
="testvm",
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
,
108 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
110 def test_vminstances_2_nets(self
):
111 net_id_1
= self
.gcp_conn
.new_network(
112 self
.net_name
, None, {"subnet_address": "10.0.0.0/25"}
114 net_id_2
= self
.gcp_conn
.new_network(
115 self
.net_name
, None, {"subnet_address": "10.9.0.0/25"}
118 vm_id_1
= self
.gcp_conn
.new_vminstance(
120 description
="testvm",
122 image_id
=self
.image_id
,
123 flavor_id
=self
.flavor_id
,
125 {"net_id": net_id_1
[0], "use": "mgmt"},
126 {"net_id": net_id_2
[0], "use": "internal"},
128 cloud_config
=self
.cloud_config
,
130 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
132 _
= self
.gcp_conn
.delete_network(net_id_1
[0])
133 _
= self
.gcp_conn
.delete_network(net_id_2
[0])
135 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(
139 description
="testvm",
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
,
146 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
148 def test_vminstances_flavor(self
):
149 machine_type
= self
.gcp_conn
.get_flavor_id_from_data(
154 "extended": {"mempage-size": "LARGE", "numas": [{"threads": 1}]},
157 vm_id_1
= self
.gcp_conn
.new_vminstance(
159 description
="testvm",
161 image_id
=self
.image_id
,
162 flavor_id
=machine_type
,
163 net_list
=[{"name": "default", "use": "mgmt"}],
164 cloud_config
=self
.cloud_config
,
166 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
169 if __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
)
179 # Setting relevant values for the tests from environment file
180 gcp_env_file
= "gcp.env"
183 with
open(gcp_env_file
) as f
:
185 var
, value
= line
.replace("\n", "").split("=")
186 if var
== "GCP_PROJECT":
188 elif var
== "GCP_REGION":
190 elif var
== "GCP_CREDENTIALS":
191 credentials_file
= value
192 elif var
== "GCP_IMAGE":
194 elif var
== "GCP_IMAGE_CONNECTOR_ID":
195 image_connector_id
= value
196 elif var
== "GCP_FLAVOR":
199 raise Exception("Wrong format of GCP test environment file")
203 or region_name
is None
204 or credentials_file
is None
206 or image_connector_id
is None
210 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
211 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
214 test_gcp
= TestGCPOperations()
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()