Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | import json |
| 17 | import os |
| 18 | from unittest import TestCase |
| 19 | import time |
| 20 | from tests.itests.docutests.test_utils import run_command_to_completion_with_raw_stdout |
| 21 | from tests.utests.chameleon.protoc_plugins.test_utils import load_file |
| 22 | |
| 23 | |
| 24 | pyang_cmd = "pyang --plugindir /voltha/experiments/plugin -f protobuf " \ |
| 25 | "-p /voltha/tests/utests/netconf/yang " \ |
| 26 | "/voltha/tests/utests/netconf/yang/{}" |
| 27 | |
| 28 | class YangToProtoBufTests(TestCase): |
| 29 | |
| 30 | def _compare_file(self, response, expected_response): |
| 31 | # compare two files and strip empty lines, blanks, etc |
| 32 | def _filter(x): |
| 33 | x.strip() |
| 34 | return x is not None |
| 35 | |
| 36 | response = filter(_filter,response.split()) |
| 37 | expected_response = filter(_filter,expected_response.split()) |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 38 | print response |
| 39 | print expected_response |
| 40 | |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 41 | self.assertEqual(set(response), set(expected_response)) |
| 42 | |
| 43 | |
| 44 | def test_01_basic_def(self): |
| 45 | print "Test_01_basic_def_Start:------------------" |
| 46 | t0 = time.time() |
| 47 | |
| 48 | # input file: /voltha/tests/utests/netconf/yang/basic.yang |
| 49 | |
| 50 | expected_response = """ |
| 51 | syntax = "proto3"; |
| 52 | package basic; |
| 53 | |
| 54 | message commonAttributes { |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 55 | uint32 my_id = 1 ; |
| 56 | string my_name = 2 ; |
| 57 | bool my_status = 3 ; |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 58 | } |
| 59 | """ |
| 60 | |
| 61 | try: |
| 62 | cmd = pyang_cmd.format('basic.yang') |
| 63 | print 'running command: {}'.format(cmd) |
| 64 | response, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 65 | self.assertEqual(rc, 0) |
| 66 | |
| 67 | self._compare_file(response, expected_response) |
| 68 | finally: |
| 69 | print "Test_01_basic_def_End:------------------ took {} " \ |
| 70 | "secs\n\n".format(time.time() - t0) |
| 71 | |
| 72 | |
| 73 | def test_02_container_def(self): |
| 74 | print "Test_02_container_def_Start:------------------" |
| 75 | t0 = time.time() |
| 76 | |
| 77 | # input file: /voltha/tests/utests/netconf/yang/container.yang |
| 78 | |
| 79 | expected_response = """ |
| 80 | syntax = "proto3"; |
| 81 | package container; |
| 82 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 83 | message int_container { |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 84 | int32 eight = 1 ; |
| 85 | int32 nine = 2 ; |
| 86 | } |
| 87 | """ |
| 88 | |
| 89 | try: |
| 90 | cmd = pyang_cmd.format('container.yang') |
| 91 | print 'running command: {}'.format(cmd) |
| 92 | response, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 93 | self.assertEqual(rc, 0) |
| 94 | |
| 95 | self._compare_file(response, expected_response) |
| 96 | finally: |
| 97 | print "Test_02_container_def_End:------------------ took {} " \ |
| 98 | "secs\n\n".format(time.time() - t0) |
| 99 | |
| 100 | |
| 101 | def test_03_mix_simple_types(self): |
| 102 | print "Test_03_mix_simple_types_Start:------------------" |
| 103 | t0 = time.time() |
| 104 | |
| 105 | # input file: /voltha/tests/utests/netconf/yang/mix_simple_types.yang |
| 106 | |
| 107 | expected_response = """ |
| 108 | syntax = "proto3"; |
| 109 | package mix_simple_types; |
| 110 | |
| 111 | message user { |
| 112 | string name = 1 ; |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 113 | string full_name = 2 ; |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 114 | string class = 3 ; |
| 115 | } |
| 116 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 117 | message int_container { |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 118 | int32 eight = 1 ; |
| 119 | int32 nine = 2 ; |
| 120 | int32 ten = 3 ; |
| 121 | } |
| 122 | message container1 { |
| 123 | bool a = 1 ; |
| 124 | Any b = 2 ; |
| 125 | string mleaf = 3 ; |
| 126 | repeated string mleaf_list = 4 ; |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 127 | message inner_container { |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 128 | string mleaf1 = 1 ; |
| 129 | string mleaf2 = 2 ; |
| 130 | } |
| 131 | } |
| 132 | """ |
| 133 | |
| 134 | try: |
| 135 | cmd = pyang_cmd.format('mix_simple_types.yang') |
| 136 | print 'running command: {}'.format(cmd) |
| 137 | response, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 138 | self.assertEqual(rc, 0) |
| 139 | |
| 140 | self._compare_file(response, expected_response) |
| 141 | finally: |
| 142 | print "Test_03_mix_simple_types_End:------------------ took {} " \ |
| 143 | "secs\n\n".format(time.time() - t0) |
| 144 | |
| 145 | |
| 146 | def test_04_cord_tenant(self): |
| 147 | print "Test_04_cord_tenant_Start:------------------" |
| 148 | t0 = time.time() |
| 149 | |
| 150 | # input file: /voltha/tests/utests/netconf/yang/cord-tenant.yang |
| 151 | |
| 152 | expected_response = """ |
| 153 | syntax = "proto3"; |
| 154 | package cord_tenant; |
| 155 | |
| 156 | message subscriber { |
| 157 | string label = 1 ; |
| 158 | enum status |
| 159 | { |
| 160 | violation = 0 ; |
| 161 | enabled = 1 ; |
| 162 | delinquent = 2 ; |
| 163 | suspended = 3 ; |
| 164 | } |
| 165 | bool demo = 3 ; |
| 166 | } |
| 167 | """ |
| 168 | try: |
| 169 | cmd = pyang_cmd.format('cord-tenant.yang') |
| 170 | print 'running command: {}'.format(cmd) |
| 171 | response, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 172 | self.assertEqual(rc, 0) |
| 173 | |
| 174 | self._compare_file(response, expected_response) |
| 175 | finally: |
| 176 | print "Test_04_cord_tenant_End:------------------ took {} " \ |
| 177 | "secs\n\n".format(time.time() - t0) |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def test_05_basic_rpc(self): |
| 181 | print "Test_05_basic_rpc_Start:------------------" |
| 182 | t0 = time.time() |
| 183 | |
| 184 | # input file: /voltha/tests/utests/netconf/yang/basic-rpc.yang |
| 185 | |
| 186 | expected_response = """ |
| 187 | syntax = "proto3"; |
| 188 | package basic_rpc; |
| 189 | |
| 190 | message my_id { |
| 191 | uint32 my_id = 1 ; |
| 192 | } |
| 193 | message my_name { |
| 194 | string my_name = 2 ; |
| 195 | } |
| 196 | message my_status { |
| 197 | bool my_status = 3 ; |
| 198 | } |
| 199 | message my_input { |
| 200 | string my_input = 4 ; |
| 201 | } |
| 202 | message my_output { |
| 203 | string my_output = 5 ; |
| 204 | } |
| 205 | |
| 206 | service basic_rpc { |
| 207 | rpc do_something(my_input) returns(my_output) {} |
| 208 | } |
| 209 | """ |
| 210 | try: |
| 211 | cmd = pyang_cmd.format('basic-rpc.yang') |
| 212 | print 'running command: {}'.format(cmd) |
| 213 | response, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 214 | self.assertEqual(rc, 0) |
| 215 | |
| 216 | self._compare_file(response, expected_response) |
| 217 | finally: |
| 218 | print "Test_05_basic_rpc_End:------------------ took {} " \ |
| 219 | "secs\n\n".format(time.time() - t0) |