github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/resource/api/client/client_listresources_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package client_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/apiserver/params" 12 "github.com/juju/juju/resource" 13 "github.com/juju/juju/resource/api/client" 14 ) 15 16 var _ = gc.Suite(&ListResourcesSuite{}) 17 18 type ListResourcesSuite struct { 19 BaseSuite 20 } 21 22 func (s *ListResourcesSuite) TestOkay(c *gc.C) { 23 expected, apiResult := newResourceResult(c, "a-application", "spam") 24 s.facade.apiResults["a-application"] = apiResult 25 26 cl := client.NewClient(s.facade, s, s.facade) 27 28 services := []string{"a-application"} 29 results, err := cl.ListResources(services) 30 c.Assert(err, jc.ErrorIsNil) 31 32 c.Check(results, jc.DeepEquals, []resource.ApplicationResources{ 33 {Resources: expected}, 34 }) 35 c.Check(s.stub.Calls(), gc.HasLen, 1) 36 s.stub.CheckCall(c, 0, "FacadeCall", 37 "ListResources", 38 ¶ms.ListResourcesArgs{[]params.Entity{{ 39 Tag: "application-a-application", 40 }}}, 41 ¶ms.ResourcesResults{ 42 Results: []params.ResourcesResult{ 43 apiResult, 44 }, 45 }, 46 ) 47 } 48 49 func (s *ListResourcesSuite) TestBulk(c *gc.C) { 50 expected1, apiResult1 := newResourceResult(c, "a-application", "spam") 51 s.facade.apiResults["a-application"] = apiResult1 52 expected2, apiResult2 := newResourceResult(c, "other-application", "eggs", "ham") 53 s.facade.apiResults["other-application"] = apiResult2 54 55 cl := client.NewClient(s.facade, s, s.facade) 56 57 services := []string{"a-application", "other-application"} 58 results, err := cl.ListResources(services) 59 c.Assert(err, jc.ErrorIsNil) 60 61 c.Check(results, jc.DeepEquals, []resource.ApplicationResources{ 62 {Resources: expected1}, 63 {Resources: expected2}, 64 }) 65 c.Check(s.stub.Calls(), gc.HasLen, 1) 66 s.stub.CheckCall(c, 0, "FacadeCall", 67 "ListResources", 68 ¶ms.ListResourcesArgs{[]params.Entity{ 69 { 70 Tag: "application-a-application", 71 }, { 72 Tag: "application-other-application", 73 }, 74 }}, 75 ¶ms.ResourcesResults{ 76 Results: []params.ResourcesResult{ 77 apiResult1, 78 apiResult2, 79 }, 80 }, 81 ) 82 } 83 84 func (s *ListResourcesSuite) TestNoServices(c *gc.C) { 85 cl := client.NewClient(s.facade, s, s.facade) 86 87 var services []string 88 results, err := cl.ListResources(services) 89 c.Assert(err, jc.ErrorIsNil) 90 91 c.Check(results, gc.HasLen, 0) 92 s.stub.CheckCallNames(c, "FacadeCall") 93 } 94 95 func (s *ListResourcesSuite) TestBadServices(c *gc.C) { 96 cl := client.NewClient(s.facade, s, s.facade) 97 98 services := []string{"???"} 99 _, err := cl.ListResources(services) 100 101 c.Check(err, gc.ErrorMatches, `.*invalid application.*`) 102 s.stub.CheckNoCalls(c) 103 } 104 105 func (s *ListResourcesSuite) TestServiceNotFound(c *gc.C) { 106 cl := client.NewClient(s.facade, s, s.facade) 107 108 services := []string{"a-application"} 109 _, err := cl.ListResources(services) 110 111 c.Check(err, jc.Satisfies, errors.IsNotFound) 112 s.stub.CheckCallNames(c, "FacadeCall") 113 } 114 115 func (s *ListResourcesSuite) TestServiceEmpty(c *gc.C) { 116 s.facade.apiResults["a-application"] = params.ResourcesResult{} 117 118 cl := client.NewClient(s.facade, s, s.facade) 119 120 services := []string{"a-application"} 121 results, err := cl.ListResources(services) 122 c.Assert(err, jc.ErrorIsNil) 123 124 c.Check(results, jc.DeepEquals, []resource.ApplicationResources{ 125 {}, 126 }) 127 s.stub.CheckCallNames(c, "FacadeCall") 128 } 129 130 func (s *ListResourcesSuite) TestServerError(c *gc.C) { 131 failure := errors.New("<failure>") 132 s.facade.FacadeCallFn = func(_ string, _, _ interface{}) error { 133 return failure 134 } 135 136 cl := client.NewClient(s.facade, s, s.facade) 137 138 services := []string{"a-application"} 139 _, err := cl.ListResources(services) 140 141 c.Check(err, gc.ErrorMatches, `<failure>`) 142 s.stub.CheckCallNames(c, "FacadeCall") 143 } 144 145 func (s *ListResourcesSuite) TestTooFew(c *gc.C) { 146 s.facade.FacadeCallFn = func(_ string, _, response interface{}) error { 147 typedResponse, ok := response.(*params.ResourcesResults) 148 c.Assert(ok, jc.IsTrue) 149 150 typedResponse.Results = []params.ResourcesResult{{ 151 Resources: nil, 152 }} 153 154 return nil 155 } 156 157 cl := client.NewClient(s.facade, s, s.facade) 158 159 services := []string{"a-application", "other-application"} 160 results, err := cl.ListResources(services) 161 162 c.Check(results, gc.HasLen, 0) 163 c.Check(err, gc.ErrorMatches, `.*got invalid data from server \(expected 2 results, got 1\).*`) 164 s.stub.CheckCallNames(c, "FacadeCall") 165 } 166 167 func (s *ListResourcesSuite) TestTooMany(c *gc.C) { 168 s.facade.FacadeCallFn = func(_ string, _, response interface{}) error { 169 typedResponse, ok := response.(*params.ResourcesResults) 170 c.Assert(ok, jc.IsTrue) 171 172 typedResponse.Results = []params.ResourcesResult{{ 173 Resources: nil, 174 }, { 175 Resources: nil, 176 }, { 177 Resources: nil, 178 }} 179 180 return nil 181 } 182 183 cl := client.NewClient(s.facade, s, s.facade) 184 185 services := []string{"a-application", "other-application"} 186 results, err := cl.ListResources(services) 187 188 c.Check(results, gc.HasLen, 0) 189 c.Check(err, gc.ErrorMatches, `.*got invalid data from server \(expected 2 results, got 3\).*`) 190 s.stub.CheckCallNames(c, "FacadeCall") 191 } 192 193 func (s *ListResourcesSuite) TestConversionFailed(c *gc.C) { 194 s.facade.FacadeCallFn = func(_ string, _, response interface{}) error { 195 typedResponse, ok := response.(*params.ResourcesResults) 196 c.Assert(ok, jc.IsTrue) 197 198 var res params.Resource 199 res.Name = "spam" 200 typedResponse.Results = []params.ResourcesResult{{ 201 Resources: []params.Resource{ 202 res, 203 }, 204 }} 205 206 return nil 207 } 208 209 cl := client.NewClient(s.facade, s, s.facade) 210 211 services := []string{"a-application"} 212 _, err := cl.ListResources(services) 213 214 c.Check(err, gc.ErrorMatches, `.*got bad data.*`) 215 s.stub.CheckCallNames(c, "FacadeCall") 216 }