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.
30 from osm_rovim_gcp
.vimconn_gcp
import vimconnector
31 from datetime
import datetime
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", self
.config
["credentials"]
71 self
.image_id
= image_id
72 self
.image_connector_id
= image_connector_id
73 self
.flavor_id
= flavor_id
75 # instantiate dummy VIM connector so we can test it
76 self
.gcp_conn
= vimconnector(
79 tenant_id
=project_name
,
80 tenant_name
=project_name
,
89 def test_networks(self
):
90 net_id_1
= self
.gcp_conn
.new_network(self
.net_name
, None, {"subnet_address": "10.0.0.0/25"})
91 net_id_2
= self
.gcp_conn
.new_network(self
.net_name
, None, {"subnet_address": "10.9.0.0/25"})
92 _
= self
.gcp_conn
.delete_network(net_id_1
[0])
93 _
= self
.gcp_conn
.delete_network(net_id_2
[0])
95 def test_vminstances_default(self
):
96 vm_id_1
= self
.gcp_conn
.new_vminstance(
100 image_id
=self
.image_id
,
101 flavor_id
=self
.flavor_id
,
102 net_list
=[{"name": "default", "use": "mgmt"}],
103 cloud_config
=self
.cloud_config
,
105 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
107 def test_vminstances_2_nets(self
):
108 net_id_1
= self
.gcp_conn
.new_network(self
.net_name
, None, {"subnet_address": "10.0.0.0/25"})
109 net_id_2
= self
.gcp_conn
.new_network(self
.net_name
, None, {"subnet_address": "10.9.0.0/25"})
111 vm_id_1
= self
.gcp_conn
.new_vminstance(
113 description
="testvm",
115 image_id
=self
.image_id
,
116 flavor_id
=self
.flavor_id
,
117 net_list
=[{"net_id": net_id_1
[0], "use": "mgmt"}, {"net_id": net_id_2
[0], "use": "internal"}],
118 cloud_config
=self
.cloud_config
,
120 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
122 _
= self
.gcp_conn
.delete_network(net_id_1
[0])
123 _
= self
.gcp_conn
.delete_network(net_id_2
[0])
126 def test_vminstances_image_connector_id(self
):
127 image_id
= self
.gcp_conn
.get_image_list({"name": self
.image_connector_id
})
128 vm_id_1
= self
.gcp_conn
.new_vminstance(
130 description
="testvm",
132 image_id
=image_id
[0].get("id"),
133 flavor_id
=self
.flavor_id
,
134 net_list
=[{"name": "default", "use": "mgmt"}],
135 cloud_config
=self
.cloud_config
,
137 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
139 def test_vminstances_flavor(self
):
140 machine_type
= self
.gcp_conn
.get_flavor_id_from_data(
141 {'disk': 10, 'ram': 2048, 'vcpus': 1, 'extended': {'mempage-size': 'LARGE', 'numas': [{'threads': 1}]}}
143 vm_id_1
= self
.gcp_conn
.new_vminstance(
145 description
="testvm",
147 image_id
=self
.image_id
,
148 flavor_id
=machine_type
,
149 net_list
=[{"name": "default", "use": "mgmt"}],
150 cloud_config
=self
.cloud_config
,
152 _
= self
.gcp_conn
.delete_vminstance(vm_id_1
[0])
155 if __name__
== "__main__":
156 # Setting logging parameters:
157 log_format
= "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(funcName)s(): %(message)s"
158 log_formatter
= logging
.Formatter(log_format
, datefmt
="%Y-%m-%dT%H:%M:%S")
159 handler
= logging
.StreamHandler()
160 handler
.setFormatter(log_formatter
)
161 logger
= logging
.getLogger("ro.vim.gcp")
162 logger
.setLevel(level
=logging
.DEBUG
)
163 logger
.addHandler(handler
)
165 # Setting relevant values for the tests from environment file
166 gcp_env_file
= "gcp.env"
169 with
open(gcp_env_file
) as f
:
171 var
, value
= line
.replace('\n', '').split("=")
172 if var
== "GCP_PROJECT":
174 elif var
== "GCP_REGION":
176 elif var
== "GCP_CREDENTIALS":
177 credentials_file
= value
178 elif var
== "GCP_IMAGE":
180 elif var
== "GCP_IMAGE_CONNECTOR_ID":
181 image_connector_id
= value
182 elif var
== "GCP_FLAVOR":
186 "Wrong format of GCP test environment file"
191 or region_name
is None
192 or credentials_file
is None
194 or image_connector_id
is None
198 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
199 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
202 test_gcp
= TestGCPOperations()
211 test_gcp
.test_networks()
212 test_gcp
.test_vminstances_default()
213 test_gcp
.test_vminstances_2_nets()
214 test_gcp
.test_vminstances_connector_id()
215 test_gcp
.test_vminstances_flavor()