mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
6.3 KiB
226 lines
6.3 KiB
9 years ago
|
/*
|
||
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||
9 years ago
|
|
||
9 years ago
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
9 years ago
|
|
||
9 years ago
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
9 years ago
|
|
||
9 years ago
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package service
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
|
||
9 years ago
|
"github.com/kubernetes/deployment-manager/pkg/common"
|
||
|
"github.com/kubernetes/deployment-manager/cmd/expandybird/expander"
|
||
|
"github.com/kubernetes/deployment-manager/pkg/util"
|
||
9 years ago
|
|
||
|
restful "github.com/emicklei/go-restful"
|
||
|
)
|
||
|
|
||
|
func GetTemplateReader(t *testing.T, description string, templateFileName string) io.Reader {
|
||
9 years ago
|
template, err := util.NewTemplateFromFileNames(templateFileName, importFileNames)
|
||
9 years ago
|
if err != nil {
|
||
|
t.Errorf("cannot create template for test case (%s): %s\n", err, description)
|
||
|
}
|
||
|
|
||
|
templateData, err := json.Marshal(template)
|
||
|
if err != nil {
|
||
|
t.Errorf("cannot marshal template for test case (%s): %s\n", err, description)
|
||
|
}
|
||
|
|
||
|
reader := bytes.NewReader(templateData)
|
||
|
return reader
|
||
|
}
|
||
|
|
||
|
func GetOutputString(t *testing.T, description string) string {
|
||
|
output, err := ioutil.ReadFile(outputFileName)
|
||
|
if err != nil {
|
||
|
t.Errorf("cannot read output file for test case (%s): %s\n", err, description)
|
||
|
}
|
||
|
|
||
|
return string(output)
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
httpGETMethod = "GET"
|
||
|
httpPOSTMethod = "POST"
|
||
|
validServiceURL = "/expand"
|
||
|
invalidServiceURL = "http://localhost:8080/invalidurlpath"
|
||
|
jsonContentType = "application/json"
|
||
|
invalidContentType = "invalid/content-type"
|
||
|
inputFileName = "../test/ValidContent.yaml"
|
||
|
outputFileName = "../test/ExpectedOutput.yaml"
|
||
|
)
|
||
|
|
||
|
var importFileNames = []string{
|
||
9 years ago
|
"../test/replicatedservice.py",
|
||
9 years ago
|
}
|
||
|
|
||
|
type ServiceWrapperTestCase struct {
|
||
|
Description string
|
||
|
HTTPMethod string
|
||
|
ServiceURLPath string
|
||
|
ContentType string
|
||
|
StatusCode int
|
||
|
}
|
||
|
|
||
|
var ServiceWrapperTestCases = []ServiceWrapperTestCase{
|
||
|
{
|
||
|
"expect error for invalid HTTP verb",
|
||
|
httpGETMethod,
|
||
|
validServiceURL,
|
||
|
jsonContentType,
|
||
|
http.StatusMethodNotAllowed,
|
||
|
},
|
||
|
{
|
||
|
"expect error for invalid URL path",
|
||
|
httpPOSTMethod,
|
||
|
invalidServiceURL,
|
||
|
jsonContentType,
|
||
|
http.StatusNotFound,
|
||
|
},
|
||
|
{
|
||
|
"expect error for invalid content type",
|
||
|
httpPOSTMethod,
|
||
|
validServiceURL,
|
||
|
invalidContentType,
|
||
|
http.StatusUnsupportedMediaType,
|
||
|
},
|
||
|
{
|
||
|
"expect success",
|
||
|
httpPOSTMethod,
|
||
|
validServiceURL,
|
||
|
jsonContentType,
|
||
|
http.StatusOK,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func TestServiceWrapper(t *testing.T) {
|
||
|
backend := expander.NewExpander("../expansion/expansion.py")
|
||
|
wrapper := NewService(NewExpansionHandler(backend))
|
||
9 years ago
|
container := restful.NewContainer()
|
||
|
container.ServeMux = http.NewServeMux()
|
||
9 years ago
|
wrapper.Register(container)
|
||
|
handlerTester := util.NewHandlerTester(container)
|
||
|
for _, swtc := range ServiceWrapperTestCases {
|
||
|
reader := GetTemplateReader(t, swtc.Description, inputFileName)
|
||
|
w, err := handlerTester(swtc.HTTPMethod, swtc.ServiceURLPath, swtc.ContentType, reader)
|
||
|
if err != nil {
|
||
|
t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
|
||
|
}
|
||
|
|
||
|
if w.Code != http.StatusOK {
|
||
|
if w.Code != swtc.StatusCode {
|
||
|
message := fmt.Sprintf("test returned code:%d, status: %s", w.Code, w.Body.String())
|
||
|
t.Errorf("error in test case '%s': %s\n", swtc.Description, message)
|
||
|
}
|
||
|
} else {
|
||
|
if swtc.StatusCode != http.StatusOK {
|
||
|
t.Errorf("expected error did not occur in test case '%s': want: %d have: %d\n",
|
||
|
swtc.Description, swtc.StatusCode, w.Code)
|
||
|
}
|
||
|
|
||
|
body := w.Body.Bytes()
|
||
|
actualResponse := &expander.ExpansionResponse{}
|
||
|
if err := json.Unmarshal(body, actualResponse); err != nil {
|
||
|
t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
|
||
|
}
|
||
|
|
||
|
actualResult, err := actualResponse.Unmarshal()
|
||
|
if err != nil {
|
||
|
t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
|
||
|
}
|
||
|
|
||
|
expectedOutput := GetOutputString(t, swtc.Description)
|
||
|
expectedResult := expandOutputOrDie(t, expectedOutput, swtc.Description)
|
||
|
|
||
|
if !reflect.DeepEqual(expectedResult, actualResult) {
|
||
|
message := fmt.Sprintf("want: %s\nhave: %s\n",
|
||
|
util.ToYAMLOrError(expectedResult), util.ToYAMLOrError(actualResult))
|
||
|
t.Errorf("error in test case '%s':\n%s\n", swtc.Description, message)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type ExpansionHandlerTestCase struct {
|
||
|
Description string
|
||
|
TemplateFileName string
|
||
|
}
|
||
|
|
||
|
var ExpansionHandlerTestCases = []ExpansionHandlerTestCase{
|
||
|
{
|
||
|
"expect error while expanding template",
|
||
|
"../test/InvalidFileName.yaml",
|
||
|
},
|
||
|
{
|
||
|
"expect error while marshaling output",
|
||
|
"../test/InvalidTypeName.yaml",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var malformedExpansionOutput = []byte(`
|
||
9 years ago
|
this: is: invalid: yaml:
|
||
9 years ago
|
`)
|
||
|
|
||
|
type mockExpander struct {
|
||
|
}
|
||
|
|
||
|
// ExpandTemplate passes the given configuration to the expander and returns the
|
||
|
// expanded configuration as a string on success.
|
||
9 years ago
|
func (e *mockExpander) ExpandTemplate(template *common.Template) (string, error) {
|
||
9 years ago
|
switch template.Name {
|
||
9 years ago
|
case "InvalidFileName.yaml":
|
||
9 years ago
|
return "", fmt.Errorf("expansion error")
|
||
9 years ago
|
case "InvalidTypeName.yaml":
|
||
9 years ago
|
return string(malformedExpansionOutput), nil
|
||
|
}
|
||
|
|
||
|
panic("unknown test case")
|
||
|
}
|
||
|
|
||
|
func TestExpansionHandler(t *testing.T) {
|
||
|
backend := &mockExpander{}
|
||
|
wrapper := NewService(NewExpansionHandler(backend))
|
||
|
container := restful.DefaultContainer
|
||
|
wrapper.Register(container)
|
||
|
handlerTester := util.NewHandlerTester(container)
|
||
|
for _, ehtc := range ExpansionHandlerTestCases {
|
||
|
reader := GetTemplateReader(t, ehtc.Description, ehtc.TemplateFileName)
|
||
|
w, err := handlerTester(httpPOSTMethod, validServiceURL, jsonContentType, reader)
|
||
|
if err != nil {
|
||
|
t.Errorf("error in test case '%s': %s\n", ehtc.Description, err)
|
||
|
}
|
||
|
|
||
|
if w.Code != http.StatusBadRequest {
|
||
|
t.Errorf("expected error did not occur in test case '%s': want: %d have: %d\n",
|
||
|
ehtc.Description, http.StatusBadRequest, w.Code)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func expandOutputOrDie(t *testing.T, output, description string) *expander.ExpansionResult {
|
||
|
result, err := expander.NewExpansionResult(output)
|
||
|
if err != nil {
|
||
|
t.Errorf("cannot expand output for test case '%s': %s\n", description, err)
|
||
|
}
|
||
|
|
||
|
return result
|
||
|
}
|