简体   繁体   English

如何在Travis-CI中运行Go服务器?

[英]How to run a Go server in Travis-CI?

I'm working on a Go project that exposes a RESTful http API. 我正在一个Go项目中,该项目公开了RESTful HTTP API。

I want to run the Go project and use Nodejs (Mocha) to test the endpoints. 我想运行Go项目并使用Nodejs(Mocha)测试端点。 It seems as if the nohup command doesn't keep running in the background. 似乎nohup命令没有在后台继续运行。

Locally everything works but I don't seem to be able to get it running in Travis-ci. 在本地,一切正常,但是我似乎无法在Travis-ci中运行它。

language: go
go:
  - 1.8
env:
  - "PATH=/home/travis/gopath/bin:$PATH"
before_install:
  - go get ./...
script:
  - npm install mocha -g
  - npm install
  - nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
  - mocha

First, you should seriously question whether writing tests in mocha makes sense. 首先,您应该认真地质疑用Mocha编写测试是否有意义。 Admittedly, there might be cases when it does make sense (ie if you're porting a nodejs app to Go, and already have tests written for node). 诚然,在某些情况下它确实有意义(例如,如果您要将nodejs应用移植到Go,并且已经为node编写了测试)。 But even then, you should consider this a stop-gap measure, and write all new tests, and even migrate old tests, to Go as soon as possible. 但是即使那样,您也应该将其视为权宜之计,并尽快将所有新测试甚至迁移旧测试写入Go。

But that aside, there's no reason you shouldn't be able to just start the process in the background normally. 除此之外,没有理由您不能仅在后台正常启动该过程。 Perhaps with a shell script called from your Travis config (can often be cleaner and easier to follow than putting all commands in the config directly): 也许使用从Travis配置中调用的shell脚本(与直接将所有命令都直接放入配置相比,它通常更干净,更易于遵循):

#!/bin/sh

go run &

mocha

If you really must run your tests in an external process, there are certain advantages from starting that from a Go test anyway. 如果您确实必须在外部流程中运行测试,无论如何从Go测试开始就有一定的优势。 Namely, you can get test coverage stats, and starting can be more easily synchronized (so you don't need a sleep ). 也就是说,您可以获得测试覆盖率统计信息,并且启动可以更轻松地同步(因此您无需sleep )。 To do this, you can follow my advice in this answer . 为此,您可以在此答案中遵循我的建议。 Specifically, for the mocha case: 具体来说,对于摩卡咖啡情况:

Write a test file that executes your main() function in a go routine: 编写一个在go例程中执行main()函数的测试文件:

func TestMainApp(t *testing.T) {
    go main() // Or whatever you need to do to start the server process
    cmd := exec.Command("mocha", ...)
    cmd.Start()
}

But seriously. 不过实话说。 You should write your tests in Go. 您应该在Go中编写测试。

Mocha runs directly after - nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 & so the server didn't start up yet. Mocha直接在- nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &因此服务器尚未启动。

Adding a sleep was enough. 增加睡眠就足够了。

script:
  - npm install mocha -g
  - npm install
  - nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
  - sleep 4
  - mocha

But, the point is taken, I will move to Go tests :-P 但是,重点是,我将转到Go测试:-P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM