1313# License for the specific language governing permissions and limitations
1414# under the License.
1515
16- import re
16+ import json
1717import uuid
1818
1919from openstackclient .tests .functional import base
@@ -27,37 +27,45 @@ class TestMeter(base.TestCase):
2727 # has its own needs and there are collisions when running
2828 # tests in parallel.
2929
30- @classmethod
31- def setUpClass (cls ):
32- # Set up some regex for matching below
33- cls .re_name = re .compile ("name\s+\|\s+([^|]+?)\s+\|" )
34- cls .re_shared = re .compile ("shared\s+\|\s+(\S+)" )
35- cls .re_description = re .compile ("description\s+\|\s+([^|]+?)\s+\|" )
36-
3730 def test_meter_delete (self ):
3831 """Test create, delete multiple"""
3932 name1 = uuid .uuid4 ().hex
4033 name2 = uuid .uuid4 ().hex
41-
42- raw_output = self .openstack (
43- 'network meter create ' + name1 ,
34+ description = 'fakedescription'
35+ json_output = json .loads (self .openstack (
36+ 'network meter create -f json ' + name1 + ' --description '
37+ + description )
4438 )
4539 self .assertEqual (
4640 name1 ,
47- re . search ( self . re_name , raw_output ). group ( 1 ),
41+ json_output . get ( 'name' ),
4842 )
4943 # Check if default shared values
5044 self .assertEqual (
51- 'False' ,
52- re .search (self .re_shared , raw_output ).group (1 )
45+ False ,
46+ json_output .get ('shared' )
47+ )
48+ self .assertEqual (
49+ 'fakedescription' ,
50+ json_output .get ('description' )
5351 )
5452
55- raw_output = self .openstack (
56- 'network meter create ' + name2 ,
53+ json_output_2 = json .loads (self .openstack (
54+ 'network meter create -f json ' + name2 + ' --description '
55+ + description )
5756 )
5857 self .assertEqual (
5958 name2 ,
60- re .search (self .re_name , raw_output ).group (1 ),
59+ json_output_2 .get ('name' ),
60+ )
61+ # Check if default shared values
62+ self .assertEqual (
63+ False ,
64+ json_output_2 .get ('shared' )
65+ )
66+ self .assertEqual (
67+ 'fakedescription' ,
68+ json_output_2 .get ('description' )
6169 )
6270
6371 raw_output = self .openstack (
@@ -68,35 +76,83 @@ def test_meter_delete(self):
6876 def test_meter_list (self ):
6977 """Test create, list filters, delete"""
7078 name1 = uuid .uuid4 ().hex
71- raw_output = self .openstack (
72- 'network meter create --description Test1 --share ' + name1 ,
79+ json_output = json .loads (self .openstack (
80+ 'network meter create -f json --description Test1 --share '
81+ + name1 )
7382 )
7483 self .addCleanup (self .openstack , 'network meter delete ' + name1 )
7584
7685 self .assertEqual (
7786 'Test1' ,
78- re . search ( self . re_description , raw_output ). group ( 1 ),
87+ json_output . get ( 'description' ),
7988 )
8089 self .assertEqual (
81- ' True' ,
82- re . search ( self . re_shared , raw_output ). group ( 1 ),
90+ True ,
91+ json_output . get ( 'shared' ),
8392 )
8493
8594 name2 = uuid .uuid4 ().hex
86- raw_output = self .openstack (
87- 'network meter create --description Test2 --no-share ' + name2 ,
95+ json_output_2 = json .loads (self .openstack (
96+ 'network meter create -f json --description Test2 --no-share '
97+ + name2 )
8898 )
8999 self .addCleanup (self .openstack , 'network meter delete ' + name2 )
90100
91101 self .assertEqual (
92102 'Test2' ,
93- re .search (self .re_description , raw_output ).group (1 ),
103+ json_output_2 .get ('description' )
104+ )
105+ self .assertEqual (
106+ False ,
107+ json_output_2 .get ('shared' )
108+ )
109+
110+ raw_output = json .loads (self .openstack ('network meter list -f json' ))
111+ name_list = [item .get ('Name' ) for item in raw_output ]
112+ self .assertIn (name1 , name_list )
113+ self .assertIn (name2 , name_list )
114+
115+ def test_meter_show (self ):
116+ """Test create, show, delete"""
117+ name1 = uuid .uuid4 ().hex
118+ description = 'fakedescription'
119+ json_output = json .loads (self .openstack (
120+ 'network meter create -f json ' + name1 + ' --description '
121+ + description )
122+ )
123+ meter_id = json_output .get ('id' )
124+ self .addCleanup (self .openstack , 'network meter delete ' + name1 )
125+
126+ # Test show with ID
127+ json_output = json .loads (self .openstack (
128+ 'network meter show -f json ' + meter_id )
94129 )
95130 self .assertEqual (
96- 'False' ,
97- re .search (self .re_shared , raw_output ).group (1 ),
131+ False ,
132+ json_output .get ('shared' )
133+ )
134+ self .assertEqual (
135+ 'fakedescription' ,
136+ json_output .get ('description' )
137+ )
138+ self .assertEqual (
139+ name1 ,
140+ json_output .get ('name' )
98141 )
99142
100- raw_output = self .openstack ('network meter list' )
101- self .assertIsNotNone (re .search (name1 + "\s+\|\s+Test1" , raw_output ))
102- self .assertIsNotNone (re .search (name2 + "\s+\|\s+Test2" , raw_output ))
143+ # Test show with name
144+ json_output = json .loads (self .openstack (
145+ 'network meter show -f json ' + name1 )
146+ )
147+ self .assertEqual (
148+ meter_id ,
149+ json_output .get ('id' )
150+ )
151+ self .assertEqual (
152+ False ,
153+ json_output .get ('shared' )
154+ )
155+ self .assertEqual (
156+ 'fakedescription' ,
157+ json_output .get ('description' )
158+ )
0 commit comments