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