| 1 | import pytest |
| 2 | |
| 3 | import grpc |
| 4 | import labgrid.remote.generated.labgrid_coordinator_pb2_grpc as labgrid_coordinator_pb2_grpc |
| 5 | import labgrid.remote.generated.labgrid_coordinator_pb2 as labgrid_coordinator_pb2 |
| 6 | |
| 7 | |
| 8 | @pytest.fixture(scope="function") |
| 9 | def channel_stub(): |
| 10 | import queue |
| 11 | |
| 12 | queue = queue.Queue() |
| 13 | |
| 14 | channel = grpc.insecure_channel("127.0.0.1:20408") |
| 15 | stub = labgrid_coordinator_pb2_grpc.CoordinatorStub(channel) |
| 16 | |
| 17 | def generate_startup(queue): |
| 18 | msg = labgrid_coordinator_pb2.ClientInMessage() |
| 19 | msg.startup.version = "2.0.0" |
| 20 | msg.startup.name = "testclient" |
| 21 | messages = [msg] |
| 22 | for msg in messages: |
| 23 | yield msg |
| 24 | while True: |
| 25 | msg = queue.get() |
| 26 | yield msg |
| 27 | queue.task_done() |
| 28 | |
| 29 | stream = stub.ClientStream(generate_startup(queue)) |
| 30 | yield stub |
| 31 | channel.close() |
| 32 | |
| 33 | |
| 34 | @pytest.fixture(scope="function") |
| 35 | def coordinator_place(channel_stub): |
| 36 | name = "test" |
| 37 | place = labgrid_coordinator_pb2.AddPlaceRequest(name=name) |
| 38 | res = channel_stub.AddPlace(place) |
| 39 | assert res, f"There was an error: {res}" |
| 40 | return channel_stub |
| 41 | |
| 42 | |
| 43 | def test_startup(coordinator): |
| 44 | pass |
| 45 | |
| 46 | |
| 47 | def test_coordinator_add_place(coordinator, channel_stub): |
| 48 | name = "test" |
| 49 | place = labgrid_coordinator_pb2.AddPlaceRequest(name=name) |
| 50 | res = channel_stub.AddPlace(place) |
| 51 | assert res, f"There was an error: {res}" |
| 52 | |
| 53 | |
| 54 | def test_coordinator_del_place(coordinator, channel_stub): |
| 55 | name = "test" |
| 56 | place = labgrid_coordinator_pb2.AddPlaceRequest(name=name) |
| 57 | res = channel_stub.AddPlace(place) |
| 58 | assert res, f"There was an error: {res}" |
| 59 | place = labgrid_coordinator_pb2.DeletePlaceRequest(name=name) |
| 60 | res = channel_stub.DeletePlace(place) |
| 61 | assert res, f"There was an error: {res}" |
| 62 | |
| 63 | |
| 64 | def test_coordinator_get_places(coordinator, channel_stub): |
| 65 | name = "test" |
| 66 | place = labgrid_coordinator_pb2.AddPlaceRequest(name=name) |
| 67 | res = channel_stub.AddPlace(place) |
| 68 | assert res, f"There was an error: {res}" |
| 69 | name = "test2" |
| 70 | place = labgrid_coordinator_pb2.AddPlaceRequest(name=name) |
| 71 | res = channel_stub.AddPlace(place) |
| 72 | assert res, f"There was an error: {res}" |
| 73 | |
| 74 | request = labgrid_coordinator_pb2.GetPlacesRequest() |
| 75 | res = channel_stub.GetPlaces(request) |
| 76 | |
| 77 | from labgrid.remote.common import Place |
| 78 | |
| 79 | places = set() |
| 80 | names = set() |
| 81 | for pb2 in res.places: |
| 82 | place = Place.from_pb2(pb2) |
| 83 | places.add(place) |
| 84 | names.add(place.name) |
| 85 | |
| 86 | assert len(places) == 2, f"Returned places not two: {places}" |
| 87 | assert set(names) == {"test", "test2"}, f"There was an error: {res}" |
| 88 | |
| 89 | |
| 90 | def test_coordinator_exporter_session(coordinator, channel_stub): |
| 91 | import queue |
| 92 | |
| 93 | queue = queue.Queue() |
| 94 | |
| 95 | def generate_startup(queue): |
| 96 | msg = labgrid_coordinator_pb2.ExporterInMessage() |
| 97 | msg.startup.version = "2.0.0" |
| 98 | msg.startup.name = "testporter" |
| 99 | messages = [msg] |
| 100 | for msg in messages: |
| 101 | yield msg |
| 102 | while True: |
| 103 | msg = queue.get() |
| 104 | yield msg |
| 105 | queue.task_done() |
| 106 | |
| 107 | coordinator = channel_stub.ExporterStream(generate_startup(queue), wait_for_ready=True) |
| 108 | |
| 109 | |
| 110 | def test_coordinator_place_acquire(coordinator, coordinator_place): |
| 111 | stub = coordinator_place |
| 112 | res = stub.AcquirePlace(labgrid_coordinator_pb2.AcquirePlaceRequest(placename="test")) |
| 113 | assert res |
| 114 | |
| 115 | |
| 116 | def test_coordinator_place_acquire_release(coordinator, coordinator_place): |
| 117 | stub = coordinator_place |
| 118 | res = stub.AcquirePlace(labgrid_coordinator_pb2.AcquirePlaceRequest(placename="test")) |
| 119 | assert res |
| 120 | res = stub.ReleasePlace(labgrid_coordinator_pb2.ReleasePlaceRequest(placename="test")) |
| 121 | assert res |
| 122 | |
| 123 | |
| 124 | def test_coordinator_place_add_alias(coordinator, coordinator_place): |
| 125 | stub = coordinator_place |
| 126 | res = stub.AddPlaceAlias(labgrid_coordinator_pb2.AddPlaceAliasRequest(placename="test", alias="testalias")) |
| 127 | assert res |
| 128 | |
| 129 | |
| 130 | def test_coordinator_place_add_remove_alias(coordinator, coordinator_place): |
| 131 | stub = coordinator_place |
| 132 | res = stub.AddPlaceAlias(labgrid_coordinator_pb2.AddPlaceAliasRequest(placename="test", alias="testalias")) |
| 133 | assert res |
| 134 | res = stub.DeletePlaceAlias(labgrid_coordinator_pb2.DeletePlaceAliasRequest(placename="test", alias="testalias")) |
| 135 | assert res |
| 136 | |
| 137 | |
| 138 | def test_coordinator_place_set_tags(coordinator, coordinator_place): |
| 139 | stub = coordinator_place |
| 140 | res = stub.SetPlaceTags(labgrid_coordinator_pb2.SetPlaceTagsRequest(placename="test", tags={"one": "two"})) |
| 141 | assert res |
| 142 | |
| 143 | |
| 144 | def test_coordinator_place_set_comment(coordinator, coordinator_place): |
| 145 | stub = coordinator_place |
| 146 | res = stub.SetPlaceComment(labgrid_coordinator_pb2.SetPlaceCommentRequest(placename="test", comment="testcomment")) |
| 147 | assert res |
| 148 | |
| 149 | |
| 150 | def test_coordinator_place_add_match(coordinator, coordinator_place): |
| 151 | stub = coordinator_place |
| 152 | res = stub.AddPlaceMatch( |
| 153 | labgrid_coordinator_pb2.AddPlaceMatchRequest(placename="test", pattern="this/test/pattern") |
| 154 | ) |
| 155 | assert res |
| 156 | |
| 157 | |
| 158 | def test_coordinator_place_add_delete_match(coordinator, coordinator_place): |
| 159 | stub = coordinator_place |
| 160 | res = stub.AddPlaceMatch( |
| 161 | labgrid_coordinator_pb2.AddPlaceMatchRequest(placename="test", pattern="this/test/pattern") |
| 162 | ) |
| 163 | assert res |
| 164 | res = stub.DeletePlaceMatch( |
| 165 | labgrid_coordinator_pb2.DeletePlaceMatchRequest(placename="test", pattern="this/test/pattern") |
| 166 | ) |
| 167 | assert res |
| 168 | |
| 169 | |
| 170 | def test_coordinator_place_allow(coordinator, coordinator_place): |
| 171 | stub = coordinator_place |
| 172 | res = stub.AcquirePlace(labgrid_coordinator_pb2.AcquirePlaceRequest(placename="test")) |
| 173 | assert res |
| 174 | res = stub.AllowPlace(labgrid_coordinator_pb2.AllowPlaceRequest(placename="test", user="othertest")) |
| 175 | assert res |
| 176 | |
| 177 | |
| 178 | def test_coordinator_create_reservation(coordinator, coordinator_place): |
| 179 | tags = {"board": "test"} |
| 180 | stub = coordinator_place |
| 181 | res = stub.SetPlaceTags(labgrid_coordinator_pb2.SetPlaceTagsRequest(placename="test", tags=tags)) |
| 182 | assert res |
| 183 | res = stub.CreateReservation( |
| 184 | labgrid_coordinator_pb2.CreateReservationRequest( |
| 185 | filters={ |
| 186 | "main": labgrid_coordinator_pb2.Reservation.Filter(filter={"board": "test"}), |
| 187 | }, |
| 188 | prio=1.0, |
| 189 | ) |
| 190 | ) |
| 191 | assert res |
| 192 | res: labgrid_coordinator_pb2.CreateReservationResponse |
| 193 | assert len(res.reservation.token) > 0 |