Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic server test #21

Merged
merged 7 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ name: Build & Deploy
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: |
make test
- name: Build image & push to ECR
if: ${{ github.event_name == 'push' }} # don't deploy branches
uses: kciter/aws-ecr-action@master
with:
access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ dev:
rm_db:
find . -path "./.dev_db/*" -not -name ".gitignore" -delete

.PHONY: dev rm_db
.PHONY: dev rm_db

test:
go test ./... -v
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Backend service for the ALC app
# Local development
1. Copy docker-compose.dev-example.yml to docker-compose.yml.
2. Fill in the OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET environment variables.
3. Run ``make dev`` to build the image and start the server.
3. Ensure Docker is running, then run ``make dev`` to build the image and start the server.
4. When you modify .go files, the server automatically rebuilds and restarts.

# Wiping local database
Expand Down
45 changes: 45 additions & 0 deletions functional_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"github.com/jmoiron/sqlx"
"github.com/lestrrat-go/test-mysqld"
"os"
"testing"
"net/http"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/avast/retry-go/v3"
)

func TestServer(t *testing.T) {
/* Set up MySQL */
mysqld, err := mysqltest.NewMysqld(nil)
if err != nil {
t.Fatalf("failed to start mysqld: %s", err)
}
db, err := sqlx.Open("mysql", mysqld.Datasource("test", "", "", 0))
if err != nil {
t.Fatalf("failed to open MySQL connection: %s", err)
}

/* Set up ENV variables */
os.Setenv("OAUTH_CLIENT_ID", "testVal")
os.Setenv("OAUTH_CLIENT_SECRET", "testVal")
os.Setenv("BASE_URL", "testVal")
os.Setenv("S3_REGION", "testVal")
os.Setenv("S3_AUTH_ID", "testVal")
os.Setenv("S3_SECRET", "testVal")
go main0(db)


var response *http.Response
retry.Do(func () error {
resp, err := http.Get("http://localhost:8080/healthcheck")
fmt.Println("{}, {}", resp, err)
if err == nil {
response = resp
}
return err
})
assert.Equal(t, response.StatusCode, 200)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ module github.com/dxe/alc-mobile-api
go 1.16

require (
github.com/avast/retry-go/v3 v3.1.1
github.com/aws/aws-sdk-go v1.39.5
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/jakehobbs/exponent-server-sdk-golang v0.0.0-20210824033332-ffcc939b98d7
github.com/jmoiron/sqlx v1.3.4
github.com/lestrrat-go/tcputil v0.0.0-20180223003554-d3c7f98154fb // indirect
github.com/lestrrat-go/test-mysqld v0.0.0-20190527004737-6c91be710371
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go/v3 v3.1.1 h1:49Scxf4v8PmiQ/nY0aY3p0hDueqSmc7++cBbtiDGu2g=
github.com/avast/retry-go/v3 v3.1.1/go.mod h1:6cXRK369RpzFL3UQGqIUp9Q7GDrams+KsYWrfNA1/nQ=
github.com/aws/aws-sdk-go v1.39.5 h1:yoJEE1NJxbpZ3CtPxvOSFJ9ByxiXmBTKk8J+XU5ldtg=
github.com/aws/aws-sdk-go v1.39.5/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -118,12 +121,17 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lestrrat-go/tcputil v0.0.0-20180223003554-d3c7f98154fb h1:sb9NxqWoS17VT3aZd4mlBm48bsaHB1Fvwro3H/uiuZM=
github.com/lestrrat-go/tcputil v0.0.0-20180223003554-d3c7f98154fb/go.mod h1:bBamYL9/WjNn0b2CS4v4F8cHmWRpClSxrpEoAY+maJo=
github.com/lestrrat-go/test-mysqld v0.0.0-20190527004737-6c91be710371 h1:3krMZFzgjxQciWgQxfaj4wd1zKsnis7MRg52/Do9btk=
github.com/lestrrat-go/test-mysqld v0.0.0-20190527004737-6c91be710371/go.mod h1:nNdGDcaEskqrh833et3XzSkflbxqVuf5OBX4S/ho/CM=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -135,6 +143,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ func getDSN() string {

func main() {
flag.Parse()
db := model.NewDB(getDSN())
main0(db)
}

func main0(db *sqlx.DB) {
flag.Parse()

// TODO(mdempsky): Generalize.
mux := http.NewServeMux()

db := model.NewDB(getDSN())

// TODO: Consider not doing this each time the application loads.
// It may be better to do it via a script instead.
if !*flagProd {
Expand Down