mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package testing
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fnproject/fn/api/id"
|
|
"github.com/fnproject/fn/api/models"
|
|
"github.com/go-openapi/strfmt"
|
|
)
|
|
|
|
var testApp = &models.App{
|
|
Name: "Test",
|
|
}
|
|
|
|
var testRoute = &models.Route{
|
|
AppName: testApp.Name,
|
|
Path: "/test",
|
|
Image: "fnproject/hello",
|
|
Type: "sync",
|
|
Format: "http",
|
|
}
|
|
|
|
func SetupTestCall() *models.Call {
|
|
var call models.Call
|
|
call.CreatedAt = strfmt.DateTime(time.Now())
|
|
call.Status = "success"
|
|
call.StartedAt = strfmt.DateTime(time.Now())
|
|
call.CompletedAt = strfmt.DateTime(time.Now())
|
|
call.AppName = testApp.Name
|
|
call.Path = testRoute.Path
|
|
return &call
|
|
}
|
|
|
|
func Test(t *testing.T, fnl models.LogStore, ds models.Datastore) {
|
|
ctx := context.Background()
|
|
call := SetupTestCall()
|
|
|
|
t.Run("call-log-insert", func(t *testing.T) {
|
|
call.ID = id.New().String()
|
|
err := ds.InsertCall(ctx, call)
|
|
if err != nil {
|
|
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
|
|
}
|
|
err = fnl.InsertLog(ctx, call.AppName, call.ID, "test")
|
|
if err != nil {
|
|
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
|
|
}
|
|
})
|
|
t.Run("call-log-insert-get", func(t *testing.T) {
|
|
call.ID = id.New().String()
|
|
err := ds.InsertCall(ctx, call)
|
|
logText := "test"
|
|
if err != nil {
|
|
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
|
|
}
|
|
err = fnl.InsertLog(ctx, call.AppName, call.ID, logText)
|
|
if err != nil {
|
|
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
|
|
}
|
|
logEntry, err := fnl.GetLog(ctx, call.AppName, call.ID)
|
|
if !strings.Contains(logEntry.Log, logText) {
|
|
t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+
|
|
"Expected: `%v`. Got `%v`.", logText, logEntry.Log)
|
|
}
|
|
})
|
|
t.Run("call-log-insert-get-delete", func(t *testing.T) {
|
|
call.ID = id.New().String()
|
|
err := ds.InsertCall(ctx, call)
|
|
logText := "test"
|
|
if err != nil {
|
|
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
|
|
}
|
|
err = fnl.InsertLog(ctx, call.AppName, call.ID, logText)
|
|
if err != nil {
|
|
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
|
|
}
|
|
logEntry, err := fnl.GetLog(ctx, call.AppName, call.ID)
|
|
if !strings.Contains(logEntry.Log, logText) {
|
|
t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+
|
|
"Expected: `%v`. Got `%v`.", logText, logEntry.Log)
|
|
}
|
|
err = fnl.DeleteLog(ctx, call.AppName, call.ID)
|
|
if err != nil {
|
|
t.Fatalf("Test DeleteLog(ctx, call.ID): unexpected error during deleting log `%v`", err)
|
|
}
|
|
})
|
|
}
|