Merge branch 'move-api-tests' into 'master'

Moving tests from CLI to server

See merge request !118
This commit is contained in:
James Jeffrey
2017-07-21 12:48:03 -07:00
17 changed files with 103 additions and 30 deletions

View File

@@ -35,7 +35,7 @@ esac
case ${DOCKER_LOCATION:-localhost} in
localhost)
cd fn/tests && API_URL="http://localhost:8080" go test -v ./...; cd ../../
cd test/fn-api-tests && API_URL="http://localhost:8080" go test -v ./...; cd ../../
;;
docker_ip)
if [[ ! -z ${DOCKER_HOST} ]]
@@ -43,9 +43,9 @@ docker_ip)
DOCKER_IP=`echo ${DOCKER_HOST} | awk -F/ '{print $3}'| awk -F: '{print $1}'`
fi
cd fn/tests && API_URL="http://${DOCKER_IP:-localhost}:8080" go test -v ./...; cd ../../
cd test/fn-api-tests && API_URL="http://${DOCKER_IP:-localhost}:8080" go test -v ./...; cd ../../
;;
container_ip)
cd fn/tests && API_URL="http://"$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server)":8080" go test -v ./...; cd ../../
cd test/fn-api-tests && API_URL="http://"$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server)":8080" go test -v ./...; cd ../../
;;
esac

View File

@@ -8,7 +8,6 @@ import (
"time"
"github.com/funcy/functions_go/client/call"
"gitlab-odx.oracle.com/odx/functions/fn/client"
)
func TestCalls(t *testing.T) {
@@ -30,7 +29,7 @@ func TestCalls(t *testing.T) {
u := url.URL{
Scheme: "http",
Host: client.Host(),
Host: Host(),
}
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)

View File

@@ -12,7 +12,6 @@ import (
"github.com/funcy/functions_go/client/call"
"github.com/funcy/functions_go/client/operations"
"gitlab-odx.oracle.com/odx/functions/fn/client"
)
type ErrMsg struct {
@@ -26,7 +25,7 @@ type TimeoutBody struct {
func CallAsync(t *testing.T, u url.URL, content io.Reader) string {
output := &bytes.Buffer{}
err := client.CallFN(u.String(), content, output, "POST", []string{})
err := CallFN(u.String(), content, output, "POST", []string{})
if err != nil {
t.Fatalf("Got unexpected error: %v", err)
}
@@ -60,14 +59,14 @@ func TestRouteExecutions(t *testing.T) {
u := url.URL{
Scheme: "http",
Host: client.Host(),
Host: Host(),
}
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)
t.Run("run-sync-funcy/hello-no-input", func(t *testing.T) {
content := &bytes.Buffer{}
output := &bytes.Buffer{}
err := client.CallFN(u.String(), content, output, "POST", []string{})
err := CallFN(u.String(), content, output, "POST", []string{})
if err != nil {
t.Fatalf("Got unexpected error: %v", err)
}
@@ -84,7 +83,7 @@ func TestRouteExecutions(t *testing.T) {
Name string
}{Name: "John"})
output := &bytes.Buffer{}
err := client.CallFN(u.String(), content, output, "POST", []string{})
err := CallFN(u.String(), content, output, "POST", []string{})
if err != nil {
t.Fatalf("Got unexpected error: %v", err)
}
@@ -153,7 +152,7 @@ func TestRouteExecutions(t *testing.T) {
u := url.URL{
Scheme: "http",
Host: client.Host(),
Host: Host(),
}
u.Path = path.Join(u.Path, "r", s.AppName, routePath)
@@ -163,7 +162,7 @@ func TestRouteExecutions(t *testing.T) {
}{Seconds: 31})
output := &bytes.Buffer{}
client.CallFN(u.String(), content, output, "POST", []string{})
CallFN(u.String(), content, output, "POST", []string{})
if !strings.Contains(output.String(), "Timed out") {
t.Fatalf("Must fail because of timeout, but got error message: %v", output.String())
@@ -199,7 +198,7 @@ func TestRouteExecutions(t *testing.T) {
t.Run("exec-multi-log-test", func(t *testing.T) {
u := url.URL{
Scheme: "http",
Host: client.Host(),
Host: Host(),
}
u.Path = path.Join(u.Path, "r", s.AppName, routePath)
@@ -239,7 +238,7 @@ func TestRouteExecutions(t *testing.T) {
t.Run("exec-log-test", func(t *testing.T) {
u := url.URL{
Scheme: "http",
Host: client.Host(),
Host: Host(),
}
u.Path = path.Join(u.Path, "r", s.AppName, routePath)
content := &bytes.Buffer{}
@@ -268,7 +267,7 @@ func TestRouteExecutions(t *testing.T) {
size := 1 * 1024 * 1024 * 1024
u := url.URL{
Scheme: "http",
Host: client.Host(),
Host: Host(),
}
u.Path = path.Join(u.Path, "r", s.AppName, routePath)
content := &bytes.Buffer{}

View File

@@ -6,16 +6,48 @@ import (
"testing"
"time"
fn "github.com/funcy/functions_go/client"
"fmt"
"github.com/funcy/functions_go/client"
"github.com/funcy/functions_go/client/apps"
"github.com/funcy/functions_go/client/routes"
"github.com/funcy/functions_go/models"
"gitlab-odx.oracle.com/odx/functions/fn/client"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"io"
"log"
"net/http"
"net/url"
"os"
)
func Host() string {
apiURL := os.Getenv("API_URL")
if apiURL == "" {
apiURL = "http://localhost:8080"
}
u, err := url.Parse(apiURL)
if err != nil {
log.Fatalln("Couldn't parse API URL:", err)
}
return u.Host
}
func APIClient() *client.Functions {
transport := httptransport.New(Host(), "/v1", []string{"http"})
if os.Getenv("FN_TOKEN") != "" {
transport.DefaultAuthentication = httptransport.BearerToken(os.Getenv("FN_TOKEN"))
}
// create the API client, with the transport
client := client.New(transport, strfmt.Default)
return client
}
type SuiteSetup struct {
Context context.Context
Client *fn.Functions
Client *client.Functions
AppName string
RoutePath string
Image string
@@ -29,7 +61,7 @@ type SuiteSetup struct {
func SetupDefaultSuite() *SuiteSetup {
return &SuiteSetup{
Context: context.Background(),
Client: client.APIClient(),
Client: APIClient(),
AppName: "test-app",
RoutePath: "/hello",
Image: "funcy/hello",
@@ -91,7 +123,7 @@ func CheckAppResponseError(t *testing.T, err error) {
}
func CreateAppNoAssert(ctx context.Context, fnclient *fn.Functions, appName string, config map[string]string) (*apps.PostAppsOK, error) {
func CreateAppNoAssert(ctx context.Context, fnclient *client.Functions, appName string, config map[string]string) (*apps.PostAppsOK, error) {
cfg := &apps.PostAppsParams{
Body: &models.AppWrapper{
App: &models.App{
@@ -105,7 +137,7 @@ func CreateAppNoAssert(ctx context.Context, fnclient *fn.Functions, appName stri
return fnclient.Apps.PostApps(cfg)
}
func CreateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName string, config map[string]string) {
func CreateApp(t *testing.T, ctx context.Context, fnclient *client.Functions, appName string, config map[string]string) {
appPayload, err := CreateAppNoAssert(ctx, fnclient, appName, config)
CheckAppResponseError(t, err)
if !strings.Contains(appName, appPayload.Payload.App.Name) {
@@ -114,7 +146,7 @@ func CreateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appNam
}
}
func UpdateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName string, config map[string]string) *apps.PatchAppsAppOK {
func UpdateApp(t *testing.T, ctx context.Context, fnclient *client.Functions, appName string, config map[string]string) *apps.PatchAppsAppOK {
CreateApp(t, ctx, fnclient, appName, map[string]string{"A": "a"})
cfg := &apps.PatchAppsAppParams{
App: appName,
@@ -131,7 +163,7 @@ func UpdateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appNam
return appPayload
}
func DeleteApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName string) {
func DeleteApp(t *testing.T, ctx context.Context, fnclient *client.Functions, appName string) {
cfg := &apps.DeleteAppsAppParams{
App: appName,
Context: ctx,
@@ -248,7 +280,7 @@ func assertRouteFields(t *testing.T, routeObject *models.Route, path, image, rou
}
func createRoute(ctx context.Context, fnclient *fn.Functions, appName, image, routePath, routeType string, routeConfig map[string]string, headers map[string][]string) (*routes.PostAppsAppRoutesOK, error) {
func createRoute(ctx context.Context, fnclient *client.Functions, appName, image, routePath, routeType string, routeConfig map[string]string, headers map[string][]string) (*routes.PostAppsAppRoutesOK, error) {
cfg := &routes.PostAppsAppRoutesParams{
App: appName,
Body: &models.RouteWrapper{
@@ -267,14 +299,14 @@ func createRoute(ctx context.Context, fnclient *fn.Functions, appName, image, ro
}
func CreateRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath, image, routeType string, routeConfig map[string]string, headers map[string][]string) {
func CreateRoute(t *testing.T, ctx context.Context, fnclient *client.Functions, appName, routePath, image, routeType string, routeConfig map[string]string, headers map[string][]string) {
routeResponse, err := createRoute(ctx, fnclient, appName, image, routePath, routeType, routeConfig, headers)
CheckRouteResponseError(t, err)
assertRouteFields(t, routeResponse.Payload.Route, routePath, image, routeType)
}
func deleteRoute(ctx context.Context, fnclient *fn.Functions, appName, routePath string) (*routes.DeleteAppsAppRoutesRouteOK, error) {
func deleteRoute(ctx context.Context, fnclient *client.Functions, appName, routePath string) (*routes.DeleteAppsAppRoutesRouteOK, error) {
cfg := &routes.DeleteAppsAppRoutesRouteParams{
App: appName,
Route: routePath,
@@ -284,12 +316,12 @@ func deleteRoute(ctx context.Context, fnclient *fn.Functions, appName, routePath
return fnclient.Routes.DeleteAppsAppRoutesRoute(cfg)
}
func DeleteRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath string) {
func DeleteRoute(t *testing.T, ctx context.Context, fnclient *client.Functions, appName, routePath string) {
_, err := deleteRoute(ctx, fnclient, appName, routePath)
CheckRouteResponseError(t, err)
}
func ListRoutes(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName string) []*models.Route {
func ListRoutes(t *testing.T, ctx context.Context, fnclient *client.Functions, appName string) []*models.Route {
cfg := &routes.GetAppsAppRoutesParams{
App: appName,
Context: ctx,
@@ -300,7 +332,7 @@ func ListRoutes(t *testing.T, ctx context.Context, fnclient *fn.Functions, appNa
return routesResponse.Payload.Routes
}
func GetRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath string) *models.Route {
func GetRoute(t *testing.T, ctx context.Context, fnclient *client.Functions, appName, routePath string) *models.Route {
cfg := &routes.GetAppsAppRoutesRouteParams{
App: appName,
Route: routePath,
@@ -312,7 +344,7 @@ func GetRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName
return routeResponse.Payload.Route
}
func UpdateRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath, image, routeType, format string, memory int64, routeConfig map[string]string, headers map[string][]string, newRoutePath string) (*routes.PatchAppsAppRoutesRouteOK, error) {
func UpdateRoute(t *testing.T, ctx context.Context, fnclient *client.Functions, appName, routePath, image, routeType, format string, memory int64, routeConfig map[string]string, headers map[string][]string, newRoutePath string) (*routes.PatchAppsAppRoutesRouteOK, error) {
routeObject := GetRoute(t, ctx, fnclient, appName, routePath)
if routeObject.Config == nil {
@@ -383,3 +415,46 @@ func assertContainsRoute(routeModels []*models.Route, expectedRoute string) bool
}
return false
}
func EnvAsHeader(req *http.Request, selectedEnv []string) {
detectedEnv := os.Environ()
if len(selectedEnv) > 0 {
detectedEnv = selectedEnv
}
for _, e := range detectedEnv {
kv := strings.Split(e, "=")
name := kv[0]
req.Header.Set(name, os.Getenv(name))
}
}
func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error {
if method == "" {
if content == nil {
method = "GET"
} else {
method = "POST"
}
}
req, err := http.NewRequest(method, u, content)
if err != nil {
return fmt.Errorf("error running route: %s", err)
}
req.Header.Set("Content-Type", "application/json")
if len(env) > 0 {
EnvAsHeader(req, env)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("error running route: %s", err)
}
io.Copy(output, resp.Body)
return nil
}