示例#1
0
文件: walter.go 项目: bluele/walter
// New creates a Walter instance.
func New(opts *config.Opts) (*Walter, error) {
	log.Infof("Pipeline file path: \"%s\"", opts.PipelineFilePath)
	configData, err := config.ReadConfig(opts.PipelineFilePath)
	if err != nil {
		log.Warn("failed to read the configuration file")
		return nil, err
	}

	envs := config.NewEnvVariables()
	parser := &config.Parser{ConfigData: configData, EnvVariables: envs}
	resources, err := parser.Parse()
	if err != nil {
		log.Warn("failed to parse the configuration")
		return nil, err
	}
	monitorCh := make(chan stages.Mediator)
	engine := &engine.Engine{
		Resources:    resources,
		Opts:         opts,
		MonitorCh:    &monitorCh,
		EnvVariables: envs,
	}
	return &Walter{
		Opts:   opts,
		Engine: engine,
	}, nil
}
示例#2
0
func TestRunOnceWithCleanup(t *testing.T) {
	cleanup := &pipelines.Pipeline{}
	cleanup.AddStage(createCommandStage("echo cleanup"))
	cleanup.AddStage(createCommandStage("echo baz"))
	resources := &pipelines.Resources{
		Reporter: &messengers.FakeMessenger{},
		Pipeline: &pipelines.Pipeline{},
		Cleanup:  cleanup,
	}

	resources.Pipeline.AddStage(createCommandStage("echo foobar"))
	resources.Pipeline.AddStage(createCommandStage("echo baz"))
	monitorCh := make(chan stages.Mediator)
	engine := &Engine{
		Resources:    resources,
		MonitorCh:    &monitorCh,
		EnvVariables: config.NewEnvVariables(),
	}
	result := engine.RunOnce()
	assert.Equal(t, "true", result.Pipeline.States["echo foobar"])
	assert.Equal(t, "true", result.Cleanup.States["echo cleanup"])
	assert.Equal(t, false, result.Pipeline.IsAnyFailure())
	assert.Equal(t, false, result.Cleanup.IsAnyFailure())
	assert.Equal(t, true, result.IsSucceeded())
}
示例#3
0
func TestRunOnceWithOnlyIfSuccess(t *testing.T) {
	resources := &pipelines.Resources{
		Reporter: &messengers.FakeMessenger{},
		Pipeline: &pipelines.Pipeline{},
		Cleanup:  &pipelines.Pipeline{},
	}
	resources.Pipeline.AddStage(createCommandStageWithOnlyIf("first", "echo first", "test 1 -eq 1"))
	resources.Pipeline.AddStage(createCommandStageWithName("second", "echo second"))
	resources.Pipeline.AddStage(createCommandStageWithName("third", "echo third"))
	monitorCh := make(chan stages.Mediator)
	o := &config.Opts{}
	engine := &Engine{
		Resources:    resources,
		MonitorCh:    &monitorCh,
		Opts:         o,
		EnvVariables: config.NewEnvVariables(),
	}
	result := engine.RunOnce()

	assert.Equal(t, 3, len(result.Pipeline.States))
	assert.Equal(t, "true", result.Pipeline.States["first"])
	assert.Equal(t, "true", result.Pipeline.States["second"])
	assert.Equal(t, "true", result.Pipeline.States["third"])
	assert.Equal(t, false, result.Pipeline.IsAnyFailure())
	assert.Equal(t, false, result.Cleanup.IsAnyFailure())
	assert.Equal(t, true, result.IsSucceeded())
}
示例#4
0
func TestRunOnceWithOptsOnStopOnAnyFailure(t *testing.T) {
	resources := &pipelines.Resources{
		Reporter: &messengers.FakeMessenger{},
		Pipeline: &pipelines.Pipeline{},
		Cleanup:  &pipelines.Pipeline{},
	}
	resources.Pipeline.AddStage(createCommandStage("echo foobar"))
	resources.Pipeline.AddStage(createCommandStage("thisiserrorcommand"))
	resources.Pipeline.AddStage(createCommandStage("echo foobar2"))
	monitorCh := make(chan stages.Mediator)
	o := &config.Opts{StopOnAnyFailure: true}
	engine := &Engine{
		Resources:    resources,
		MonitorCh:    &monitorCh,
		Opts:         o,
		EnvVariables: config.NewEnvVariables(),
	}

	result := engine.RunOnce()

	assert.Equal(t, "true", result.Pipeline.States["echo foobar2"])
	assert.Equal(t, true, result.Pipeline.IsAnyFailure())
	assert.Equal(t, false, result.Cleanup.IsAnyFailure())
	assert.Equal(t, false, result.IsSucceeded())
}
示例#5
0
func TestRunOnceWithShellScriptStage(t *testing.T) {
	resources := &pipelines.Resources{
		Reporter: &messengers.FakeMessenger{},
		Pipeline: &pipelines.Pipeline{},
		Cleanup:  &pipelines.Pipeline{},
	}
	resources.Pipeline.AddStage(createShellScriptStage("foobar-shell", "../stages/test_sample.sh"))
	monitorCh := make(chan stages.Mediator)
	engine := &Engine{
		Resources:    resources,
		MonitorCh:    &monitorCh,
		EnvVariables: config.NewEnvVariables(),
	}
	result := engine.RunOnce()

	assert.Equal(t, "true", result.Pipeline.States["foobar-shell"])
	assert.Equal(t, false, result.Pipeline.IsAnyFailure())
	assert.Equal(t, false, result.Cleanup.IsAnyFailure())
	assert.Equal(t, true, result.IsSucceeded())
}
示例#6
0
func execute(stage stages.Stage) stages.Mediator {
	mon := make(chan stages.Mediator)
	e := &Engine{
		MonitorCh: &mon,
		Resources: &pipelines.Resources{
			Reporter: &messengers.FakeMessenger{},
		},
		EnvVariables: config.NewEnvVariables(),
	}

	go e.ExecuteStage(stage)

	mediator := stages.Mediator{States: make(map[string]string), Type: "start"}
	go func() {
		*stage.GetInputCh() <- mediator
		close(*stage.GetInputCh())
	}()

	for {
		_, ok := <-*stage.GetOutputCh()
		if !ok {
			break
		}
	}

	var m stages.Mediator
	acm := stages.Mediator{States: make(map[string]string)}
	for {
		m = <-mon
		for k, v := range m.States {
			acm.States[k] = v
		}
		if m.Type == "end" {
			break
		}
	}
	return acm
}