Skip to content

Commit

Permalink
Merge pull request #551 from kinvolk/api_test
Browse files Browse the repository at this point in the history
Add API integration tests
  • Loading branch information
joaquimrocha authored Jan 11, 2022
2 parents 65f5fdb + d5dc961 commit 34fb639
Show file tree
Hide file tree
Showing 17 changed files with 3,228 additions and 11 deletions.
21 changes: 10 additions & 11 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ coverage.out: check-code-coverage
check-code-coverage:
go test -p 1 -coverprofile=coverage.out ./...

container_id:
./tools/setup_local_db.sh \
--id-file container_id.tmp \
--db-name nebraska_tests \
--password nebraska \
--pg-version 13.3
mv container_id.tmp container_id
.PHONY: test-service-up
test-service-up:
docker compose -f ./docker-compose.test.yaml up -d

.PHONY: test-service-down
test-service-down:
docker compose -f ./docker-compose.test.yaml down

.PHONY: check-backend-with-container
check-backend-with-container: container_id
set -e; \
trap "$(DOCKER_CMD) kill $$(cat container_id); $(DOCKER_CMD) rm $$(cat container_id); rm -f container_id" EXIT; \
go test -p 1 ./...
check-backend-with-container: test-service-up
go clean -testcache && go test -p 1 ./...
$(MAKE) test-service-down

run: bin/nebraska
./bin/nebraska -auth-mode noop -debug
Expand Down
30 changes: 30 additions & 0 deletions backend/docker-compose.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: "3.9"

services:
postgres:
image: postgres:13.3
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: nebraska
POSTGRES_DB: nebraska_tests
POSTGRES_USER: postgres
TZ: UTC
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5

server:
build:
context: ../
dockerfile: Dockerfile
ports:
- "8000:8000"
depends_on:
postgres:
condition: service_healthy
environment:
- NEBRASKA_DB_URL=postgres://postgres:nebraska@postgres:5432/nebraska_tests?sslmode=disable&connect_timeout=10
command: sh -c "/nebraska/nebraska --auth-mode=noop --http-static-dir=/nebraska/static"
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.2.0
github.com/gorilla/securecookie v1.1.1
github.com/jackc/pgx/v4 v4.11.0
github.com/jinzhu/copier v0.3.4
github.com/jmoiron/sqlx v1.3.3
github.com/kevinburke/go-bindata v3.22.0+incompatible
github.com/kinvolk/go-omaha v0.0.1
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ github.com/jgautheron/goconst v1.4.0/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7H
github.com/jhump/protoreflect v1.6.1/go.mod h1:RZQ/lnuN+zqeRVpQigTwO6o0AJUkxbnSnpuG7toUTG4=
github.com/jingyugao/rowserrcheck v0.0.0-20210315055705-d907ca737bb1 h1:4Rlb26NqzNtbDH69CRpr0vZooj3jAlXTycWCX3xRYAY=
github.com/jingyugao/rowserrcheck v0.0.0-20210315055705-d907ca737bb1/go.mod h1:TOQpc2SLx6huPfoFGK3UOnEG+u02D3C1GeosjupAKCA=
github.com/jinzhu/copier v0.3.4 h1:mfU6jI9PtCeUjkjQ322dlff9ELjGDu975C2p/nrubVI=
github.com/jinzhu/copier v0.3.4/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af h1:KA9BjwUk7KlCh6S9EAGWBt1oExIUv9WyNCiRz5amv48=
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af/go.mod h1:HEWGJkRDzjJY2sqdDwxccsGicWEf9BQOZsq2tV+xzM0=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
9 changes: 9 additions & 0 deletions backend/test/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# E2E Test

1. Run the docker compose file with the following command to get the service up and running.

> docker compose -f ./docker-compose.test.yaml up -d
2. Run the test using the following command

> go test -v .
40 changes: 40 additions & 0 deletions backend/test/api/activity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package api_test

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/kinvolk/nebraska/backend/pkg/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestListActivity(t *testing.T) {
t.Run("success", func(t *testing.T) {
// establish DB connection
db := newDBForTest(t)
teamID := getTeamID(t, db)

endTime := time.Now()
startTime := time.Now().Add(time.Duration(-1 * 24 * 7 * time.Hour))
activitiesDB, err := db.GetActivity(teamID, api.ActivityQueryParams{Start: startTime, End: endTime})
require.NoError(t, err)
require.NotNil(t, activitiesDB)

// fetch activity from api
url := fmt.Sprintf("%s/api/activity?start=%s&end=%s", testServerURL, startTime.Format(time.RFC3339), endTime.Format(time.RFC3339))
method := "GET"

// response
var activities []api.Activity

httpDo(t, url, method, nil, http.StatusOK, "json", &activities)

assert.Equal(t, len(activitiesDB), len(activities))
assert.Equal(t, activitiesDB[0].AppID, activities[0].AppID)
assert.Equal(t, activitiesDB[0].GroupID, activities[0].GroupID)
assert.Equal(t, activitiesDB[0].GroupName, activities[0].GroupName)
})
}
35 changes: 35 additions & 0 deletions backend/test/api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package api_test

import (
"log"
"os"
"testing"

"github.com/kinvolk/nebraska/backend/pkg/api"
)

const (
testServerURL = "http://localhost:8000"
defaultTestDbURL = "postgres://postgres:[email protected]:5432/nebraska_tests?sslmode=disable&connect_timeout=10"
)

func TestMain(m *testing.M) {
if os.Getenv("NEBRASKA_SKIP_TESTS") != "" {
return
}

if _, ok := os.LookupEnv("NEBRASKA_DB_URL"); !ok {
log.Printf("NEBRASKA_DB_URL not set, setting to default %q\n", defaultTestDbURL)
_ = os.Setenv("NEBRASKA_DB_URL", defaultTestDbURL)
}

a, err := api.New(api.OptionInitDB)
if err != nil {
log.Printf("Failed to init DB: %v\n", err)
log.Println("These tests require PostgreSQL running and a tests database created, please adjust NEBRASKA_DB_URL as needed.")
os.Exit(1)
}
a.Close()

os.Exit(m.Run())
}
166 changes: 166 additions & 0 deletions backend/test/api/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package api_test

import (
"fmt"
"net/http"
"strings"
"testing"

"github.com/kinvolk/nebraska/backend/pkg/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestListApp(t *testing.T) {
t.Run("success", func(t *testing.T) {
// establish DB connection
db := newDBForTest(t)

// fetch teamID
teamID := getTeamID(t, db)

// get apps from DB
appsDB, err := db.GetApps(teamID, 1, 10)
require.NoError(t, err)
require.NotNil(t, appsDB)

// fetch apps from the API
url := fmt.Sprintf("%s/api/apps", testServerURL)
method := "GET"

// TODO: will require change as response struct is changed in POC2 branch
var appsResp []*api.Application

httpDo(t, url, method, nil, http.StatusOK, "json", &appsResp)

assert.NotEqual(t, len(appsDB), 0)
assert.Equal(t, len(appsDB), len(appsResp))

for i := range appsDB {
assert.Equal(t, appsResp[i].ID, appsDB[i].ID)
assert.Equal(t, appsResp[i].Name, appsDB[i].Name)
}
})
}

func TestCreateApp(t *testing.T) {
// establish DB connection
db := newDBForTest(t)

t.Run("success_do_not_copy", func(t *testing.T) {
// Create App request
url := fmt.Sprintf("%s%s", testServerURL, "/api/apps")
method := "POST"

appName := "test"
payload := strings.NewReader(fmt.Sprintf(`{"name":"%s"}`, appName))

// response struct
var application api.Application

httpDo(t, url, method, payload, http.StatusOK, "json", &application)

assert.Equal(t, appName, application.Name)

// check if app exists in DB
app, err := db.GetApp(application.ID)
require.NoError(t, err)

assert.Equal(t, application.ID, app.ID)
})

t.Run("success_with_copy", func(t *testing.T) {
app := getRandomApp(t, db)

// Create App request
url := fmt.Sprintf("%s/api/apps?clone_from=%s", testServerURL, app.ID)
method := "POST"

appName := "test_with_clone"
payload := strings.NewReader(fmt.Sprintf(`{"name":"%s"}`, appName))

// response
var application api.Application

httpDo(t, url, method, payload, http.StatusOK, "json", &application)

// check if app exists in DB
app, err := db.GetApp(application.ID)
require.NoError(t, err)

assert.Equal(t, application.ID, app.ID)
})
}

func TestGetApp(t *testing.T) {
t.Run("success", func(t *testing.T) {
// establish DB connection
db := newDBForTest(t)

app := getRandomApp(t, db)

// fetch app by id request
url := fmt.Sprintf("%s/api/apps/%s", testServerURL, app.ID)
method := "GET"

// check response
var application api.Application

httpDo(t, url, method, nil, http.StatusOK, "json", &application)

assert.Equal(t, app.Name, application.Name)
assert.Equal(t, app.Instances, application.Instances)
})
}

func TestUpdateApp(t *testing.T) {
t.Run("success", func(t *testing.T) {
// establish DB connection
db := newDBForTest(t)

// get random app from DB to update
app := getRandomApp(t, db)

// Update App Request
url := fmt.Sprintf("%s/api/apps/%s", testServerURL, app.ID)
method := "PUT"

name := "updated_name"
payload := strings.NewReader(fmt.Sprintf(`{"name":"%s","description":"%s","id":"%s"}`, name, app.Description, app.ID))

// response struct
var application api.Application

httpDo(t, url, method, payload, http.StatusOK, "json", &application)

assert.Equal(t, name, application.Name)

// check name in DB

app, err := db.GetApp(app.ID)
require.NoError(t, err)

assert.Equal(t, name, app.Name)
})
}

func TestDeleteApp(t *testing.T) {
t.Run("success", func(t *testing.T) {
// establish DB connection
db := newDBForTest(t)

// get random app from db
app := getRandomApp(t, db)

// Update App Request
url := fmt.Sprintf("%s/api/apps/%s", testServerURL, app.ID)
method := "DELETE"

httpDo(t, url, method, nil, http.StatusNoContent, "", nil)

// check if app exists in db
app, err := db.GetApp(app.ID)
assert.Error(t, err)
assert.Nil(t, app)
})
}
Loading

0 comments on commit 34fb639

Please sign in to comment.