|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.devtools.mobileharness.infra.client.api.mode.local; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.mockito.ArgumentMatchers.eq; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import com.google.common.collect.ImmutableListMultimap; |
| 25 | +import com.google.devtools.mobileharness.api.model.lab.DeviceId; |
| 26 | +import com.google.devtools.mobileharness.infra.client.api.util.dimension.DeviceTempRequiredDimensionManager; |
| 27 | +import com.google.devtools.mobileharness.infra.client.api.util.dimension.DeviceTempRequiredDimensionManager.DeviceKey; |
| 28 | +import com.google.devtools.mobileharness.infra.controller.device.DeviceIdManager; |
| 29 | +import com.google.wireless.qa.mobileharness.shared.model.lab.DeviceLocator; |
| 30 | +import com.google.wireless.qa.mobileharness.shared.model.lab.LabLocator; |
| 31 | +import java.time.Duration; |
| 32 | +import java.util.Optional; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.junit.runners.JUnit4; |
| 38 | +import org.mockito.ArgumentCaptor; |
| 39 | +import org.mockito.Captor; |
| 40 | +import org.mockito.Mock; |
| 41 | +import org.mockito.junit.MockitoJUnit; |
| 42 | +import org.mockito.junit.MockitoRule; |
| 43 | + |
| 44 | +@RunWith(JUnit4.class) |
| 45 | +public class LocalDeviceReserverTest { |
| 46 | + |
| 47 | + @Rule public final MockitoRule mockito = MockitoJUnit.rule(); |
| 48 | + |
| 49 | + @Mock private DeviceTempRequiredDimensionManager manager; |
| 50 | + @Mock private DeviceIdManager idManager; |
| 51 | + |
| 52 | + @Captor private ArgumentCaptor<DeviceKey> deviceKeyCaptor; |
| 53 | + |
| 54 | + private LocalDeviceReserver reserver; |
| 55 | + |
| 56 | + @Before |
| 57 | + public void setUp() throws Exception { |
| 58 | + reserver = new LocalDeviceReserver(manager, idManager); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void addTempAllocationKeyToDevice_inputIsUuid_usesUuid() { |
| 63 | + when(idManager.containsUuid("uuid-123")).thenReturn(true); |
| 64 | + |
| 65 | + DeviceLocator locator = new DeviceLocator("uuid-123", LabLocator.LOCALHOST); |
| 66 | + |
| 67 | + reserver.addTempAllocationKeyToDevice(locator, "dimension", "value", Duration.ofMinutes(1)); |
| 68 | + |
| 69 | + verify(manager) |
| 70 | + .addOrRemoveDimensions( |
| 71 | + deviceKeyCaptor.capture(), |
| 72 | + eq(ImmutableListMultimap.of("dimension", "value")), |
| 73 | + eq(Duration.ofMinutes(1))); |
| 74 | + |
| 75 | + DeviceKey key = deviceKeyCaptor.getValue(); |
| 76 | + assertThat(key.deviceUuid()).isEqualTo("uuid-123"); |
| 77 | + assertThat(key.labHostName()).isEqualTo(LabLocator.LOCALHOST.getHostName()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void addTempAllocationKeyToDevice_inputIsControlId_translatesToUuid() { |
| 82 | + DeviceId deviceId = DeviceId.of("control-123", "uuid-123"); |
| 83 | + when(idManager.containsUuid("control-123")).thenReturn(false); |
| 84 | + when(idManager.getDeviceIdFromControlId("control-123")).thenReturn(Optional.of(deviceId)); |
| 85 | + |
| 86 | + DeviceLocator locator = new DeviceLocator("control-123", LabLocator.LOCALHOST); |
| 87 | + |
| 88 | + reserver.addTempAllocationKeyToDevice(locator, "dimension", "value", Duration.ofMinutes(1)); |
| 89 | + |
| 90 | + verify(manager) |
| 91 | + .addOrRemoveDimensions( |
| 92 | + deviceKeyCaptor.capture(), |
| 93 | + eq(ImmutableListMultimap.of("dimension", "value")), |
| 94 | + eq(Duration.ofMinutes(1))); |
| 95 | + |
| 96 | + DeviceKey key = deviceKeyCaptor.getValue(); |
| 97 | + assertThat(key.deviceUuid()).isEqualTo("uuid-123"); |
| 98 | + assertThat(key.labHostName()).isEqualTo(LabLocator.LOCALHOST.getHostName()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void addTempAllocationKeyToDevice_inputIsUnknown_fallsBackToInput() { |
| 103 | + when(idManager.containsUuid("unknown-id")).thenReturn(false); |
| 104 | + |
| 105 | + DeviceLocator locator = new DeviceLocator("unknown-id", LabLocator.LOCALHOST); |
| 106 | + |
| 107 | + reserver.addTempAllocationKeyToDevice(locator, "dimension", "value", Duration.ofMinutes(1)); |
| 108 | + |
| 109 | + verify(manager) |
| 110 | + .addOrRemoveDimensions( |
| 111 | + deviceKeyCaptor.capture(), |
| 112 | + eq(ImmutableListMultimap.of("dimension", "value")), |
| 113 | + eq(Duration.ofMinutes(1))); |
| 114 | + |
| 115 | + DeviceKey key = deviceKeyCaptor.getValue(); |
| 116 | + assertThat(key.deviceUuid()).isEqualTo("unknown-id"); |
| 117 | + assertThat(key.labHostName()).isEqualTo(LabLocator.LOCALHOST.getHostName()); |
| 118 | + } |
| 119 | +} |
0 commit comments