github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/charm_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	"bytes"
     8  
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/charm.v6-unstable"
    13  
    14  	"github.com/juju/juju/state"
    15  	"github.com/juju/juju/testcharms"
    16  )
    17  
    18  type CharmSuite struct {
    19  	ConnSuite
    20  	curl *charm.URL
    21  }
    22  
    23  var _ = gc.Suite(&CharmSuite{})
    24  
    25  func (s *CharmSuite) SetUpTest(c *gc.C) {
    26  	s.ConnSuite.SetUpTest(c)
    27  	added := s.AddTestingCharm(c, "dummy")
    28  	s.curl = added.URL()
    29  }
    30  
    31  func (s *CharmSuite) TestCharm(c *gc.C) {
    32  	dummy, err := s.State.Charm(s.curl)
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	c.Assert(dummy.URL().String(), gc.Equals, s.curl.String())
    35  	c.Assert(dummy.Revision(), gc.Equals, 1)
    36  	c.Assert(dummy.StoragePath(), gc.Equals, "dummy-path")
    37  	c.Assert(dummy.BundleSha256(), gc.Equals, "quantal-dummy-1-sha256")
    38  	c.Assert(dummy.IsUploaded(), jc.IsTrue)
    39  	meta := dummy.Meta()
    40  	c.Assert(meta.Name, gc.Equals, "dummy")
    41  	config := dummy.Config()
    42  	c.Assert(config.Options["title"], gc.Equals,
    43  		charm.Option{
    44  			Default:     "My Title",
    45  			Description: "A descriptive title used for the service.",
    46  			Type:        "string",
    47  		},
    48  	)
    49  	actions := dummy.Actions()
    50  	c.Assert(actions, gc.NotNil)
    51  	c.Assert(actions.ActionSpecs, gc.Not(gc.HasLen), 0)
    52  	c.Assert(actions.ActionSpecs["snapshot"], gc.NotNil)
    53  	c.Assert(actions.ActionSpecs["snapshot"].Params, gc.Not(gc.HasLen), 0)
    54  	c.Assert(actions.ActionSpecs["snapshot"], gc.DeepEquals,
    55  		charm.ActionSpec{
    56  			Description: "Take a snapshot of the database.",
    57  			Params: map[string]interface{}{
    58  				"type":        "object",
    59  				"title":       "snapshot",
    60  				"description": "Take a snapshot of the database.",
    61  				"properties": map[string]interface{}{
    62  					"outfile": map[string]interface{}{
    63  						"description": "The file to write out to.",
    64  						"type":        "string",
    65  						"default":     "foo.bz2",
    66  					},
    67  				},
    68  			},
    69  		})
    70  }
    71  
    72  func (s *CharmSuite) TestCharmNotFound(c *gc.C) {
    73  	curl := charm.MustParseURL("local:anotherseries/dummy-1")
    74  	_, err := s.State.Charm(curl)
    75  	c.Assert(err, gc.ErrorMatches, `charm "local:anotherseries/dummy-1" not found`)
    76  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    77  }
    78  
    79  type CharmTestHelperSuite struct {
    80  	ConnSuite
    81  }
    82  
    83  var _ = gc.Suite(&CharmTestHelperSuite{})
    84  
    85  func assertCustomCharm(c *gc.C, ch *state.Charm, series string, meta *charm.Meta, config *charm.Config, metrics *charm.Metrics, revision int) {
    86  	// Check Charm interface method results.
    87  	c.Assert(ch.Meta(), gc.DeepEquals, meta)
    88  	c.Assert(ch.Config(), gc.DeepEquals, config)
    89  	c.Assert(ch.Metrics(), gc.DeepEquals, metrics)
    90  	c.Assert(ch.Revision(), gc.DeepEquals, revision)
    91  
    92  	// Test URL matches charm and expected series.
    93  	url := ch.URL()
    94  	c.Assert(url.Series, gc.Equals, series)
    95  	c.Assert(url.Revision, gc.Equals, ch.Revision())
    96  
    97  	// Ignore the StoragePath and BundleSHA256 methods, they're irrelevant.
    98  }
    99  
   100  func assertStandardCharm(c *gc.C, ch *state.Charm, series string) {
   101  	chd := testcharms.Repo.CharmDir(ch.Meta().Name)
   102  	assertCustomCharm(c, ch, series, chd.Meta(), chd.Config(), chd.Metrics(), chd.Revision())
   103  }
   104  
   105  func forEachStandardCharm(c *gc.C, f func(name string)) {
   106  	for _, name := range []string{
   107  		"logging", "mysql", "riak", "wordpress",
   108  	} {
   109  		c.Logf("checking %s", name)
   110  		f(name)
   111  	}
   112  }
   113  
   114  func (s *CharmTestHelperSuite) TestSimple(c *gc.C) {
   115  	forEachStandardCharm(c, func(name string) {
   116  		chd := testcharms.Repo.CharmDir(name)
   117  		meta := chd.Meta()
   118  		config := chd.Config()
   119  		metrics := chd.Metrics()
   120  		revision := chd.Revision()
   121  
   122  		ch := s.AddTestingCharm(c, name)
   123  		assertCustomCharm(c, ch, "quantal", meta, config, metrics, revision)
   124  
   125  		ch = s.AddSeriesCharm(c, name, "anotherseries")
   126  		assertCustomCharm(c, ch, "anotherseries", meta, config, metrics, revision)
   127  	})
   128  }
   129  
   130  var configYaml = `
   131  options:
   132    working:
   133      description: when set to false, prevents service from functioning correctly
   134      default: true
   135      type: boolean
   136  `
   137  
   138  func (s *CharmTestHelperSuite) TestConfigCharm(c *gc.C) {
   139  	config, err := charm.ReadConfig(bytes.NewBuffer([]byte(configYaml)))
   140  	c.Assert(err, jc.ErrorIsNil)
   141  
   142  	forEachStandardCharm(c, func(name string) {
   143  		chd := testcharms.Repo.CharmDir(name)
   144  		meta := chd.Meta()
   145  		metrics := chd.Metrics()
   146  
   147  		ch := s.AddConfigCharm(c, name, configYaml, 123)
   148  		assertCustomCharm(c, ch, "quantal", meta, config, metrics, 123)
   149  	})
   150  }
   151  
   152  var actionsYaml = `
   153  actions:
   154     dump:
   155        description: Dump the database to STDOUT.
   156        params:
   157           redirect-file:
   158              description: Redirect to a log file.
   159              type: string
   160  `
   161  
   162  func (s *CharmTestHelperSuite) TestActionsCharm(c *gc.C) {
   163  	actions, err := charm.ReadActionsYaml(bytes.NewBuffer([]byte(actionsYaml)))
   164  	c.Assert(err, jc.ErrorIsNil)
   165  
   166  	forEachStandardCharm(c, func(name string) {
   167  		ch := s.AddActionsCharm(c, name, actionsYaml, 123)
   168  		c.Assert(ch.Actions(), gc.DeepEquals, actions)
   169  	})
   170  }
   171  
   172  var metricsYaml = `
   173  metrics:
   174    blips:
   175      description: A custom metric.
   176      type: gauge
   177  `
   178  
   179  func (s *CharmTestHelperSuite) TestMetricsCharm(c *gc.C) {
   180  	metrics, err := charm.ReadMetrics(bytes.NewBuffer([]byte(metricsYaml)))
   181  	c.Assert(err, jc.ErrorIsNil)
   182  
   183  	forEachStandardCharm(c, func(name string) {
   184  		chd := testcharms.Repo.CharmDir(name)
   185  		meta := chd.Meta()
   186  		config := chd.Config()
   187  
   188  		ch := s.AddMetricsCharm(c, name, metricsYaml, 123)
   189  		assertCustomCharm(c, ch, "quantal", meta, config, metrics, 123)
   190  	})
   191  }
   192  
   193  var metaYamlSnippet = `
   194  summary: blah
   195  description: blah blah
   196  `
   197  
   198  func (s *CharmTestHelperSuite) TestMetaCharm(c *gc.C) {
   199  	forEachStandardCharm(c, func(name string) {
   200  		chd := testcharms.Repo.CharmDir(name)
   201  		config := chd.Config()
   202  		metrics := chd.Metrics()
   203  		metaYaml := "name: " + name + metaYamlSnippet
   204  		meta, err := charm.ReadMeta(bytes.NewBuffer([]byte(metaYaml)))
   205  		c.Assert(err, jc.ErrorIsNil)
   206  
   207  		ch := s.AddMetaCharm(c, name, metaYaml, 123)
   208  		assertCustomCharm(c, ch, "quantal", meta, config, metrics, 123)
   209  	})
   210  }
   211  
   212  func (s *CharmTestHelperSuite) TestTestingCharm(c *gc.C) {
   213  	added := s.AddTestingCharm(c, "metered")
   214  	c.Assert(added.Metrics(), gc.NotNil)
   215  
   216  	chd := testcharms.Repo.CharmDir("metered")
   217  	c.Assert(chd.Metrics(), gc.DeepEquals, added.Metrics())
   218  }