From 57b5238677314573b7c10c47ed8321c97db1f5a4 Mon Sep 17 00:00:00 2001 From: Santhosh Nagaraj S Date: Tue, 18 Jan 2022 22:58:02 +0530 Subject: [PATCH] backend: Update e2e tests This patch updates the e2e tests according to the changes that were introduced when replacing gin with echo framework Signed-off-by: Santhosh Nagaraj S --- backend/test/api/activity_test.go | 15 ++++++++++----- backend/test/api/app_test.go | 29 ++++++++++++++++++----------- backend/test/api/channel_test.go | 15 ++++++++------- backend/test/api/config_test.go | 2 +- backend/test/api/group_test.go | 16 ++++++++++------ backend/test/api/helper_test.go | 4 ++++ backend/test/api/instance_test.go | 8 ++++---- backend/test/api/omaha_test.go | 4 ++-- backend/test/api/package_test.go | 28 ++++++++++++++++++++++++---- 9 files changed, 81 insertions(+), 40 deletions(-) diff --git a/backend/test/api/activity_test.go b/backend/test/api/activity_test.go index 994ed0119..3bdbd9e51 100644 --- a/backend/test/api/activity_test.go +++ b/backend/test/api/activity_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/kinvolk/nebraska/backend/pkg/api" + "github.com/kinvolk/nebraska/backend/pkg/codegen" ) func TestListActivity(t *testing.T) { @@ -29,13 +30,17 @@ func TestListActivity(t *testing.T) { method := "GET" // response - var activities []api.Activity + var activityResp codegen.ActivityPage - httpDo(t, url, method, nil, http.StatusOK, "json", &activities) + httpDo(t, url, method, nil, http.StatusOK, "json", &activityResp) + + activities := activityResp.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) + for i := range activitiesDB { + assert.Equal(t, activitiesDB[i].AppID.String, activities[i].AppID) + assert.Equal(t, activitiesDB[i].GroupID.String, activities[i].GroupID) + assert.Equal(t, activitiesDB[i].GroupName.String, activities[i].GroupName) + } }) } diff --git a/backend/test/api/app_test.go b/backend/test/api/app_test.go index b0445b1f3..b8797f61a 100644 --- a/backend/test/api/app_test.go +++ b/backend/test/api/app_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/kinvolk/nebraska/backend/pkg/api" + "github.com/kinvolk/nebraska/backend/pkg/codegen" ) func TestListApp(t *testing.T) { @@ -29,26 +30,23 @@ func TestListApp(t *testing.T) { 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 - + var appsResp codegen.AppsPage httpDo(t, url, method, nil, http.StatusOK, "json", &appsResp) assert.NotEqual(t, len(appsDB), 0) - assert.Equal(t, len(appsDB), len(appsResp)) - + assert.Equal(t, len(appsDB), len(appsResp.Applications)) for i := range appsDB { - assert.Equal(t, appsResp[i].ID, appsDB[i].ID) - assert.Equal(t, appsResp[i].Name, appsDB[i].Name) + assert.Equal(t, appsDB[i].ID, appsResp.Applications[i].Id) + assert.Equal(t, appsDB[i].Name, appsResp.Applications[i].Name) } }) } func TestCreateApp(t *testing.T) { - // establish DB connection - db := newDBForTest(t) - t.Run("success_do_not_copy", func(t *testing.T) { + // establish DB connection + db := newDBForTest(t) + // Create App request url := fmt.Sprintf("%s%s", testServerURL, "/api/apps") method := "POST" @@ -71,6 +69,9 @@ func TestCreateApp(t *testing.T) { }) t.Run("success_with_copy", func(t *testing.T) { + // establish DB connection + db := newDBForTest(t) + app := getRandomApp(t, db) // Create App request @@ -85,8 +86,14 @@ func TestCreateApp(t *testing.T) { httpDo(t, url, method, payload, http.StatusOK, "json", &application) + // close and create new db session to clear and update cached appIds + db.Close() + db, err := api.New() + require.NoError(t, err) + require.NotNil(t, db) + // check if app exists in DB - app, err := db.GetApp(application.ID) + app, err = db.GetApp(application.ID) require.NoError(t, err) assert.Equal(t, application.ID, app.ID) diff --git a/backend/test/api/channel_test.go b/backend/test/api/channel_test.go index 3a77d7281..0a0c6fb07 100644 --- a/backend/test/api/channel_test.go +++ b/backend/test/api/channel_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/require" "github.com/kinvolk/nebraska/backend/pkg/api" + "github.com/kinvolk/nebraska/backend/pkg/codegen" ) func TestListChannels(t *testing.T) { @@ -32,15 +33,15 @@ func TestListChannels(t *testing.T) { url := fmt.Sprintf("%s/api/apps/%s/channels", testServerURL, app.ID) method := "GET" - // response - // TODO: will require change as response struct is changed in POC2 branch - var channels []*api.Channel + var channelsResp codegen.ChannelPage - httpDo(t, url, method, nil, http.StatusOK, "json", &channels) + httpDo(t, url, method, nil, http.StatusOK, "json", &channelsResp) - assert.NotEqual(t, 0, len(channels)) - assert.Equal(t, len(channelsDB), len(channels)) - assert.Equal(t, channelsDB[0].ApplicationID, channels[0].ApplicationID) + for i := range channelsDB { + assert.NotEqual(t, 0, len(channelsResp.Channels)) + assert.Equal(t, len(channelsDB), len(channelsResp.Channels)) + assert.Equal(t, channelsDB[i].ApplicationID, channelsResp.Channels[i].ApplicationID) + } }) } diff --git a/backend/test/api/config_test.go b/backend/test/api/config_test.go index cee258c07..5787c7953 100644 --- a/backend/test/api/config_test.go +++ b/backend/test/api/config_test.go @@ -17,7 +17,7 @@ func TestConfig(t *testing.T) { httpDo(t, url, method, nil, http.StatusOK, "json", &rMap) - assert.Equal(t, "", rMap["auth_mode"]) + assert.Equal(t, "noop", rMap["auth_mode"]) assert.Equal(t, "", rMap["access_management_url"]) assert.Equal(t, "", rMap["login_url"]) assert.Equal(t, "", rMap["logout_url"]) diff --git a/backend/test/api/group_test.go b/backend/test/api/group_test.go index 1798e5a44..19f69ce20 100644 --- a/backend/test/api/group_test.go +++ b/backend/test/api/group_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/require" "github.com/kinvolk/nebraska/backend/pkg/api" + "github.com/kinvolk/nebraska/backend/pkg/codegen" ) func TestListGroups(t *testing.T) { @@ -32,14 +33,17 @@ func TestListGroups(t *testing.T) { url := fmt.Sprintf("%s/api/apps/%s/groups", testServerURL, app.ID) method := "GET" - // response - // TODO: will require change as response struct is changed in POC2 branch - var groups []*api.Group + var groupResp codegen.GroupPage + + httpDo(t, url, method, nil, http.StatusOK, "json", &groupResp) - httpDo(t, url, method, nil, http.StatusOK, "json", &groups) + assert.NotEqual(t, 0, len(groupResp.Groups)) + assert.Equal(t, len(groupsDB), len(groupResp.Groups)) - assert.NotEqual(t, 0, len(groups)) - assert.Equal(t, len(groupsDB), len(groups)) + for i := range groupsDB { + assert.Equal(t, groupsDB[i].ID, groupResp.Groups[i].Id) + assert.Equal(t, groupsDB[i].Name, groupResp.Groups[i].Name) + } }) } diff --git a/backend/test/api/helper_test.go b/backend/test/api/helper_test.go index 650293fa3..81e79b6a4 100644 --- a/backend/test/api/helper_test.go +++ b/backend/test/api/helper_test.go @@ -3,6 +3,7 @@ package api_test import ( "encoding/json" "encoding/xml" + "fmt" "io" "io/ioutil" "math/rand" @@ -83,6 +84,9 @@ func httpDo(t *testing.T, url string, method string, payload io.Reader, statusco require.NoError(t, err) require.NotNil(t, req) + req.Header = http.Header{ + "Content-Type": []string{fmt.Sprintf("application/%s", responseType)}, + } resp, err := http.DefaultClient.Do(req) require.NoError(t, err) require.NotNil(t, resp) diff --git a/backend/test/api/instance_test.go b/backend/test/api/instance_test.go index a7db02c33..aa3a73d10 100644 --- a/backend/test/api/instance_test.go +++ b/backend/test/api/instance_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/require" "github.com/kinvolk/nebraska/backend/pkg/api" + "github.com/kinvolk/nebraska/backend/pkg/codegen" ) func TestListInstances(t *testing.T) { @@ -63,10 +64,9 @@ func TestGetInstanceCount(t *testing.T) { url := fmt.Sprintf("%s/api/apps/%s/groups/%s/instancescount?duration=30d", testServerURL, appWithInstance.ID, appWithInstance.Groups[0].ID) method := "GET" - // TODO: will require change as response struct is changed in POC2 branch - var instancesCount int + var instancesCountResp codegen.InstanceCount - httpDo(t, url, method, nil, http.StatusOK, "json", &instancesCount) + httpDo(t, url, method, nil, http.StatusOK, "json", &instancesCountResp) count, err := db.GetInstancesCount(api.InstancesQueryParams{ ApplicationID: appWithInstance.ID, @@ -74,7 +74,7 @@ func TestGetInstanceCount(t *testing.T) { }, "30d") require.NoError(t, err) - assert.Equal(t, int(count), instancesCount) + assert.Equal(t, uint64(count), instancesCountResp.Count) }) } diff --git a/backend/test/api/omaha_test.go b/backend/test/api/omaha_test.go index 5438df18e..9bc0b64e1 100644 --- a/backend/test/api/omaha_test.go +++ b/backend/test/api/omaha_test.go @@ -22,7 +22,7 @@ func TestOmaha(t *testing.T) { t.Run("success", func(t *testing.T) { track := app.Groups[0].Track - url := fmt.Sprintf("%s/omaha", testServerURL) + url := fmt.Sprintf("%s/v1/update", testServerURL) method := "POST" @@ -52,7 +52,7 @@ func TestOmaha(t *testing.T) { }) t.Run("large_request_body", func(t *testing.T) { - url := fmt.Sprintf("%s/omaha", testServerURL) + url := fmt.Sprintf("%s/v1/update", testServerURL) method := "POST" diff --git a/backend/test/api/package_test.go b/backend/test/api/package_test.go index e3ad4ece1..722b892d5 100644 --- a/backend/test/api/package_test.go +++ b/backend/test/api/package_test.go @@ -8,11 +8,14 @@ import ( "strings" "testing" + "github.com/google/uuid" "github.com/jinzhu/copier" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v4" "github.com/kinvolk/nebraska/backend/pkg/api" + "github.com/kinvolk/nebraska/backend/pkg/codegen" ) func TestListPackages(t *testing.T) { @@ -34,12 +37,16 @@ func TestListPackages(t *testing.T) { // response // TODO: will require change as response struct is changed in POC2 branch - var packages []*api.Package + var packagesResp codegen.PackagePage - httpDo(t, url, method, nil, http.StatusOK, "json", &packages) + httpDo(t, url, method, nil, http.StatusOK, "json", &packagesResp) - assert.NotEqual(t, 0, len(packages)) - assert.Equal(t, len(packagesDB), len(packages)) + assert.NotEqual(t, 0, len(packagesResp.Packages)) + assert.Equal(t, len(packagesDB), len(packagesResp.Packages)) + for i := range packagesDB { + assert.Equal(t, packagesDB[i].ApplicationID, packagesResp.Packages[i].ApplicationID) + assert.Equal(t, packagesDB[i].ID, packagesResp.Packages[i].Id) + } }) } @@ -118,6 +125,19 @@ func TestUpdatePackage(t *testing.T) { err = copier.Copy(&packageDB, packagesDB[0]) require.NoError(t, err) + if packageDB.ChannelsBlacklist == nil { + packageDB.ChannelsBlacklist = []string{} + } + if packageDB.Description.IsZero() { + packageDB.Description = null.StringFrom("some desc") + } + if packageDB.Size.IsZero() { + packageDB.Size = null.StringFrom("20") + } + if packageDB.Hash.IsZero() { + packageDB.Hash = null.StringFrom(uuid.New().String()) + } + packageVersion := "20.2.2" packageDB.Version = packageVersion