public static void checkSlotVersion(SlotStatus slotStatus, String expectedSlotVersion) { Preconditions.checkNotNull(slotStatus, "slotStatus is null"); if (expectedSlotVersion == null) { return; } if (!expectedSlotVersion.equals(slotStatus.getVersion())) { throw new VersionConflictException(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()); } }
public static String createSlotsVersion(Iterable<SlotStatus> slots) { Preconditions.checkNotNull(slots, "slots is null"); // canonicalize slot order Map<UUID, String> slotVersions = new TreeMap<UUID, String>(); for (SlotStatus slot : slots) { slotVersions.put(slot.getId(), slot.getVersion()); } return DigestUtils.md5Hex(slotVersions.values().toString()); }
public static String createAgentVersion(String agentId, AgentLifecycleState state, Iterable<SlotStatus> slots, Map<String, Integer> resources) { List<Object> parts = new ArrayList<Object>(); parts.add(agentId); parts.add(state); // canonicalize slot order Map<UUID, String> slotVersions = new TreeMap<UUID, String>(); for (SlotStatus slot : slots) { slotVersions.put(slot.getId(), slot.getVersion()); } parts.addAll(slotVersions.values()); // canonicalize resources parts.add(Joiner.on("--").withKeyValueSeparator("=").join(ImmutableSortedMap.copyOf(resources))); String data = Joiner.on("||").useForNull("--NULL--").join(parts); return DigestUtils.md5Hex(data); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response installSlot(@HeaderParam(AIRSHIP_AGENT_VERSION_HEADER) String agentVersion, InstallationRepresentation installation, @Context UriInfo uriInfo) { Preconditions.checkNotNull(installation, "installation must not be null"); checkAgentVersion(agent.getAgentStatus(), agentVersion); SlotStatus slotStatus = agent.install(installation.toInstallation()); return Response .created(getSelfUri(slotStatus.getId(), uriInfo.getBaseUri())) .entity(SlotStatusRepresentation.from(slotStatus)) .header(AIRSHIP_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); }
@Test public void testAssignNoAgentVersion() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); assertEquals(slotStatus.getState(), STOPPED); assertUpgrade(slotStatus, null, slotStatus.getVersion()); }
@Test public void testGetSlotStatus() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); URI requestUri = URI.create("http://localhost/v1/agent/slot/" + slotStatus.getId().toString()); Response response = resource.getSlotStatus(slotStatus.getId(), MockUriInfo.from(requestUri)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(response.getEntity(), SlotStatusRepresentation.from(slotStatus)); assertEquals(response.getMetadata().get(AIRSHIP_AGENT_VERSION_HEADER).get(0), agent.getAgentStatus().getVersion()); assertEquals(response.getMetadata().get(AIRSHIP_SLOT_VERSION_HEADER).get(0), slotStatus.getVersion()); assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
@Test public void testAssignNoAgentVersion() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); assertEquals(slotStatus.getState(), STOPPED); assertUpgrade(slotStatus, null, slotStatus.getVersion()); }
@Test public void testGetSlotStatus() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); URI requestUri = URI.create("http://localhost/v1/agent/slot/" + slotStatus.getId().toString()); Response response = resource.getSlotStatus(slotStatus.getId(), MockUriInfo.from(requestUri)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(response.getEntity(), SlotStatusRepresentation.from(slotStatus)); assertEquals(response.getMetadata().get(AIRSHIP_AGENT_VERSION_HEADER).get(0), agent.getAgentStatus().getVersion()); assertEquals(response.getMetadata().get(AIRSHIP_SLOT_VERSION_HEADER).get(0), slotStatus.getVersion()); assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
@Test public void testTerminateSlot() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); Response response = resource.terminateSlot(null, null, slotStatus.getId()); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); SlotStatus expectedStatus = slotStatus.changeState(TERMINATED); assertEquals(response.getEntity(), SlotStatusRepresentation.from(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 assertNull(agent.getSlot(slotStatus.getId())); }
@Test public void testInvalidVersion() { try { resource.setState("invalid-version", slot.getId(), "running"); fail("Expected VersionConflictException"); } catch (VersionConflictException e) { assertEquals(e.getName(), AIRSHIP_SLOT_VERSION_HEADER); assertEquals(e.getVersion(), slot.status().getVersion()); } }
@Test public void testInvalidVersion() { try { resource.setState("invalid-version", slot.getId(), "running"); fail("Expected VersionConflictException"); } catch (VersionConflictException e) { assertEquals(e.getName(), AIRSHIP_SLOT_VERSION_HEADER); assertEquals(e.getVersion(), slot.status().getVersion()); } }
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 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 } }
public void assertInstallSlot(String agentVersion) { Response response = resource.installSlot(agentVersion, InstallationRepresentation.from(APPLE_INSTALLATION), uriInfo); // find the new slot Slot slot = agent.getAllSlots().iterator().next(); assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); assertEquals(response.getMetadata().getFirst(HttpHeaders.LOCATION), URI.create("http://localhost/v1/agent/slot/" + slot.getId().toString())); SlotStatus status = slot.status(); SlotStatus expectedStatus = status.changeAssignment(STOPPED, APPLE_ASSIGNMENT, status.getResources()); assertEquals(response.getEntity(), SlotStatusRepresentation.from(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 }
public void assertInstallSlot(String agentVersion) { Response response = resource.installSlot(agentVersion, InstallationRepresentation.from(APPLE_INSTALLATION), uriInfo); // find the new slot Slot slot = agent.getAllSlots().iterator().next(); assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); assertEquals(response.getMetadata().getFirst(HttpHeaders.LOCATION), URI.create("http://localhost/v1/agent/slot/" + slot.getId().toString())); SlotStatus status = slot.status(); SlotStatus expectedStatus = status.changeAssignment(STOPPED, APPLE_ASSIGNMENT, status.getResources()); assertEquals(response.getEntity(), SlotStatusRepresentation.from(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 }
@Test public void testTerminateSlot() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); Response response = resource.terminateSlot(null, null, slotStatus.getId()); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); SlotStatus expectedStatus = slotStatus.changeState(TERMINATED); assertEquals(response.getEntity(), SlotStatusRepresentation.from(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 assertNull(agent.getSlot(slotStatus.getId())); }
@Test public void testAssign() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); assertUpgrade(slotStatus, agent.getAgentStatus().getVersion(), slotStatus.getVersion()); }
@Test public void testAssign() { SlotStatus slotStatus = agent.install(APPLE_INSTALLATION); assertUpgrade(slotStatus, agent.getAgentStatus().getVersion(), slotStatus.getVersion()); }
@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()); }
@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()); }