github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  			expectedActions: &charm.Actions{ActionSpecs: nil},
    99  			url:             "not-valid",
   100  			err:             "charm or bundle url series is not resolved",
   101  		},
   102  		{
   103  			about:           "invalid schema",
   104  			charm:           "wordpress",
   105  			expectedActions: &charm.Actions{ActionSpecs: nil},
   106  			url:             "not-valid:your-arguments",
   107  			err:             `charm or bundle URL has invalid schema: "not-valid:your-arguments"`,
   108  		},
   109  		{
   110  			about:           "unknown charm",
   111  			charm:           "wordpress",
   112  			expectedActions: &charm.Actions{ActionSpecs: nil},
   113  			url:             "cs:missing/one-1",
   114  			err:             `charm "cs:missing/one-1" not found`,
   115  		},
   116  	}
   117  
   118  	for i, t := range clientCharmInfoTests {
   119  		c.Logf("test %d. %s", i, t.about)
   120  		aCharm := s.AddTestingCharm(c, t.charm)
   121  		info, err := s.APIState.Client().CharmInfo(t.url)
   122  		if t.err != "" {
   123  			c.Check(err, gc.ErrorMatches, t.err)
   124  			continue
   125  		}
   126  		c.Assert(err, jc.ErrorIsNil)
   127  		expected := &api.CharmInfo{
   128  			Revision: aCharm.Revision(),
   129  			URL:      aCharm.URL().String(),
   130  			Config:   aCharm.Config(),
   131  			Meta:     aCharm.Meta(),
   132  			Actions:  aCharm.Actions(),
   133  		}
   134  		c.Check(info, jc.DeepEquals, expected)
   135  		c.Check(info.Actions, jc.DeepEquals, t.expectedActions)
   136  	}
   137  }
   138  func (s *charmsSuite) TestListCharmsNoFilter(c *gc.C) {
   139  	s.assertListCharms(c, []string{"dummy"}, []string{}, []string{"local:quantal/dummy-1"})
   140  }
   141  
   142  func (s *charmsSuite) TestListCharmsWithFilterMatchingNone(c *gc.C) {
   143  	s.assertListCharms(c, []string{"dummy"}, []string{"notdummy"}, []string{})
   144  }
   145  
   146  func (s *charmsSuite) TestListCharmsFilteredOnly(c *gc.C) {
   147  	s.assertListCharms(c, []string{"dummy", "wordpress"}, []string{"dummy"}, []string{"local:quantal/dummy-1"})
   148  }
   149  
   150  func (s *charmsSuite) assertListCharms(c *gc.C, someCharms, args, expected []string) {
   151  	for _, aCharm := range someCharms {
   152  		s.AddTestingCharm(c, aCharm)
   153  	}
   154  	found, err := s.api.List(params.CharmsList{Names: args})
   155  	c.Assert(err, jc.ErrorIsNil)
   156  	c.Check(found.CharmURLs, gc.HasLen, len(expected))
   157  	c.Check(found.CharmURLs, jc.DeepEquals, expected)
   158  }
   159  
   160  func (s *charmsSuite) TestIsMeteredFalse(c *gc.C) {
   161  	charm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "wordpress"})
   162  	metered, err := s.api.IsMetered(params.CharmInfo{
   163  		CharmURL: charm.URL().String(),
   164  	})
   165  	c.Assert(err, jc.ErrorIsNil)
   166  	c.Assert(metered.Metered, jc.IsFalse)
   167  }
   168  
   169  func (s *charmsSuite) TestIsMeteredTrue(c *gc.C) {
   170  	meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "cs:quantal/metered"})
   171  	metered, err := s.api.IsMetered(params.CharmInfo{
   172  		CharmURL: meteredCharm.URL().String(),
   173  	})
   174  	c.Assert(err, jc.ErrorIsNil)
   175  	c.Assert(metered.Metered, jc.IsTrue)
   176  }