Bye bye openapi (#1081)

* add DateTime sans mgo

* change all uses of strfmt.DateTime to common.DateTime, remove test strfmt usage

* remove api tests, system-test dep on api test

multiple reasons to remove the api tests:

* awkward dependency with fn_go meant generating bindings on a branched fn to
vendor those to test new stuff. this is at a minimum not at all intuitive,
worth it, nor a fun way to spend the finite amount of time we have to live.
* api tests only tested a subset of functionality that the server/ api tests
already test, and we risk having tests where one tests some thing and the
other doesn't. let's not. we have too many test suites as it is, and these
pretty much only test that we updated the fn_go bindings, which is actually a
hassle as noted above and the cli will pretty quickly figure out anyway.
* fn_go relies on openapi, which relies on mgo, which is deprecated and we'd
like to remove as a dependency. openapi is a _huge_ dep built in a NIH
fashion, that cannot simply remove the mgo dep as users may be using it.
we've now stolen their date time and otherwise killed usage of it in fn core,
for fn_go it still exists but that's less of a problem.

* update deps

removals:

* easyjson
* mgo
* go-openapi
* mapstructure
* fn_go
* purell
* go-validator

also, had to lock docker. we shouldn't use docker on master anyway, they
strongly advise against that. had no luck with latest version rev, so i locked
it to what we were using before. until next time.

the rest is just playing dep roulette, those end up removing a ton tho

* fix exec test to work

* account for john le cache
This commit is contained in:
Reed Allman
2018-06-21 11:09:16 -07:00
committed by GitHub
parent aa5d7169f4
commit 51ff7caeb2
2635 changed files with 440440 additions and 402994 deletions

View File

@@ -52,7 +52,7 @@ func myHandler(ctx context.Context, in io.Reader, out io.Writer) {
return
}
if fnctx.Config["FN_METHOD"] != "PUT" {
if fnctx.Method != "PUT" {
fdk.WriteStatus(out, 404)
fdk.SetHeader(out, "Content-Type", "application/json")
io.WriteString(out, `{"error":"route not found"}`)

View File

@@ -24,23 +24,29 @@ func (f HandlerFunc) Serve(ctx context.Context, in io.Reader, out io.Writer) {
func Context(ctx context.Context) *Ctx {
utilsCtx := utils.Context(ctx)
return &Ctx{
Header: utilsCtx.Header,
Config: utilsCtx.Config,
Header: utilsCtx.Header,
Config: utilsCtx.Config,
RequestURL: utilsCtx.RequestURL,
Method: utilsCtx.Method,
}
}
func WithContext(ctx context.Context, fnctx *Ctx) context.Context {
utilsCtx := &utils.Ctx{
Header: fnctx.Header,
Config: fnctx.Config,
Header: fnctx.Header,
Config: fnctx.Config,
RequestURL: fnctx.RequestURL,
Method: fnctx.Method,
}
return utils.WithContext(ctx, utilsCtx)
}
// Ctx provides access to Config and Headers from fn.
type Ctx struct {
Header http.Header
Config map[string]string
Header http.Header
Config map[string]string
RequestURL string
Method string
}
// AddHeader will add a header on the function response, for hot function

View File

@@ -15,6 +15,7 @@ import (
"testing"
"github.com/fnproject/fdk-go/utils"
"time"
)
func echoHTTPHandler(_ context.Context, in io.Reader, out io.Writer) {
@@ -55,12 +56,12 @@ func JSONHandler(_ context.Context, in io.Reader, out io.Writer) {
Name string `json:"name"`
}
json.NewDecoder(in).Decode(&person)
if person.Name == "" {
person.Name = "world"
}
body := fmt.Sprintf("Hello %s!\n", person.Name)
SetHeader(out, "Content-Type", "application/json")
err := json.NewEncoder(out).Encode(body)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
@@ -80,8 +81,9 @@ func TestJSON(t *testing.T) {
Deadline: "2018-01-30T16:52:39.786Z",
Protocol: utils.CallRequestHTTP{
Type: "http",
RequestURL: "someURL",
RequestURL: "http://localhost:8080/r/myapp/yodawg",
Headers: http.Header{},
Method: "POST",
},
}
@@ -151,8 +153,9 @@ func TestJSONOverwriteStatusCodeAndHeaders(t *testing.T) {
Deadline: "2018-01-30T16:52:39.786Z",
Protocol: utils.CallRequestHTTP{
Type: "json",
RequestURL: "someURL",
RequestURL: "http://localhost:8080/r/myapp/yodawg",
Headers: http.Header{},
Method: "POST",
},
}
@@ -242,3 +245,78 @@ func HTTPreq(t *testing.T, bod string) io.Reader {
}
return bytes.NewReader(byts)
}
func setupTestFromRequest(t *testing.T, data interface{}, contentType, nameTest string) {
req := &utils.CloudEventIn{
CloudEvent: utils.CloudEvent{
EventID: "someid",
Source: "fn-api",
EventType: "test-type",
EventTypeVersion: "1.0",
EventTime: time.Now(),
ContentType: contentType,
Data: data,
},
Extensions: utils.CloudEventInExtension{
Deadline: "2018-01-30T16:52:39.786Z",
Protocol: utils.CallRequestHTTP{
Type: "http",
RequestURL: "http://localhost:8080/r/myapp/yodawg",
Headers: http.Header{},
Method: "POST",
},
},
}
var in bytes.Buffer
err := json.NewEncoder(&in).Encode(req)
if err != nil {
t.Fatal("Unable to marshal request")
}
t.Log(in.String())
var out, buf bytes.Buffer
err = utils.DoCloudEventOnce(HandlerFunc(JSONHandler), utils.BuildCtx(),
&in, &out, &buf, make(http.Header))
if err != nil {
t.Fatal("should not return error", err)
}
t.Log(out.String())
ceOut := &utils.CloudEventOut{}
err = json.NewDecoder(&out).Decode(ceOut)
if err != nil {
t.Fatal(err.Error())
}
if ceOut.Extensions.Protocol.StatusCode != 200 {
t.Fatalf("Response code must equal to 200, but have: %v", ceOut.Extensions.Protocol.StatusCode)
}
var respData string
json.Unmarshal([]byte(ceOut.Data.(string)), &respData)
if respData != "Hello "+nameTest+"!\n" {
t.Fatalf("Output assertion mismatch. Expected: `Hello %v!\n`. Actual: %v", nameTest, ceOut.Data)
}
}
func TestCloudEventWithJSONData(t *testing.T) {
data := map[string]string{
"name": "John",
}
contentType := "application/json"
setupTestFromRequest(t, data, contentType, "John")
}
func TestCloudEventWithStringData(t *testing.T) {
data := `{"name":"John"}`
contentType := "text/plain"
setupTestFromRequest(t, data, contentType, "John")
}
func TestCloudEventWithPerfectlyValidJSONValue(t *testing.T) {
// https://tools.ietf.org/html/rfc7159#section-3
data := false
contentType := "application/json"
setupTestFromRequest(t, data, contentType, "world")
}

117
vendor/github.com/fnproject/fdk-go/utils/cloudevent.go generated vendored Normal file
View File

@@ -0,0 +1,117 @@
package utils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
type CloudEvent struct {
CloudEventsVersion string `json:"cloudEventsVersion"`
EventID string `json:"eventID"`
Source string `json:"source"`
EventType string `json:"eventType"`
EventTypeVersion string `json:"eventTypeVersion"`
EventTime time.Time `json:"eventTime"`
SchemaURL string `json:"schemaURL"`
ContentType string `json:"contentType"`
Data interface{} `json:"data"` // from docs: the payload is encoded into a media format which is specified by the contentType attribute (e.g. application/json)
}
type CloudEventInExtension struct {
Protocol CallRequestHTTP `json:"protocol"`
Deadline string `json:"deadline"`
}
type CloudEventOutExtension struct {
Protocol CallResponseHTTP `json:"protocol"`
}
type CloudEventIn struct {
CloudEvent
Extensions CloudEventInExtension `json:"extensions"`
}
type CloudEventOut struct {
CloudEvent
Extensions CloudEventOutExtension `json:"extensions"`
}
func writeError(ceOut *CloudEventOut, err error) {
ceOut.Extensions.Protocol.StatusCode = http.StatusInternalServerError
ceOut.Data = fmt.Sprintf(`{"error": %v}`, err.Error())
ceOut.ContentType = "application/json"
ceOut.EventTime = time.Now()
}
func DoCloudEventOnce(handler Handler, ctx context.Context, in io.Reader, out io.Writer, buf *bytes.Buffer, hdr http.Header) error {
buf.Reset()
ResetHeaders(hdr)
resp := Response{
Writer: buf,
Status: 200,
Header: hdr,
}
ceOut := CloudEventOut{
Extensions: CloudEventOutExtension{
Protocol: CallResponseHTTP{
StatusCode: http.StatusOK,
Headers: hdr,
},
},
CloudEvent: CloudEvent{
ContentType: "text/plain",
},
}
var ceIn CloudEventIn
err := json.NewDecoder(in).Decode(&ceIn)
if err != nil {
if err == io.EOF {
return err
}
writeError(&ceOut, err)
} else {
SetHeaders(ctx, ceIn.Extensions.Protocol.Headers)
SetRequestURL(ctx, ceIn.Extensions.Protocol.RequestURL)
SetMethod(ctx, ceIn.Extensions.Protocol.Method)
ctx, cancel := CtxWithDeadline(ctx, ceIn.Extensions.Deadline)
defer cancel()
if ceIn.ContentType == "application/json" {
// TODO this is lame, need to make FDK cloud event native and not io.Reader
err = json.NewEncoder(buf).Encode(ceIn.Data)
in := strings.NewReader(buf.String()) // string is immutable, we need a copy
buf.Reset()
handler.Serve(ctx, in, &resp)
} else {
handler.Serve(ctx, strings.NewReader(ceIn.Data.(string)), &resp)
}
}
ceOut.EventID = ceIn.EventID
ceOut.EventTime = time.Now()
ceOut.ContentType = ceOut.Extensions.Protocol.Headers.Get("Content-Type")
ceOut.Data = buf.String()
return json.NewEncoder(out).Encode(ceOut)
}
func DoCloudEvent(handler Handler, ctx context.Context, in io.Reader, out io.Writer) {
var buf bytes.Buffer
hdr := make(http.Header)
for {
err := DoCloudEventOnce(handler, ctx, in, out, &buf, hdr)
if err != nil {
log.Println(err.Error())
break
}
}
}

62
vendor/github.com/fnproject/fdk-go/utils/http.go generated vendored Normal file
View File

@@ -0,0 +1,62 @@
package utils
import (
"bufio"
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"strconv"
)
func GetHTTPResp(buf *bytes.Buffer, fnResp *Response, req *http.Request) http.Response {
fnResp.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
hResp := http.Response{
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: fnResp.Status,
Request: req,
Body: ioutil.NopCloser(buf),
ContentLength: int64(buf.Len()),
Header: fnResp.Header,
}
return hResp
}
func DoHTTPOnce(handler Handler, ctx context.Context, in io.Reader, out io.Writer, buf *bytes.Buffer, hdr http.Header) error {
buf.Reset()
ResetHeaders(hdr)
resp := Response{
Writer: buf,
Status: 200,
Header: hdr,
}
req, err := http.ReadRequest(bufio.NewReader(in))
if err != nil {
// stdin now closed
if err == io.EOF {
return err
}
// TODO it would be nice if we could let the user format this response to their preferred style..
resp.Status = http.StatusInternalServerError
io.WriteString(resp, err.Error())
} else {
fnDeadline := Context(ctx).Header.Get("FN_DEADLINE")
ctx, cancel := CtxWithDeadline(ctx, fnDeadline)
defer cancel()
SetHeaders(ctx, req.Header)
SetRequestURL(ctx, req.URL.String())
SetMethod(ctx, req.Method)
handler.Serve(ctx, req.Body, &resp)
}
hResp := GetHTTPResp(buf, &resp, req)
hResp.Write(out)
return nil
}

96
vendor/github.com/fnproject/fdk-go/utils/json.go generated vendored Normal file
View File

@@ -0,0 +1,96 @@
package utils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func DoJSON(handler Handler, ctx context.Context, in io.Reader, out io.Writer) {
var buf bytes.Buffer
hdr := make(http.Header)
for {
err := DoJSONOnce(handler, ctx, in, out, &buf, hdr)
if err != nil {
break
}
}
}
type CallRequestHTTP struct {
Type string `json:"type"`
RequestURL string `json:"request_url"`
Method string `json:"method"`
Headers http.Header `json:"headers"`
}
type JsonIn struct {
CallID string `json:"call_id"`
Deadline string `json:"deadline"`
Body string `json:"body"`
ContentType string `json:"content_type"`
Protocol CallRequestHTTP `json:"protocol"`
}
type CallResponseHTTP struct {
StatusCode int `json:"status_code,omitempty"`
Headers http.Header `json:"headers,omitempty"`
}
type JsonOut struct {
Body string `json:"body"`
ContentType string `json:"content_type"`
Protocol CallResponseHTTP `json:"protocol,omitempty"`
}
func GetJSONResp(buf *bytes.Buffer, fnResp *Response) *JsonOut {
hResp := &JsonOut{
Body: buf.String(),
ContentType: "",
Protocol: CallResponseHTTP{
StatusCode: fnResp.Status,
Headers: fnResp.Header,
},
}
return hResp
}
func DoJSONOnce(handler Handler, ctx context.Context, in io.Reader, out io.Writer, buf *bytes.Buffer, hdr http.Header) error {
buf.Reset()
ResetHeaders(hdr)
resp := Response{
Writer: buf,
Status: 200,
Header: hdr,
}
var jsonRequest JsonIn
err := json.NewDecoder(in).Decode(&jsonRequest)
if err != nil {
// stdin now closed
if err == io.EOF {
return err
}
resp.Status = http.StatusInternalServerError
io.WriteString(resp, fmt.Sprintf(`{"error": %v}`, err.Error()))
} else {
SetHeaders(ctx, jsonRequest.Protocol.Headers)
SetRequestURL(ctx, jsonRequest.Protocol.RequestURL)
SetMethod(ctx, jsonRequest.Protocol.Method)
ctx, cancel := CtxWithDeadline(ctx, jsonRequest.Deadline)
defer cancel()
handler.Serve(ctx, strings.NewReader(jsonRequest.Body), &resp)
}
jsonResponse := GetJSONResp(buf, &resp)
json.NewEncoder(out).Encode(jsonResponse)
return nil
}

View File

@@ -1,16 +1,11 @@
package utils
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
@@ -31,8 +26,10 @@ func WithContext(ctx context.Context, fnctx *Ctx) context.Context {
// Ctx provides access to Config and Headers from fn.
type Ctx struct {
Header http.Header
Config map[string]string
Header http.Header
Config map[string]string
RequestURL string
Method string
}
type key struct{}
@@ -46,6 +43,8 @@ func Do(handler Handler, format string, in io.Reader, out io.Writer) {
DoHTTP(handler, ctx, in, out)
case "json":
DoJSON(handler, ctx, in, out)
case "cloudevent":
DoCloudEvent(handler, ctx, in, out)
case "default":
DoDefault(handler, ctx, in, out)
default:
@@ -81,88 +80,6 @@ func DoHTTP(handler Handler, ctx context.Context, in io.Reader, out io.Writer) {
}
}
func DoJSON(handler Handler, ctx context.Context, in io.Reader, out io.Writer) {
var buf bytes.Buffer
hdr := make(http.Header)
for {
err := DoJSONOnce(handler, ctx, in, out, &buf, hdr)
if err != nil {
break
}
}
}
type CallRequestHTTP struct {
Type string `json:"type"`
RequestURL string `json:"request_url"`
Method string `json:"method"`
Headers http.Header `json:"headers"`
}
type JsonIn struct {
CallID string `json:"call_id"`
Deadline string `json:"deadline"`
Body string `json:"body"`
ContentType string `json:"content_type"`
Protocol CallRequestHTTP `json:"protocol"`
}
type CallResponseHTTP struct {
StatusCode int `json:"status_code,omitempty"`
Headers http.Header `json:"headers,omitempty"`
}
type JsonOut struct {
Body string `json:"body"`
ContentType string `json:"content_type"`
Protocol CallResponseHTTP `json:"protocol,omitempty"`
}
func GetJSONResp(buf *bytes.Buffer, fnResp *Response, req *JsonIn) *JsonOut {
hResp := &JsonOut{
Body: buf.String(),
ContentType: "",
Protocol: CallResponseHTTP{
StatusCode: fnResp.Status,
Headers: fnResp.Header,
},
}
return hResp
}
func DoJSONOnce(handler Handler, ctx context.Context, in io.Reader, out io.Writer, buf *bytes.Buffer, hdr http.Header) error {
buf.Reset()
ResetHeaders(hdr)
resp := Response{
Writer: buf,
Status: 200,
Header: hdr,
}
var jsonRequest JsonIn
err := json.NewDecoder(in).Decode(&jsonRequest)
if err != nil {
// stdin now closed
if err == io.EOF {
return err
}
resp.Status = http.StatusInternalServerError
io.WriteString(resp, fmt.Sprintf(`{"error": %v}`, err.Error()))
} else {
SetHeaders(ctx, jsonRequest.Protocol.Headers)
ctx, cancel := CtxWithDeadline(ctx, jsonRequest.Deadline)
defer cancel()
handler.Serve(ctx, strings.NewReader(jsonRequest.Body), &resp)
}
jsonResponse := GetJSONResp(buf, &resp, &jsonRequest)
json.NewEncoder(out).Encode(jsonResponse)
return nil
}
func CtxWithDeadline(ctx context.Context, fnDeadline string) (context.Context, context.CancelFunc) {
t, err := time.Parse(time.RFC3339, fnDeadline)
if err == nil {
@@ -171,54 +88,6 @@ func CtxWithDeadline(ctx context.Context, fnDeadline string) (context.Context, c
return context.WithCancel(ctx)
}
func GetHTTPResp(buf *bytes.Buffer, fnResp *Response, req *http.Request) http.Response {
fnResp.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
hResp := http.Response{
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: fnResp.Status,
Request: req,
Body: ioutil.NopCloser(buf),
ContentLength: int64(buf.Len()),
Header: fnResp.Header,
}
return hResp
}
func DoHTTPOnce(handler Handler, ctx context.Context, in io.Reader, out io.Writer, buf *bytes.Buffer, hdr http.Header) error {
buf.Reset()
ResetHeaders(hdr)
resp := Response{
Writer: buf,
Status: 200,
Header: hdr,
}
req, err := http.ReadRequest(bufio.NewReader(in))
if err != nil {
// stdin now closed
if err == io.EOF {
return err
}
// TODO it would be nice if we could let the user format this response to their preferred style..
resp.Status = http.StatusInternalServerError
io.WriteString(resp, err.Error())
} else {
fnDeadline := Context(ctx).Header.Get("FN_DEADLINE")
ctx, cancel := CtxWithDeadline(ctx, fnDeadline)
defer cancel()
SetHeaders(ctx, req.Header)
handler.Serve(ctx, req.Body, &resp)
}
hResp := GetHTTPResp(buf, &resp, req)
hResp.Write(out)
return nil
}
func ResetHeaders(m http.Header) {
for k := range m { // compiler optimizes this to 1 instruction now
delete(m, k)
@@ -258,6 +127,16 @@ func SetHeaders(ctx context.Context, hdr http.Header) {
fctx.Header = hdr
}
func SetRequestURL(ctx context.Context, requestURL string) {
fctx := ctx.Value(ctxKey).(*Ctx)
fctx.RequestURL = requestURL
}
func SetMethod(ctx context.Context, method string) {
fctx := ctx.Value(ctxKey).(*Ctx)
fctx.Method = method
}
func BuildCtx() context.Context {
ctx := &Ctx{
Config: BuildConfig(),