use of com.mesosphere.sdk.specification.ServiceSpec in project dcos-commons by mesosphere.
the class PodSpecsCannotShrinkTest method testIncreasingPodCountIsValid.
@Test
public void testIncreasingPodCountIsValid() {
final ServiceSpec serviceWithManyPods = getServiceSpec(mockPodSpecA1WithHigherCount);
final ServiceSpec serviceWithFewPods = getServiceSpec(mockPodSpecA1);
assertThat(VALIDATOR.validate(Optional.of(serviceWithFewPods), serviceWithManyPods), is(empty()));
}
use of com.mesosphere.sdk.specification.ServiceSpec in project dcos-commons by mesosphere.
the class PreReservationCannotChange method validate.
@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
if (!oldConfig.isPresent()) {
return Collections.emptyList();
}
final Map<String, PodSpec> newPods = newConfig.getPods().stream().collect(Collectors.toMap(podSpec -> podSpec.getType(), podSpec -> podSpec));
final List<ConfigValidationError> errors = new ArrayList<>();
for (final PodSpec oldPod : oldConfig.get().getPods()) {
final PodSpec newPod = newPods.get(oldPod.getType());
if (newPod == null) {
continue;
}
final String oldPodPreReservedRole = oldPod.getPreReservedRole();
final String newPodPreReservedRole = newPod.getPreReservedRole();
if (!StringUtils.equals(newPodPreReservedRole, oldPodPreReservedRole)) {
errors.add(ConfigValidationError.transitionError(String.format("PodSpec[pre-reserved-role:%s]", oldPodPreReservedRole), oldPodPreReservedRole, newPodPreReservedRole, String.format("New config has changed the pre-reserved-role of PodSpec named '%s'" + " (expected it to stay '%s')", oldPod.getType(), oldPodPreReservedRole)));
}
}
return errors;
}
use of com.mesosphere.sdk.specification.ServiceSpec in project dcos-commons by mesosphere.
the class PodSpecsCannotChangeNetworkRegime method validate.
@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
if (!oldConfig.isPresent()) {
return Collections.emptyList();
}
Map<String, PodSpec> newPods;
try {
newPods = newConfig.getPods().stream().collect(Collectors.toMap(podSpec -> podSpec.getType(), podSpec -> podSpec));
} catch (IllegalStateException e) {
return Arrays.asList(ConfigValidationError.valueError("PodSpecs", "null", "Duplicate pod types detected."));
}
// check the PodSpecs to make sure none of them make a transition from a state where they use host ports
// to one where they don't (or vice versa).
Collection<ConfigValidationError> errors = new ArrayList<>();
Map<String, PodSpec> oldPods = oldConfig.get().getPods().stream().collect(Collectors.toMap(PodSpec::getType, podSpec -> podSpec));
for (Map.Entry<String, PodSpec> kv : newPods.entrySet()) {
PodSpec newPod = kv.getValue();
// first we check that the new pod's networks (if present) do not erroneously use port mapping
List<String> offendingNetworkNames = getNetworksWithInvalidPortMappings(newPod);
if (offendingNetworkNames.size() > 0) {
errors.add(ConfigValidationError.transitionError(String.format("PodSpec[name:%s]", newPod.getType()), "null", newPod.getNetworks().toString(), String.format("New config has pod %s that indicates port mapping for virtual network(s) %s, " + "that do not support port mapping.", newPod.getType(), StringUtils.join(offendingNetworkNames, ", "))));
}
// now we check that the none of the new pods move from not using host ports to using them (or vice
// versa)
PodSpec oldPod = oldPods.get(kv.getKey());
if (oldPod != null && podSpecUsesHostPorts(oldPod) != podSpecUsesHostPorts(newPod)) {
errors.add(ConfigValidationError.transitionError(String.format("PodSpec[name:%s]", oldPod.getType()), oldPod.getNetworks().toString(), newPod.getNetworks().toString(), String.format("New config has pod %s moving networks from %s to %s, changing its " + "host ports requirements from %s to %s, not allowed.", newPod.getType(), oldPod.getNetworks().toString(), newPod.getNetworks().toString(), podSpecUsesHostPorts(oldPod), podSpecUsesHostPorts(newPod))));
}
}
return errors;
}
use of com.mesosphere.sdk.specification.ServiceSpec in project dcos-commons by mesosphere.
the class UserCannotChangeTest method testMultiplePodsOldSettingOneUserNewSettingMultipleUsers.
@Test
public void testMultiplePodsOldSettingOneUserNewSettingMultipleUsers() {
when(mockOldPodSpec.getUser()).thenReturn(Optional.of(USER_A + "-1"));
when(mockOldPodSpec2.getUser()).thenReturn(Optional.of(USER_A + "-1"));
when(mockNewPodSpec.getUser()).thenReturn(Optional.of(USER_B + "-1"));
when(mockNewPodSpec2.getUser()).thenReturn(Optional.of(USER_B + "-2"));
ServiceSpec oldServiceSpec = DefaultServiceSpec.newBuilder().name("svc").role(TestConstants.ROLE).principal(TestConstants.PRINCIPAL).pods(Arrays.asList(mockOldPodSpec, mockOldPodSpec2)).build();
ServiceSpec newServiceSpec = DefaultServiceSpec.newBuilder().name("svc").role(TestConstants.ROLE).principal(TestConstants.PRINCIPAL).pods(Arrays.asList(mockNewPodSpec, mockNewPodSpec2)).build();
Assert.assertEquals(3, VALIDATOR.validate(Optional.of(oldServiceSpec), newServiceSpec).size());
}
use of com.mesosphere.sdk.specification.ServiceSpec in project dcos-commons by mesosphere.
the class PodSpecsCannotShrinkTest method testReducingSourceAllowedPodCountIsInvalid.
@Test
public void testReducingSourceAllowedPodCountIsInvalid() {
when(mockPodSpecA1WithHigherCount.getAllowDecommission()).thenReturn(true);
final ServiceSpec serviceWithManyPods = getServiceSpec(mockPodSpecA1WithHigherCount);
final ServiceSpec serviceWithFewPods = getServiceSpec(mockPodSpecA1);
assertThat(VALIDATOR.validate(Optional.of(serviceWithManyPods), serviceWithFewPods), hasSize(1));
}
Aggregations