github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/charms/client_test.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package charms_test 5 6 import ( 7 "github.com/juju/juju/api" 8 "github.com/juju/juju/apiserver/charms" 9 "github.com/juju/juju/apiserver/common" 10 "github.com/juju/juju/apiserver/params" 11 "github.com/juju/juju/apiserver/testing" 12 jujutesting "github.com/juju/juju/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 "gopkg.in/juju/charm.v4" 16 ) 17 18 type baseCharmsSuite struct { 19 // TODO(anastasiamac) mock to remove JujuConnSuite 20 jujutesting.JujuConnSuite 21 } 22 23 type charmsSuite struct { 24 baseCharmsSuite 25 api *charms.API 26 } 27 28 var _ = gc.Suite(&charmsSuite{}) 29 30 func (s *charmsSuite) SetUpTest(c *gc.C) { 31 s.baseCharmsSuite.SetUpTest(c) 32 33 var err error 34 auth := testing.FakeAuthorizer{ 35 Tag: s.AdminUserTag(c), 36 EnvironManager: true, 37 } 38 s.api, err = charms.NewAPI(s.State, common.NewResources(), auth) 39 c.Assert(err, jc.ErrorIsNil) 40 } 41 42 var _ = gc.Suite(&baseCharmsSuite{}) 43 44 func (s *baseCharmsSuite) TestClientCharmInfo(c *gc.C) { 45 var clientCharmInfoTests = []struct { 46 about string 47 charm string 48 url string 49 expectedActions *charm.Actions 50 err string 51 }{ 52 { 53 about: "dummy charm which contains an expectedActions spec", 54 charm: "dummy", 55 url: "local:quantal/dummy-1", 56 expectedActions: &charm.Actions{ 57 ActionSpecs: map[string]charm.ActionSpec{ 58 "snapshot": { 59 Description: "Take a snapshot of the database.", 60 Params: map[string]interface{}{ 61 "type": "object", 62 "title": "snapshot", 63 "description": "Take a snapshot of the database.", 64 "properties": map[string]interface{}{ 65 "outfile": map[string]interface{}{ 66 "default": "foo.bz2", 67 "description": "The file to write out to.", 68 "type": "string", 69 }, 70 }, 71 }, 72 }, 73 }, 74 }, 75 }, 76 { 77 about: "retrieves charm info", 78 // Use wordpress for tests so that we can compare Provides and Requires. 79 charm: "wordpress", 80 expectedActions: &charm.Actions{ActionSpecs: map[string]charm.ActionSpec{ 81 "fakeaction": { 82 Description: "No description", 83 Params: map[string]interface{}{ 84 "type": "object", 85 "title": "fakeaction", 86 "description": "No description", 87 "properties": map[string]interface{}{}, 88 }, 89 }, 90 }}, 91 url: "local:quantal/wordpress-3", 92 }, 93 { 94 about: "invalid URL", 95 charm: "wordpress", 96 expectedActions: &charm.Actions{ActionSpecs: nil}, 97 url: "not-valid", 98 err: "charm url series is not resolved", 99 }, 100 { 101 about: "invalid schema", 102 charm: "wordpress", 103 expectedActions: &charm.Actions{ActionSpecs: nil}, 104 url: "not-valid:your-arguments", 105 err: `charm URL has invalid schema: "not-valid:your-arguments"`, 106 }, 107 { 108 about: "unknown charm", 109 charm: "wordpress", 110 expectedActions: &charm.Actions{ActionSpecs: nil}, 111 url: "cs:missing/one-1", 112 err: `charm "cs:missing/one-1" not found`, 113 }, 114 } 115 116 for i, t := range clientCharmInfoTests { 117 c.Logf("test %d. %s", i, t.about) 118 aCharm := s.AddTestingCharm(c, t.charm) 119 info, err := s.APIState.Client().CharmInfo(t.url) 120 if t.err != "" { 121 c.Check(err, gc.ErrorMatches, t.err) 122 continue 123 } 124 c.Assert(err, jc.ErrorIsNil) 125 expected := &api.CharmInfo{ 126 Revision: aCharm.Revision(), 127 URL: aCharm.URL().String(), 128 Config: aCharm.Config(), 129 Meta: aCharm.Meta(), 130 Actions: aCharm.Actions(), 131 } 132 c.Check(info, jc.DeepEquals, expected) 133 c.Check(info.Actions, jc.DeepEquals, t.expectedActions) 134 } 135 } 136 func (s *charmsSuite) TestListCharmsNoFilter(c *gc.C) { 137 s.assertListCharms(c, []string{"dummy"}, []string{}, []string{"local:quantal/dummy-1"}) 138 } 139 140 func (s *charmsSuite) TestListCharmsWithFilterMatchingNone(c *gc.C) { 141 s.assertListCharms(c, []string{"dummy"}, []string{"notdummy"}, []string{}) 142 } 143 144 func (s *charmsSuite) TestListCharmsFilteredOnly(c *gc.C) { 145 s.assertListCharms(c, []string{"dummy", "wordpress"}, []string{"dummy"}, []string{"local:quantal/dummy-1"}) 146 } 147 148 func (s *charmsSuite) assertListCharms(c *gc.C, someCharms, args, expected []string) { 149 for _, aCharm := range someCharms { 150 s.AddTestingCharm(c, aCharm) 151 } 152 found, err := s.api.List(params.CharmsList{Names: args}) 153 c.Assert(err, jc.ErrorIsNil) 154 c.Check(found.CharmURLs, gc.HasLen, len(expected)) 155 c.Check(found.CharmURLs, jc.DeepEquals, expected) 156 }