func ensureEnvironment(t *testing.T) (*runtime.Environment, engines.Engine, plugins.Plugin) { tempPath := filepath.Join(os.TempDir(), slugid.Nice()) tempStorage, err := runtime.NewTemporaryStorage(tempPath) if err != nil { t.Fatal(err) } environment := &runtime.Environment{ TemporaryStorage: tempStorage, } engineProvider := engines.Engines()["mock"] engine, err := engineProvider.NewEngine(engines.EngineOptions{ Environment: environment, Log: logger.WithField("engine", "mock"), }) if err != nil { t.Fatal(err.Error()) } pluginOptions := plugins.PluginOptions{ Environment: environment, Engine: engine, Log: logger.WithField("component", "Plugin Manager"), } pm, err := plugins.Plugins()["success"].NewPlugin(pluginOptions) if err != nil { t.Fatalf("Error creating task manager. Could not create plugin manager. %s", err) } return environment, engine, pm }
// 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) }