| ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame^] | 1 | from unittest import main |
| 2 | from time import time, sleep |
| 3 | from tests.itests.voltha.rest_base import RestBase |
| 4 | from google.protobuf.json_format import MessageToDict |
| 5 | from voltha.protos.device_pb2 import Device |
| 6 | import simplejson, jsonschema |
| 7 | |
| 8 | # ~~~~~~~ Common variables ~~~~~~~ |
| 9 | |
| 10 | IMAGES_SCHEMA = { |
| 11 | "properties": { |
| 12 | "image": { |
| 13 | "items": { |
| 14 | "properties": { |
| 15 | "hash": { |
| 16 | "type": "string" |
| 17 | }, |
| 18 | "install_datetime": { |
| 19 | "type": "string" |
| 20 | }, |
| 21 | "is_active": { |
| 22 | "type": "boolean" |
| 23 | }, |
| 24 | "is_committed": { |
| 25 | "type": "boolean" |
| 26 | }, |
| 27 | "is_valid": { |
| 28 | "type": "boolean" |
| 29 | }, |
| 30 | "name": { |
| 31 | "type": "string" |
| 32 | }, |
| 33 | "version": { |
| 34 | "type": "string" |
| 35 | } |
| 36 | }, |
| 37 | "type": "object" |
| 38 | }, |
| 39 | "type": "array" |
| 40 | } |
| 41 | }, |
| 42 | "type": "object" |
| 43 | } |
| 44 | |
| 45 | |
| 46 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 47 | |
| 48 | ###################################################### |
| 49 | # Requirements for the test: # |
| 50 | # Ensure voltha and chameleon are running fine and # |
| 51 | # chameleon is available on port 8881 to listen for # |
| 52 | # any REST requests # |
| 53 | ###################################################### |
| 54 | |
| 55 | |
| 56 | class VolthaDeviceManagementRetrieveSoftwareInfo(RestBase): |
| 57 | # Retrieve details on the REST entry point |
| 58 | rest_endpoint = "127.0.0.1:8881" |
| 59 | |
| 60 | # Construct the base_url |
| 61 | base_url = 'http://' + rest_endpoint |
| 62 | |
| 63 | def wait_till(self, msg, predicate, interval=0.1, timeout=5.0): |
| 64 | deadline = time() + timeout |
| 65 | while time() < deadline: |
| 66 | if predicate(): |
| 67 | return |
| 68 | sleep(interval) |
| 69 | self.fail('Timed out while waiting for condition: {}'.format(msg)) |
| 70 | |
| 71 | # ~~~~~~~~~~~~ Tests ~~~~~~~~~~~~ |
| 72 | def test_01_voltha_device_management_retrieve_images(self): |
| 73 | # Make sure the Voltha REST interface is available |
| 74 | self.verify_rest() |
| 75 | |
| 76 | # Create a new device |
| 77 | device = self.add_device() |
| 78 | |
| 79 | # Activate the new device |
| 80 | self.activate_device(device['id']) |
| 81 | |
| 82 | # wait till device moves to ACTIVE state |
| 83 | self.wait_till( |
| 84 | 'admin state moves from ACTIVATING to ACTIVE', |
| 85 | lambda: self.get('/api/v1/devices/{}'.format(device['id']))['oper_status'] in ('ACTIVE'), |
| 86 | timeout=5.0) |
| 87 | |
| 88 | # Give some time before ONUs are detected |
| 89 | sleep(2.0) |
| 90 | |
| 91 | # Retrieve the images for the device |
| 92 | images = self.get_images(device['id']) |
| 93 | |
| 94 | # Validate the schema for the software info |
| 95 | self.validate_images_schema(images) |
| 96 | |
| 97 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 98 | |
| 99 | def verify_rest(self): |
| 100 | self.get('/api/v1') |
| 101 | |
| 102 | # Create a new simulated device |
| 103 | def add_device(self): |
| 104 | device = Device( |
| 105 | type='simulated_olt', |
| 106 | ) |
| 107 | device = self.post('/api/v1/local/devices', MessageToDict(device), |
| 108 | expected_code=200) |
| 109 | return device |
| 110 | |
| 111 | # Active the simulated device. |
| 112 | def activate_device(self, device_id): |
| 113 | path = '/api/v1/local/devices/{}'.format(device_id) |
| 114 | self.post(path + '/enable', expected_code=200) |
| 115 | device = self.get(path) |
| 116 | self.assertEqual(device['admin_state'], 'ENABLED') |
| 117 | |
| 118 | # Retrieve software info on the device |
| 119 | def get_images(self, device_id): |
| 120 | path = '/api/v1/local/devices/{}/images'.format(device_id) |
| 121 | images = self.get(path) |
| 122 | return images |
| 123 | |
| 124 | def validate_images_schema(self, images): |
| 125 | try: |
| 126 | jsonschema.validate(images, IMAGES_SCHEMA) |
| 127 | except Exception as e: |
| 128 | self.assertTrue( |
| 129 | False, 'Validation failed for images: {}'.format(e.message)) |
| 130 | |
| 131 | |
| 132 | if __name__ == '__main__': |
| 133 | main() |