func (s *RunSuite) TestAllMachines(c *gc.C) { mock := s.setupMockAPI() mock.setMachinesAlive("0", "1") response0 := mockResponse{ stdout: "megatron\n", machineId: "0", } response1 := mockResponse{ error: "command timed out", machineId: "1", } mock.setResponse("0", response0) unformatted := ConvertRunResults([]params.RunResult{ makeRunResult(response0), makeRunResult(response1), }) jsonFormatted, err := cmd.FormatJson(unformatted) c.Assert(err, gc.IsNil) context, err := testing.RunCommand(c, &RunCommand{}, "--format=json", "--all", "hostname") c.Assert(err, gc.IsNil) c.Check(testing.Stdout(context), gc.Equals, string(jsonFormatted)+"\n") }
func (s *RunSuite) TestRunForMachineAndUnit(c *gc.C) { mock := s.setupMockAPI() machineResponse := mockResponse{ stdout: "megatron\n", machineId: "0", } unitResponse := mockResponse{ stdout: "bumblebee", machineId: "1", unitId: "unit/0", } mock.setResponse("0", machineResponse) mock.setResponse("unit/0", unitResponse) unformatted := ConvertRunResults([]params.RunResult{ makeRunResult(machineResponse), makeRunResult(unitResponse), }) jsonFormatted, err := cmd.FormatJson(unformatted) c.Assert(err, gc.IsNil) context, err := testing.RunCommand(c, envcmd.Wrap(&RunCommand{}), "--format=json", "--machine=0", "--unit=unit/0", "hostname", ) c.Assert(err, gc.IsNil) c.Check(testing.Stdout(context), gc.Equals, string(jsonFormatted)+"\n") }
func (s *RunSuite) TestSingleResponse(c *gc.C) { mock := s.setupMockAPI() mock.setMachinesAlive("0") mockResponse := mockResponse{ stdout: "stdout\n", stderr: "stderr\n", code: 42, machineId: "0", } mock.setResponse("0", mockResponse) unformatted := ConvertRunResults([]params.RunResult{ makeRunResult(mockResponse)}) yamlFormatted, err := cmd.FormatYaml(unformatted) c.Assert(err, gc.IsNil) jsonFormatted, err := cmd.FormatJson(unformatted) c.Assert(err, gc.IsNil) for i, test := range []struct { message string format string stdout string stderr string errorMatch string }{{ message: "smart (default)", stdout: "stdout\n", stderr: "stderr\n", errorMatch: "subprocess encountered error code 42", }, { message: "yaml output", format: "yaml", stdout: string(yamlFormatted) + "\n", }, { message: "json output", format: "json", stdout: string(jsonFormatted) + "\n", }} { c.Log(fmt.Sprintf("%v: %s", i, test.message)) args := []string{} if test.format != "" { args = append(args, "--format", test.format) } args = append(args, "--all", "ignored") context, err := testing.RunCommand(c, envcmd.Wrap(&RunCommand{}), args...) if test.errorMatch != "" { c.Check(err, gc.ErrorMatches, test.errorMatch) } else { c.Check(err, gc.IsNil) } c.Check(testing.Stdout(context), gc.Equals, test.stdout) c.Check(testing.Stderr(context), gc.Equals, test.stderr) } }