9a2d5ad78214a4bb320c9f543d738ce419806309
[osm/N2VC.git] / tests / test_primitive_non-string_parameter.py
1 # A simple test to exercise the libraries' functionality
2 import asyncio
3 import functools
4 import os
5 import sys
6 import logging
7 import unittest
8 import yaml
9 from n2vc.vnf import N2VC
10
11 NSD_YAML = """
12 nsd:nsd-catalog:
13 nsd:
14 - id: multicharmvdu-ns
15 name: multicharmvdu-ns
16 short-name: multicharmvdu-ns
17 description: NS with 1 VNF
18 version: '1.0'
19 logo: osm.png
20 constituent-vnfd:
21 - vnfd-id-ref: multicharmvdu-vnf
22 member-vnf-index: '1'
23 vld:
24 - id: datanet
25 name: datanet
26 short-name: datanet
27 type: ELAN
28 vnfd-connection-point-ref:
29 - vnfd-id-ref: multicharmvdu-vnf
30 member-vnf-index-ref: '1'
31 vnfd-connection-point-ref: vnf-data
32 """
33
34 VNFD_YAML = """
35 vnfd:vnfd-catalog:
36 vnfd:
37 - id: multicharmvdu-vnf
38 name: multicharmvdu-vnf
39 short-name: multicharmvdu-vnf
40 version: '1.0'
41 description: A VNF consisting of 1 VDUs w/charm
42 logo: osm.png
43 connection-point:
44 - id: vnf-data
45 name: vnf-data
46 short-name: vnf-data
47 type: VPORT
48 mgmt-interface:
49 cp: vnf-data
50 internal-vld:
51 - id: internal
52 name: internal
53 short-name: internal
54 type: ELAN
55 internal-connection-point:
56 - id-ref: dataVM-internal
57 vdu:
58 - id: dataVM
59 name: dataVM
60 image: xenial
61 count: '1'
62 vm-flavor:
63 vcpu-count: '1'
64 memory-mb: '1024'
65 storage-gb: '10'
66 interface:
67 - name: dataVM-eth0
68 position: '1'
69 type: INTERNAL
70 virtual-interface:
71 type: VIRTIO
72 internal-connection-point-ref: dataVM-internal
73 - name: dataVM-xe0
74 position: '2'
75 type: EXTERNAL
76 virtual-interface:
77 type: VIRTIO
78 external-connection-point-ref: vnf-data
79 internal-connection-point:
80 - id: dataVM-internal
81 name: dataVM-internal
82 short-name: dataVM-internal
83 type: VPORT
84 vdu-configuration:
85 juju:
86 charm: simple
87 initial-config-primitive:
88 - seq: '1'
89 name: config
90 parameter:
91 - name: ssh-hostname
92 data-type: STRING
93 value: <rw_mgmt_ip>
94 - name: ssh-username
95 data-type: STRING
96 value: ubuntu
97 - name: ssh-password
98 data-type: STRING
99 value: ubuntu
100 - seq: '2'
101 name: touch
102 parameter:
103 - name: filename
104 data-type: STRING
105 value: '/home/ubuntu/first-touch-dataVM'
106 - seq: '3'
107 name: testint
108 parameter:
109 - name: interval
110 data-type: INTEGER
111 value: 20
112 config-primitive:
113 - name: touch
114 parameter:
115 - name: filename
116 data-type: STRING
117 default-value: '/home/ubuntu/touched'
118 """
119
120 class PythonTest(unittest.TestCase):
121 n2vc = None
122
123 def setUp(self):
124
125 self.log = logging.getLogger()
126 self.log.level = logging.DEBUG
127
128 self.loop = asyncio.get_event_loop()
129
130 # self.loop = asyncio.new_event_loop()
131 # asyncio.set_event_loop(None)
132
133 # Extract parameters from the environment in order to run our test
134 vca_host = os.getenv('VCA_HOST', '127.0.0.1')
135 vca_port = os.getenv('VCA_PORT', 17070)
136 vca_user = os.getenv('VCA_USER', 'admin')
137 vca_charms = os.getenv('VCA_CHARMS', None)
138 vca_secret = os.getenv('VCA_SECRET', None)
139 self.n2vc = N2VC(
140 log=self.log,
141 server=vca_host,
142 port=vca_port,
143 user=vca_user,
144 secret=vca_secret,
145 artifacts=vca_charms,
146 )
147
148 def tearDown(self):
149 self.loop.run_until_complete(self.n2vc.logout())
150
151 def get_descriptor(self, descriptor):
152 desc = None
153 try:
154 tmp = yaml.load(descriptor)
155
156 # Remove the envelope
157 root = list(tmp.keys())[0]
158 if root == "nsd:nsd-catalog":
159 desc = tmp['nsd:nsd-catalog']['nsd'][0]
160 elif root == "vnfd:vnfd-catalog":
161 desc = tmp['vnfd:vnfd-catalog']['vnfd'][0]
162 except ValueError:
163 assert False
164 return desc
165
166 def n2vc_callback(self, model_name, application_name, workload_status, task=None):
167 """We pass the vnfd when setting up the callback, so expect it to be
168 returned as a tuple."""
169 if workload_status and not task:
170 self.log.debug("Callback: workload status \"{}\"".format(workload_status))
171
172 if workload_status in ["blocked"]:
173 task = asyncio.ensure_future(
174 self.n2vc.ExecutePrimitive(
175 model_name,
176 application_name,
177 "config",
178 None,
179 params={
180 'ssh-hostname': '10.195.8.78',
181 'ssh-username': 'ubuntu',
182 'ssh-password': 'ubuntu'
183 }
184 )
185 )
186 task.add_done_callback(functools.partial(self.n2vc_callback, None, None, None))
187 pass
188 elif workload_status in ["active"]:
189 self.log.debug("Removing charm")
190 task = asyncio.ensure_future(
191 self.n2vc.RemoveCharms(model_name, application_name, self.n2vc_callback)
192 )
193 task.add_done_callback(functools.partial(self.n2vc_callback, None, None, None))
194
195 def test_deploy_application(self):
196 stream_handler = logging.StreamHandler(sys.stdout)
197 self.log.addHandler(stream_handler)
198 try:
199 self.log.info("Log handler installed")
200 nsd = self.get_descriptor(NSD_YAML)
201 vnfd = self.get_descriptor(VNFD_YAML)
202
203 if nsd and vnfd:
204
205 vca_charms = os.getenv('VCA_CHARMS', None)
206
207 params = {}
208 vnf_index = 0
209
210 def deploy():
211 """An inner function to do the deployment of a charm from
212 either a vdu or vnf.
213 """
214 charm_dir = "{}/{}".format(vca_charms, charm)
215
216 # Setting this to an IP that will fail the initial config.
217 # This will be detected in the callback, which will execute
218 # the "config" primitive with the right IP address.
219 params['rw_mgmt_ip'] = '10.195.8.78'
220
221 # self.loop.run_until_complete(n.CreateNetworkService(nsd))
222 ns_name = "default"
223
224 vnf_name = self.n2vc.FormatApplicationName(
225 ns_name,
226 vnfd['name'],
227 str(vnf_index),
228 )
229
230 self.loop.run_until_complete(
231 self.n2vc.DeployCharms(
232 ns_name,
233 vnf_name,
234 vnfd,
235 charm_dir,
236 params,
237 {},
238 self.n2vc_callback
239 )
240 )
241
242 # Check if the VDUs in this VNF have a charm
243 for vdu in vnfd['vdu']:
244 vdu_config = vdu.get('vdu-configuration')
245 if vdu_config:
246 juju = vdu_config['juju']
247 self.assertIsNotNone(juju)
248
249 charm = juju['charm']
250 self.assertIsNotNone(charm)
251
252 params['initial-config-primitive'] = vdu_config['initial-config-primitive']
253
254 deploy()
255 vnf_index += 1
256
257 # Check if this VNF has a charm
258 vnf_config = vnfd.get("vnf-configuration")
259 if vnf_config:
260 juju = vnf_config['juju']
261 self.assertIsNotNone(juju)
262
263 charm = juju['charm']
264 self.assertIsNotNone(charm)
265
266 params['initial-config-primitive'] = vnf_config['initial-config-primitive']
267
268 deploy()
269 vnf_index += 1
270
271 self.loop.run_forever()
272
273 # self.loop.run_until_complete(n.GetMetrics(vnfd, nsd=nsd))
274
275 # Test actions
276 # ExecutePrimitive(self, nsd, vnfd, vnf_member_index, primitive, callback, *callback_args, **params):
277
278 # self.loop.run_until_complete(n.DestroyNetworkService(nsd))
279
280 # self.loop.run_until_complete(self.n2vc.logout())
281 finally:
282 self.log.removeHandler(stream_handler)