Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package com.openframe.data.nats.delivery;

import com.openframe.data.document.delivery.DeliveryType;
import com.openframe.data.document.device.Machine;
import com.openframe.data.document.toolagent.ToolOsSupport;
import com.openframe.data.nats.mapper.ToolAgentUpdateMessageMapper;
import com.openframe.data.nats.model.DownloadConfiguration;
import com.openframe.data.nats.model.ToolAgentUpdateMessage;
import com.openframe.delivery.spec.DeliveryRequest;
import com.openframe.delivery.spec.DeliverySpec;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;

@Component
Expand Down Expand Up @@ -61,21 +54,4 @@ public String subject(String machineId) {
public boolean canDeliverToEveryMachine() {
return true;
}

@Override
public boolean admits(Machine machine, ToolAgentUpdateMessage payload) {
List<DownloadConfiguration> configurations = payload.getDownloadConfigurations();
if (configurations == null) {
return true;
}
Optional<String> minOsVersion = ToolOsSupport.downloadOs(machine.getOsType())
.flatMap(os -> configurations.stream()
.filter(configuration -> os.equalsIgnoreCase(configuration.getOs()))
.map(DownloadConfiguration::getMinOsVersion)
.filter(Objects::nonNull)
.findFirst());
return minOsVersion
.map(minimum -> !ToolOsSupport.isBelow(machine.getOsVersion(), minimum))
.orElse(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.openframe.data.nats.delivery;

import com.openframe.data.document.delivery.DeliveryType;
import com.openframe.data.document.device.Machine;
import com.openframe.data.nats.model.ToolUninstallMessage;
import com.openframe.delivery.spec.DeliveryAudience;
import com.openframe.delivery.spec.DeliveryRequest;
Expand Down Expand Up @@ -103,16 +102,4 @@ void canDeliverToEveryMachine_toolUninstall_false() {
// verifications
assertThat(everyMachine).isFalse();
}

@Test
void admits_anyMachine_true() {
// setup
Machine machine = new Machine();

// execution
boolean admitted = spec.admits(machine, new ToolUninstallMessage());

// verifications
assertThat(admitted).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@

import com.openframe.data.document.delivery.DeliveryType;
import com.openframe.data.document.device.DeviceStatus;
import com.openframe.data.document.device.Machine;
import com.openframe.data.document.rmm.script.OsType;
import com.openframe.data.document.toolagent.IntegratedToolAgent;
import com.openframe.data.nats.mapper.ToolAgentUpdateMessageMapper;
import com.openframe.data.nats.model.DownloadConfiguration;
import com.openframe.data.nats.model.ToolAgentUpdateMessage;
import com.openframe.delivery.spec.DeliveryAudience;
import com.openframe.delivery.spec.DeliveryRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -37,18 +30,13 @@ class ToolUpdateDeliverySpecTest {

private IntegratedToolAgent toolAgent;
private ToolAgentUpdateMessage message;
private Machine machine;

@BeforeEach
void setUp() {
toolAgent = new IntegratedToolAgent();
toolAgent.setKey(TOOL_AGENT_KEY);
toolAgent.setVersion("1.2.3");
message = new ToolAgentUpdateMessage();
message.setDownloadConfigurations(List.of(downloadConfiguration("windows", null),
downloadConfiguration("macos", "13.0")));
machine = new Machine();
machine.setOsType(OsType.MAC_OS);
}

@Test
Expand Down Expand Up @@ -108,102 +96,4 @@ void subject_machineId_oneToolUpdateSubjectPerMachine() {
// verifications
assertThat(subject).isEqualTo("machine.mach-42.tool-update.v2");
}

@Test
void admits_macBelowMinimum_false() {
// setup
machine.setOsVersion("11.7.10");

// execution
boolean admitted = spec.admits(machine, message);

// verifications
assertThat(admitted).isFalse();
}

@ParameterizedTest
@ValueSource(strings = {"13.0", "13", "14.2"})
void admits_macAtOrAboveMinimum_true(String osVersion) {
// setup
machine.setOsVersion(osVersion);

// execution
boolean admitted = spec.admits(machine, message);

// verifications
assertThat(admitted).isTrue();
}

@ParameterizedTest
@NullSource
@ValueSource(strings = {"", "unknown"})
void admits_macVersionUnknown_true(String osVersion) {
// setup
machine.setOsVersion(osVersion);

// execution
boolean admitted = spec.admits(machine, message);

// verifications
assertThat(admitted).isTrue();
}

@Test
void admits_windowsMachine_true() {
// setup
machine.setOsType(OsType.WINDOWS);
machine.setOsVersion("6.1");

// execution
boolean admitted = spec.admits(machine, message);

// verifications
assertThat(admitted).isTrue();
}

@Test
void admits_osTypeUnknown_true() {
// setup
machine.setOsType(null);
machine.setOsVersion("11.7.10");

// execution
boolean admitted = spec.admits(machine, message);

// verifications
assertThat(admitted).isTrue();
}

@Test
void admits_payloadWithoutDownloadConfigurations_true() {
// setup
message.setDownloadConfigurations(null);
machine.setOsVersion("11.7.10");

// execution
boolean admitted = spec.admits(machine, message);

// verifications
assertThat(admitted).isTrue();
}

@Test
void admits_macosConfigurationWithoutMinimum_true() {
// setup
message.setDownloadConfigurations(List.of(downloadConfiguration("macos", null)));
machine.setOsVersion("11.7.10");

// execution
boolean admitted = spec.admits(machine, message);

// verifications
assertThat(admitted).isTrue();
}

private static DownloadConfiguration downloadConfiguration(String os, String minOsVersion) {
DownloadConfiguration configuration = new DownloadConfiguration();
configuration.setOs(os);
configuration.setMinOsVersion(minOsVersion);
return configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class DeliveryMetrics {
private static final String ROLLOUT_DURATION_TIMER = "openframe.delivery.rollout.duration";
private static final String ROLLOUT_RUNNING_GAUGE = "openframe.delivery.rollout.running";
private static final String ROLLOUT_BELOW_FLOOR_COUNTER = "openframe.delivery.rollout.below_floor";
private static final String ROLLOUT_NOT_ADMITTED_COUNTER = "openframe.delivery.rollout.not_admitted";
private static final String ROUTE_UNREACHABLE_COUNTER = "openframe.delivery.route.unreachable";
private static final String TAG_TYPE = "type";
private static final String TAG_REASON = "reason";
Expand Down Expand Up @@ -137,11 +136,6 @@ public void recordRolloutBelowFloor(DeliveryType type, int machines) {
meterRegistry.counter(ROLLOUT_BELOW_FLOOR_COUNTER, TAG_TYPE, typeTag).increment(machines);
}

public void recordRolloutNotAdmitted(DeliveryType type, int machines) {
String typeTag = tagValue(type.name());
meterRegistry.counter(ROLLOUT_NOT_ADMITTED_COUNTER, TAG_TYPE, typeTag).increment(machines);
}

// a command no route carried: an agent below the floor on a type without a per-machine legacy subject, or a
// type closed everywhere; a dashboard figure, not an alert
public void recordUnreachable(DeliveryType type, DeliveryAudience audience) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void dispatchNextBatch(MachineDeliveryRollout rollout) {
int batchSize = properties.getSweep().getBatchSize();
String cursor = rollout.getCursor();
List<Machine> machines = machineRepository.findByStatusInAndIdGreaterThanOrderByIdAsc(statuses, cursor, Limit.of(batchSize));
List<Machine> admitted = admittedBySpec(admittedByTheFloor(machines, type), rollout, spec);
List<Machine> admitted = admittedByTheFloor(machines, type);
admitted.forEach(machine -> dispatchTo(machine, rollout, spec));
String id = rollout.getId();
int sequence = rollout.getSequence();
Expand Down Expand Up @@ -123,30 +123,6 @@ private List<Machine> admittedByTheFloor(List<Machine> machines, DeliveryType ty
return admitted;
}

private List<Machine> admittedBySpec(List<Machine> machines, MachineDeliveryRollout rollout,
DeliverySpec<DeliverySeed, DeliveryPayload> spec) {
if (machines.isEmpty()) {
return machines;
}
DeliveryPayload payload;
try {
payload = payloadJson.read(rollout.getPayloadJson(), spec.getPayloadClass());
} catch (RuntimeException unreadable) {
log.warn("Delivery rollout payload unreadable, every machine admitted: id={}", rollout.getId(), unreadable);
return machines;
}
List<Machine> admitted = machines.stream()
.filter(machine -> spec.admits(machine, payload))
.toList();
int left = machines.size() - admitted.size();
if (left > 0) {
DeliveryType type = rollout.getType();
metrics.recordRolloutNotAdmitted(type, left);
log.info("Delivery rollout walked past {} machine(s) the type does not admit: type={}", left, type);
}
return admitted;
}

// Mongo being away stops the batch, and the tick after retries it; any other failure is this one machine's and
// must not hold the rest of the tenant: the machine is skipped and counted, it catches up at registration or on
// the next rollout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.openframe.data.document.delivery.DeliveryType;
import com.openframe.data.document.device.DeviceStatus;
import com.openframe.data.document.device.Machine;

import java.util.EnumSet;
import java.util.Set;
Expand Down Expand Up @@ -37,8 +36,4 @@ default Long getResultTimeoutSeconds() {
default boolean canDeliverToEveryMachine() {
return false;
}

default boolean admits(Machine machine, P payload) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ void setUp() {
doReturn(spec).when(registry).require(DeliveryType.CLIENT_UPDATE);
doReturn(DELIVERABLE).when(spec).getDeliverableStatuses();
lenient().doReturn(TestPayload.class).when(spec).getPayloadClass();
lenient().doReturn(true).when(spec).admits(any(), any());
lenient().when(floors.of(DeliveryType.CLIENT_UPDATE)).thenReturn(AgentVersionFloor.EVERY_VERSION);
}

Expand Down Expand Up @@ -227,83 +226,6 @@ void advance_noFloor_versionsNeverLookedUp() {
verifyNoInteractions(agentVersions);
}

@Test
void advance_specDoesNotAdmitAMachine_walkedPastCountedAndTheOthersDispatched() {
// setup
Machine refused = machine("m-1");
when(machineRepository.findByStatusInAndIdGreaterThanOrderByIdAsc(DELIVERABLE, DeliveryRolloutRecorder.BEFORE_FIRST_MACHINE, Limit.of(BATCH_SIZE)))
.thenReturn(List.of(refused, machine("m-2")));
doReturn(false).when(spec).admits(eq(refused), any(TestPayload.class));

// execution
service.advance();

// verifications
verify(sink).accept(requestCaptor.capture());
assertThat(requestCaptor.getValue().getMachineId()).isEqualTo("m-2");
verify(metrics).recordRolloutNotAdmitted(DeliveryType.CLIENT_UPDATE, 1);
verify(metrics).recordRolloutMachines(DeliveryType.CLIENT_UPDATE, 1);
}

@Test
void advance_specDoesNotAdmitAMachine_cursorStillMovesPastIt() {
// setup
Machine refused = machine("m-2");
when(machineRepository.findByStatusInAndIdGreaterThanOrderByIdAsc(DELIVERABLE, DeliveryRolloutRecorder.BEFORE_FIRST_MACHINE, Limit.of(BATCH_SIZE)))
.thenReturn(List.of(machine("m-1"), refused));
doReturn(false).when(spec).admits(eq(refused), any(TestPayload.class));

// execution
service.advance();

// verifications
verify(repository).advance(ROLLOUT_ID, SEQUENCE, "id-m-2", 1);
}

@Test
void advance_everyMachineAdmitted_notAdmittedNeverCounted() {
// setup
when(machineRepository.findByStatusInAndIdGreaterThanOrderByIdAsc(DELIVERABLE, DeliveryRolloutRecorder.BEFORE_FIRST_MACHINE, Limit.of(BATCH_SIZE)))
.thenReturn(List.of(machine("m-1"), machine("m-2")));

// execution
service.advance();

// verifications
verify(metrics, never()).recordRolloutNotAdmitted(any(), anyInt());
}

@Test
void advance_unreadablePayload_everyMachineAdmittedWithoutAskingTheSpec() {
// setup
rollout.setPayloadJson("not-json");
when(machineRepository.findByStatusInAndIdGreaterThanOrderByIdAsc(DELIVERABLE, DeliveryRolloutRecorder.BEFORE_FIRST_MACHINE, Limit.of(BATCH_SIZE)))
.thenReturn(List.of(machine("m-1"), machine("m-2")));

// execution
service.advance();

// verifications
verify(spec, never()).admits(any(), any());
verify(metrics, never()).recordRolloutNotAdmitted(any(), anyInt());
verify(metrics).recordRolloutMachines(DeliveryType.CLIENT_UPDATE, 2);
verify(repository).advance(ROLLOUT_ID, SEQUENCE, "id-m-2", 2);
}

@Test
void advance_noMachines_specNeverAsked() {
// setup
when(machineRepository.findByStatusInAndIdGreaterThanOrderByIdAsc(DELIVERABLE, DeliveryRolloutRecorder.BEFORE_FIRST_MACHINE, Limit.of(BATCH_SIZE)))
.thenReturn(List.of());

// execution
service.advance();

// verifications
verify(spec, never()).admits(any(), any());
verify(repository).finish(ROLLOUT_ID, SEQUENCE, 0);
}

private static Machine machine(String machineId) {
Machine machine = new Machine();
machine.setId("id-" + machineId);
Expand Down
Loading