1from redfish_requests import RedfishHttpStatus, RedfishRequest, Duration
2import pytest
3import time
4
5
6def test_get_telemetry_service(redfish):
7    r = redfish.get(redfish.telemetry_service_path)
8    assert r['Status']['State'] == 'Enabled', 'Invalid status of service'
9    assert Duration.to_seconds(r['MinCollectionInterval']) > 0, \
10        'Invalid duration format'
11    assert r['MaxReports'] > 0, 'Invalid count of max reports'
12
13
14def test_get_metric_definition_collection(redfish):
15    r = redfish.get(redfish.metric_definition_path)
16    assert 'Members' in r, 'Missing members property'
17    assert 'Members@odata.count' in r, 'Missing members count property'
18
19
20def test_verify_metric_definition_members_if_contains_metrics(redfish):
21    r = redfish.get(redfish.metric_definition_path)
22    for m in r['Members']:
23        path = m['@odata.id']
24        metricDefinition = redfish.get(path)
25        assert 'MetricProperties' in metricDefinition, 'Missing metrics'
26        assert len(metricDefinition['MetricProperties']) > 0, 'Missing metrics'
27
28
29def test_get_metric_definition_that_not_exist_expect_not_found(redfish):
30    r = redfish.get(f'{redfish.metric_definition_path}/NotExisting',
31                    code=RedfishHttpStatus.not_found)
32
33
34def test_get_metric_report_definition_collection(redfish):
35    r = redfish.get(redfish.metric_report_definition_path)
36    assert 'Members' in r, 'Missing members property'
37    assert 'Members@odata.count' in r, 'Missing members count property'
38
39
40def test_get_metric_report_definition_that_not_exist_expect_not_found(
41        redfish):
42    r = redfish.get(f'{redfish.metric_report_definition_path}/NotExisting',
43                    code=RedfishHttpStatus.not_found)
44
45
46def test_get_metric_report_collection(redfish):
47    r = redfish.get(redfish.metric_report_path)
48    assert 'Members' in r, 'Missing members property'
49    assert 'Members@odata.count' in r, 'Missing members count property'
50
51
52def test_get_metric_report_that_not_exist_expect_not_found(redfish):
53    r = redfish.get(f'{redfish.metric_report_path}/NotExisting',
54                    code=RedfishHttpStatus.not_found)
55
56
57def test_post_report_definition_with_empty_body_expect_bad_request(redfish):
58    redfish.post(redfish.metric_report_definition_path, body={},
59                 code=RedfishHttpStatus.bad_request)
60
61
62def test_post_report_definition_with_some_body_expect_bad_request(redfish):
63    redfish.post(redfish.metric_report_definition_path, body={'key': 'value'},
64                 code=RedfishHttpStatus.bad_request)
65
66
67def test_delete_non_exisiting_metric_report_definition(redfish):
68    redfish.delete(
69        f'{redfish.metric_report_definition_path}/NonExisitingReport',
70        code=RedfishHttpStatus.not_found)
71
72
73def test_add_report(redfish, report_definitions):
74    id = 'Test'
75    path = report_definitions.add_report(id)
76    assert 1 == len(report_definitions.get_collection())
77    r = redfish.get(f'{redfish.metric_report_definition_path}/{id}')
78    assert r['Id'] == id, 'Invalid Id, different then requested'
79    r = redfish.get(f'{redfish.metric_report_path}/{id}')
80    assert r['Id'] == id, 'Invalid Id, different then requested'
81
82
83def test_add_report_above_max_report_expect_bad_request(
84        telemetry, report_definitions):
85    id = 'Test'
86    for i in range(telemetry.max_reports):
87        report_definitions.add_report(id + str(i))
88    assert telemetry.max_reports == len(report_definitions.get_collection())
89    report_definitions.add_report(id + str(telemetry.max_reports),
90                                  metrics=[], interval=telemetry.min_interval,
91                                  code=RedfishHttpStatus.bad_request)
92
93
94def test_add_report_long_name(report_definitions):
95    report_definitions.add_report('Test' * 65)
96
97
98def test_add_report_twice_expect_bad_request(report_definitions):
99    report_definitions.add_report('Test')
100    report_definitions.add_report('Test', code=RedfishHttpStatus.bad_request)
101
102
103@pytest.mark.parametrize(
104    'actions', [[], ['RedfishEvent'], ['LogToMetricReportsCollection'],
105                ['RedfishEvent', 'LogToMetricReportsCollection']])
106def test_add_report_with_actions(actions, redfish, report_definitions):
107    report_definitions.add_report('Test', actions=actions)
108    r = redfish.get(f'{redfish.metric_report_definition_path}/Test')
109    assert r['ReportActions'] == actions, \
110        'Invalid actions, different then requested'
111
112
113@pytest.mark.parametrize(
114    'invalid_actions', [['NonExisting'], ['RedfishEvent', 'Partially'],
115                        ['LogToMetricNotThisOne']])
116def test_add_report_with_invalid_actions_expect_bad_request(
117        invalid_actions, report_definitions):
118    report_definitions.add_report('Test', actions=invalid_actions,
119                                  code=RedfishHttpStatus.bad_request)
120
121
122@pytest.mark.parametrize('invalid_id', ['test_-', 't t', 'T.T', 'T,t', 'T:t'])
123def test_add_report_with_invalid_id_expect_bad_request(
124        invalid_id, report_definitions):
125    report_definitions.add_report(invalid_id,
126                                  code=RedfishHttpStatus.bad_request)
127
128
129def test_add_report_with_metric(redfish, telemetry, report_definitions):
130    if len(telemetry.metrics) <= 0:
131        pytest.skip('Redfish has no sensor available')
132    metric = {'MetricId': 'Id1', 'MetricProperties': [telemetry.metrics[0]]}
133    report_definitions.add_report('Test', metrics=[metric])
134    r = redfish.get(redfish.metric_report_definition_path + '/Test')
135    assert len(r['Metrics']) == 1, 'Invalid Metrics, different then requested'
136    assert r['Metrics'][0]['MetricId'] == metric['MetricId'], \
137        'Invalid MetricId, different then requested'
138    assert r['Metrics'][0]['MetricProperties'] == metric['MetricProperties'], \
139        'Invalid MetricProperties, different then requested'
140
141
142def test_add_report_with_invalid_metric_expect_bad_request(report_definitions):
143    metric = {
144        'MetricId': 'Id1',
145        'MetricProperties':
146            ['/redfish/v1/Chassis/chassis/Sensors/NonExisting/Reading']
147    }
148    report_definitions.add_report('Test', metrics=[metric],
149                                  code=RedfishHttpStatus.bad_request)
150
151
152def test_add_report_with_many_metrics(redfish, telemetry, report_definitions):
153    if len(telemetry.metrics) <= 0:
154        pytest.skip('Redfish has no sensor available')
155    metrics = []
156    for i, prop in enumerate(telemetry.metrics):
157        metrics.append({'MetricId': f'Id{str(i)}', 'MetricProperties': [prop]})
158    report_definitions.add_report('Test', metrics=metrics)
159    r = redfish.get(redfish.metric_report_definition_path + '/Test')
160    assert len(r['Metrics']) == len(telemetry.metrics), \
161        'Invalid Metrics, different then requested'
162
163
164def test_add_report_on_request_with_metric_expect_updated_metric_report(
165        redfish, telemetry, report_definitions):
166    if len(telemetry.metrics) <= 0:
167        pytest.skip('Redfish has no sensor available')
168    metric = {'MetricId': 'Id1', 'MetricProperties': [telemetry.metrics[0]]}
169    report_definitions.add_report('Test', metrics=[metric], type='OnRequest')
170    r = redfish.get(redfish.metric_report_path + '/Test')
171    assert len(r['MetricValues']) > 0, 'Missing MetricValues'
172    metric_value = r['MetricValues'][0]
173    assert metric_value['MetricValue'], 'Missing MetricValues'
174    assert metric_value['MetricId'] == metric['MetricId'], \
175        'Different Id then set in request'
176    assert metric_value['MetricProperty'] == metric['MetricProperties'][0], \
177        'Different MetricProperty then set in request'
178
179
180def test_add_report_periodic_with_metric_expect_updated_metric_report(
181        redfish, telemetry, report_definitions):
182    if len(telemetry.metrics) <= 0:
183        pytest.skip('Redfish has no sensor available')
184    metric = {'MetricId': 'Id1', 'MetricProperties': [telemetry.metrics[0]]}
185    report_definitions.add_report(
186        'Test', metrics=[metric], type='Periodic',
187        interval=Duration.to_iso8061(telemetry.min_interval))
188    time.sleep(telemetry.min_interval + 1)
189    r = redfish.get(redfish.metric_report_path + '/Test')
190    assert len(r['MetricValues']) > 0, 'Missing MetricValues'
191    metric_value = r['MetricValues'][0]
192    assert metric_value['MetricValue'], 'Missing MetricValues'
193    assert metric_value['MetricId'] == metric['MetricId'], \
194        'Different Id then set in request'
195    assert metric_value['MetricProperty'] == metric['MetricProperties'][0], \
196        'Different MetricProperty then set in request'
197
198
199@pytest.mark.parametrize('interval', [10, 60, 2400, 90000])
200def test_add_report_check_if_duration_is_set(interval, redfish, telemetry,
201                                             report_definitions):
202    if interval < telemetry.min_interval:
203        pytest.skip('Interval is below minimal acceptable value, skipping')
204    id = f'Test{str(interval)}'
205    report_definitions.add_report(id, type='Periodic',
206                                  interval=Duration.to_iso8061(interval))
207    r = redfish.get(f'{redfish.metric_report_definition_path}/{id}')
208    assert r['Schedule']['RecurrenceInterval'], 'Missing RecurrenceInterval'
209    r_interval = Duration.to_seconds(r['Schedule']['RecurrenceInterval'])
210    assert interval == r_interval, 'Invalid interval, different then requested'
211
212
213@pytest.mark.parametrize(
214    'invalid', ['50000', 'P12ST', 'PT12S12', 'PPP' 'PD222T222H222M222.222S'])
215def test_add_report_with_invalid_duration_response_bad_request(
216        invalid, report_definitions):
217    r = report_definitions.add_report('Test', type='Periodic', interval=invalid,
218                                      code=RedfishHttpStatus.bad_request)
219    assert r['error']['@Message.ExtendedInfo'][0], \
220        'Wrong response, not an error'
221    info = r['error']['@Message.ExtendedInfo'][0]
222    assert 'RecurrenceInterval' in info['MessageArgs'], \
223        'Wrong response, should contain "RecurrenceInterval"'
224
225
226def test_stress_add_reports_with_many_metrics_check_metric_reports(
227        redfish, telemetry, report_definitions):
228    if len(telemetry.metrics) <= 0:
229        pytest.skip('Redfish has no sensor available')
230    metrics = []
231    for i, prop in enumerate(telemetry.metrics):
232        metrics.append({'MetricId': f'Id{str(i)}', 'MetricProperties': [prop]})
233    for i in range(telemetry.max_reports):
234        report_definitions.add_report(f'Test{str(i)}', metrics=metrics)
235    for i in range(telemetry.max_reports):
236        r = redfish.get(f'{redfish.metric_report_definition_path}/Test{str(i)}')
237        assert len(r['Metrics']) == len(telemetry.metrics), \
238            'Invalid Metrics, different then requested'
239    for i in range(telemetry.max_reports):
240        r = redfish.get(f'{redfish.metric_report_path}/Test{str(i)}')
241        assert len(r['MetricValues']) > 0, 'Missing MetricValues'
242