blob: 4601141bb7131c46c397d81abee0cfb4769346e0 [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
ggowdru236bd952017-06-20 20:32:55 -070014from unittest import main
15from time import time, sleep
ubuntuc5c83d72017-07-01 17:57:19 -070016from common.utils.consulhelpers import get_endpoint_from_consul
ggowdru236bd952017-06-20 20:32:55 -070017from tests.itests.voltha.rest_base import RestBase
18from google.protobuf.json_format import MessageToDict
19from voltha.protos.device_pb2 import Device
20import simplejson, jsonschema
21
22# ~~~~~~~ Common variables ~~~~~~~
23
24IMAGES_SCHEMA = {
25 "properties": {
26 "image": {
27 "items": {
28 "properties": {
29 "hash": {
30 "type": "string"
31 },
32 "install_datetime": {
33 "type": "string"
34 },
35 "is_active": {
36 "type": "boolean"
37 },
38 "is_committed": {
39 "type": "boolean"
40 },
41 "is_valid": {
42 "type": "boolean"
43 },
44 "name": {
45 "type": "string"
46 },
47 "version": {
48 "type": "string"
49 }
50 },
51 "type": "object"
52 },
53 "type": "array"
54 }
55 },
56 "type": "object"
57}
58
ubuntuc5c83d72017-07-01 17:57:19 -070059LOCAL_CONSUL = "localhost:8500"
ggowdru236bd952017-06-20 20:32:55 -070060
61# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62
63######################################################
64# Requirements for the test: #
Stephane Barbariecd51f992017-09-07 16:37:02 -040065# Ensure voltha and envoy are running fine and #
66# envoy is available on port 8443 to listen for #
ggowdru236bd952017-06-20 20:32:55 -070067# any REST requests #
68######################################################
69
70
71class VolthaDeviceManagementRetrieveSoftwareInfo(RestBase):
72 # Retrieve details on the REST entry point
Stephane Barbariecd51f992017-09-07 16:37:02 -040073 rest_endpoint = get_endpoint_from_consul(LOCAL_CONSUL, 'envoy-8443')
ggowdru236bd952017-06-20 20:32:55 -070074
75 # Construct the base_url
ubuntuc5c83d72017-07-01 17:57:19 -070076 base_url = 'https://' + rest_endpoint
ggowdru236bd952017-06-20 20:32:55 -070077
78 def wait_till(self, msg, predicate, interval=0.1, timeout=5.0):
79 deadline = time() + timeout
80 while time() < deadline:
81 if predicate():
82 return
83 sleep(interval)
84 self.fail('Timed out while waiting for condition: {}'.format(msg))
85
86 # ~~~~~~~~~~~~ Tests ~~~~~~~~~~~~
87 def test_01_voltha_device_management_retrieve_images(self):
88 # Make sure the Voltha REST interface is available
89 self.verify_rest()
90
91 # Create a new device
92 device = self.add_device()
93
94 # Activate the new device
95 self.activate_device(device['id'])
96
97 # wait till device moves to ACTIVE state
98 self.wait_till(
99 'admin state moves from ACTIVATING to ACTIVE',
100 lambda: self.get('/api/v1/devices/{}'.format(device['id']))['oper_status'] in ('ACTIVE'),
101 timeout=5.0)
102
103 # Give some time before ONUs are detected
104 sleep(2.0)
105
106 # Retrieve the images for the device
107 images = self.get_images(device['id'])
108
109 # Validate the schema for the software info
110 self.validate_images_schema(images)
111
112 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113
114 def verify_rest(self):
115 self.get('/api/v1')
116
117 # Create a new simulated device
118 def add_device(self):
119 device = Device(
120 type='simulated_olt',
121 )
Stephane Barbariecd51f992017-09-07 16:37:02 -0400122 device = self.post('/api/v1/devices', MessageToDict(device),
123 expected_http_code=200)
ggowdru236bd952017-06-20 20:32:55 -0700124 return device
125
126 # Active the simulated device.
127 def activate_device(self, device_id):
Stephane Barbariecd51f992017-09-07 16:37:02 -0400128 path = '/api/v1/devices/{}'.format(device_id)
129 self.post(path + '/enable', expected_http_code=200)
ggowdru236bd952017-06-20 20:32:55 -0700130 device = self.get(path)
131 self.assertEqual(device['admin_state'], 'ENABLED')
132
133 # Retrieve software info on the device
134 def get_images(self, device_id):
Stephane Barbariecd51f992017-09-07 16:37:02 -0400135 path = '/api/v1/devices/{}/images'.format(device_id)
ggowdru236bd952017-06-20 20:32:55 -0700136 images = self.get(path)
137 return images
138
139 def validate_images_schema(self, images):
140 try:
141 jsonschema.validate(images, IMAGES_SCHEMA)
142 except Exception as e:
143 self.assertTrue(
144 False, 'Validation failed for images: {}'.format(e.message))
145
146
147if __name__ == '__main__':
148 main()