github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/runner/jujuc/status-get_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "encoding/json" 8 "io/ioutil" 9 "path/filepath" 10 11 "github.com/juju/cmd" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 goyaml "gopkg.in/yaml.v2" 15 16 "github.com/juju/juju/testing" 17 "github.com/juju/juju/worker/uniter/runner/jujuc" 18 ) 19 20 type statusGetSuite struct { 21 ContextSuite 22 } 23 24 var _ = gc.Suite(&statusGetSuite{}) 25 26 func (s *statusGetSuite) SetUpTest(c *gc.C) { 27 s.ContextSuite.SetUpTest(c) 28 } 29 30 var ( 31 statusAttributes = map[string]interface{}{ 32 "status": "error", 33 "message": "doing work", 34 "status-data": map[string]interface{}{"foo": "bar"}, 35 } 36 ) 37 38 var statusGetTests = []struct { 39 args []string 40 format int 41 out interface{} 42 }{ 43 {[]string{"--format", "json", "--include-data"}, formatJson, statusAttributes}, 44 {[]string{"--format", "yaml"}, formatYaml, map[string]interface{}{"status": "error"}}, 45 {[]string{}, -1, "error\n"}, 46 } 47 48 func setFakeStatus(ctx *Context) { 49 ctx.SetUnitStatus(jujuc.StatusInfo{ 50 Status: statusAttributes["status"].(string), 51 Info: statusAttributes["message"].(string), 52 Data: statusAttributes["status-data"].(map[string]interface{}), 53 }) 54 } 55 56 func setFakeServiceStatus(ctx *Context) { 57 ctx.info.Status.SetServiceStatus( 58 jujuc.StatusInfo{ 59 Status: "active", 60 Info: "this is a service status", 61 Data: nil, 62 }, 63 []jujuc.StatusInfo{{ 64 Status: "active", 65 Info: "this is a unit status", 66 Data: nil, 67 }}, 68 ) 69 } 70 71 func (s *statusGetSuite) TestOutputFormatJustStatus(c *gc.C) { 72 for i, t := range statusGetTests { 73 c.Logf("test %d: %#v", i, t.args) 74 hctx := s.GetStatusHookContext(c) 75 setFakeStatus(hctx) 76 com, err := jujuc.NewCommand(hctx, cmdString("status-get")) 77 c.Assert(err, jc.ErrorIsNil) 78 ctx := testing.Context(c) 79 code := cmd.Main(com, ctx, t.args) 80 c.Assert(code, gc.Equals, 0) 81 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 82 83 var out interface{} 84 var outMap map[string]interface{} 85 switch t.format { 86 case formatYaml: 87 c.Check(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil) 88 out = outMap 89 case formatJson: 90 c.Check(json.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil) 91 out = outMap 92 default: 93 out = string(bufferBytes(ctx.Stdout)) 94 } 95 c.Check(out, gc.DeepEquals, t.out) 96 } 97 } 98 99 func (s *statusGetSuite) TestHelp(c *gc.C) { 100 hctx := s.GetStatusHookContext(c) 101 com, err := jujuc.NewCommand(hctx, cmdString("status-get")) 102 c.Assert(err, jc.ErrorIsNil) 103 ctx := testing.Context(c) 104 code := cmd.Main(com, ctx, []string{"--help"}) 105 c.Assert(code, gc.Equals, 0) 106 expectedHelp := "" + 107 "Usage: status-get [options] [--include-data] [--service]\n" + 108 "\n" + 109 "Summary:\n" + 110 "print status information\n" + 111 "\n" + 112 "Options:\n" + 113 "--format (= smart)\n" + 114 " Specify output format (json|smart|yaml)\n" + 115 "--include-data (= false)\n" + 116 " print all status data\n" + 117 "-o, --output (= \"\")\n" + 118 " Specify an output file\n" + 119 "--service (= false)\n" + 120 " print status for all units of this service if this unit is the leader\n" + 121 "\n" + 122 "Details:\n" + 123 "By default, only the status value is printed.\n" + 124 "If the --include-data flag is passed, the associated data are printed also.\n" 125 126 c.Assert(bufferString(ctx.Stdout), gc.Equals, expectedHelp) 127 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 128 } 129 130 func (s *statusGetSuite) TestOutputPath(c *gc.C) { 131 hctx := s.GetStatusHookContext(c) 132 setFakeStatus(hctx) 133 com, err := jujuc.NewCommand(hctx, cmdString("status-get")) 134 c.Assert(err, jc.ErrorIsNil) 135 ctx := testing.Context(c) 136 code := cmd.Main(com, ctx, []string{"--format", "json", "--output", "some-file", "--include-data"}) 137 c.Assert(code, gc.Equals, 0) 138 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 139 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 140 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) 141 c.Assert(err, jc.ErrorIsNil) 142 143 var out map[string]interface{} 144 c.Assert(json.Unmarshal(content, &out), gc.IsNil) 145 c.Assert(out, gc.DeepEquals, statusAttributes) 146 } 147 148 func (s *statusGetSuite) TestServiceStatus(c *gc.C) { 149 expected := map[string]interface{}{ 150 "service-status": map[interface{}]interface{}{ 151 "status-data": map[interface{}]interface{}{}, 152 "units": map[interface{}]interface{}{ 153 "": map[interface{}]interface{}{ 154 "message": "this is a unit status", 155 "status": "active", 156 "status-data": map[interface{}]interface{}{}, 157 }, 158 }, 159 "message": "this is a service status", 160 "status": "active"}, 161 } 162 hctx := s.GetStatusHookContext(c) 163 setFakeServiceStatus(hctx) 164 com, err := jujuc.NewCommand(hctx, cmdString("status-get")) 165 c.Assert(err, jc.ErrorIsNil) 166 ctx := testing.Context(c) 167 code := cmd.Main(com, ctx, []string{"--format", "json", "--include-data", "--service"}) 168 c.Assert(code, gc.Equals, 0) 169 170 var out map[string]interface{} 171 c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil) 172 c.Assert(out, gc.DeepEquals, expected) 173 174 }