Compare commits

..

1 Commits

Author SHA1 Message Date
Minghe
7675656a54 fix force option when deploy a function on Docker infra (#443)
* fix force option when deploy a function on Docker infra

* fix test
2020-01-02 17:13:10 +08:00
3 changed files with 67 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
package handlers
import (
"github.com/apex/log"
"github.com/metrue/fx/context"
"github.com/metrue/fx/infra"
"github.com/metrue/fx/pkg/render"
@@ -20,6 +21,12 @@ func Up(ctx context.Contexter) (err error) {
name := ctx.Get("name").(string)
deployer := ctx.Get("deployer").(infra.Deployer)
bindings := ctx.Get("bindings").([]types.PortBinding)
force := ctx.Get("force").(bool)
if force && name != "" {
if err := deployer.Destroy(ctx.GetContext(), name); err != nil {
log.Warnf("destroy service %s failed: %v", name, err)
}
}
if err := deployer.Deploy(
ctx.GetContext(),

View File

@@ -11,30 +11,64 @@ import (
)
func TestUp(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Run("normally up", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := mockCtx.NewMockContexter(ctrl)
deployer := mockDeployer.NewMockDeployer(ctrl)
ctx := mockCtx.NewMockContexter(ctrl)
deployer := mockDeployer.NewMockDeployer(ctrl)
bindings := []types.PortBinding{}
name := "sample-name"
image := "sample-image"
data := "sample-data"
ctx.EXPECT().Get("name").Return(name)
ctx.EXPECT().Get("image").Return(image)
ctx.EXPECT().Get("deployer").Return(deployer)
ctx.EXPECT().Get("bindings").Return(bindings)
ctx.EXPECT().Get("data").Return(data)
ctx.EXPECT().GetContext().Return(context.Background()).Times(2)
deployer.EXPECT().Deploy(gomock.Any(), data, name, image, bindings).Return(nil)
deployer.EXPECT().GetStatus(gomock.Any(), name).Return(types.Service{
ID: "id-1",
Name: name,
Host: "127.0.0.1",
Port: 2100,
}, nil)
if err := Up(ctx); err != nil {
t.Fatal(err)
}
bindings := []types.PortBinding{}
name := "sample-name"
image := "sample-image"
data := "sample-data"
ctx.EXPECT().Get("name").Return(name)
ctx.EXPECT().Get("image").Return(image)
ctx.EXPECT().Get("deployer").Return(deployer)
ctx.EXPECT().Get("bindings").Return(bindings)
ctx.EXPECT().Get("data").Return(data)
ctx.EXPECT().Get("force").Return(false)
ctx.EXPECT().GetContext().Return(context.Background()).Times(2)
deployer.EXPECT().Deploy(gomock.Any(), data, name, image, bindings).Return(nil)
deployer.EXPECT().GetStatus(gomock.Any(), name).Return(types.Service{
ID: "id-1",
Name: name,
Host: "127.0.0.1",
Port: 2100,
}, nil)
if err := Up(ctx); err != nil {
t.Fatal(err)
}
})
t.Run("normally up forcely", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := mockCtx.NewMockContexter(ctrl)
deployer := mockDeployer.NewMockDeployer(ctrl)
bindings := []types.PortBinding{}
name := "sample-name"
image := "sample-image"
data := "sample-data"
ctx.EXPECT().Get("name").Return(name)
ctx.EXPECT().Get("image").Return(image)
ctx.EXPECT().Get("deployer").Return(deployer)
ctx.EXPECT().Get("bindings").Return(bindings)
ctx.EXPECT().Get("data").Return(data)
ctx.EXPECT().Get("force").Return(true)
ctx.EXPECT().GetContext().Return(context.Background()).Times(3)
deployer.EXPECT().Deploy(gomock.Any(), data, name, image, bindings).Return(nil)
deployer.EXPECT().Destroy(gomock.Any(), name).Return(nil)
deployer.EXPECT().GetStatus(gomock.Any(), name).Return(types.Service{
ID: "id-1",
Name: name,
Host: "127.0.0.1",
Port: 2100,
}, nil)
if err := Up(ctx); err != nil {
t.Fatal(err)
}
})
}

View File

@@ -22,6 +22,8 @@ func Parse(action string) func(ctx context.Contexter) (err error) {
ctx.Set("name", name)
port := cli.Int("port")
ctx.Set("port", port)
force := cli.Bool("force")
ctx.Set("force", force)
case "down":
services := cli.Args()
if len(services) == 0 {