本文整理汇总了Golang中github.com/juju/cmd.FormatJson函数的典型用法代码示例。如果您正苦于以下问题:Golang FormatJson函数的具体用法?Golang FormatJson怎么用?Golang FormatJson使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FormatJson函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAllMachines
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, jc.ErrorIsNil)
context, err := testing.RunCommand(c, newRunCommand(), "--format=json", "--all", "hostname")
c.Assert(err, jc.ErrorIsNil)
c.Check(testing.Stdout(context), gc.Equals, string(jsonFormatted)+"\n")
}
示例2: TestRunForMachineAndUnit
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, jc.ErrorIsNil)
context, err := testing.RunCommand(c, newRunCommand(),
"--format=json", "--machine=0", "--unit=unit/0", "hostname",
)
c.Assert(err, jc.ErrorIsNil)
c.Check(testing.Stdout(context), gc.Equals, string(jsonFormatted)+"\n")
}
示例3: TestSingleResponse
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, jc.ErrorIsNil)
jsonFormatted, err := cmd.FormatJson(unformatted)
c.Assert(err, jc.ErrorIsNil)
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, newRunCommand(), args...)
if test.errorMatch != "" {
c.Check(err, gc.ErrorMatches, test.errorMatch)
} else {
c.Check(err, jc.ErrorIsNil)
}
c.Check(testing.Stdout(context), gc.Equals, test.stdout)
c.Check(testing.Stderr(context), gc.Equals, test.stderr)
}
}
示例4: TestAllMachines
func (s *RunSuite) TestAllMachines(c *gc.C) {
mock := s.setupMockAPI()
mock.setMachinesAlive("0", "1", "2")
response0 := mockResponse{
stdout: "megatron\n",
machineTag: "machine-0",
}
response1 := mockResponse{
message: "command timed out",
machineTag: "machine-1",
}
response2 := mockResponse{
message: "command timed out",
machineTag: "machine-2",
}
mock.setResponse("0", response0)
mock.setResponse("1", response1)
mock.setResponse("2", response2)
machine0Result := mock.runResponses["0"]
machine1Result := mock.runResponses["1"]
mock.actionResponses = map[string]params.ActionResult{
mock.receiverIdMap["0"]: machine0Result,
mock.receiverIdMap["1"]: machine1Result,
}
machine0Query := makeActionQuery(mock.receiverIdMap["0"], "MachineId", names.NewMachineTag("0"))
machine1Query := makeActionQuery(mock.receiverIdMap["1"], "MachineId", names.NewMachineTag("1"))
unformatted := []interface{}{
ConvertActionResults(machine0Result, machine0Query),
ConvertActionResults(machine1Result, machine1Query),
map[string]interface{}{
"Action": mock.receiverIdMap["2"],
"MachineId": "2",
"Error": "action not found",
},
}
buff := &bytes.Buffer{}
err := cmd.FormatJson(buff, unformatted)
c.Assert(err, jc.ErrorIsNil)
context, err := testing.RunCommand(c, newRunCommand(), "--format=json", "--all", "hostname")
c.Assert(err, jc.ErrorIsNil)
c.Check(testing.Stdout(context), gc.Equals, buff.String())
c.Check(testing.Stderr(context), gc.Equals, "")
}
示例5: TestRunForMachineAndUnit
func (s *RunSuite) TestRunForMachineAndUnit(c *gc.C) {
mock := s.setupMockAPI()
machineResponse := mockResponse{
stdout: "megatron\n",
machineTag: "machine-0",
}
unitResponse := mockResponse{
stdout: "bumblebee",
unitTag: "unit-unit-0",
}
mock.setResponse("0", machineResponse)
mock.setResponse("unit/0", unitResponse)
machineResult := mock.runResponses["0"]
unitResult := mock.runResponses["unit/0"]
mock.actionResponses = map[string]params.ActionResult{
mock.receiverIdMap["0"]: machineResult,
mock.receiverIdMap["unit/0"]: unitResult,
}
machineQuery := makeActionQuery(mock.receiverIdMap["0"], "MachineId", names.NewMachineTag("0"))
unitQuery := makeActionQuery(mock.receiverIdMap["unit/0"], "UnitId", names.NewUnitTag("unit/0"))
unformatted := []interface{}{
ConvertActionResults(machineResult, machineQuery),
ConvertActionResults(unitResult, unitQuery),
}
buff := &bytes.Buffer{}
err := cmd.FormatJson(buff, unformatted)
c.Assert(err, jc.ErrorIsNil)
context, err := testing.RunCommand(c, newRunCommand(),
"--format=json", "--machine=0", "--unit=unit/0", "hostname",
)
c.Assert(err, jc.ErrorIsNil)
c.Check(testing.Stdout(context), gc.Equals, buff.String())
}
示例6: TestSingleResponse
func (s *RunSuite) TestSingleResponse(c *gc.C) {
mock := s.setupMockAPI()
mock.setMachinesAlive("0")
mockResponse := mockResponse{
stdout: "stdout\n",
stderr: "stderr\n",
code: "42",
machineTag: "machine-0",
}
mock.setResponse("0", mockResponse)
machineResult := mock.runResponses["0"]
mock.actionResponses = map[string]params.ActionResult{
mock.receiverIdMap["0"]: machineResult,
}
query := makeActionQuery(mock.receiverIdMap["0"], "MachineId", names.NewMachineTag("0"))
unformatted := []interface{}{
ConvertActionResults(machineResult, query),
}
jsonFormatted, err := cmd.FormatJson(unformatted)
c.Assert(err, jc.ErrorIsNil)
yamlFormatted, err := cmd.FormatYaml(unformatted)
c.Assert(err, jc.ErrorIsNil)
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, newRunCommand(), args...)
if test.errorMatch != "" {
c.Check(err, gc.ErrorMatches, test.errorMatch)
} else {
c.Check(err, jc.ErrorIsNil)
}
c.Check(testing.Stdout(context), gc.Equals, test.stdout)
c.Check(testing.Stderr(context), gc.Equals, test.stderr)
}
}