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
34 __author__
= "Sergio G.R."
35 __date__
= "$05-nov-2021 12:00:00$"
38 class TestGCPOperations
:
41 time_id
= datetime
.today().strftime("%Y%m%d%H%M%S")
42 vim_id
= "gcp-test-" + time_id
44 vm_name
= "gcp-test-vm-" + time_id
45 net_name
= "gcp-test-net-" + time_id
48 credentials_file
= None
50 image_connector_id
= None
62 self
.config
["project_name"] = project_name
63 self
.config
["region_name"] = region_name
65 with
open(credentials_file
) as file:
66 self
.config
["credentials"] = json
.load(file)
69 "Not possible to read credentials JSON file %s",
70 self
.config
["credentials"],
72 self
.image_id
= image_id
73 self
.image_connector_id
= image_connector_id
74 self
.flavor_id
= flavor_id
76 # instantiate dummy VIM connector so we can test it
77 self
.gcp_conn
= vimconnector(
80 tenant_id
=project_name
,
81 tenant_name
=project_name
,
90 def test_networks(self
):
91 net_id_1
= self
.gcp_conn
.new_network(
92 self
.net_name
, None, {"subnet_address": "10.0.0.0/25"}
94 net_id_2
= self
.gcp_conn
.new_network(
95 self
.net_name
, None, {"subnet_address": "10.9.0.0/25"}
97 _
= self
.gcp_conn
.delete_network(net_id_1
[0])
98 _
= self
.gcp_conn
.delete_network(net_id_2
[0])
100 def test_vminstances_default(self
):
101 vm_id_1
= self
.gcp_conn
.new_vminstance(
103 description
="testvm",
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
,
110 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
112 def test_vminstances_2_nets(self
):
113 net_id_1
= self
.gcp_conn
.new_network(
114 self
.net_name
, None, {"subnet_address": "10.0.0.0/25"}
116 net_id_2
= self
.gcp_conn
.new_network(
117 self
.net_name
, None, {"subnet_address": "10.9.0.0/25"}
120 vm_id_1
= self
.gcp_conn
.new_vminstance(
122 description
="testvm",
124 image_id
=self
.image_id
,
125 flavor_id
=self
.flavor_id
,
127 {"net_id": net_id_1
[0], "use": "mgmt"},
128 {"net_id": net_id_2
[0], "use": "internal"},
130 cloud_config
=self
.cloud_config
,
132 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
134 _
= self
.gcp_conn
.delete_network(net_id_1
[0])
135 _
= self
.gcp_conn
.delete_network(net_id_2
[0])
137 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(
141 description
="testvm",
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
,
148 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
150 def test_vminstances_flavor(self
):
151 machine_type
= self
.gcp_conn
.get_flavor_id_from_data(
157 "mempage-size": "LARGE",
166 vm_id_1
= self
.gcp_conn
.new_vminstance(
168 description
="testvm",
170 image_id
=self
.image_id
,
171 flavor_id
=machine_type
,
172 net_list
=[{"name": "default", "use": "mgmt"}],
173 cloud_config
=self
.cloud_config
,
175 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
178 if __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
)
188 # Setting relevant values for the tests from environment file
189 gcp_env_file
= "gcp.env"
192 with
open(gcp_env_file
) as f
:
194 var
, value
= line
.replace("\n", "").split("=")
195 if var
== "GCP_PROJECT":
197 elif var
== "GCP_REGION":
199 elif var
== "GCP_CREDENTIALS":
200 credentials_file
= value
201 elif var
== "GCP_IMAGE":
203 elif var
== "GCP_IMAGE_CONNECTOR_ID":
204 image_connector_id
= value
205 elif var
== "GCP_FLAVOR":
208 raise Exception("Wrong format of GCP test environment file")
212 or region_name
is None
213 or credentials_file
is None
215 or image_connector_id
is None
219 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
220 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
223 test_gcp
= TestGCPOperations()
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()