Ejemplo n.º 1
0
func Test_Main_should_Start_An_Nsq_Service(t *testing.T) {
	//given
	initVar()
	b := startLookUp()
	defer b.Stop()
	// when
	startListening()
	defer graceFullShutDown()
	sendMsg()
	// then
	receip, consumer := testtools.SetupListener("report", b.LookUpHttpAddrr+":"+b.LookUpHttpPort)
	defer close(receip)
	defer consumer.Stop()
	assert.NotNil(t, consumer)
	assert.NotNil(t, receip)
	// first get the hello from the agent, then the message with the build log and finally the last report with
	// the build status (should be SUCCESS here)
	hello := <-receip
	buildTrace := <-receip
	finalReport := <-receip

	assert.Contains(t, testtools.FromSliceToString(hello.Logs), "Hello")
	assert.Equal(t, hello.Status, message.SUCCESS)
	assert.Contains(t, testtools.FromSliceToString(buildTrace.Logs), "toto-build-agent/testapp")
	assert.Equal(t, buildTrace.Status, message.WORKING)
	assert.Equal(t, finalReport.Status, message.SUCCESS)
}
// test the command execution
func Test_execCommand_should_failed_for_non_existing_command(t *testing.T) {
	// given
	reportChan := make(chan message.Report, 1)
	defer close(reportChan)
	mes := message.ToWork{int64(1), message.PACKAGE, "toto", "go1.6", "https://github.com/vil-coyote-acme/toto-example.git"}
	// when
	execCommand(exec.Command("toto", "isHappy"), mes, reportChan)
	out := <-reportChan
	// then
	t.Logf("test the exec command failure. Output : %s, %d", out.Logs, len(out.Logs))
	assert.Contains(t, testtools.FromSliceToString(out.Logs), "executable file not found in $PATH")
	assert.Equal(t, out.Status, message.FAILED)
}
// test the build function
func Test_Should_Build_Test_Sources(t *testing.T) {
	// given
	reportChan := make(chan message.Report, 1)
	defer close(reportChan)
	mes := message.ToWork{int64(1), message.PACKAGE, "toto-build-agent/testapp", "go1.6", "https://github.com/vil-coyote-acme/toto-example.git"}
	// when
	build.BuildPackage(mes, reportChan)
	msg := <-reportChan
	end := <-reportChan
	t.Logf("Test the go build command with succes. Output : %s\n\r", msg.Logs)
	assert.Contains(t, testtools.FromSliceToString(msg.Logs), "toto-build-agent/testapp")
	assert.Equal(t, msg.Status, message.WORKING)
	assert.Equal(t, end.Status, message.SUCCESS)
}
func Test_ExecuteJob_Should_Reply_To_Hello(t *testing.T) {
	// given
	toWorkChan := make(chan message.ToWork)
	reportChan := make(chan message.Report, 1)
	defer close(reportChan)
	defer close(toWorkChan)
	// when
	build.ExecuteJob(toWorkChan, reportChan)
	toWorkChan <- message.ToWork{int64(1), message.HELLO, "", "go1.6", "https://github.com/vil-coyote-acme/toto-example.git"}
	msg := <-reportChan
	t.Logf("Test the go build command with succes. Output : %s\n\r", msg.Logs)
	assert.Contains(t, testtools.FromSliceToString(msg.Logs), "Hello")
	assert.Equal(t, msg.Status, message.SUCCESS)
}
func Test_BuildPackage_should_failed_for_unknown_package(t *testing.T) {
	// given
	reportChan := make(chan message.Report, 1)
	defer close(reportChan)
	mes := message.ToWork{int64(1), message.PACKAGE, "plop/", "go1.6", "https://github.com/vil-coyote-acme/toto-example.git"}
	// when
	build.BuildPackage(mes, reportChan)
	msg := <-reportChan
	end := <-reportChan
	// then
	t.Logf("test the exec command failure. Output : %s, %d", msg.Logs, len(msg.Logs))
	assert.Contains(t, testtools.FromSliceToString(msg.Logs), "can't load package: package plop")
	assert.Equal(t, msg.Status, message.WORKING)
	assert.Equal(t, end.Status, message.FAILED)
}