提交 8a7453f5 编写于 作者: P Piotr Bryk

Merge pull request #321 from batikanu/fix-deploy-file-test

Update deploy file test
......@@ -87,13 +87,13 @@ func CreateHttpApiHandler(client *client.Client, heapsterClient HeapsterClient)
deployFromFileWs := new(restful.WebService)
deployFromFileWs.Path("/api/appdeploymentfromfile").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
deployFromFileWs.Route(
deployFromFileWs.POST("").
To(apiHandler.handleDeployFromFile).
Reads(AppDeploymentFromFileSpec{}).
Writes(AppDeploymentFromFileSpec{}))
To(apiHandler.handleDeployFromFile).
Reads(AppDeploymentFromFileSpec{}).
Writes(AppDeploymentFromFileSpec{}))
wsContainer.Add(deployFromFileWs)
replicaSetWs := new(restful.WebService)
......@@ -205,7 +205,7 @@ func (apiHandler *ApiHandler) handleDeployFromFile(request *restful.Request, res
handleInternalError(response, err)
return
}
if err := DeployAppFromFile(deploymentSpec); err != nil {
if err := DeployAppFromFile(deploymentSpec, CreateObjectFromInfoFn); err != nil {
handleInternalError(response, err)
return
}
......
......@@ -243,8 +243,16 @@ func getLabelsMap(labels []Label) map[string]string {
return result
}
type createObjectFromInfo func(info *kubectlResource.Info) error
// Implementation of createObjectFromInfo
var CreateObjectFromInfoFn = func(info *kubectlResource.Info) error {
_, err := kubectlResource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object)
return err
}
// Deploys an app based on the given yaml or json file.
func DeployAppFromFile(spec *AppDeploymentFromFileSpec) error {
func DeployAppFromFile(spec *AppDeploymentFromFileSpec, createObjectFromInfoFn createObjectFromInfo) error {
const (
validate = true
emptyCacheDir = ""
......@@ -267,8 +275,7 @@ func DeployAppFromFile(spec *AppDeploymentFromFileSpec) error {
Do()
return r.Visit(func(info *kubectlResource.Info, err error) error {
// creates an object from input info
_, err = kubectlResource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object)
err = createObjectFromInfoFn(info)
if err != nil {
return err
}
......@@ -276,4 +283,3 @@ func DeployAppFromFile(spec *AppDeploymentFromFileSpec) error {
return nil
})
}
......@@ -22,6 +22,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
kubectlResource "k8s.io/kubernetes/pkg/kubectl/resource"
)
func TestDeployApp(t *testing.T) {
......@@ -161,19 +162,21 @@ func TestGetAvailableProtocols(t *testing.T) {
}
func TestDeployAppFromFileWithValidContent(t *testing.T) {
const (
testNamespace = "test-deployfile-namespace"
)
validContent := "{\"kind\": \"Namespace\"," +
"\"apiVersion\": \"v1\"," +
"\"metadata\": {" +
"\"name\": \"development\"," +
"\"name\": \"" + testNamespace + "\"," +
"\"labels\": {\"name\": \"development\"}}}"
spec := &AppDeploymentFromFileSpec{
Name: "foo-name",
Content: validContent,
}
fakeCreateObjectFromInfo := func(info *kubectlResource.Info) error { return nil }
err := DeployAppFromFile(spec)
err := DeployAppFromFile(spec, fakeCreateObjectFromInfo)
if err != nil {
t.Errorf("Expected return value to be %#v but got %#v", nil, err)
}
......@@ -184,9 +187,9 @@ func TestDeployAppFromFileWithInvalidContent(t *testing.T) {
Name: "foo-name",
Content: "foo-content-invalid",
}
fakeCreateObjectFromInfo := func(info *kubectlResource.Info) error { return nil }
err := DeployAppFromFile(spec)
err := DeployAppFromFile(spec, fakeCreateObjectFromInfo)
if err == nil {
t.Errorf("Expected return value to be an error but got %#v", nil)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册