1#!/usr/bin/env python3
2import json
3
4import requests
5import urllib3
6
7"""
8Script for vmembers_counterifying correct paging of the Redfish LogService for the Journal.
9"""
10
11hostname = "192.168.7.2"
12entries_uri = (
13    f"https://{hostname}/redfish/v1/Managers/bmc/LogServices/Journal/Entries"
14)
15verify_ssl = False
16
17
18def login(session):
19    response = session.post(
20        f"https://{hostname}/redfish/v1/SessionService/Sessions",
21        verify=verify_ssl,
22        json={"UserName": "root", "Password": "0penBmc"},
23    )
24    response.raise_for_status()
25
26    auth = response.headers["X-Auth-Token"]
27
28    session.headers.update({"X-Auth-Token": auth})
29
30
31def get_golden(session):
32    response = session.get(
33        entries_uri,
34        verify=verify_ssl,
35    )
36    response.raise_for_status()
37
38    members = response.json()["Members"]
39
40    return members
41
42
43def main():
44    urllib3.disable_warnings()
45    session = requests.Session()
46
47    login(session)
48    members = get_golden(session)
49
50    failing = 0
51
52    print("Checking paging")
53    for top in [3, 2, 1]:
54        print(f"Checking {len(members)} pages with top {top}")
55        for page in range(0, len(members)):
56            skip = page
57            page_uri = f"{entries_uri}?$top={top}&$skip={skip}"
58            response3 = session.get(page_uri, verify=verify_ssl)
59            response3.raise_for_status()
60
61            # checking single paging
62            members3 = response3.json()["Members"]
63            if len(members3) != top:
64                print(f"Fail, size didn't match.  Size was {len(members3)}")
65            for index in range(0, len(members3)):
66                if skip + index >= len(members):
67                    print("Skipping entries created after golden page")
68                    continue
69                if members3[index] != members[skip + index]:
70                    print(f"Fail {page_uri} ")
71                    print(json.dumps(members3[0], indent=4))
72                    print("Returned Does not equal expected")
73                    print(json.dumps(members[page], indent=4))
74                    failing += 1
75                    continue
76
77            print(f"Checked {page_uri}")
78
79    print("Checking individual lookup")
80    for member_to_check in members:
81        odata_id = member_to_check["@odata.id"]
82        entry_uri = f"https://{hostname}{odata_id}"
83        response2 = session.get(
84            entry_uri,
85            verify=verify_ssl,
86        )
87        response2.raise_for_status()
88
89        member = response2.json()
90        if member != member_to_check:
91            print(f"Fail {entry_uri} ")
92            print("{response2} != {member_to_check}")
93            failing += 1
94            continue
95        print(f"Checked {entry_uri}")
96
97    if failing == 0:
98        print("ALL TESTS PASSED")
99    else:
100        print(f"{failing} TESTS FAILED")
101
102
103if __name__ == "__main__":
104    main()
105