func makeResultSet(t *testing.T) resultset {
	temp, err := runtime.NewTemporaryStorage(os.TempDir())
	if err != nil {
		t.Fatal(err)
	}

	context, _, err := runtime.NewTaskContext(temp.NewFilePath(), runtime.TaskInfo{}, nil)
	if err != nil {
		t.Fatal(err)
	}

	config := configType{
		CreateUser: false,
	}

	e := engine{
		config: &config,
		log:    logrus.New().WithField("component", "test"),
	}

	return resultset{
		ResultSetBase: engines.ResultSetBase{},
		taskUser:      user{},
		context:       context,
		success:       true,
		engine:        &e,
	}
}
Example #2
0
func newTaskRun(
	config *configType,
	claim *taskClaim,
	environment *runtime.Environment,
	engine engines.Engine,
	plugin plugins.Plugin,
	log *logrus.Entry,
) (*TaskRun, error) {

	tp := environment.TemporaryStorage.NewFilePath()
	info := runtime.TaskInfo{
		TaskID:   claim.taskClaim.Status.TaskID,
		RunID:    claim.taskClaim.RunID,
		Created:  claim.taskClaim.Task.Created,
		Deadline: claim.taskClaim.Task.Deadline,
		Expires:  claim.taskClaim.Task.Expires,
	}
	ctxt, ctxtctl, err := runtime.NewTaskContext(tp, info, environment.WebHookServer)

	queueClient := queue.New(&tcclient.Credentials{
		ClientID:    claim.taskClaim.Credentials.ClientID,
		AccessToken: claim.taskClaim.Credentials.AccessToken,
		Certificate: claim.taskClaim.Credentials.Certificate,
	})

	if config.QueueBaseURL != "" {
		queueClient.BaseURL = config.QueueBaseURL
	}

	ctxtctl.SetQueueClient(queueClient)

	if err != nil {
		return nil, err
	}

	t := &TaskRun{
		TaskID:       claim.taskID,
		RunID:        claim.runID,
		definition:   claim.definition,
		log:          log,
		context:      ctxt,
		controller:   ctxtctl,
		engine:       engine,
		plugin:       plugin,
		queueURL:     config.QueueBaseURL,
		stopReclaims: make(chan struct{}),
		reclaimsDone: make(chan struct{}),
	}

	go t.reclaim(time.Time(claim.taskClaim.TakenUntil))

	return t, nil
}
func newTestSandbox(taskPayload *payloadType, env []string) (*sandbox, error) {
	temp, err := runtime.NewTemporaryStorage(os.TempDir())
	if err != nil {
		return nil, err
	}

	context, _, err := runtime.NewTaskContext(temp.NewFilePath(), runtime.TaskInfo{}, nil)
	if err != nil {
		return nil, err
	}

	config := configType{
		CreateUser: false,
	}

	e := engine{
		EngineBase: engines.EngineBase{},
		config:     &config,
		log:        logrus.New().WithField("component", "test"),
	}

	return newSandbox(context, taskPayload, env, &e), nil
}
func (p *EngineProvider) newTestTaskContext() (*runtime.TaskContext, *runtime.TaskContextController) {
	ctx, control, err := runtime.NewTaskContext(p.environment.TemporaryStorage.NewFilePath(), runtime.TaskInfo{}, nil)
	nilOrPanic(err, "Failed to create new TaskContext")
	return ctx, control
}
// Test is called to trigger a plugintest.Case to run
func (c Case) Test() {
	runtimeEnvironment := newTestEnvironment()

	testServer, err := webhookserver.NewTestServer()
	nilOrPanic(err)
	defer testServer.Stop()
	runtimeEnvironment.WebHookServer = testServer

	engineProvider := engines.Engines()["mock"]
	engine, err := engineProvider.NewEngine(engines.EngineOptions{
		Environment: runtimeEnvironment,
		Log:         runtimeEnvironment.Log.WithField("engine", "mock"),
		// TODO: Add engine config
	})
	nilOrPanic(err, "engineProvider.NewEngine failed")

	taskID := c.TaskID
	if taskID == "" {
		taskID = slugid.Nice()
	}

	context, controller, err := runtime.NewTaskContext(runtimeEnvironment.TemporaryStorage.NewFilePath(), runtime.TaskInfo{
		TaskID: taskID,
		RunID:  c.RunID,
	}, testServer)
	nilOrPanic(err)

	if c.QueueMock != nil {
		controller.SetQueueClient(c.QueueMock)
	}

	sandboxBuilder, err := engine.NewSandboxBuilder(engines.SandboxOptions{
		TaskContext: context,
		Payload:     parseEnginePayload(engine, c.Payload),
	})
	nilOrPanic(err, "engine.NewSandboxBuilder failed")

	provider := plugins.Plugins()[c.Plugin]
	assert(provider != nil, "Plugin does not exist! You tried to load: ", c.Plugin)
	p, err := provider.NewPlugin(plugins.PluginOptions{
		Environment: runtimeEnvironment,
		Engine:      engine,
		Log:         runtimeEnvironment.Log.WithField("plugin", c.Plugin),
		Config:      parsePluginConfig(provider, c.PluginConfig),
	})
	nilOrPanic(err, "pluginProvider.NewPlugin failed")

	tp, err := p.NewTaskPlugin(plugins.TaskPluginOptions{
		TaskInfo: &context.TaskInfo,
		Payload:  parsePluginPayload(p, c.Payload),
		Log:      runtimeEnvironment.Log.WithField("plugin", c.Plugin).WithField("taskId", taskID),
	})
	nilOrPanic(err, "plugin.NewTaskPlugin failed")
	// taskPlugin can be nil, if the plugin doesn't want any hooks
	if tp == nil {
		tp = plugins.TaskPluginBase{}
	}

	options := Options{
		Environment:    runtimeEnvironment,
		SandboxBuilder: sandboxBuilder,
		Engine:         engine,
		Plugin:         p,
		TaskPlugin:     tp,
	}

	err = tp.Prepare(context)
	nilOrPanic(err, "taskPlugin.Prepare failed")

	// Set environment variables and proxies
	for key, val := range c.Env {
		nilOrPanic(err, sandboxBuilder.SetEnvironmentVariable(key, val),
			"Error setting env var: %s = %s", key, val)
	}
	for hostname, handler := range c.Proxies {
		nilOrPanic(err, sandboxBuilder.AttachProxy(hostname, handler),
			"Error attaching proxy for hostname: %s", hostname)
	}

	c.maybeRun(c.BeforeBuildSandbox, options)
	err = tp.BuildSandbox(sandboxBuilder)
	nilOrPanic(err, "taskPlugin.BuildSandbox failed")
	c.maybeRun(c.AfterBuildSandbox, options)

	c.maybeRun(c.BeforeStarted, options)
	sandbox, err := sandboxBuilder.StartSandbox()
	nilOrPanic(err, "sandboxBuilder.StartSandbox failed")
	err = tp.Started(sandbox)
	nilOrPanic(err, "taskPlugin.Started failed")
	c.maybeRun(c.AfterStarted, options)

	c.maybeRun(c.BeforeStopped, options)
	resultSet, err := sandbox.WaitForResult()
	nilOrPanic(err, "sandbox.WaitForResult failed")
	assert(resultSet.Success() == c.EngineSuccess)
	success, err := tp.Stopped(resultSet)
	nilOrPanic(err, "taskPlugin.Stopped failed")
	assert(success == c.PluginSuccess)
	c.maybeRun(c.AfterStopped, options)

	c.maybeRun(c.BeforeFinished, options)
	controller.CloseLog()
	err = tp.Finished(success)
	nilOrPanic(err, "taskPlugin.Finished failed")
	c.grepLog(context)
	c.maybeRun(c.AfterFinished, options)

	c.maybeRun(c.BeforeDisposed, options)
	controller.Dispose()
	err = tp.Dispose()
	nilOrPanic(err, "taskPlugin.Dispose failed")
	c.maybeRun(c.AfterDisposed, options)
}