Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | # 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. |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 14 | from unittest import main |
| 15 | from time import time, sleep |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 16 | from common.utils.consulhelpers import get_endpoint_from_consul |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 17 | from tests.itests.voltha.rest_base import RestBase |
| 18 | from google.protobuf.json_format import MessageToDict |
| 19 | from voltha.protos.device_pb2 import Device |
| 20 | import simplejson, jsonschema |
| 21 | |
| 22 | # ~~~~~~~ Common variables ~~~~~~~ |
| 23 | |
| 24 | IMAGES_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 | |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 59 | LOCAL_CONSUL = "localhost:8500" |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 60 | |
| 61 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 62 | |
| 63 | ###################################################### |
| 64 | # Requirements for the test: # |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 65 | # Ensure voltha and envoy are running fine and # |
| 66 | # envoy is available on port 8443 to listen for # |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 67 | # any REST requests # |
| 68 | ###################################################### |
| 69 | |
| 70 | |
| 71 | class VolthaDeviceManagementRetrieveSoftwareInfo(RestBase): |
| 72 | # Retrieve details on the REST entry point |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 73 | rest_endpoint = get_endpoint_from_consul(LOCAL_CONSUL, 'envoy-8443') |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 74 | |
| 75 | # Construct the base_url |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 76 | base_url = 'https://' + rest_endpoint |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 77 | |
| 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 Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 122 | device = self.post('/api/v1/devices', MessageToDict(device), |
| 123 | expected_http_code=200) |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 124 | return device |
| 125 | |
| 126 | # Active the simulated device. |
| 127 | def activate_device(self, device_id): |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 128 | path = '/api/v1/devices/{}'.format(device_id) |
| 129 | self.post(path + '/enable', expected_http_code=200) |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 130 | 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 Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 135 | path = '/api/v1/devices/{}/images'.format(device_id) |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 136 | 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 | |
| 147 | if __name__ == '__main__': |
| 148 | main() |