github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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.v1" 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 "purpose: print status information\n" + 109 "\n" + 110 "options:\n" + 111 "--format (= smart)\n" + 112 " specify output format (json|smart|yaml)\n" + 113 "--include-data (= false)\n" + 114 " print all status data\n" + 115 "-o, --output (= \"\")\n" + 116 " specify an output file\n" + 117 "--service (= false)\n" + 118 " print status for all units of this service if this unit is the leader\n" + 119 "\n" + 120 "By default, only the status value is printed.\n" + 121 "If the --include-data flag is passed, the associated data are printed also.\n" 122 123 c.Assert(bufferString(ctx.Stdout), gc.Equals, expectedHelp) 124 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 125 } 126 127 func (s *statusGetSuite) TestOutputPath(c *gc.C) { 128 hctx := s.GetStatusHookContext(c) 129 setFakeStatus(hctx) 130 com, err := jujuc.NewCommand(hctx, cmdString("status-get")) 131 c.Assert(err, jc.ErrorIsNil) 132 ctx := testing.Context(c) 133 code := cmd.Main(com, ctx, []string{"--format", "json", "--output", "some-file", "--include-data"}) 134 c.Assert(code, gc.Equals, 0) 135 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 136 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 137 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) 138 c.Assert(err, jc.ErrorIsNil) 139 140 var out map[string]interface{} 141 c.Assert(json.Unmarshal(content, &out), gc.IsNil) 142 c.Assert(out, gc.DeepEquals, statusAttributes) 143 } 144 145 func (s *statusGetSuite) TestServiceStatus(c *gc.C) { 146 expected := map[string]interface{}{ 147 "service-status": map[interface{}]interface{}{ 148 "status-data": map[interface{}]interface{}{}, 149 "units": map[interface{}]interface{}{ 150 "": map[interface{}]interface{}{ 151 "message": "this is a unit status", 152 "status": "active", 153 "status-data": map[interface{}]interface{}{}, 154 }, 155 }, 156 "message": "this is a service status", 157 "status": "active"}, 158 } 159 hctx := s.GetStatusHookContext(c) 160 setFakeServiceStatus(hctx) 161 com, err := jujuc.NewCommand(hctx, cmdString("status-get")) 162 c.Assert(err, jc.ErrorIsNil) 163 ctx := testing.Context(c) 164 code := cmd.Main(com, ctx, []string{"--format", "json", "--include-data", "--service"}) 165 c.Assert(code, gc.Equals, 0) 166 167 var out map[string]interface{} 168 c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil) 169 c.Assert(out, gc.DeepEquals, expected) 170 171 }