Search in sources :

Example 36 with ServiceSpec

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()));
}
Also used : DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) Test(org.junit.Test)

Example 37 with ServiceSpec

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;
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec) java.util(java.util) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec)

Example 38 with ServiceSpec

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;
}
Also used : Arrays(java.util.Arrays) Collection(java.util.Collection) NetworkSpec(com.mesosphere.sdk.specification.NetworkSpec) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) ArrayList(java.util.ArrayList) PodSpec(com.mesosphere.sdk.specification.PodSpec) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Collections(java.util.Collections) DcosConstants(com.mesosphere.sdk.dcos.DcosConstants) PodSpec(com.mesosphere.sdk.specification.PodSpec) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 39 with ServiceSpec

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());
}
Also used : DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) Test(org.junit.Test)

Example 40 with ServiceSpec

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));
}
Also used : DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) Test(org.junit.Test)

Aggregations

ServiceSpec (com.mesosphere.sdk.specification.ServiceSpec)61 DefaultServiceSpec (com.mesosphere.sdk.specification.DefaultServiceSpec)55 Test (org.junit.Test)51 MemPersister (com.mesosphere.sdk.storage.MemPersister)7 PodSpec (com.mesosphere.sdk.specification.PodSpec)6 Collectors (java.util.stream.Collectors)5 ConfigStoreException (com.mesosphere.sdk.state.ConfigStoreException)3 java.util (java.util)3 Protos (org.apache.mesos.Protos)3 Capabilities (com.mesosphere.sdk.dcos.Capabilities)2 TaskException (com.mesosphere.sdk.offer.TaskException)2 PlacementRule (com.mesosphere.sdk.offer.evaluate.placement.PlacementRule)2 TaskLabelWriter (com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)2 DefaultPodSpec (com.mesosphere.sdk.specification.DefaultPodSpec)2 Persister (com.mesosphere.sdk.storage.Persister)2 PersisterException (com.mesosphere.sdk.storage.PersisterException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Optional (java.util.Optional)2