@@ -117,9 +117,28 @@ def tearDown(self):
117117 def delete_tariffs (self ):
118118 for tariff in self .tariffs :
119119 cmd = quotaTariffDelete .quotaTariffDeleteCmd ()
120- cmd .id = tariff .uuid
120+ cmd .id = tariff .id
121121 self .apiclient .quotaTariffDelete (cmd )
122122
123+ def insert_usage_and_update_quota (self , zone_id , account_id , domain_id , relative_start_date , relative_end_date ):
124+ start_date = f"DATE_ADD(UTC_TIMESTAMP(), INTERVAL { relative_start_date } HOUR)"
125+ end_date = f"DATE_ADD(UTC_TIMESTAMP(), INTERVAL { relative_end_date } HOUR)"
126+
127+ # Manually insert a usage regarding the usage type 21 (VM_DISK_IO_READ)
128+ sql_query = (f"INSERT INTO cloud_usage.cloud_usage (zone_id,account_id,domain_id,description,usage_display,usage_type,raw_usage,vm_instance_id,vm_name,offering_id,template_id,"
129+ f"usage_id,`type`,`size`,network_id,start_date,end_date,virtual_size,cpu_speed,cpu_cores,memory,quota_calculated,is_hidden,state)"
130+ f" VALUES ('{ zone_id } ','{ account_id } ','{ domain_id } ','Test','1 Hrs',21,1,NULL,NULL,NULL,NULL,NULL,'VirtualMachine',NULL,NULL,{ start_date } ,{ end_date } ,NULL,NULL,NULL,NULL,0,0,NULL);" )
131+ self .debug (sql_query )
132+ self .dbclient .execute (sql_query )
133+
134+ # Update quota to calculate the balance of the account
135+ cmd = quotaUpdate .quotaUpdateCmd ()
136+ self .apiclient .quotaUpdate (cmd )
137+ time .sleep (1 )
138+
139+ def format_date (self , date ):
140+ return date .strftime ("%Y-%m-%d %H:%M:%S" )
141+
123142 @attr (tags = ["advanced" , "smoke" , "quota" ], required_hardware = "false" )
124143 def test_quota_balance (self ):
125144 """
@@ -146,6 +165,7 @@ def test_quota_balance(self):
146165 cmd .domainid = self .domain .id
147166 cmd .value = 100
148167 self .apiclient .quotaCredits (cmd )
168+ time .sleep (1 )
149169
150170 # Fetch account ID from account_uuid
151171 account_id_select = f"SELECT id FROM account WHERE uuid = '{ self .account .id } ';"
@@ -165,27 +185,24 @@ def test_quota_balance(self):
165185 qresultset = self .dbclient .execute (zone_id_select )
166186 zone_id = qresultset [0 ][0 ]
167187
168- start_date = datetime .datetime .now () + datetime .timedelta (seconds = 1 )
169- end_date = datetime .datetime .now () + datetime .timedelta (hours = 1 )
170-
171- # Manually insert a usage regarding the usage type 21 (VM_DISK_IO_READ)
172- sql_query = (f"INSERT INTO cloud_usage.cloud_usage (zone_id,account_id,domain_id,description,usage_display,usage_type,raw_usage,vm_instance_id,vm_name,offering_id,template_id,"
173- f"usage_id,`type`,`size`,network_id,start_date,end_date,virtual_size,cpu_speed,cpu_cores,memory,quota_calculated,is_hidden,state)"
174- f" VALUES ('{ zone_id } ','{ account_id } ','{ domain_id } ','Test','1 Hrs',21,1,NULL,NULL,NULL,NULL,NULL,'VirtualMachine',NULL,NULL,'{ start_date } ','{ end_date } ',NULL,NULL,NULL,NULL,0,0,NULL);" )
175- self .debug (sql_query )
176- self .dbclient .execute (sql_query )
177-
178- # Update quota to calculate the balance of the account
179- cmd = quotaUpdate .quotaUpdateCmd ()
180- self .apiclient .quotaUpdate (cmd )
188+ # Generate three quota_balance entries
189+ self .insert_usage_and_update_quota (zone_id , account_id , domain_id , 0 , 1 )
190+ self .insert_usage_and_update_quota (zone_id , account_id , domain_id , 1 , 2 )
191+ self .insert_usage_and_update_quota (zone_id , account_id , domain_id , 2 , 3 )
181192
182193 # Retrieve the quota balance of the account
183194 cmd = quotaBalance .quotaBalanceCmd ()
184195 cmd .domainid = self .account .domainid
185196 cmd .account = self .account .name
197+ cmd .startdate = datetime .datetime .now () + datetime .timedelta (hours = - 1 )
198+ cmd .enddate = datetime .datetime .now () + datetime .timedelta (hours = 3 )
186199 response = self .apiclient .quotaBalance (cmd )
187200
188- self .debug (f"The quota balance for the account { self .account .name } is { response .balance } ." )
189- self .assertEqual (response .balance .startquota , 90 , f"The `startQuota` response field is supposed to be 90 but was { response .balance .startquota } ." )
201+ self .assertTrue (len (response .balance .balances ) == 4 , f"Expected 4 balance entries for between { self .format_date (cmd .startdate )} " +
202+ f"and { self .format_date (cmd .enddate )} but got { len (response .balance .balances )} ." )
203+ for i , balance in enumerate (response .balance .balances ):
204+ expected_balance = 100 - 10 * i
205+ self .debug (f"The quota balance for the account { self .account .name } at { balance .date } was { balance .balance } ." )
206+ self .assertEqual (balance .balance , expected_balance , f"The `balance` field at { balance .date } is supposed to be { expected_balance } but was { balance .balance } ." )
190207
191208 return
0 commit comments