String shortBinary = null; String shortConfig = null; if (slotStatus.getAssignment() != null) { binary = slotStatus.getAssignment().getBinary(); config = slotStatus.getAssignment().getConfig(); if (repository != null) { shortBinary = Objects.firstNonNull(repository.binaryRelativize(binary), binary); if (slotStatus.getExpectedAssignment() != null) { expectedBinary = slotStatus.getExpectedAssignment().getBinary(); expectedConfig = slotStatus.getExpectedAssignment().getConfig(); if (slotStatus.getExpectedState() != null) { expectedStatus = slotStatus.getExpectedState().toString(); return new SlotStatusRepresentation(slotStatus.getId(), safeTruncate(slotStatus.getId().toString(), shortIdPrefixSize), slotStatus.getSelf(), slotStatus.getExternalUri(), slotStatus.getInstanceId(), slotStatus.getLocation(), slotStatus.getLocation(), binary, shortBinary, config, shortConfig, slotStatus.getState().toString(), slotStatus.getVersion(), slotStatus.getStatusMessage(),
@Override public SlotStatus stop() { if (slotStatus.getAssignment() == null) { throw new IllegalStateException("Slot can not be stopped because the slot is not assigned"); } slotStatus = slotStatus.changeState(STOPPED); mockRemoteAgent.setSlotStatus(slotStatus); return slotStatus; }
@Override public SlotStatus status() { try { lock(); } catch (LockTimeoutException e) { // could not get the lock because there is an operation in progress // just return the last state we saw // todo consider adding "in-process" states like starting return lastSlotStatus.get(); } try { if (terminated) { return lastSlotStatus.get().changeState(TERMINATED); } Deployment activeDeployment = deploymentManager.getDeployment(); if (activeDeployment == null) { return lastSlotStatus.get().changeAssignment(UNKNOWN, null, ImmutableMap.<String, Integer>of()); } SlotStatus slotStatus = lastSlotStatus.get().changeState(lifecycleManager.status(activeDeployment)); lastSlotStatus.set(slotStatus); return slotStatus; } finally { unlock(); } }
private SlotStatus setErrorStatus(String statusMessage) { slotStatus = slotStatus.changeState(UNKNOWN); return slotStatus.changeStatusMessage(statusMessage); }
public SlotStatus apply(SlotStatus slotStatus) { if (!Objects.equal(slotStatus.getInstanceId(), instanceId)) { slotStatus = slotStatus.changeInstanceId(instanceId); } return slotStatus; } });
@Override public SlotStatus apply(SlotStatus slotStatus) { if (slotStatus.getState() != SlotLifecycleState.UNKNOWN) { stateManager.setExpectedState(new ExpectedSlotStatus(slotStatus.getId(), slotStatus.getState(), slotStatus.getAssignment())); } else { stateManager.deleteExpectedState(slotStatus.getId()); } return slotStatus; } }));
@Test public void testGetSlotStatus() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); Request request = Request.Builder.prepareGet().setUri(urlFor("/v1/agent/slot", slotStatus.getId().toString())).build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = mapCodec.fromJson(Resources.toString(Resources.getResource("slot-status.json"), UTF_8)); expected.put("id", slotStatus.getId().toString()); expected.put("shortId", slotStatus.getId().toString()); expected.put("self", urlFor(slotStatus).toASCIIString()); expected.put("externalUri", urlFor(slotStatus).toASCIIString()); expected.put("version", slotStatus.getVersion()); expected.put("location", slotStatus.getLocation()); expected.put("shortLocation", slotStatus.getLocation()); expected.put("installPath", slotStatus.getInstallPath()); // agent does not return instance id or expected status expected.remove("instanceId"); expected.remove("expectedBinary"); expected.remove("expectedConfig"); expected.remove("expectedStatus"); assertEquals(response, expected); }
assertFalse(buildFilter("assignable", APPLE_ASSIGNMENT.getBinary() + APPLE_ASSIGNMENT.getConfig(), false, repository).apply(status)); SlotStatus slot = createSlotStatus( UUID.fromString("99999999-1234-1234-1234-123456789012"), URI.create("fake://localhost"), assertFalse(buildFilter("assignable", BANANA_ASSIGNMENT.getBinary() + BANANA_ASSIGNMENT.getConfig(), true, repository).apply(status)); slot = createSlotStatus( slot.getId(), slot.getSelf(), slot.getExternalUri(), slot.getInstanceId(), slot.getLocation(), UNKNOWN, null, slot.getInstallPath(), slot.getResources()); status = status.changeSlotStatus(slot);
private void assertUpgrade(SlotStatus slotStatus, String agentVersion, String slotVersion) { Response response = resource.assign(agentVersion, slotVersion, slotStatus.getId(), UPGRADE); assertEquals(response.getStatus(), Status.OK.getStatusCode()); SlotStatusRepresentation actualStatus = (SlotStatusRepresentation) response.getEntity(); SlotStatus expectedStatus = slotStatus.changeAssignment(slotStatus.getState(), APPLE_V2, slotStatus.getResources()); assertEquals(actualStatus, SlotStatusRepresentation.from(expectedStatus)); assertEquals(actualStatus.toSlotStatus(null), expectedStatus); assertEquals(response.getMetadata().get(AIRSHIP_AGENT_VERSION_HEADER).get(0), agent.getAgentStatus().getVersion()); assertEquals(response.getMetadata().get(AIRSHIP_SLOT_VERSION_HEADER).get(0), expectedStatus.getVersion()); assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces } }
private void assertAppleSlot(SlotStatus slot) { assertEquals(slot.getAssignment(), RESOLVED_APPLE_ASSIGNMENT); assertEquals(slot.getState(), STOPPED); assertEquals(slot.getResources(), ImmutableMap.of("cpu", 1, "memory", 512)); } }
@Test public void testStart() throws Exception { // setup assertEquals(slot.assign(APPLE_INSTALLATION).getState(), STOPPED); // test remoteAgent.setStatus(agent.getAgentStatus()); RemoteSlot remoteSlot = new HttpRemoteSlot(slot.status(), client, remoteAgent); SlotStatus actual = remoteSlot.start(); // verify SlotStatus status = slot.status(); SlotStatus expected = status.changeAssignment(RUNNING, APPLE_ASSIGNMENT, status.getResources()); assertEquals(actual, expected); }
@Override public ImmutableList<ServiceDescriptor> getServiceInventory(Iterable<SlotStatus> allSlotStatus) { ImmutableList.Builder<ServiceDescriptor> newDescriptors = ImmutableList.builder(); for (SlotStatus slotStatus : allSlotStatus) { // if the self reference is null, the slot is totally offline so skip for now if (slotStatus.getSelf() == null) { continue; } List<ServiceDescriptor> serviceDescriptors = getServiceInventory(slotStatus); if (serviceDescriptors == null) { continue; } for (ServiceDescriptor serviceDescriptor : serviceDescriptors) { newDescriptors.add(new ServiceDescriptor(null, slotStatus.getId().toString(), serviceDescriptor.getType(), serviceDescriptor.getPool(), slotStatus.getLocation(), slotStatus.getState() == SlotLifecycleState.RUNNING ? ServiceState.RUNNING : ServiceState.STOPPED, interpolateProperties(serviceDescriptor.getProperties(), slotStatus))); } } return newDescriptors.build(); }
@Override public SlotStatus terminate() { try { Request request = Request.Builder.prepareDelete() .setUri(slotStatus.getSelf()) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Test public void testStart() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); Request request = Request.Builder.preparePut() .setUri(urlFor(slotStatus, "lifecycle")) .setBodyGenerator(createStaticBodyGenerator("running", UTF_8)) .build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = ImmutableMap.<String, Object>builder() .put("id", slotStatus.getId().toString()) .put("shortId", slotStatus.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slotStatus).toASCIIString()) .put("externalUri", urlFor(slotStatus).toASCIIString()) .put("location", slotStatus.getLocation()) .put("shortLocation", slotStatus.getLocation()) .put("status", RUNNING.toString()) .put("version", VersionsUtil.createSlotVersion(slotStatus.getId(), RUNNING, appleInstallation.getAssignment())) .put("installPath", slotStatus.getInstallPath()) .put("resources", ImmutableMap.<String, Integer>of("memory", 512)) .build(); assertEquals(response, expected); }
@Test public void testGetAllSlotsWithFilter() SlotStatus slot1 = createSlotStatus(UUID.randomUUID(), URI.create("fake://foo/v1/agent/slot/slot1"), URI.create("fake://foo/v1/agent/slot/slot1"), "/slot1", ImmutableMap.<String, Integer>of()); SlotStatus slot2 = createSlotStatus(UUID.randomUUID(), URI.create("fake://bar/v1/agent/slot/slot2"), URI.create("fake://bar/v1/agent/slot/slot2"), coordinator.updateAllAgentsAndWait(); int prefixSize = shortestUniquePrefix(asList(slot1.getId().toString(), slot2.getId().toString()), MIN_PREFIX_SIZE);
@Test public void testTerminate() throws Exception { // setup assertEquals(slot.assign(APPLE_INSTALLATION).getAssignment(), APPLE_ASSIGNMENT); // test remoteAgent.setStatus(agent.getAgentStatus()); RemoteSlot remoteSlot = new HttpRemoteSlot(slot.status(), client, remoteAgent); SlotStatus actual = remoteSlot.terminate(); // verify SlotStatus expected = createSlotStatus(slot.getId(), slot.getSelf(), slot.getExternalUri(), slot.status().getInstanceId(), slot.status().getLocation(), TERMINATED, null, null, ImmutableMap.<String, Integer>of()); assertEquals(actual, expected); }
public AgentStatus changeSlotStatus(SlotStatus slotStatus) { Map<UUID, SlotStatus> slots = newHashMap(this.slots); if (slotStatus.getState() != TERMINATED) { slots.put(slotStatus.getId(), slotStatus); } else { slots.remove(slotStatus.getId()); } return new AgentStatus(agentId, state, instanceId, internalUri, externalUri, location, instanceType, slots.values(), resources); }
@Test public void testAssignRestarts() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); slotStatus = agent.getSlot(slotStatus.getId()).start(); assertEquals(slotStatus.getState(), RUNNING); assertUpgrade(slotStatus, agent.getAgentStatus().getVersion(), slotStatus.getVersion()); }
public String apply(SlotStatus input) { return input.getId().toString(); } };
@Override public SlotStatus assign(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); slotStatus = slotStatus.changeAssignment(STOPPED, installation.getAssignment(), slotStatus.getResources()); mockRemoteAgent.setSlotStatus(slotStatus); return slotStatus; }