Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openfaas-faas
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Team Jaz CS 598 CCC Final Project
openfaas-faas
Commits
4367fc4e
Commit
4367fc4e
authored
6 years ago
by
Alex Ellis (VMware)
Committed by
Alex Ellis
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add test coverage for buildUpstreamRequest
Signed-off-by:
Alex Ellis (VMware)
<
alexellis2@gmail.com
>
parent
2f98ca88
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
gateway/build.sh
+1
-1
1 addition, 1 deletion
gateway/build.sh
gateway/handlers/forwarding_proxy.go
+4
-26
4 additions, 26 deletions
gateway/handlers/forwarding_proxy.go
gateway/handlers/forwarding_proxy_test.go
+125
-5
125 additions, 5 deletions
gateway/handlers/forwarding_proxy_test.go
with
130 additions
and
32 deletions
gateway/build.sh
+
1
−
1
View file @
4367fc4e
...
@@ -19,7 +19,7 @@ if [ "$1" ] ; then
...
@@ -19,7 +19,7 @@ if [ "$1" ] ; then
fi
fi
fi
fi
NS
=
openfaa
s
NS
=
alexelli
s
echo
Building
$NS
/gateway:
$eTAG
echo
Building
$NS
/gateway:
$eTAG
...
...
This diff is collapsed.
Click to expand it.
gateway/handlers/forwarding_proxy.go
+
4
−
26
View file @
4367fc4e
...
@@ -16,15 +16,15 @@ import (
...
@@ -16,15 +16,15 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus"
)
)
//
P
arse out the service name (group 1) and rest of path (group 2).
//
functionMatcher p
arse
s
out the service name (group 1) and rest of path (group 2).
var
functionMatcher
=
regexp
.
MustCompile
(
"^/?(?:async-)?function/([^/?]+)([^?]*)"
)
var
functionMatcher
=
regexp
.
MustCompile
(
"^/?(?:async-)?function/([^/?]+)([^?]*)"
)
// Indicies and meta-data for functionMatcher regex parts
// Indicies and meta-data for functionMatcher regex parts
const
(
const
(
hasPathCount
=
3
hasPathCount
=
3
routeIndex
=
0
routeIndex
=
0
// routeIndex corresponds to /function/ or /async-function/
nameIndex
=
1
nameIndex
=
1
// nameIndex is the function name
pathIndex
=
2
pathIndex
=
2
// pathIndex is the path i.e. /employee/:id/
)
)
// HTTPNotifier notify about HTTP request/response
// HTTPNotifier notify about HTTP request/response
...
@@ -218,28 +218,6 @@ func (f TransparentURLPathTransformer) Transform(r *http.Request) string {
...
@@ -218,28 +218,6 @@ func (f TransparentURLPathTransformer) Transform(r *http.Request) string {
return
r
.
URL
.
Path
return
r
.
URL
.
Path
}
}
// FunctionPathTruncatingURLPathTransformer always truncated the path to "/".
type
FunctionPathTruncatingURLPathTransformer
struct
{
}
// Transform always return a path of "/".
func
(
f
FunctionPathTruncatingURLPathTransformer
)
Transform
(
r
*
http
.
Request
)
string
{
ret
:=
r
.
URL
.
Path
if
ret
!=
""
{
matcher
:=
functionMatcher
.
Copy
()
parts
:=
matcher
.
FindStringSubmatch
(
ret
)
// In the following regex, in the case of a match the r.URL.Path will be at `0`,
// the function name at `1` and the rest of the path (the part we are interested in)
// at `2`. For this transformer, all we need to do is confirm it is a function.
if
len
(
parts
)
==
hasPathCount
{
ret
=
"/"
}
}
return
ret
}
// FunctionPrefixTrimmingURLPathTransformer removes the "/function/servicename/" prefix from the URL path.
// FunctionPrefixTrimmingURLPathTransformer removes the "/function/servicename/" prefix from the URL path.
type
FunctionPrefixTrimmingURLPathTransformer
struct
{
type
FunctionPrefixTrimmingURLPathTransformer
struct
{
}
}
...
...
This diff is collapsed.
Click to expand it.
gateway/handlers/forwarding_proxy_test.go
+
125
−
5
View file @
4367fc4e
...
@@ -2,6 +2,7 @@ package handlers
...
@@ -2,6 +2,7 @@ package handlers
import
(
import
(
"bytes"
"bytes"
"fmt"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"net/url"
"net/url"
...
@@ -151,12 +152,126 @@ func Test_getServiceName(t *testing.T) {
...
@@ -151,12 +152,126 @@ func Test_getServiceName(t *testing.T) {
}
}
}
}
func
Test_buildUpstreamRequest_
Body_Method_Query_Path
(
t
*
testing
.
T
)
{
func
Test_buildUpstreamRequest_
WithPathNoQuery
(
t
*
testing
.
T
)
{
srcBytes
:=
[]
byte
(
"hello world"
)
srcBytes
:=
[]
byte
(
"hello world"
)
path
:=
"/my/deep/path"
functionPath
:=
"/employee/info/300"
requestPath
:=
fmt
.
Sprintf
(
"/function/xyz%s"
,
functionPath
)
reader
:=
bytes
.
NewReader
(
srcBytes
)
request
,
_
:=
http
.
NewRequest
(
http
.
MethodPost
,
requestPath
,
reader
)
request
.
Header
.
Set
(
"X-Source"
,
"unit-test"
)
queryWant
:=
""
if
request
.
URL
.
RawQuery
!=
queryWant
{
t
.
Errorf
(
"Query - want: %s, got: %s"
,
queryWant
,
request
.
URL
.
RawQuery
)
t
.
Fail
()
}
transformer
:=
FunctionPrefixTrimmingURLPathTransformer
{}
transformedPath
:=
transformer
.
Transform
(
request
)
wantTransformedPath
:=
functionPath
if
transformedPath
!=
wantTransformedPath
{
t
.
Errorf
(
"transformedPath want: %s, got %s"
,
wantTransformedPath
,
transformedPath
)
}
upstream
:=
buildUpstreamRequest
(
request
,
"http://xyz:8080"
,
transformedPath
)
if
request
.
Method
!=
upstream
.
Method
{
t
.
Errorf
(
"Method - want: %s, got: %s"
,
request
.
Method
,
upstream
.
Method
)
t
.
Fail
()
}
upstreamBytes
,
_
:=
ioutil
.
ReadAll
(
upstream
.
Body
)
if
string
(
upstreamBytes
)
!=
string
(
srcBytes
)
{
t
.
Errorf
(
"Body - want: %s, got: %s"
,
string
(
upstreamBytes
),
string
(
srcBytes
))
t
.
Fail
()
}
if
request
.
Header
.
Get
(
"X-Source"
)
!=
upstream
.
Header
.
Get
(
"X-Source"
)
{
t
.
Errorf
(
"Header X-Source - want: %s, got: %s"
,
request
.
Header
.
Get
(
"X-Source"
),
upstream
.
Header
.
Get
(
"X-Source"
))
t
.
Fail
()
}
if
request
.
URL
.
RawQuery
!=
upstream
.
URL
.
RawQuery
{
t
.
Errorf
(
"URL.RawQuery - want: %s, got: %s"
,
request
.
URL
.
RawQuery
,
upstream
.
URL
.
RawQuery
)
t
.
Fail
()
}
if
functionPath
!=
upstream
.
URL
.
Path
{
t
.
Errorf
(
"URL.Path - want: %s, got: %s"
,
functionPath
,
upstream
.
URL
.
Path
)
t
.
Fail
()
}
}
func
Test_buildUpstreamRequest_WithNoPathNoQuery
(
t
*
testing
.
T
)
{
srcBytes
:=
[]
byte
(
"hello world"
)
functionPath
:=
"/"
requestPath
:=
fmt
.
Sprintf
(
"/function/xyz%s"
,
functionPath
)
reader
:=
bytes
.
NewReader
(
srcBytes
)
reader
:=
bytes
.
NewReader
(
srcBytes
)
request
,
_
:=
http
.
NewRequest
(
http
.
MethodPost
,
"/function/xyz"
+
path
+
"?code=1"
,
reader
)
request
,
_
:=
http
.
NewRequest
(
http
.
MethodPost
,
requestPath
,
reader
)
request
.
Header
.
Set
(
"X-Source"
,
"unit-test"
)
queryWant
:=
""
if
request
.
URL
.
RawQuery
!=
queryWant
{
t
.
Errorf
(
"Query - want: %s, got: %s"
,
queryWant
,
request
.
URL
.
RawQuery
)
t
.
Fail
()
}
transformer
:=
FunctionPrefixTrimmingURLPathTransformer
{}
transformedPath
:=
transformer
.
Transform
(
request
)
wantTransformedPath
:=
"/"
if
transformedPath
!=
wantTransformedPath
{
t
.
Errorf
(
"transformedPath want: %s, got %s"
,
wantTransformedPath
,
transformedPath
)
}
upstream
:=
buildUpstreamRequest
(
request
,
"http://xyz:8080"
,
transformedPath
)
if
request
.
Method
!=
upstream
.
Method
{
t
.
Errorf
(
"Method - want: %s, got: %s"
,
request
.
Method
,
upstream
.
Method
)
t
.
Fail
()
}
upstreamBytes
,
_
:=
ioutil
.
ReadAll
(
upstream
.
Body
)
if
string
(
upstreamBytes
)
!=
string
(
srcBytes
)
{
t
.
Errorf
(
"Body - want: %s, got: %s"
,
string
(
upstreamBytes
),
string
(
srcBytes
))
t
.
Fail
()
}
if
request
.
Header
.
Get
(
"X-Source"
)
!=
upstream
.
Header
.
Get
(
"X-Source"
)
{
t
.
Errorf
(
"Header X-Source - want: %s, got: %s"
,
request
.
Header
.
Get
(
"X-Source"
),
upstream
.
Header
.
Get
(
"X-Source"
))
t
.
Fail
()
}
if
request
.
URL
.
RawQuery
!=
upstream
.
URL
.
RawQuery
{
t
.
Errorf
(
"URL.RawQuery - want: %s, got: %s"
,
request
.
URL
.
RawQuery
,
upstream
.
URL
.
RawQuery
)
t
.
Fail
()
}
if
functionPath
!=
upstream
.
URL
.
Path
{
t
.
Errorf
(
"URL.Path - want: %s, got: %s"
,
functionPath
,
upstream
.
URL
.
Path
)
t
.
Fail
()
}
}
func
Test_buildUpstreamRequest_WithPathAndQuery
(
t
*
testing
.
T
)
{
srcBytes
:=
[]
byte
(
"hello world"
)
functionPath
:=
"/employee/info/300"
requestPath
:=
fmt
.
Sprintf
(
"/function/xyz%s?code=1"
,
functionPath
)
reader
:=
bytes
.
NewReader
(
srcBytes
)
request
,
_
:=
http
.
NewRequest
(
http
.
MethodPost
,
requestPath
,
reader
)
request
.
Header
.
Set
(
"X-Source"
,
"unit-test"
)
request
.
Header
.
Set
(
"X-Source"
,
"unit-test"
)
if
request
.
URL
.
RawQuery
!=
"code=1"
{
if
request
.
URL
.
RawQuery
!=
"code=1"
{
...
@@ -167,6 +282,11 @@ func Test_buildUpstreamRequest_Body_Method_Query_Path(t *testing.T) {
...
@@ -167,6 +282,11 @@ func Test_buildUpstreamRequest_Body_Method_Query_Path(t *testing.T) {
transformer
:=
FunctionPrefixTrimmingURLPathTransformer
{}
transformer
:=
FunctionPrefixTrimmingURLPathTransformer
{}
transformedPath
:=
transformer
.
Transform
(
request
)
transformedPath
:=
transformer
.
Transform
(
request
)
wantTransformedPath
:=
functionPath
if
transformedPath
!=
wantTransformedPath
{
t
.
Errorf
(
"transformedPath want: %s, got %s"
,
wantTransformedPath
,
transformedPath
)
}
upstream
:=
buildUpstreamRequest
(
request
,
"http://xyz:8080"
,
transformedPath
)
upstream
:=
buildUpstreamRequest
(
request
,
"http://xyz:8080"
,
transformedPath
)
if
request
.
Method
!=
upstream
.
Method
{
if
request
.
Method
!=
upstream
.
Method
{
...
@@ -191,8 +311,8 @@ func Test_buildUpstreamRequest_Body_Method_Query_Path(t *testing.T) {
...
@@ -191,8 +311,8 @@ func Test_buildUpstreamRequest_Body_Method_Query_Path(t *testing.T) {
t
.
Fail
()
t
.
Fail
()
}
}
if
p
ath
!=
upstream
.
URL
.
Path
{
if
functionP
ath
!=
upstream
.
URL
.
Path
{
t
.
Errorf
(
"URL.Path - want: %s, got: %s"
,
p
ath
,
upstream
.
URL
.
Path
)
t
.
Errorf
(
"URL.Path - want: %s, got: %s"
,
functionP
ath
,
upstream
.
URL
.
Path
)
t
.
Fail
()
t
.
Fail
()
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment