blob: aa4e240db776254f4690fe39fb31bef9d9a9901c [file] [log] [blame]
gallardo90ba8032021-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
38class TestGCPOperations():
39
40 gcp_conn = None
41 time_id = datetime.today().strftime('%Y%m%d%H%M%S')
42 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,
60 flavor_id
61 ):
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(
69 "Not possible to read credentials JSON file %s", self.config["credentials"]
70 )
71 self.image_id = image_id
72 self.image_connector_id = image_connector_id
73 self.flavor_id = flavor_id
74
75 # instantiate dummy VIM connector so we can test it
76 self.gcp_conn = vimconnector(
77 uuid=self.vim_id,
78 name=self.vim_name,
79 tenant_id=project_name,
80 tenant_name=project_name,
81 url=None,
82 url_admin=None,
83 user=None,
84 passwd=None,
85 log_level=None,
86 config=self.config,
87 )
88
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])
94
95 def test_vminstances_default(self):
96 vm_id_1 = self.gcp_conn.new_vminstance(
97 name=self.vm_name,
98 description="testvm",
99 start=True,
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,
104 )
105 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
106
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"})
110
111 vm_id_1 = self.gcp_conn.new_vminstance(
112 name=self.vm_name,
113 description="testvm",
114 start=True,
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,
119 )
120 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
121
122 _ = self.gcp_conn.delete_network(net_id_1[0])
123 _ = self.gcp_conn.delete_network(net_id_2[0])
124
125
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(
129 name=self.vm_name,
130 description="testvm",
131 start=True,
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,
136 )
137 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
138
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}]}}
142 )
143 vm_id_1 = self.gcp_conn.new_vminstance(
144 name=self.vm_name,
145 description="testvm",
146 start=True,
147 image_id=self.image_id,
148 flavor_id=machine_type,
149 net_list=[{"name": "default", "use": "mgmt"}],
150 cloud_config=self.cloud_config,
151 )
152 _ = self.gcp_conn.delete_vminstance(vm_id_1[0])
153
154
155if __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)
164
165 # Setting relevant values for the tests from environment file
166 gcp_env_file = "gcp.env"
167
168 try:
169 with open(gcp_env_file) as f:
170 for line in f:
171 var, value = line.replace('\n', '').split("=")
172 if var == "GCP_PROJECT":
173 project_name = value
174 elif var == "GCP_REGION":
175 region_name = value
176 elif var == "GCP_CREDENTIALS":
177 credentials_file = value
178 elif var == "GCP_IMAGE":
179 image_id = value
180 elif var == "GCP_IMAGE_CONNECTOR_ID":
181 image_connector_id = value
182 elif var == "GCP_FLAVOR":
183 flavor_id = value
184 except ValueError:
185 raise Exception(
186 "Wrong format of GCP test environment file"
187 )
188
189 if (
190 project_name is None
191 or region_name is None
192 or credentials_file is None
193 or image_id is None
194 or image_connector_id is None
195 or flavor_id is None
196 ):
197 raise Exception(
198 "GCP test environment file must include at least GCP_PROJECT, GCP_REGION, "
199 "GCP_CREDENTIALS, GCP_IMAGE, GCP_IMAGE_PATTERN and GCP_FLAVOR"
200 )
201
202 test_gcp = TestGCPOperations()
203 test_gcp.setUp(
204 project_name,
205 region_name,
206 credentials_file,
207 image_id,
208 image_connector_id,
209 flavor_id
210 )
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()
216