diff --git a/.travis.yml b/.travis.yml index cb6289e..499747a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,5 @@ install: - go get github.com/mattn/goveralls - go get "golang.org/x/tools/cmd/cover" script: - - ./.test-coverage + - ./.test-coverage travis-ci - go install github.com/genofire/hs_master-kss-monolith/cmd/stock diff --git a/circle.yml b/circle.yml index 888c0bc..a112df5 100644 --- a/circle.yml +++ b/circle.yml @@ -26,4 +26,4 @@ deployment: staging: branch: master commands: - - ./deploy.sh STAGING + - ./deploy.sh $HOST_FOR_STAGING $PORT_FOR_STAGING diff --git a/deploy.sh b/deploy.sh index 006db0a..e9a3fea 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,10 +1,20 @@ #!/bin/bash -host="HOST_FOR_$1" -port="PORT_FOR_$1" -remote="circleci@${!host}" -echo "deploying on: $remote" - -scp -p $port bin/stock $remote:~/bin/stock; -rsync -e "ssh -p $port $host" -a webroot/ $remote:~/lib/stock/www; -rsync -e "ssh -p $port $host" -a contrib/ $remote:~/lib/stock/contrib; -ssh -p $port $remote sudo systemctl restart stock; +host=$1 +port=$2 +remote="circleci@${host}" +echo "deploying..." +ssh -p $port $remote sudo systemctl stop stock; +RETVAL=$? +[ $RETVAL -ne 0 ] && exit 1 +scp -q -P $port ~/.go_workspace/bin/stock $remote:~/bin/stock; +RETVAL=$? +sed -ie 's/http:\/\/localhost:8080/https:\/\/stock.pub.warehost.de/g' webroot/static/js/main.js +[ $RETVAL -eq 0 ] && RETVAL=$? +rsync -e "ssh -p $port" -a webroot/ $remote:~/lib/stock/www; +[ $RETVAL -eq 0 ] && RETVAL=$? +rsync -e "ssh -p $port" -a contrib/ $remote:~/lib/stock/contrib; +[ $RETVAL -eq 0 ] && RETVAL=$? +ssh -p $port $remote sudo systemctl start stock; +[ $RETVAL -eq 0 ] && RETVAL=$? +[ $RETVAL -ne 0 ] && exit 1 +echo "deployed" diff --git a/http/good_test.go b/http/good_test.go index 36987e2..838dd62 100644 --- a/http/good_test.go +++ b/http/good_test.go @@ -36,6 +36,9 @@ func TestAddGood(t *testing.T) { _, w = session.JSONRequest("POST", "/api/good/7", good) assertion.Equal(http.StatusNotFound, w.StatusCode) + _, w = session.JSONRequest("POST", "/api/good/1", true) + assertion.Equal(http.StatusBadRequest, w.StatusCode) + _, w = session.JSONRequest("POST", "/api/good/1", good) assertion.Equal(http.StatusOK, w.StatusCode) diff --git a/runtime/auth_test.go b/runtime/auth_test.go index 2de1c9b..540f2e0 100644 --- a/runtime/auth_test.go +++ b/runtime/auth_test.go @@ -5,6 +5,7 @@ import ( "net/http" "testing" + "github.com/genofire/hs_master-kss-monolith/test" "github.com/stretchr/testify/assert" ) @@ -14,11 +15,10 @@ func TestAuth(t *testing.T) { PermissionURL = "http://localhost:8080/api-test/session/%s/%d/" router := http.FileServer(http.Dir("../webroot")) - srv := &http.Server{ - Addr: ":8080", - Handler: router, - } - go srv.ListenAndServe() + + mock := test.MockTransport{Handler: router} + http.DefaultClient.Transport = &mock + mock.Start() perm, err := HasPermission("testsessionkey", PermissionCreateGood) assert.NoError(err) @@ -28,5 +28,5 @@ func TestAuth(t *testing.T) { assert.NoError(err) assert.True(perm) - srv.Close() + mock.Close() } diff --git a/runtime/productcache_test.go b/runtime/productcache_test.go index 71de270..d541715 100644 --- a/runtime/productcache_test.go +++ b/runtime/productcache_test.go @@ -5,6 +5,7 @@ import ( "net/http" "testing" + "github.com/genofire/hs_master-kss-monolith/test" "github.com/stretchr/testify/assert" ) @@ -14,11 +15,9 @@ func TestProductExists(t *testing.T) { ProductURL = "http://localhost:8080/api-test/product/%d/" router := http.FileServer(http.Dir("../webroot")) - srv := &http.Server{ - Addr: ":8080", - Handler: router, - } - go srv.ListenAndServe() + mock := test.MockTransport{Handler: router} + http.DefaultClient.Transport = &mock + mock.Start() ok, err := ProductExists(3) assert.True(ok) @@ -29,11 +28,10 @@ func TestProductExists(t *testing.T) { assert.True(ok) assert.NoError(err) + mock.Close() productExistCache = make(map[int64]boolMicroServiceCache) - ProductURL = "http://localhost:8081/api-test/product/%d/" ok, err = ProductExists(3) assert.Error(err) - srv.Close() } diff --git a/test/testrest.go b/test/testrest.go index 9d34edc..e64440f 100644 --- a/test/testrest.go +++ b/test/testrest.go @@ -17,20 +17,27 @@ import ( ) // Pointer to the server -var srv bool +var mock *MockTransport -type mockTransport struct { - handler http.Handler +type MockTransport struct { + Handler http.Handler + running bool } -func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) { - if !srv { +func (t *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if !t.running { return nil, errors.New("mock a error") } w := httptest.NewRecorder() - t.handler.ServeHTTP(w, req) + t.Handler.ServeHTTP(w, req) return w.Result(), nil } +func (t *MockTransport) Start() { + t.running = true +} +func (t *MockTransport) Close() { + t.running = false +} // Function to initialize a test api (with test files of depending microservice) func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { @@ -42,23 +49,22 @@ func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { }) router = goji.NewMux() - srv = true mockBackend := http.FileServer(http.Dir("../webroot")) - - http.DefaultClient.Transport = &mockTransport{handler: mockBackend} + mock = &MockTransport{Handler: mockBackend, running: true} + http.DefaultClient.Transport = mock return } // Function to close the static webserver func CloseServer() { - srv = false + mock.Close() } // Function to close and stop the whole microservice func Close() { database.Close() - srv = false + mock.Close() } // Handle a test session with cookies