Search in sources :

Example 1 with AbstractScheduler

use of com.mesosphere.sdk.scheduler.AbstractScheduler 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

Capabilities (com.mesosphere.sdk.dcos.Capabilities)1 AbstractScheduler (com.mesosphere.sdk.scheduler.AbstractScheduler)1 SchedulerBuilder (com.mesosphere.sdk.scheduler.SchedulerBuilder)1 SchedulerConfig (com.mesosphere.sdk.scheduler.SchedulerConfig)1 RawServiceSpec (com.mesosphere.sdk.specification.yaml.RawServiceSpec)1 SchedulerDriver (org.apache.mesos.SchedulerDriver)1