func (s *suite) TestGet(c *gc.C) { ch := charmRepo.CharmDir("wordpress") url := charm.MustParseReference("~charmers/utopic/wordpress-42") err := s.client.UploadCharmWithRevision(url, ch, 42) c.Assert(err, gc.IsNil) for i, test := range getTests { c.Logf("test %d: %s", i, test.about) // Send the request. var result json.RawMessage var resultPtr interface{} if !test.nilResult { resultPtr = &result } err = s.client.Get(test.path, resultPtr) // Check the response. if test.expectError != "" { c.Assert(err, gc.ErrorMatches, test.expectError, gc.Commentf("error is %T; %#v", err, err)) c.Assert(result, gc.IsNil) cause := errgo.Cause(err) if code, ok := cause.(params.ErrorCode); ok { c.Assert(code, gc.Equals, test.expectErrorCode) } else { c.Assert(test.expectErrorCode, gc.Equals, params.ErrorCode("")) } continue } c.Assert(err, gc.IsNil) if test.expectResult != nil { c.Assert(string(result), jc.JSONEquals, test.expectResult) } } }
func (s *suite) TestPutError(c *gc.C) { err := s.client.UploadCharmWithRevision( charm.MustParseReference("~charmers/utopic/wordpress-42"), charmRepo.CharmDir("wordpress"), 42) c.Assert(err, gc.IsNil) for i, test := range putErrorTests { c.Logf("test %d: %s", i, test.about) err := s.client.Put(test.path, test.val) c.Assert(err, gc.ErrorMatches, test.expectError) cause := errgo.Cause(err) if code, ok := cause.(params.ErrorCode); ok { c.Assert(code, gc.Equals, test.expectErrorCode) } else { c.Assert(test.expectErrorCode, gc.Equals, params.ErrorCode("")) } } }
func (s *suite) TestMeta(c *gc.C) { ch := charmRepo.CharmDir("wordpress") url := charm.MustParseReference("~charmers/utopic/wordpress-42") purl := charm.MustParseReference("utopic/wordpress-42") err := s.client.UploadCharmWithRevision(url, ch, 42) c.Assert(err, gc.IsNil) // Put some extra-info. err = s.client.PutExtraInfo(url, map[string]interface{}{ "attr": "value", }) c.Assert(err, gc.IsNil) tests := []struct { about string id string expectResult interface{} expectError string expectErrorCode params.ErrorCode }{{ about: "no fields", id: "utopic/wordpress", expectResult: &struct{}{}, }, { about: "single field", id: "utopic/wordpress", expectResult: &struct { CharmMetadata *charm.Meta }{ CharmMetadata: ch.Meta(), }, }, { about: "three fields", id: "wordpress", expectResult: &struct { CharmMetadata *charm.Meta CharmConfig *charm.Config ExtraInfo map[string]string }{ CharmMetadata: ch.Meta(), CharmConfig: ch.Config(), ExtraInfo: map[string]string{"attr": "value"}, }, }, { about: "tagged field", id: "wordpress", expectResult: &struct { Foo *charm.Meta `csclient:"charm-metadata"` Attr string `csclient:"extra-info/attr"` }{ Foo: ch.Meta(), Attr: "value", }, }, { about: "id not found", id: "bogus", expectResult: &struct{}{}, expectError: `cannot get "/bogus/meta/any": no matching charm or bundle for "cs:bogus"`, expectErrorCode: params.ErrNotFound, }, { about: "unmarshal into invalid type", id: "wordpress", expectResult: new(struct { CharmMetadata []string }), expectError: `cannot unmarshal charm-metadata: json: cannot unmarshal object into Go value of type \[]string`, }, { about: "unmarshal into struct with unexported fields", id: "wordpress", expectResult: &struct { unexported int CharmMetadata *charm.Meta // Embedded anonymous fields don't get tagged as unexported // due to https://code.google.com/p/go/issues/detail?id=7247 // TODO fix in go 1.5. // embed }{ CharmMetadata: ch.Meta(), }, }, { about: "metadata not appropriate for charm", id: "wordpress", expectResult: &struct { CharmMetadata *charm.Meta BundleMetadata *charm.BundleData }{ CharmMetadata: ch.Meta(), }, }} for i, test := range tests { c.Logf("test %d: %s", i, test.about) // Make a result value of the same type as the expected result, // but empty. result := reflect.New(reflect.TypeOf(test.expectResult).Elem()).Interface() id, err := s.client.Meta(charm.MustParseReference(test.id), result) if test.expectError != "" { c.Assert(err, gc.ErrorMatches, test.expectError) if code, ok := errgo.Cause(err).(params.ErrorCode); ok { c.Assert(code, gc.Equals, test.expectErrorCode) } else { c.Assert(test.expectErrorCode, gc.Equals, params.ErrorCode("")) } c.Assert(id, gc.IsNil) continue } c.Assert(err, gc.IsNil) c.Assert(id, jc.DeepEquals, purl) c.Assert(result, jc.DeepEquals, test.expectResult) } }