genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[BUGFIX] autodeployment

This commit is contained in:
Martin Geno 2017-05-18 00:34:26 +02:00
parent 978f65eb7d
commit 64379f77ad
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
7 changed files with 52 additions and 35 deletions

View File

@ -6,5 +6,5 @@ install:
- go get github.com/mattn/goveralls - go get github.com/mattn/goveralls
- go get "golang.org/x/tools/cmd/cover" - go get "golang.org/x/tools/cmd/cover"
script: script:
- ./.test-coverage - ./.test-coverage travis-ci
- go install github.com/genofire/hs_master-kss-monolith/cmd/stock - go install github.com/genofire/hs_master-kss-monolith/cmd/stock

View File

@ -26,4 +26,4 @@ deployment:
staging: staging:
branch: master branch: master
commands: commands:
- ./deploy.sh STAGING - ./deploy.sh $HOST_FOR_STAGING $PORT_FOR_STAGING

View File

@ -1,10 +1,20 @@
#!/bin/bash #!/bin/bash
host="HOST_FOR_$1" host=$1
port="PORT_FOR_$1" port=$2
remote="circleci@${!host}" remote="circleci@${host}"
echo "deploying on: $remote" echo "deploying..."
ssh -p $port $remote sudo systemctl stop stock;
scp -p $port bin/stock $remote:~/bin/stock; RETVAL=$?
rsync -e "ssh -p $port $host" -a webroot/ $remote:~/lib/stock/www; [ $RETVAL -ne 0 ] && exit 1
rsync -e "ssh -p $port $host" -a contrib/ $remote:~/lib/stock/contrib; scp -q -P $port ~/.go_workspace/bin/stock $remote:~/bin/stock;
ssh -p $port $remote sudo systemctl restart 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"

View File

@ -36,6 +36,9 @@ func TestAddGood(t *testing.T) {
_, w = session.JSONRequest("POST", "/api/good/7", good) _, w = session.JSONRequest("POST", "/api/good/7", good)
assertion.Equal(http.StatusNotFound, w.StatusCode) 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) _, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusOK, w.StatusCode)

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/genofire/hs_master-kss-monolith/test"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -14,11 +15,10 @@ func TestAuth(t *testing.T) {
PermissionURL = "http://localhost:8080/api-test/session/%s/%d/" PermissionURL = "http://localhost:8080/api-test/session/%s/%d/"
router := http.FileServer(http.Dir("../webroot")) router := http.FileServer(http.Dir("../webroot"))
srv := &http.Server{
Addr: ":8080", mock := test.MockTransport{Handler: router}
Handler: router, http.DefaultClient.Transport = &mock
} mock.Start()
go srv.ListenAndServe()
perm, err := HasPermission("testsessionkey", PermissionCreateGood) perm, err := HasPermission("testsessionkey", PermissionCreateGood)
assert.NoError(err) assert.NoError(err)
@ -28,5 +28,5 @@ func TestAuth(t *testing.T) {
assert.NoError(err) assert.NoError(err)
assert.True(perm) assert.True(perm)
srv.Close() mock.Close()
} }

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/genofire/hs_master-kss-monolith/test"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -14,11 +15,9 @@ func TestProductExists(t *testing.T) {
ProductURL = "http://localhost:8080/api-test/product/%d/" ProductURL = "http://localhost:8080/api-test/product/%d/"
router := http.FileServer(http.Dir("../webroot")) router := http.FileServer(http.Dir("../webroot"))
srv := &http.Server{ mock := test.MockTransport{Handler: router}
Addr: ":8080", http.DefaultClient.Transport = &mock
Handler: router, mock.Start()
}
go srv.ListenAndServe()
ok, err := ProductExists(3) ok, err := ProductExists(3)
assert.True(ok) assert.True(ok)
@ -29,11 +28,10 @@ func TestProductExists(t *testing.T) {
assert.True(ok) assert.True(ok)
assert.NoError(err) assert.NoError(err)
mock.Close()
productExistCache = make(map[int64]boolMicroServiceCache) productExistCache = make(map[int64]boolMicroServiceCache)
ProductURL = "http://localhost:8081/api-test/product/%d/"
ok, err = ProductExists(3) ok, err = ProductExists(3)
assert.Error(err) assert.Error(err)
srv.Close()
} }

View File

@ -17,20 +17,27 @@ import (
) )
// Pointer to the server // Pointer to the server
var srv bool var mock *MockTransport
type mockTransport struct { type MockTransport struct {
handler http.Handler Handler http.Handler
running bool
} }
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) { func (t *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if !srv { if !t.running {
return nil, errors.New("mock a error") return nil, errors.New("mock a error")
} }
w := httptest.NewRecorder() w := httptest.NewRecorder()
t.handler.ServeHTTP(w, req) t.Handler.ServeHTTP(w, req)
return w.Result(), nil 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) // Function to initialize a test api (with test files of depending microservice)
func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { 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() router = goji.NewMux()
srv = true
mockBackend := http.FileServer(http.Dir("../webroot")) mockBackend := http.FileServer(http.Dir("../webroot"))
mock = &MockTransport{Handler: mockBackend, running: true}
http.DefaultClient.Transport = &mockTransport{handler: mockBackend} http.DefaultClient.Transport = mock
return return
} }
// Function to close the static webserver // Function to close the static webserver
func CloseServer() { func CloseServer() {
srv = false mock.Close()
} }
// Function to close and stop the whole microservice // Function to close and stop the whole microservice
func Close() { func Close() {
database.Close() database.Close()
srv = false mock.Close()
} }
// Handle a test session with cookies // Handle a test session with cookies