Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Commit

Permalink
switched client to go-rancher and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenfdev committed Sep 18, 2017
1 parent ec49ed5 commit 3e2cb80
Show file tree
Hide file tree
Showing 225 changed files with 29,529 additions and 297 deletions.
7 changes: 5 additions & 2 deletions handlers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// MakeDeleteHandler delete a function
func MakeDeleteHandler(client *rancher.Client) VarsHandler {
func MakeDeleteHandler(client rancher.BridgeClient) VarsHandler {
return func(w http.ResponseWriter, r *http.Request, vars map[string]string) {

defer r.Body.Close()
Expand All @@ -35,11 +35,14 @@ func MakeDeleteHandler(client *rancher.Client) VarsHandler {
// This makes sure we don't delete non-labelled deployments
service, findErr := client.FindServiceByName(request.FunctionName)
if findErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else if service == nil {
w.WriteHeader(http.StatusNotFound)
return
}

_, delErr := client.DeleteService(service)
delErr := client.DeleteService(service)
if delErr != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
201 changes: 201 additions & 0 deletions handlers/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"

"github.com/kenfdev/faas-rancher/mocks"
"github.com/kenfdev/faas/gateway/requests"
"github.com/rancher/go-rancher/v2"
"github.com/stretchr/testify/assert"
)

func Test_MakeDeleteHandler_Service_Delete_Success(t *testing.T) {
assert := assert.New(t)
// Arrange
mockClient := new(mocks.BridgeClient)
handler := MakeDeleteHandler(mockClient)
functionName := "some_function"

body := requests.DeleteFunctionRequest{
FunctionName: functionName,
}
b, jsonErr := json.Marshal(body)
if jsonErr != nil {
log.Fatal(jsonErr)
}

req, reqErr := http.NewRequest("POST", "/system/functions", bytes.NewReader(b))
if reqErr != nil {
log.Fatal(reqErr)
}

rr := httptest.NewRecorder()

expectedService := client.Service{
Name: "some_rancher_service",
}
mockClient.On("FindServiceByName", functionName).Return(&expectedService, nil)
mockClient.On("DeleteService", &expectedService).Return(nil)

// Act
handler(rr, req, nil)

// Assert
assert.Equal(rr.Code, http.StatusOK)
mockClient.AssertExpectations(t)
}

func Test_MakeDeleteHandler_InvalidBody(t *testing.T) {
assert := assert.New(t)
// Arrange
mockClient := new(mocks.BridgeClient)
handler := MakeDeleteHandler(mockClient)

b := []byte(`{"name":what?}`)
req, reqErr := http.NewRequest("POST", "/system/functions", bytes.NewReader(b))
if reqErr != nil {
log.Fatal(reqErr)
}

rr := httptest.NewRecorder()

// Act
handler(rr, req, nil)

// Assert
assert.Equal(http.StatusBadRequest, rr.Code)
}

func Test_MakeDeleteHandler_Empty_FunctionName(t *testing.T) {
assert := assert.New(t)
// Arrange
mockClient := new(mocks.BridgeClient)
handler := MakeDeleteHandler(mockClient)
emptyFunctionName := ""

invalidBody := requests.DeleteFunctionRequest{
FunctionName: emptyFunctionName,
}
b, jsonErr := json.Marshal(invalidBody)
if jsonErr != nil {
log.Fatal(jsonErr)
}

req, reqErr := http.NewRequest("POST", "/system/functions", bytes.NewReader(b))
if reqErr != nil {
log.Fatal(reqErr)
}

rr := httptest.NewRecorder()

// Act
handler(rr, req, nil)

// Assert
assert.Equal(http.StatusBadRequest, rr.Code)
}

func Test_MakeDeleteHandler_Service_Find_Error(t *testing.T) {
assert := assert.New(t)
// Arrange
mockClient := new(mocks.BridgeClient)
handler := MakeDeleteHandler(mockClient)
functionName := "some_function"

body := requests.DeleteFunctionRequest{
FunctionName: functionName,
}
b, jsonErr := json.Marshal(body)
if jsonErr != nil {
log.Fatal(jsonErr)
}

req, reqErr := http.NewRequest("POST", "/system/functions", bytes.NewReader(b))
if reqErr != nil {
log.Fatal(reqErr)
}

rr := httptest.NewRecorder()

mockClient.On("FindServiceByName", functionName).Return(nil, fmt.Errorf("Internal Server Error"))

// Act
handler(rr, req, nil)

// Assert
assert.Equal(http.StatusInternalServerError, rr.Code)
mockClient.AssertExpectations(t)
}

func Test_MakeDeleteHandler_Service_Nil_Error(t *testing.T) {
assert := assert.New(t)
// Arrange
mockClient := new(mocks.BridgeClient)
handler := MakeDeleteHandler(mockClient)
functionName := "some_function"

body := requests.DeleteFunctionRequest{
FunctionName: functionName,
}
b, jsonErr := json.Marshal(body)
if jsonErr != nil {
log.Fatal(jsonErr)
}

req, reqErr := http.NewRequest("POST", "/system/functions", bytes.NewReader(b))
if reqErr != nil {
log.Fatal(reqErr)
}

rr := httptest.NewRecorder()

mockClient.On("FindServiceByName", functionName).Return(nil, nil)

// Act
handler(rr, req, nil)

// Assert
assert.Equal(http.StatusNotFound, rr.Code)
mockClient.AssertExpectations(t)
}
func Test_MakeDeleteHandler_Service_Delete_Error(t *testing.T) {
assert := assert.New(t)
// Arrange
mockClient := new(mocks.BridgeClient)
handler := MakeDeleteHandler(mockClient)
functionName := "some_function"

body := requests.DeleteFunctionRequest{
FunctionName: functionName,
}
b, jsonErr := json.Marshal(body)
if jsonErr != nil {
log.Fatal(jsonErr)
}

req, reqErr := http.NewRequest("POST", "/system/functions", bytes.NewReader(b))
if reqErr != nil {
log.Fatal(reqErr)
}

rr := httptest.NewRecorder()

expectedService := client.Service{
Name: "some_rancher_service",
}
mockClient.On("FindServiceByName", functionName).Return(&expectedService, nil)
mockClient.On("DeleteService", &expectedService).Return(fmt.Errorf("Service Delete Failed"))

// Act
handler(rr, req, nil)

// Assert
assert.Equal(rr.Code, http.StatusBadRequest)
mockClient.AssertExpectations(t)
}
29 changes: 14 additions & 15 deletions handlers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/alexellis/faas/gateway/requests"
"github.com/kenfdev/faas-rancher/rancher"
"github.com/rancher/go-rancher/v2"
)

// ValidateDeployRequest validates that the service name is valid for Kubernetes
Expand All @@ -27,7 +28,7 @@ func ValidateDeployRequest(request *requests.CreateFunctionRequest) error {
}

// MakeDeployHandler creates a handler to create new functions in the cluster
func MakeDeployHandler(client *rancher.Client) VarsHandler {
func MakeDeployHandler(client rancher.BridgeClient) VarsHandler {
return func(w http.ResponseWriter, r *http.Request, vars map[string]string) {

defer r.Body.Close()
Expand Down Expand Up @@ -65,30 +66,28 @@ func MakeDeployHandler(client *rancher.Client) VarsHandler {
}
}

func makeServiceSpec(request requests.CreateFunctionRequest) *rancher.Service {
envVars := request.EnvVars
if envVars == nil {
envVars = make(map[string]string)
func makeServiceSpec(request requests.CreateFunctionRequest) *client.Service {

envVars := make(map[string]interface{})
for k, v := range request.EnvVars {
envVars[k] = v
}

if len(request.EnvProcess) > 0 {
envVars["fprocess"] = request.EnvProcess
}

restartPolicy := make(map[string]string)
restartPolicy["name"] = "always"

labels := make(map[string]string)
labels := make(map[string]interface{})
labels[FaasFunctionLabel] = request.Service
labels["io.rancher.container.pull_image"] = "always"

launchConfig := &rancher.LaunchConfig{
Environment: envVars,
RestartPolicy: restartPolicy,
ImageUUID: "docker:" + request.Image, // not sure if it's ok to just prefix with 'docker:'
Labels: labels,
launchConfig := &client.LaunchConfig{
Environment: envVars,
ImageUuid: "docker:" + request.Image, // not sure if it's ok to just prefix with 'docker:'
Labels: labels,
}
serviceSpec := &rancher.Service{

serviceSpec := &client.Service{
Name: request.Service,
Scale: 1,
StartOnCreate: true,
Expand Down
Loading

0 comments on commit 3e2cb80

Please sign in to comment.