Merge master into breakout_swarm

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis
2018-02-01 09:25:39 +00:00
parent afeb7bbce4
commit f954bf0733
1953 changed files with 614131 additions and 175582 deletions

View File

@ -1,215 +0,0 @@
package tests
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/openfaas/faas/gateway/handlers"
"github.com/openfaas/faas/gateway/metrics"
"github.com/openfaas/faas/gateway/requests"
"golang.org/x/net/context"
)
type testServiceApiClient struct {
serviceListServices []swarm.Service
serviceListError error
}
func (c testServiceApiClient) ServiceCreate(ctx context.Context, service swarm.ServiceSpec, options types.ServiceCreateOptions) (types.ServiceCreateResponse, error) {
return types.ServiceCreateResponse{}, nil
}
func (c testServiceApiClient) ServiceInspectWithRaw(ctx context.Context, serviceID string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
return swarm.Service{}, []byte{}, nil
}
func (c testServiceApiClient) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
return c.serviceListServices, c.serviceListError
}
func (c testServiceApiClient) ServiceRemove(ctx context.Context, serviceID string) error {
return nil
}
func (c testServiceApiClient) ServiceUpdate(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) {
return types.ServiceUpdateResponse{}, nil
}
func (c testServiceApiClient) ServiceLogs(ctx context.Context, serviceID string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
return nil, nil
}
func (c testServiceApiClient) TaskLogs(ctx context.Context, taskID string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
return nil, nil
}
func (c testServiceApiClient) TaskInspectWithRaw(ctx context.Context, taskID string) (swarm.Task, []byte, error) {
return swarm.Task{}, []byte{}, nil
}
func (c testServiceApiClient) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, nil
}
func TestReaderSuccessReturnsOK(t *testing.T) {
m := metrics.MetricOptions{}
c := &testServiceApiClient{
serviceListServices: []swarm.Service{},
serviceListError: nil,
}
handler := handlers.MakeFunctionReader(m, c)
w := httptest.NewRecorder()
r := &http.Request{}
handler.ServeHTTP(w, r)
expected := http.StatusOK
if status := w.Code; status != expected {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expected)
}
}
func TestReaderSuccessReturnsJsonContent(t *testing.T) {
m := metrics.MetricOptions{}
c := &testServiceApiClient{
serviceListServices: []swarm.Service{},
serviceListError: nil,
}
handler := handlers.MakeFunctionReader(m, c)
w := httptest.NewRecorder()
r := &http.Request{}
handler.ServeHTTP(w, r)
expected := "application/json"
if contentType := w.Header().Get("Content-Type"); contentType != expected {
t.Errorf("content type header does not match: got %v want %v",
contentType, expected)
}
}
func TestReaderSuccessReturnsCorrectBodyWithZeroFunctions(t *testing.T) {
m := metrics.MetricOptions{}
c := &testServiceApiClient{
serviceListServices: []swarm.Service{},
serviceListError: nil,
}
handler := handlers.MakeFunctionReader(m, c)
w := httptest.NewRecorder()
r := &http.Request{}
handler.ServeHTTP(w, r)
expected := "[]"
if w.Body.String() != expected {
t.Errorf("handler returned wrong body: got %v want %v",
w.Body.String(), expected)
}
}
func TestReaderSuccessReturnsCorrectBodyWithOneFunction(t *testing.T) {
replicas := uint64(5)
labels := map[string]string{
"function": "bar",
}
services := []swarm.Service{
swarm.Service{
Spec: swarm.ServiceSpec{
Mode: swarm.ServiceMode{
Replicated: &swarm.ReplicatedService{
Replicas: &replicas,
},
},
Annotations: swarm.Annotations{
Name: "bar",
Labels: labels,
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: swarm.ContainerSpec{
Env: []string{
"fprocess=bar",
},
Image: "foo/bar:latest",
Labels: labels,
},
},
},
},
}
m := metrics.MetricOptions{}
c := &testServiceApiClient{
serviceListServices: services,
serviceListError: nil,
}
handler := handlers.MakeFunctionReader(m, c)
w := httptest.NewRecorder()
r := &http.Request{}
handler.ServeHTTP(w, r)
functions := []requests.Function{
requests.Function{
Name: "bar",
Image: "foo/bar:latest",
InvocationCount: 0,
Replicas: 5,
EnvProcess: "bar",
Labels: &map[string]string{
"function": "bar",
},
},
}
marshalled, _ := json.Marshal(functions)
expected := string(marshalled)
if w.Body.String() != expected {
t.Errorf("handler returned wrong body: got %v want %v",
w.Body.String(), expected)
}
}
func TestReaderErrorReturnsInternalServerError(t *testing.T) {
m := metrics.MetricOptions{}
c := &testServiceApiClient{
serviceListServices: nil,
serviceListError: errors.New("error"),
}
handler := handlers.MakeFunctionReader(m, c)
w := httptest.NewRecorder()
r := &http.Request{}
handler.ServeHTTP(w, r)
expected := http.StatusInternalServerError
if status := w.Code; status != expected {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expected)
}
}
func TestReaderErrorReturnsCorrectBody(t *testing.T) {
m := metrics.MetricOptions{}
c := &testServiceApiClient{
serviceListServices: nil,
serviceListError: errors.New("error"),
}
handler := handlers.MakeFunctionReader(m, c)
w := httptest.NewRecorder()
r := &http.Request{}
handler.ServeHTTP(w, r)
expected := "Error getting service list"
if w.Body.String() != expected {
t.Errorf("handler returned wrong body: got %v want %v",
w.Body.String(), expected)
}
}

View File

@ -1,75 +0,0 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package tests
import (
"encoding/base64"
"encoding/json"
"strings"
"testing"
"github.com/docker/docker/api/types"
"github.com/openfaas/faas/gateway/handlers"
)
func TestBuildEncodedAuthConfig(t *testing.T) {
// custom repository with valid data
testValidEncodedAuthConfig(t, "user", "password", "my.repository.com/user/imagename", "my.repository.com")
testValidEncodedAuthConfig(t, "user", "weird:password:", "my.repository.com/user/imagename", "my.repository.com")
testValidEncodedAuthConfig(t, "userWithNoPassword", "", "my.repository.com/user/imagename", "my.repository.com")
testValidEncodedAuthConfig(t, "", "", "my.repository.com/user/imagename", "my.repository.com")
// docker hub default repository
testValidEncodedAuthConfig(t, "user", "password", "user/imagename", "docker.io")
testValidEncodedAuthConfig(t, "", "", "user/imagename", "docker.io")
// invalid base64 basic auth
assertEncodedAuthError(t, "invalidBasicAuth", "my.repository.com/user/imagename")
// invalid docker image name
assertEncodedAuthError(t, b64BasicAuth("user", "password"), "")
assertEncodedAuthError(t, b64BasicAuth("user", "password"), "invalid name")
}
func testValidEncodedAuthConfig(t *testing.T, user, password, imageName, expectedRegistryHost string) {
encodedAuthConfig, err := handlers.BuildEncodedAuthConfig(b64BasicAuth(user, password), imageName)
if err != nil {
t.Log("Unexpected error while building auth config with correct values")
t.Fail()
}
authConfig := &types.AuthConfig{}
authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuthConfig))
if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
t.Log("Invalid encoded auth", err)
t.Fail()
}
if user != authConfig.Username {
t.Log("Auth config username mismatch", user, authConfig.Username)
t.Fail()
}
if password != authConfig.Password {
t.Log("Auth config password mismatch", password, authConfig.Password)
t.Fail()
}
if expectedRegistryHost != authConfig.ServerAddress {
t.Log("Auth config registry server address mismatch", expectedRegistryHost, authConfig.ServerAddress)
t.Fail()
}
}
func assertEncodedAuthError(t *testing.T, b64BasicAuth, imageName string) {
_, err := handlers.BuildEncodedAuthConfig(b64BasicAuth, imageName)
if err == nil {
t.Log("Expected an error to be returned")
t.Fail()
}
}
func b64BasicAuth(user, password string) string {
return base64.StdEncoding.EncodeToString([]byte(user + ":" + password))
}

View File

@ -1,22 +0,0 @@
package tests
import (
"testing"
"github.com/openfaas/faas/gateway/handlers"
)
// Test_ParseMemory exploratory testing to document how to convert
// from Docker limits notation to bytes value.
func Test_ParseMemory(t *testing.T) {
value := "512 m"
val, err := handlers.ParseMemory(value)
if err != nil {
t.Error(err)
}
if val != 1024*1024*512 {
t.Errorf("want: %d got: %d", 1024, val)
}
}