blob: 16b12b7ca51eda61422a921d4964655455d4c0a9 [file] [log] [blame]
Khen Nursimuluf8abbc92016-11-04 19:56:45 -04001#
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#
16import json
17import os
18from unittest import TestCase
19import time
20from tests.itests.docutests.test_utils import run_command_to_completion_with_raw_stdout
21from tests.utests.chameleon.protoc_plugins.test_utils import load_file
22
23
Khen Nursimulu2ef4ce72016-11-23 11:37:38 -050024pyang_cmd = "pyang --plugindir /voltha/experiments/netconf/yang2proto -f " \
25 "proto " \
26 "-p /voltha/experiments/netconf/tests/yang2proto " \
27 "/voltha/experiments/netconf/tests/yang2proto/{}"
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040028
29class YangToProtoBufTests(TestCase):
30
31 def _compare_file(self, response, expected_response):
32 # compare two files and strip empty lines, blanks, etc
33 def _filter(x):
34 x.strip()
35 return x is not None
36
37 response = filter(_filter,response.split())
38 expected_response = filter(_filter,expected_response.split())
Khen Nursimulubf8bf282016-11-07 15:21:35 -050039 print response
40 print expected_response
41
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040042 self.assertEqual(set(response), set(expected_response))
43
44
45 def test_01_basic_def(self):
46 print "Test_01_basic_def_Start:------------------"
47 t0 = time.time()
48
49 # input file: /voltha/tests/utests/netconf/yang/basic.yang
50
51 expected_response = """
52 syntax = "proto3";
53 package basic;
54
55 message commonAttributes {
Khen Nursimulubf8bf282016-11-07 15:21:35 -050056 uint32 my_id = 1 ;
57 string my_name = 2 ;
58 bool my_status = 3 ;
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040059 }
60 """
61
62 try:
63 cmd = pyang_cmd.format('basic.yang')
64 print 'running command: {}'.format(cmd)
65 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
66 self.assertEqual(rc, 0)
67
68 self._compare_file(response, expected_response)
69 finally:
70 print "Test_01_basic_def_End:------------------ took {} " \
71 "secs\n\n".format(time.time() - t0)
72
73
74 def test_02_container_def(self):
75 print "Test_02_container_def_Start:------------------"
76 t0 = time.time()
77
78 # input file: /voltha/tests/utests/netconf/yang/container.yang
79
80 expected_response = """
81 syntax = "proto3";
82 package container;
83
Khen Nursimulubf8bf282016-11-07 15:21:35 -050084 message int_container {
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040085 int32 eight = 1 ;
86 int32 nine = 2 ;
87 }
88 """
89
90 try:
91 cmd = pyang_cmd.format('container.yang')
92 print 'running command: {}'.format(cmd)
93 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
94 self.assertEqual(rc, 0)
95
96 self._compare_file(response, expected_response)
97 finally:
98 print "Test_02_container_def_End:------------------ took {} " \
99 "secs\n\n".format(time.time() - t0)
100
101
102 def test_03_mix_simple_types(self):
103 print "Test_03_mix_simple_types_Start:------------------"
104 t0 = time.time()
105
106 # input file: /voltha/tests/utests/netconf/yang/mix_simple_types.yang
107
108 expected_response = """
109 syntax = "proto3";
110 package mix_simple_types;
111
112 message user {
113 string name = 1 ;
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500114 string full_name = 2 ;
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400115 string class = 3 ;
116 }
117
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500118 message int_container {
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400119 int32 eight = 1 ;
120 int32 nine = 2 ;
121 int32 ten = 3 ;
122 }
123 message container1 {
124 bool a = 1 ;
125 Any b = 2 ;
126 string mleaf = 3 ;
127 repeated string mleaf_list = 4 ;
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500128 message inner_container {
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400129 string mleaf1 = 1 ;
130 string mleaf2 = 2 ;
131 }
132 }
133 """
134
135 try:
136 cmd = pyang_cmd.format('mix_simple_types.yang')
137 print 'running command: {}'.format(cmd)
138 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
139 self.assertEqual(rc, 0)
140
141 self._compare_file(response, expected_response)
142 finally:
143 print "Test_03_mix_simple_types_End:------------------ took {} " \
144 "secs\n\n".format(time.time() - t0)
145
146
147 def test_04_cord_tenant(self):
148 print "Test_04_cord_tenant_Start:------------------"
149 t0 = time.time()
150
151 # input file: /voltha/tests/utests/netconf/yang/cord-tenant.yang
152
153 expected_response = """
154 syntax = "proto3";
155 package cord_tenant;
156
157 message subscriber {
158 string label = 1 ;
159 enum status
160 {
161 violation = 0 ;
162 enabled = 1 ;
163 delinquent = 2 ;
164 suspended = 3 ;
165 }
166 bool demo = 3 ;
167 }
168 """
169 try:
170 cmd = pyang_cmd.format('cord-tenant.yang')
171 print 'running command: {}'.format(cmd)
172 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
173 self.assertEqual(rc, 0)
174
175 self._compare_file(response, expected_response)
176 finally:
177 print "Test_04_cord_tenant_End:------------------ took {} " \
178 "secs\n\n".format(time.time() - t0)
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500179
180
181 def test_05_basic_rpc(self):
182 print "Test_05_basic_rpc_Start:------------------"
183 t0 = time.time()
184
185 # input file: /voltha/tests/utests/netconf/yang/basic-rpc.yang
186
187 expected_response = """
188 syntax = "proto3";
189 package basic_rpc;
190
191 message my_id {
192 uint32 my_id = 1 ;
193 }
194 message my_name {
195 string my_name = 2 ;
196 }
197 message my_status {
198 bool my_status = 3 ;
199 }
200 message my_input {
201 string my_input = 4 ;
202 }
203 message my_output {
204 string my_output = 5 ;
205 }
206
207 service basic_rpc {
208 rpc do_something(my_input) returns(my_output) {}
209 }
210 """
211 try:
212 cmd = pyang_cmd.format('basic-rpc.yang')
213 print 'running command: {}'.format(cmd)
214 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
215 self.assertEqual(rc, 0)
216
217 self._compare_file(response, expected_response)
218 finally:
219 print "Test_05_basic_rpc_End:------------------ took {} " \
220 "secs\n\n".format(time.time() - t0)