-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #551 from kinvolk/api_test
Add API integration tests
- Loading branch information
Showing
17 changed files
with
3,228 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
Oops, something went wrong.