Search in sources :

Example 1 with RawServiceSpec

use of com.mesosphere.sdk.specification.yaml.RawServiceSpec in project dcos-commons by mesosphere.

the class DefaultPlanGeneratorTest method testCustomPhases.

@Test
public void testCustomPhases() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("custom-phases.yml").getFile());
    RawServiceSpec rawServiceSpec = RawServiceSpec.newBuilder(file).build();
    DefaultServiceSpec serviceSpec = DefaultServiceSpec.newGenerator(rawServiceSpec, SCHEDULER_CONFIG, file.getParentFile()).build();
    Persister persister = new MemPersister();
    stateStore = new StateStore(persister);
    configStore = new ConfigStore<>(DefaultServiceSpec.getConfigurationFactory(serviceSpec), persister);
    Assert.assertNotNull(serviceSpec);
    DefaultPlanGenerator generator = new DefaultPlanGenerator(configStore, stateStore);
    for (Map.Entry<String, RawPlan> entry : rawServiceSpec.getPlans().entrySet()) {
        Plan plan = generator.generate(entry.getValue(), entry.getKey(), serviceSpec.getPods());
        Assert.assertNotNull(plan);
        Assert.assertEquals(6, plan.getChildren().size());
        Phase serverPhase = plan.getChildren().get(0);
        Phase oncePhase = plan.getChildren().get(1);
        Phase interleavePhase = plan.getChildren().get(2);
        Phase fullCustomPhase = plan.getChildren().get(3);
        Phase partialCustomPhase = plan.getChildren().get(4);
        Phase omitStepPhase = plan.getChildren().get(5);
        validatePhase(serverPhase, Arrays.asList(Arrays.asList("server"), Arrays.asList("server"), Arrays.asList("server")));
        validatePhase(oncePhase, Arrays.asList(Arrays.asList("once"), Arrays.asList("once"), Arrays.asList("once")));
        validatePhase(interleavePhase, Arrays.asList(Arrays.asList("once"), Arrays.asList("server"), Arrays.asList("once"), Arrays.asList("server"), Arrays.asList("once"), Arrays.asList("server")));
        validatePhase(fullCustomPhase, Arrays.asList(Arrays.asList("once"), Arrays.asList("server"), Arrays.asList("server"), Arrays.asList("once"), Arrays.asList("server")));
        validatePhase(partialCustomPhase, Arrays.asList(Arrays.asList("server"), Arrays.asList("once"), Arrays.asList("once"), Arrays.asList("server"), Arrays.asList("server"), Arrays.asList("once")));
        validatePhase(omitStepPhase, Arrays.asList(Arrays.asList("once"), Arrays.asList("server")));
        Assert.assertEquals("hello-1:[once]", omitStepPhase.getChildren().get(0).getName());
        Assert.assertEquals("hello-1:[server]", omitStepPhase.getChildren().get(1).getName());
    }
}
Also used : Phase(com.mesosphere.sdk.scheduler.plan.Phase) MemPersister(com.mesosphere.sdk.storage.MemPersister) RawPlan(com.mesosphere.sdk.specification.yaml.RawPlan) StateStore(com.mesosphere.sdk.state.StateStore) RawPlan(com.mesosphere.sdk.specification.yaml.RawPlan) Plan(com.mesosphere.sdk.scheduler.plan.Plan) RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) MemPersister(com.mesosphere.sdk.storage.MemPersister) Persister(com.mesosphere.sdk.storage.Persister) File(java.io.File) Map(java.util.Map)

Example 2 with RawServiceSpec

use of com.mesosphere.sdk.specification.yaml.RawServiceSpec in project dcos-commons by mesosphere.

the class DefaultServiceSpecTest method invalidPlanSteps.

@Test(expected = IllegalStateException.class)
public void invalidPlanSteps() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("invalid-plan-steps.yml").getFile());
    RawServiceSpec rawSpec = RawServiceSpec.newBuilder(file).build();
    DefaultScheduler.newBuilder(DefaultServiceSpec.newGenerator(rawSpec, SCHEDULER_CONFIG, file.getParentFile()).build(), SCHEDULER_CONFIG, new MemPersister()).setPlansFrom(rawSpec).build();
}
Also used : RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) MemPersister(com.mesosphere.sdk.storage.MemPersister) File(java.io.File) Test(org.junit.Test)

Example 3 with RawServiceSpec

use of com.mesosphere.sdk.specification.yaml.RawServiceSpec in project dcos-commons by mesosphere.

the class DefaultServiceSpecTest method validBridgeNetworkWithPortForwarding.

@Test
public void validBridgeNetworkWithPortForwarding() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("valid-automatic-cni-port-forwarding.yml").getFile());
    // load the raw service spec and check that it parsed correctly
    RawServiceSpec rawServiceSpec = RawServiceSpec.newBuilder(file).build();
    Assert.assertNotNull(rawServiceSpec);
    Assert.assertEquals(rawServiceSpec.getPods().get("pod-type").getNetworks().get("mesos-bridge").numberOfPortMappings(), 0);
    Assert.assertTrue(rawServiceSpec.getPods().get("meta-data-with-port-mapping").getNetworks().get("mesos-bridge").numberOfPortMappings() == 2);
    // Check that the raw service spec was correctly translated into the ServiceSpec
    ServiceSpec serviceSpec = DefaultServiceSpec.newGenerator(rawServiceSpec, SCHEDULER_CONFIG, file.getParentFile()).build();
    Assert.assertNotNull(serviceSpec);
    Assert.assertTrue(serviceSpec.getPods().size() == 3);
    // check the first pod
    PodSpec podSpec = serviceSpec.getPods().get(0);
    Assert.assertTrue(podSpec.getNetworks().size() == 1);
    NetworkSpec networkSpec = Iterables.get(podSpec.getNetworks(), 0);
    Assert.assertTrue(networkSpec.getPortMappings().size() == 1);
    Assert.assertTrue(networkSpec.getPortMappings().get(8080) == 8080);
    // check the second pod
    podSpec = serviceSpec.getPods().get(1);
    Assert.assertTrue(podSpec.getNetworks().size() == 1);
    networkSpec = Iterables.get(podSpec.getNetworks(), 0);
    Assert.assertTrue(networkSpec.getPortMappings().size() == 2);
    Assert.assertTrue(networkSpec.getPortMappings().get(8080) == 8080);
    Assert.assertTrue(networkSpec.getPortMappings().get(8081) == 8081);
    // check the third
    podSpec = serviceSpec.getPods().get(2);
    Assert.assertTrue(podSpec.getNetworks().size() == 1);
    networkSpec = Iterables.get(podSpec.getNetworks(), 0);
    Assert.assertTrue(String.format("%s", networkSpec.getPortMappings()), networkSpec.getPortMappings().size() == 2);
    Assert.assertTrue(networkSpec.getPortMappings().get(4040) == 8080);
    Assert.assertTrue(networkSpec.getPortMappings().get(4041) == 8081);
    validateServiceSpec("valid-automatic-cni-port-forwarding.yml", DcosConstants.DEFAULT_GPU_POLICY);
}
Also used : RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) File(java.io.File) Test(org.junit.Test)

Example 4 with RawServiceSpec

use of com.mesosphere.sdk.specification.yaml.RawServiceSpec in project dcos-commons by mesosphere.

the class Main method createSchedulerBuilder.

private static SchedulerBuilder createSchedulerBuilder(File yamlSpecFile) throws Exception {
    RawServiceSpec rawServiceSpec = RawServiceSpec.newBuilder(yamlSpecFile).build();
    SchedulerConfig schedulerConfig = SchedulerConfig.fromEnv();
    // Allow users to manually specify a ZK location for kafka itself. Otherwise default to our service ZK location:
    String kafkaZookeeperUri = System.getenv(KAFKA_ZK_URI_ENV);
    if (StringUtils.isEmpty(kafkaZookeeperUri)) {
        // "master.mesos:2181" + "/dcos-service-path__to__my__kafka":
        kafkaZookeeperUri = FrameworkConfig.fromRawServiceSpec(rawServiceSpec).getZookeeperHostPort() + CuratorUtils.getServiceRootPath(rawServiceSpec.getName());
    }
    LOGGER.info("Running Kafka with zookeeper path: {}", kafkaZookeeperUri);
    SchedulerBuilder schedulerBuilder = DefaultScheduler.newBuilder(DefaultServiceSpec.newGenerator(rawServiceSpec, schedulerConfig, yamlSpecFile.getParentFile()).setAllPodsEnv(KAFKA_ZK_URI_ENV, kafkaZookeeperUri).build(), schedulerConfig).setCustomConfigValidators(Arrays.asList(new KafkaZoneValidator())).setPlansFrom(rawServiceSpec);
    return schedulerBuilder.setEndpointProducer("zookeeper", EndpointProducer.constant(kafkaZookeeperUri)).setCustomResources(getResources(kafkaZookeeperUri)).withSingleRegionConstraint();
}
Also used : RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) SchedulerBuilder(com.mesosphere.sdk.scheduler.SchedulerBuilder) SchedulerConfig(com.mesosphere.sdk.scheduler.SchedulerConfig)

Example 5 with RawServiceSpec

use of com.mesosphere.sdk.specification.yaml.RawServiceSpec in project dcos-commons by mesosphere.

the class ServiceTestRunner method run.

/**
 * Exercises the service's packaging and resulting Service Specification YAML file, then runs the provided
 * simulation ticks, if any are provided.
 *
 * @return a {@link ServiceTestResult} containing the resulting scheduler environment and spec information
 * @throws Exception if the test failed
 */
public ServiceTestResult run(Collection<SimulationTick> ticks) throws Exception {
    SchedulerConfig mockSchedulerConfig = Mockito.mock(SchedulerConfig.class);
    Mockito.when(mockSchedulerConfig.getExecutorURI()).thenReturn("test-executor-uri");
    Mockito.when(mockSchedulerConfig.getLibmesosURI()).thenReturn("test-libmesos-uri");
    Mockito.when(mockSchedulerConfig.getJavaURI()).thenReturn("test-java-uri");
    Mockito.when(mockSchedulerConfig.getBootstrapURI()).thenReturn("bootstrap-uri");
    Mockito.when(mockSchedulerConfig.getApiServerPort()).thenReturn(8080);
    Mockito.when(mockSchedulerConfig.getDcosSpace()).thenReturn("test-space");
    Mockito.when(mockSchedulerConfig.getServiceTLD()).thenReturn(Constants.DNS_TLD);
    Mockito.when(mockSchedulerConfig.getSchedulerRegion()).thenReturn(Optional.of("test-scheduler-region"));
    Capabilities mockCapabilities = Mockito.mock(Capabilities.class);
    Mockito.when(mockCapabilities.supportsGpuResource()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsCNINetworking()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsNamedVips()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsRLimits()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsPreReservedResources()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsFileBasedSecrets()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsEnvBasedSecretsProtobuf()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsEnvBasedSecretsDirectiveLabel()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsDomains()).thenReturn(true);
    Mockito.when(mockCapabilities.supportsDefaultExecutor()).thenReturn(supportsDefaultExecutor);
    Capabilities.overrideCapabilities(mockCapabilities);
    // Disable background TaskKiller thread, to avoid erroneous kill invocations
    TaskKiller.reset(false);
    Map<String, String> schedulerEnvironment = CosmosRenderer.renderSchedulerEnvironment(cosmosOptions, buildTemplateParams);
    schedulerEnvironment.putAll(customSchedulerEnv);
    // Test 1: Does RawServiceSpec render?
    RawServiceSpec rawServiceSpec = RawServiceSpec.newBuilder(specPath).setEnv(schedulerEnvironment).build();
    // Test 2: Does ServiceSpec render?
    ServiceSpec serviceSpec = DefaultServiceSpec.newGenerator(rawServiceSpec, mockSchedulerConfig, schedulerEnvironment, configTemplateDir).build();
    // Test 3: Does the scheduler build?
    SchedulerBuilder schedulerBuilder = DefaultScheduler.newBuilder(serviceSpec, mockSchedulerConfig, persister).setPlansFrom(rawServiceSpec).setRecoveryManagerFactory(recoveryManagerFactory).setCustomConfigValidators(validators);
    if (namespace.isPresent()) {
        schedulerBuilder.setNamespace(namespace.get());
    }
    AbstractScheduler abstractScheduler = schedulerBuilder.build().disableThreading().disableApiServer();
    // Test 4: Can we render the per-task config templates without any missing values?
    Collection<ServiceTestResult.TaskConfig> taskConfigs = getTaskConfigs(serviceSpec, mockSchedulerConfig);
    // Test 5: Run simulation, if any was provided
    ClusterState clusterState;
    if (oldClusterState == null) {
        // Initialize new cluster state
        clusterState = ClusterState.create(serviceSpec, abstractScheduler);
    } else {
        // Carry over prior cluster state
        clusterState = ClusterState.withUpdatedConfig(oldClusterState, serviceSpec, abstractScheduler);
    }
    SchedulerDriver mockDriver = Mockito.mock(SchedulerDriver.class);
    for (SimulationTick tick : ticks) {
        if (tick instanceof Expect) {
            LOGGER.info("EXPECT: {}", tick.getDescription());
            try {
                ((Expect) tick).expect(clusterState, mockDriver);
            } catch (Throwable e) {
                throw buildSimulationError(ticks, tick, e);
            }
        } else if (tick instanceof Send) {
            LOGGER.info("SEND:   {}", tick.getDescription());
            ((Send) tick).send(clusterState, mockDriver, abstractScheduler.getMesosScheduler().get());
        } else {
            throw new IllegalArgumentException(String.format("Unrecognized tick type: %s", tick));
        }
    }
    // Reset Capabilities API to default behavior:
    Capabilities.overrideCapabilities(null);
    // Re-enable background TaskKiller thread for other tests
    TaskKiller.reset(true);
    return new ServiceTestResult(serviceSpec, rawServiceSpec, schedulerEnvironment, taskConfigs, persister, clusterState);
}
Also used : RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) Capabilities(com.mesosphere.sdk.dcos.Capabilities) SchedulerBuilder(com.mesosphere.sdk.scheduler.SchedulerBuilder) SchedulerConfig(com.mesosphere.sdk.scheduler.SchedulerConfig) AbstractScheduler(com.mesosphere.sdk.scheduler.AbstractScheduler) SchedulerDriver(org.apache.mesos.SchedulerDriver)

Aggregations

RawServiceSpec (com.mesosphere.sdk.specification.yaml.RawServiceSpec)8 SchedulerConfig (com.mesosphere.sdk.scheduler.SchedulerConfig)4 File (java.io.File)4 SchedulerBuilder (com.mesosphere.sdk.scheduler.SchedulerBuilder)2 MemPersister (com.mesosphere.sdk.storage.MemPersister)2 Test (org.junit.Test)2 TaskEnvCannotChange (com.mesosphere.sdk.config.validate.TaskEnvCannotChange)1 Capabilities (com.mesosphere.sdk.dcos.Capabilities)1 AbstractScheduler (com.mesosphere.sdk.scheduler.AbstractScheduler)1 Phase (com.mesosphere.sdk.scheduler.plan.Phase)1 Plan (com.mesosphere.sdk.scheduler.plan.Plan)1 DefaultServiceSpec (com.mesosphere.sdk.specification.DefaultServiceSpec)1 RawPlan (com.mesosphere.sdk.specification.yaml.RawPlan)1 StateStore (com.mesosphere.sdk.state.StateStore)1 Persister (com.mesosphere.sdk.storage.Persister)1 Map (java.util.Map)1 SchedulerDriver (org.apache.mesos.SchedulerDriver)1