add test code
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,7 @@
|
|||||||
.env
|
.env
|
||||||
|
|
||||||
|
ci/app/go-site
|
||||||
|
|
||||||
data/gitlab-test/gitlab
|
data/gitlab-test/gitlab
|
||||||
data/gitlab-test/postgresql
|
data/gitlab-test/postgresql
|
||||||
data/gitlab-test/redis
|
data/gitlab-test/redis
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ variables:
|
|||||||
|
|
||||||
stages:
|
stages:
|
||||||
- build
|
- build
|
||||||
|
- test
|
||||||
- push
|
- push
|
||||||
- artifacts
|
- artifacts
|
||||||
- release
|
- release
|
||||||
@@ -26,6 +27,16 @@ stages:
|
|||||||
when: never
|
when: never
|
||||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||||
|
|
||||||
|
test_code:
|
||||||
|
stage: test
|
||||||
|
image: golang:latest
|
||||||
|
<<: *rules
|
||||||
|
script:
|
||||||
|
- cd app
|
||||||
|
- CGO_ENABLED=0 GOOS=linux GO111MODULE=auto go test
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
build_main:
|
build_main:
|
||||||
stage: build
|
stage: build
|
||||||
image: golang:latest
|
image: golang:latest
|
||||||
|
|||||||
26
ci/app/go_test.go
Normal file
26
ci/app/go_test.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func TestHealthMessage(t *testing.T) {
|
||||||
|
wr := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||||
|
|
||||||
|
healthMessage(wr, req)
|
||||||
|
if wr.Code != http.StatusOK {
|
||||||
|
t.Errorf("got HTTP status code %d, expected 200", wr.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(wr.Body.String(), `{"health":"OK"}`) {
|
||||||
|
t.Errorf(
|
||||||
|
`response body "%s" does not contain "health OK"`,
|
||||||
|
wr.Body.String(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte("<h1>Hello World, im ver:"+Version+" !</h1>"))
|
w.Write([]byte("<h1>Hello World, im ver:"+Version+" !</h1>"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func healthMessage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
w.Write([]byte(`{"health":"OK"}`))
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
@@ -22,5 +27,6 @@ func main() {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/", indexHandler)
|
mux.HandleFunc("/", indexHandler)
|
||||||
|
mux.HandleFunc("/health", healthMessage)
|
||||||
http.ListenAndServe(":"+port, mux)
|
http.ListenAndServe(":"+port, mux)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user