| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
"""Test Timing pseudo-command""" |
| 14 |
|
| 15 |
import datetime |
| 16 |
|
| 17 |
from keystoneauth1 import session |
| 18 |
|
| 19 |
from osc_lib.command import timing |
| 20 |
from osc_lib.test import base |
| 21 |
|
| 22 |
AUTH_TOKEN = "foobar" |
| 23 |
AUTH_URL = "http://0.0.0.0" |
| 24 |
|
| 25 |
timing_method = 'GET' |
| 26 |
timing_url = 'http://localhost:5000' |
| 27 |
timing_elapsed = 0.872809 |
| 28 |
|
| 29 |
|
| 30 |
class FakeGenericClient: |
| 31 |
def __init__(self, **kwargs): |
| 32 |
self.auth_token = kwargs['token'] |
| 33 |
self.management_url = kwargs['endpoint'] |
| 34 |
|
| 35 |
|
| 36 |
class TestTiming(base.TestCommand): |
| 37 |
columns = ( |
| 38 |
'URL', |
| 39 |
'Seconds', |
| 40 |
) |
| 41 |
|
| 42 |
def setUp(self): |
| 43 |
super().setUp() |
| 44 |
|
| 45 |
self.app.timing_data = [] |
| 46 |
|
| 47 |
self.app.client_manager.compute = FakeGenericClient( |
| 48 |
endpoint=AUTH_URL, token=AUTH_TOKEN |
| 49 |
) |
| 50 |
|
| 51 |
self.app.client_manager.volume = FakeGenericClient( |
| 52 |
endpoint=AUTH_URL, |
| 53 |
token=AUTH_TOKEN, |
| 54 |
) |
| 55 |
|
| 56 |
# Get the command object to test |
| 57 |
self.cmd = timing.Timing(self.app, None) |
| 58 |
|
| 59 |
def test_timing_list_no_data(self): |
| 60 |
arglist = [] |
| 61 |
verifylist = [] |
| 62 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 63 |
|
| 64 |
# In base command class Lister in cliff, abstract method take_action() |
| 65 |
# returns a tuple containing the column names and an iterable |
| 66 |
# containing the data to be listed. |
| 67 |
columns, data = self.cmd.take_action(parsed_args) |
| 68 |
|
| 69 |
self.assertEqual(self.columns, columns) |
| 70 |
datalist = [ |
| 71 |
( |
| 72 |
'Total', |
| 73 |
0.0, |
| 74 |
) |
| 75 |
] |
| 76 |
self.assertEqual(datalist, data) |
| 77 |
|
| 78 |
def test_timing_list(self): |
| 79 |
self.app.timing_data = [ |
| 80 |
session.RequestTiming( |
| 81 |
method=timing_method, |
| 82 |
url=timing_url, |
| 83 |
elapsed=datetime.timedelta( |
| 84 |
microseconds=timing_elapsed * 1000000 |
| 85 |
), |
| 86 |
) |
| 87 |
] |
| 88 |
|
| 89 |
arglist = [] |
| 90 |
verifylist = [] |
| 91 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 92 |
|
| 93 |
# In base command class Lister in cliff, abstract method take_action() |
| 94 |
# returns a tuple containing the column names and an iterable |
| 95 |
# containing the data to be listed. |
| 96 |
columns, data = self.cmd.take_action(parsed_args) |
| 97 |
self.assertEqual(self.columns, columns) |
| 98 |
datalist = [ |
| 99 |
(timing_method + ' ' + timing_url, timing_elapsed), |
| 100 |
('Total', timing_elapsed), |
| 101 |
] |
| 102 |
self.assertEqual(datalist, data) |