From f4eab3cc206e0d90cb13274e6c953dc40102ecc9 Mon Sep 17 00:00:00 2001 From: Alexis Deschamps Date: Sun, 29 Aug 2021 19:47:33 -0700 Subject: [PATCH 1/7] add updates --- Makefile | 5 ++++- README.md | 2 +- functional_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++++ go.sum | 10 ++++++++++ main.go | 8 ++++++-- 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 functional_test.go diff --git a/Makefile b/Makefile index 48dbee0..e5ebe05 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,7 @@ dev: rm_db: find . -path "./.dev_db/*" -not -name ".gitignore" -delete -.PHONY: dev rm_db \ No newline at end of file +.PHONY: dev rm_db + +test: + go test ./... -v diff --git a/README.md b/README.md index a773660..6e044aa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/functional_test.go b/functional_test.go new file mode 100644 index 0000000..83d0ac6 --- /dev/null +++ b/functional_test.go @@ -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) +} diff --git a/go.mod b/go.mod index 9f2d06e..dac6a92 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 5530380..802155b 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/main.go b/main.go index 48a7391..022e4e3 100644 --- a/main.go +++ b/main.go @@ -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 { From bf1a93ed40613777d02b5c2b5144067577295645 Mon Sep 17 00:00:00 2001 From: Alexis Deschamps Date: Sun, 29 Aug 2021 19:49:42 -0700 Subject: [PATCH 2/7] add updates --- .github/workflows/main.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 53510b2..c3b42c3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,3 +20,6 @@ jobs: region: us-west-2 tags: latest,${{ github.sha }} dockerfile: Dockerfile-prod + - name: Run tests + run: | + make test From ba153d131b86bb8c53c3a71f4c366b7af044f5bd Mon Sep 17 00:00:00 2001 From: Alexis Deschamps Date: Sun, 29 Aug 2021 19:51:17 -0700 Subject: [PATCH 3/7] add updates --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c3b42c3..9db9fd2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,6 +3,8 @@ name: Build & Deploy on: push: branches: [ main ] + pull_request: + branches: [ main ] workflow_dispatch: jobs: From ba1bdefc9e23f73e8e9a81b2a32c54d2b61558c1 Mon Sep 17 00:00:00 2001 From: Alexis Deschamps Date: Sun, 29 Aug 2021 19:53:43 -0700 Subject: [PATCH 4/7] add updates --- .github/workflows/main.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9db9fd2..2ad9fdf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,3 +25,12 @@ jobs: - name: Run tests run: | make test + uses: kciter/aws-ecr-action@master + with: + access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} + secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + account_id: ${{ secrets.AWS_ACCOUNT_ID }} + repo: dxe/alc-mobile-api + region: us-west-2 + tags: latest,${{ github.sha }} + dockerfile: Dockerfile-prod \ No newline at end of file From 1ea4773955d7936c7c60cd8714124713adb353d3 Mon Sep 17 00:00:00 2001 From: Alexis Deschamps Date: Sun, 29 Aug 2021 19:54:50 -0700 Subject: [PATCH 5/7] add updates --- .github/workflows/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2ad9fdf..d8526df 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,7 +25,6 @@ jobs: - name: Run tests run: | make test - uses: kciter/aws-ecr-action@master with: access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} From a8610dfd75af58e5c066b227541a9705173157ab Mon Sep 17 00:00:00 2001 From: Alexis Deschamps Date: Sun, 29 Aug 2021 20:00:01 -0700 Subject: [PATCH 6/7] add updates --- .github/workflows/main.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d8526df..9db9fd2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,11 +25,3 @@ jobs: - name: Run tests run: | make test - with: - access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} - secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - account_id: ${{ secrets.AWS_ACCOUNT_ID }} - repo: dxe/alc-mobile-api - region: us-west-2 - tags: latest,${{ github.sha }} - dockerfile: Dockerfile-prod \ No newline at end of file From 4c91cf1b9afe947a4f4d04c041e9836327179151 Mon Sep 17 00:00:00 2001 From: Alexis Deschamps Date: Sun, 29 Aug 2021 20:02:11 -0700 Subject: [PATCH 7/7] add updates --- .github/workflows/main.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9db9fd2..d1999bc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,11 @@ jobs: 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 }} @@ -22,6 +26,3 @@ jobs: region: us-west-2 tags: latest,${{ github.sha }} dockerfile: Dockerfile-prod - - name: Run tests - run: | - make test