88from cliff import show
99
1010from coriolisclient .cli import licensing
11+ from coriolisclient import constants
1112from coriolisclient .tests import test_base
1213
14+ # sentinel for "this licence has no 'licence_version' attribute at all":
15+ _UNSET = object ()
16+
1317
1418class LicensingStatusFormatterTestCase (test_base .CoriolisBaseTestCase ):
1519 """Test suite for the Coriolis Client Licensing Status Formatter."""
@@ -18,48 +22,92 @@ def setUp(self):
1822 super (LicensingStatusFormatterTestCase , self ).setUp ()
1923 self .licence = licensing .LicensingStatusFormatter ()
2024
21- def test_get_formatted_data (self ):
25+ @staticmethod
26+ def _make_stats (offset ):
27+ """Builds a stats body whose counters are all distinguishable."""
28+ return {
29+ field : offset + i
30+ for i , field in enumerate (constants .LICENCE_STATS_FIELDS )}
31+
32+ def _make_status (self , standard_stats , sap_stats , licence_editions ):
2233 obj = mock .Mock ()
2334 obj .appliance_id = mock .sentinel .appliance_id
2435 obj .earliest_licence_expiry_time = \
2536 mock .sentinel .earliest_licence_expiry_time
2637 obj .latest_licence_expiry_time = \
2738 mock .sentinel .latest_licence_expiry_time
28- obj .current_performed_migrations = \
29- mock .sentinel .current_performed_migrations
30- obj .current_performed_replicas = \
31- mock .sentinel .current_performed_replicas
32- obj .current_available_migrations = \
33- mock .sentinel .current_available_migrations
34- obj .current_available_replicas = \
35- mock .sentinel .current_available_replicas
36- obj .lifetime_performed_migrations = \
37- mock .sentinel .lifetime_performed_migrations
38- obj .lifetime_performed_replicas = \
39- mock .sentinel .lifetime_performed_replicas
40- obj .lifetime_available_migrations = \
41- mock .sentinel .lifetime_available_migrations
42- obj .lifetime_available_replicas = \
43- mock .sentinel .lifetime_available_replicas
39+ obj .licence_editions = licence_editions
40+ setattr (obj , constants .LICENCE_STATS_KEY_STANDARD , standard_stats )
41+ setattr (obj , constants .LICENCE_STATS_KEY_SAP , sap_stats )
42+ for field in constants .LICENCE_STATS_FIELDS :
43+ setattr (obj , field , standard_stats [field ] + sap_stats [field ])
44+ return obj
45+
46+ def test_columns (self ):
47+ self .assertEqual (
48+ [
49+ "appliance_id" ,
50+ "licence_editions" ,
51+ "earliest_licence_expiry_time" ,
52+ "latest_licence_expiry_time" ,
53+ ] + list (constants .LICENCE_STATS_FIELDS ) + [
54+ "standard_%s" % f for f in constants .LICENCE_STATS_FIELDS
55+ ] + [
56+ "sap_%s" % f for f in constants .LICENCE_STATS_FIELDS
57+ ],
58+ self .licence .columns
59+ )
60+
61+ def test_get_formatted_data (self ):
62+ standard_stats = self ._make_stats (0 )
63+ sap_stats = self ._make_stats (100 )
64+ obj = self ._make_status (standard_stats , sap_stats ,
65+ ["Standard" , "SAP" ])
4466
4567 result = self .licence ._get_formatted_data (obj )
4668
4769 self .assertEqual (
4870 [
4971 mock .sentinel .appliance_id ,
72+ "Standard, SAP" ,
5073 mock .sentinel .earliest_licence_expiry_time ,
5174 mock .sentinel .latest_licence_expiry_time ,
52- mock .sentinel .current_performed_migrations ,
53- mock .sentinel .current_performed_replicas ,
54- mock .sentinel .current_available_migrations ,
55- mock .sentinel .current_available_replicas ,
56- mock .sentinel .lifetime_performed_migrations ,
57- mock .sentinel .lifetime_performed_replicas ,
58- mock .sentinel .lifetime_available_migrations ,
59- mock .sentinel .lifetime_available_replicas
75+ ] + [
76+ standard_stats [f ] + sap_stats [f ]
77+ for f in constants .LICENCE_STATS_FIELDS
78+ ] + [
79+ standard_stats [f ] for f in constants .LICENCE_STATS_FIELDS
80+ ] + [
81+ sap_stats [f ] for f in constants .LICENCE_STATS_FIELDS
6082 ],
6183 result
6284 )
85+ self .assertEqual (len (self .licence .columns ), len (result ))
86+
87+ def test_get_formatted_data_standard_only (self ):
88+ standard_stats = self ._make_stats (0 )
89+ sap_stats = dict .fromkeys (constants .LICENCE_STATS_FIELDS , 0 )
90+ obj = self ._make_status (standard_stats , sap_stats , ["Standard" ])
91+
92+ result = self .licence ._get_formatted_data (obj )
93+
94+ self .assertEqual ("Standard" , result [1 ])
95+ self .assertEqual (
96+ [standard_stats [f ] for f in constants .LICENCE_STATS_FIELDS ],
97+ result [4 :4 + len (constants .LICENCE_STATS_FIELDS )]
98+ )
99+ self .assertEqual (
100+ [0 ] * len (constants .LICENCE_STATS_FIELDS ),
101+ result [- len (constants .LICENCE_STATS_FIELDS ):]
102+ )
103+
104+ def test_get_formatted_data_no_licences (self ):
105+ empty = dict .fromkeys (constants .LICENCE_STATS_FIELDS , 0 )
106+ obj = self ._make_status (empty , dict (empty ), [])
107+
108+ result = self .licence ._get_formatted_data (obj )
109+
110+ self .assertIsNone (result [1 ])
63111
64112
65113class LicenceFormatterTestCase (test_base .CoriolisBaseTestCase ):
@@ -85,19 +133,24 @@ def test_get_sorted_list(self):
85133 result
86134 )
87135
88- def test_get_formatted_data (self ):
89- obj = mock .Mock ()
136+ @staticmethod
137+ def _make_licence (licence_version = _UNSET ):
138+ obj = mock .Mock (spec = [
139+ 'id' , 'issue_date' , 'migrations' , 'replicas' , 'period_start' ,
140+ 'period_end' , 'period_duration' ] + (
141+ [] if licence_version is _UNSET else ['licence_version' ]))
90142 obj .id = mock .sentinel .id
91143 obj .issue_date = mock .sentinel .issue_date
92144 obj .migrations = mock .sentinel .migrations
93145 obj .replicas = mock .sentinel .replicas
94146 obj .period_start = mock .sentinel .period_start
95147 obj .period_end = mock .sentinel .period_end
96148 obj .period_duration = mock .sentinel .period_duration
97- obj . licence_version = mock . sentinel . licence_version
98-
99- result = self . licence . _get_formatted_data ( obj )
149+ if licence_version is not _UNSET :
150+ obj . licence_version = licence_version
151+ return obj
100152
153+ def _assert_formatted (self , obj , licence_version , licence_edition ):
101154 self .assertEqual (
102155 (
103156 mock .sentinel .id ,
@@ -107,11 +160,38 @@ def test_get_formatted_data(self):
107160 mock .sentinel .period_start ,
108161 mock .sentinel .period_end ,
109162 mock .sentinel .period_duration ,
110- mock .sentinel .licence_version
163+ licence_version ,
164+ licence_edition ,
111165 ),
112- result
166+ self . licence . _get_formatted_data ( obj )
113167 )
114168
169+ def test_get_formatted_data (self ):
170+ self ._assert_formatted (
171+ self ._make_licence (constants .LICENCE_VERSION_V2 ),
172+ constants .LICENCE_VERSION_V2 , constants .LICENCE_EDITION_STANDARD )
173+
174+ def test_get_formatted_data_sap (self ):
175+ self ._assert_formatted (
176+ self ._make_licence (constants .LICENCE_VERSION_V2_SAP ),
177+ constants .LICENCE_VERSION_V2_SAP , constants .LICENCE_EDITION_SAP )
178+
179+ def test_get_formatted_data_v1 (self ):
180+ self ._assert_formatted (
181+ self ._make_licence (constants .LICENCE_VERSION_V1 ),
182+ constants .LICENCE_VERSION_V1 , constants .LICENCE_EDITION_STANDARD )
183+
184+ def test_get_formatted_data_unversioned_licence (self ):
185+ # licences issued before licence versioning carry no version at all:
186+ self ._assert_formatted (
187+ self ._make_licence (), None , constants .LICENCE_EDITION_STANDARD )
188+
189+ def test_get_formatted_data_unknown_version (self ):
190+ # an unknown version must not be misreported as a standard licence:
191+ self ._assert_formatted (
192+ self ._make_licence ("v3-something" ),
193+ "v3-something" , "v3-something" )
194+
115195
116196class LicensingApplianceStatusTestCase (test_base .CoriolisBaseTestCase ):
117197 """Test suite for the Coriolis Client Licensing Appliance Status."""
0 commit comments