func (s *S) TestRebalanceContainersRunAskingForConfirmation(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, Stdin: bytes.NewBufferString("y"), } msg, _ := json.Marshal(tsuruIo.SimpleJsonMessage{Message: "progress msg"}) result := string(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) expected := map[string]string{ "dry": "false", } result := map[string]string{} err = json.Unmarshal(body, &result) c.Assert(expected, check.DeepEquals, result) return req.URL.Path == "/docker/containers/rebalance" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := rebalanceContainersCmd{} err := cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "Are you sure you want to rebalance containers? (y/n) progress msg") cmd2 := rebalanceContainersCmd{} err = cmd2.Run(&context, client) c.Assert(err, check.IsNil) }
func (s *S) TestBsEnvSetRunForPool(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, Args: []string{"A=1", "B=2"}, } trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: "", Status: http.StatusNoContent}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) expected := Config{ Pools: []PoolEnvs{{ Name: "pool1", Envs: []Env{{Name: "A", Value: "1"}, {Name: "B", Value: "2"}}, }}, } var conf Config err = json.Unmarshal(body, &conf) c.Assert(conf, check.DeepEquals, expected) return req.URL.Path == "/docker/bs/env" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := EnvSetCmd{} err := cmd.Flags().Parse(true, []string{"--pool", "pool1"}) c.Assert(err, check.IsNil) err = cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "Variables successfully set.\n") }
func (s *S) TestTokenGenWithExportOn(c *gocheck.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Args: []string{"myapp"}, Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("glb", "0.2", "ad-ver", &stdout, &stderr, nil, nil) result := `{"token":"secret123"}` trans := testing.ConditionalTransport{ Transport: testing.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, gocheck.IsNil) c.Assert(string(body), gocheck.Equals, `{"client":"myapp","export":true}`) return req.Method == "POST" && req.URL.Path == "/tokens" }, } expected := `Application token: "secret123".` + "\n" client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) command := tokenGen{} command.Flags().Parse(true, []string{"-e"}) err := command.Run(&context, client) c.Assert(err, gocheck.IsNil) c.Assert(stdout.String(), gocheck.Equals, expected) }
func (s *S) TestListUsersRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("glb", "0.2", "ad-ver", &stdout, &stderr, nil, nil) result := `[{"email": "*****@*****.**","teams":["team1", "team2", "team3"]}]` trans := cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { return req.Method == "GET" && req.URL.Path == "/users" }, } expected := `+---------------+---------------------+ | User | Teams | +---------------+---------------------+ | [email protected] | team1, team2, team3 | +---------------+---------------------+ ` client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) command := listUsers{} err := command.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, expected) }
func (s *S) TestListUsersRunWithoutFlags(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("glb", "0.2", "ad-ver", &stdout, &stderr, nil, nil) result := `[{"email": "*****@*****.**", "roles":[ {"name": "role1", "contexttype": "team", "contextvalue": "a"}, {"name": "role2", "contexttype": "app", "contextvalue": "x"} ] }]` trans := cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { return req.Method == "GET" && req.URL.Path == "/users" }, } expected := `+---------------+---------------+ | User | Roles | +---------------+---------------+ | [email protected] | role1(team a) | | | role2(app x) | +---------------+---------------+ ` client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) command := listUsers{} err := command.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, expected) }
func (s *S) TestListUsersRunWithMoreThanOneFlagReturnsError(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("glb", "0.2", "ad-ver", &stdout, &stderr, nil, nil) result := `[{"email": "*****@*****.**", "roles":[ {"name": "role1", "contexttype": "team", "contextvalue": "a"}, {"name": "role2", "contexttype": "app", "contextvalue": "x"} ], "permissions":[ {"name": "app.create", "contexttype": "team", "contextvalue": "a"}, {"name": "app.deploy", "contexttype": "app", "contextvalue": "x"} ] }]` trans := cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { return req.Method == "GET" && req.URL.Path == "/users" && req.URL.RawQuery == "[email protected]&role=role2" }, } client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) command := listUsers{} command.Flags().Parse(true, []string{"-u", "*****@*****.**", "-r", "role2"}) err := command.Run(&context, client) c.Assert(err, check.ErrorMatches, "You cannot set more than one flag. Enter <tsuru user-list --help> for more information.") }
func (s *S) TestChangeUserQuotaRun(c *check.C) { var called bool var stdout, stderr bytes.Buffer context := cmd.Context{ Args: []string{"*****@*****.**", "5"}, Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("tsuru", "0.5", "ad-ver", &stdout, &stderr, nil, nil) trans := cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: "", Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { called = true defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) c.Assert(string(body), check.Equals, `limit=5`) return req.Method == "POST" && req.URL.Path == "/users/[email protected]/quota" }, } client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) command := changeUserQuota{} err := command.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "Quota successfully updated.\n") c.Assert(called, check.Equals, true) }
func (s *S) TestChangeAppQuotaRunUnlimited(c *check.C) { var called bool var stdout, stderr bytes.Buffer context := cmd.Context{ Args: []string{"myapp", "unlimited"}, Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("tsuru", "0.5", "ad-ver", &stdout, &stderr, nil, nil) trans := cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: "", Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { called = true defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) c.Assert(string(body), check.Equals, "limit=-1") c.Assert(req.Header.Get("Content-Type"), check.Equals, "application/x-www-form-urlencoded") return req.Method == "POST" && req.URL.Path == "/apps/myapp/quota" }, } client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) command := changeAppQuota{} err := command.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "Quota successfully updated.\n") c.Assert(called, check.Equals, true) }
func (s *S) TestBsUpgradeRun(c *check.C) { var called bool var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, Args: []string{"A=1", "B=2"}, } msg := io.SimpleJsonMessage{Message: "it worked!"} result, err := json.Marshal(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: string(result), Status: http.StatusNoContent}, CondFunc: func(req *http.Request) bool { called = true return req.URL.Path == "/docker/bs/upgrade" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := UpgradeCmd{} err = cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "it worked!") c.Assert(called, check.Equals, true) }
func (s *S) TestDockerLogUpdateRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } msg := tsuruIo.SimpleJsonMessage{Message: "success!!!"} result, _ := json.Marshal(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: string(result), Status: http.StatusNoContent}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) expected := provision.ScopedConfig{ Envs: []provision.Entry{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "log-driver", Value: "x"}}, } var data logsSetData err = json.Unmarshal(body, &data) c.Assert(err, check.IsNil) sort.Sort(provision.ConfigEntryList(data.Config.Envs)) c.Assert(data.Config, check.DeepEquals, expected) return req.URL.Path == "/1.0/docker/logs" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := dockerLogUpdate{} err := cmd.Flags().Parse(true, []string{"--log-driver", "x", "--log-opt", "a=1", "--log-opt", "b=2"}) c.Assert(err, check.IsNil) err = cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "success!!!") }
func (s *S) TestBsEnvSetRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, Args: []string{"A=1", "B=2"}, } msg := io.SimpleJsonMessage{Message: "env-set success"} result, _ := json.Marshal(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: string(result), Status: http.StatusNoContent}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) expected := Config{ Envs: []Env{{Name: "A", Value: "1"}, {Name: "B", Value: "2"}}, } var conf Config err = json.Unmarshal(body, &conf) c.Assert(conf, check.DeepEquals, expected) return req.URL.Path == "/docker/bs/env" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := EnvSetCmd{} err := cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "env-set success") }
func (s *S) TestDockerLogUpdateForPoolRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } msg := tsuruIo.SimpleJsonMessage{Message: "success!!!"} result, _ := json.Marshal(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: string(result), Status: http.StatusNoContent}, CondFunc: func(req *http.Request) bool { err := req.ParseForm() c.Assert(err, check.IsNil) c.Assert(req.Form, check.DeepEquals, url.Values{ "restart": []string{"false"}, "pool": []string{"p1"}, "Driver": []string{"x"}, "LogOpts.a": []string{"1"}, "LogOpts.b": []string{"2"}, }) return req.URL.Path == "/1.0/docker/logs" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := dockerLogUpdate{} err := cmd.Flags().Parse(true, []string{"--pool", "p1", "--log-driver", "x", "--log-opt", "a=1", "--log-opt", "b=2"}) c.Assert(err, check.IsNil) err = cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "success!!!") }
func (s *S) TestMoveContainerRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, Args: []string{"contId", "toHost"}, } msg, _ := json.Marshal(tsuruIo.SimpleJsonMessage{Message: "progress msg"}) result := string(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { path := req.URL.Path == "/1.0/docker/container/contId/move" method := req.Method == "POST" to := req.FormValue("to") == "toHost" return path && method && to }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := moveContainerCmd{} err := cmd.Run(&context, client) c.Assert(err, check.IsNil) expected := "progress msg" c.Assert(stdout.String(), check.Equals, expected) }
func (s *S) TestRebalanceContainersRunAskingForConfirmation(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, Stdin: bytes.NewBufferString("y"), } msg, _ := json.Marshal(tsuruIo.SimpleJsonMessage{Message: "progress msg"}) result := string(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { req.ParseForm() var params rebalanceOptions dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) err := dec.DecodeValues(¶ms, req.Form) c.Assert(err, check.IsNil) c.Assert(params.Dry, check.Equals, false) path := req.URL.Path == "/1.0/docker/containers/rebalance" method := req.Method == "POST" return path && method }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := rebalanceContainersCmd{} err := cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "Are you sure you want to rebalance containers? (y/n) progress msg") cmd2 := rebalanceContainersCmd{} err = cmd2.Run(&context, client) c.Assert(err, check.IsNil) }
func (s *S) TestMoveContainerRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, Args: []string{"contId", "toHost"}, } msg, _ := json.Marshal(tsuruIo.SimpleJsonMessage{Message: "progress msg"}) result := string(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) expected := map[string]string{ "to": "toHost", } result := map[string]string{} err = json.Unmarshal(body, &result) c.Assert(expected, check.DeepEquals, result) return req.URL.Path == "/docker/container/contId/move" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := moveContainerCmd{} err := cmd.Run(&context, client) c.Assert(err, check.IsNil) expected := "progress msg" c.Assert(stdout.String(), check.Equals, expected) }
func buildManager() *cmd.Manager { m := cmd.NewManager("tsr", "0.6.0", "", os.Stdout, os.Stderr, os.Stdin, nil) m.Register(&tsrCommand{Command: &apiCmd{}}) m.Register(&tsrCommand{Command: tokenCmd{}}) m.Register(&tsrCommand{Command: &healerCmd{}}) registerProvisionersCommands(m) return m }
func (s *S) TestCommandsFromBaseManagerAreRegistered(c *gocheck.C) { baseManager := cmd.NewManager("tsr", "0.3.0", "", os.Stdout, os.Stderr, os.Stdin, nil) manager := buildManager() for name, instance := range baseManager.Commands { command, ok := manager.Commands[name] c.Assert(ok, gocheck.Equals, true) c.Assert(command, gocheck.FitsTypeOf, instance) } }
func buildManager() *cmd.Manager { m := cmd.NewManager("tsurud", api.Version, "", os.Stdout, os.Stderr, os.Stdin, nil) m.Register(&tsurudCommand{Command: &apiCmd{}}) m.Register(&tsurudCommand{Command: tokenCmd{}}) m.Register(&tsurudCommand{Command: &migrateCmd{}}) m.Register(&tsurudCommand{Command: gandalfSyncCmd{}}) registerProvisionersCommands(m) return m }
func (s *S) TestTokenRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Args: []string{}, Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("glb", "", "", &stdout, &stderr, os.Stdin, nil) client := cmd.NewClient(&http.Client{}, nil, manager) command := tokenCmd{} err := command.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Not(check.Equals), "") }
func buildManager() *cmd.Manager { m := cmd.NewManager("tsurud", api.Version, "", os.Stdout, os.Stderr, os.Stdin, nil) m.Register(&tsurudCommand{Command: &apiCmd{}}) m.Register(&tsurudCommand{Command: tokenCmd{}}) m.Register(&tsurudCommand{Command: &migrateCmd{}}) m.Register(&tsurudCommand{Command: gandalfSyncCmd{}}) m.Register(&tsurudCommand{Command: createRootUserCmd{}}) m.Register(&migrationListCmd{}) err := registerProvisionersCommands(m) if err != nil { log.Fatalf("unable to register commands: %v", err) } return m }
func (s *S) TestDockerLogInfoRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } conf := provision.ScopedConfig{ Envs: []provision.Entry{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "log-driver", Value: "x"}}, Pools: []provision.PoolEntry{ { Name: "p1", Envs: []provision.Entry{{Name: "a", Value: "9"}, {Name: "log-driver", Value: "x"}}, }, { Name: "p2", Envs: []provision.Entry{{Name: "log-driver", Value: "bs"}}, }, }, } result, _ := json.Marshal(conf) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: string(result), Status: http.StatusNoContent}, CondFunc: func(req *http.Request) bool { return req.URL.Path == "/1.0/docker/logs" && req.Method == "GET" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := dockerLogInfo{} err := cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, `Log driver [default]: x +------+-------+ | Name | Value | +------+-------+ | a | 1 | | b | 2 | +------+-------+ Log driver [pool p1]: x +------+-------+ | Name | Value | +------+-------+ | a | 9 | +------+-------+ Log driver [pool p2]: bs `) }
func (s *S) TestFixContainersCmdRun(c *gocheck.C) { var buf bytes.Buffer context := cmd.Context{Stdout: &buf, Stderr: &buf} trans := &testing.ConditionalTransport{ Transport: testing.Transport{Message: "", Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { return req.URL.Path == "/docker/fix-containers" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &buf, &buf, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := fixContainersCmd{} err := cmd.Run(&context, client) c.Assert(err, gocheck.IsNil) c.Assert(buf.String(), gocheck.Equals, "") }
func (s *S) TestBsEnvSetRunInvalidInput(c *check.C) { var stdout, stderr bytes.Buffer manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{}, nil, manager) command := EnvSetCmd{} err := command.Run(&cmd.Context{ Stdout: &stdout, Stderr: &stderr, Args: []string{"xxx"}, }, client) c.Assert(err, check.ErrorMatches, "invalid variable values") err = command.Run(&cmd.Context{ Stdout: &stdout, Stderr: &stderr, Args: []string{"a=1", "="}, }, client) c.Assert(err, check.ErrorMatches, "invalid variable values") }
func (s *S) TestChangeAppQuotaFailure(c *check.C) { var stdout, stderr bytes.Buffer manager := cmd.NewManager("tsuru", "0.5", "ad-ver", &stdout, &stderr, nil, nil) trans := &cmdtest.Transport{ Message: "app not found", Status: http.StatusNotFound, } context := cmd.Context{ Args: []string{"myapp", "5"}, Stdout: &stdout, Stderr: &stderr, } client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) command := changeAppQuota{} err := command.Run(&context, client) c.Assert(err, check.NotNil) c.Assert(err.Error(), check.Equals, "app not found") }
func (s *S) TestRebalanceContainersRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } msg, _ := json.Marshal(tsuruIo.SimpleJsonMessage{Message: "progress msg"}) result := string(msg) expectedRebalance := rebalanceOptions{ Dry: true, } trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { req.ParseForm() var params rebalanceOptions dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) err := dec.DecodeValues(¶ms, req.Form) c.Assert(err, check.IsNil) c.Assert(params, check.DeepEquals, expectedRebalance) path := req.URL.Path == "/1.0/docker/containers/rebalance" method := req.Method == "POST" return path && method }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := rebalanceContainersCmd{} err := cmd.Flags().Parse(true, []string{"--dry", "-y"}) c.Assert(err, check.IsNil) err = cmd.Run(&context, client) c.Assert(err, check.IsNil) expected := "progress msg" c.Assert(stdout.String(), check.Equals, expected) expectedRebalance = rebalanceOptions{ Dry: false, } cmd2 := rebalanceContainersCmd{} cmd2.Flags().Parse(true, []string{"-y"}) err = cmd2.Run(&context, client) c.Assert(err, check.IsNil) }
func (s *S) TestRebalanceContainersRun(c *gocheck.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } msg, _ := json.Marshal(progressLog{Message: "progress msg"}) result := string(msg) expectedDry := "true" trans := &testing.ConditionalTransport{ Transport: testing.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, gocheck.IsNil) expected := map[string]string{ "dry": expectedDry, } result := map[string]string{} err = json.Unmarshal(body, &result) c.Assert(expected, gocheck.DeepEquals, result) return req.URL.Path == "/docker/containers/rebalance" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := rebalanceContainersCmd{} err := cmd.Flags().Parse(true, []string{"--dry", "-y"}) c.Assert(err, gocheck.IsNil) err = cmd.Run(&context, client) c.Assert(err, gocheck.IsNil) expected := "progress msg\n" c.Assert(stdout.String(), gocheck.Equals, expected) expectedDry = "false" cmd2 := rebalanceContainersCmd{} cmd2.Flags().Parse(true, []string{"-y"}) err = cmd2.Run(&context, client) c.Assert(err, gocheck.IsNil) }
func (s *S) TestCreateRootUserCmdRun(c *check.C) { var stdout, stderr bytes.Buffer reader := strings.NewReader("foo123\nfoo123\n") context := cmd.Context{ Args: []string{"*****@*****.**"}, Stdout: &stdout, Stderr: &stderr, Stdin: reader, } manager := cmd.NewManager("glb", "", "", &stdout, &stderr, os.Stdin, nil) client := cmd.NewClient(&http.Client{}, nil, manager) command := createRootUserCmd{} err := command.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, "Password: \nConfirm: \nRoot user successfully created.\n") u, err := auth.GetUserByEmail("*****@*****.**") c.Assert(err, check.IsNil) perms, err := u.Permissions() c.Assert(err, check.IsNil) c.Assert(perms, check.HasLen, 1) c.Assert(perms[0].Scheme, check.Equals, permission.PermAll) }
func (s *S) TestViewUserQuotaRun(c *check.C) { result := `{"inuse":3,"limit":4}` var stdout, stderr bytes.Buffer context := cmd.Context{ Args: []string{"*****@*****.**"}, Stdout: &stdout, Stderr: &stderr, } manager := cmd.NewManager("tsuru", "0.5", "ad-ver", &stdout, &stderr, nil, nil) trans := cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { return req.Method == "GET" && req.URL.Path == "/users/[email protected]/quota" }, } client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) command := viewUserQuota{} err := command.Run(&context, client) c.Assert(err, check.IsNil) expected := `User: [email protected] Apps usage: 3/4 ` c.Assert(stdout.String(), check.Equals, expected) }
func (s *S) TestRebalanceContainersRunWithFilters(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } msg, _ := json.Marshal(tsuruIo.SimpleJsonMessage{Message: "progress msg"}) result := string(msg) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: result, Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) c.Assert(err, check.IsNil) expected := map[string]interface{}{ "dry": "false", "metadataFilter": map[string]interface{}{"pool": "x", "a": "b"}, "appFilter": []interface{}{"x", "y"}, } var result map[string]interface{} err = json.Unmarshal(body, &result) c.Assert(err, check.IsNil) c.Assert(result, check.DeepEquals, expected) return req.URL.Path == "/docker/containers/rebalance" && req.Method == "POST" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := rebalanceContainersCmd{} err := cmd.Flags().Parse(true, []string{"-y", "--metadata", "pool=x", "--metadata", "a=b", "--app", "x", "--app", "y"}) c.Assert(err, check.IsNil) err = cmd.Run(&context, client) c.Assert(err, check.IsNil) expected := "progress msg" c.Assert(stdout.String(), check.Equals, expected) }
func (s *S) TestBsInfoRun(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } conf := Config{ Image: "tsuru/bs", Envs: []Env{ {Name: "A", Value: "1"}, {Name: "B", Value: "2"}, }, Pools: []PoolEnvs{ {Name: "pool1", Envs: []Env{ {Name: "A", Value: "9"}, {Name: "Z", Value: "8"}, }}, {Name: "pool2", Envs: []Env{ {Name: "Y", Value: "7"}, }}, }, } result, err := json.Marshal(conf) c.Assert(err, check.IsNil) trans := &cmdtest.ConditionalTransport{ Transport: cmdtest.Transport{Message: string(result), Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { return req.URL.Path == "/docker/bs" && req.Method == "GET" }, } manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil) client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager) cmd := InfoCmd{} err = cmd.Run(&context, client) c.Assert(err, check.IsNil) c.Assert(stdout.String(), check.Equals, `Image: tsuru/bs Environment Variables [Default]: +------+-------+ | Name | Value | +------+-------+ | A | 1 | | B | 2 | +------+-------+ Environment Variables [pool1]: +------+-------+ | Name | Value | +------+-------+ | A | 9 | | Z | 8 | +------+-------+ Environment Variables [pool2]: +------+-------+ | Name | Value | +------+-------+ | Y | 7 | +------+-------+ `) }