launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/charm-admin/deletecharm_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path"
    10  
    11  	gc "launchpad.net/gocheck"
    12  
    13  	"launchpad.net/juju-core/charm"
    14  	"launchpad.net/juju-core/store"
    15  	"launchpad.net/juju-core/testing"
    16  	"launchpad.net/juju-core/testing/testbase"
    17  )
    18  
    19  type DeleteCharmSuite struct {
    20  	testbase.LoggingSuite
    21  }
    22  
    23  var _ = gc.Suite(&DeleteCharmSuite{})
    24  
    25  const testDeleteCharm = `
    26  mongo-url: localhost:23456
    27  `
    28  
    29  func (s *DeleteCharmSuite) SetUpSuite(c *gc.C) {
    30  	s.LoggingSuite.SetUpSuite(c)
    31  }
    32  
    33  func (s *DeleteCharmSuite) TearDownSuite(c *gc.C) {
    34  	s.LoggingSuite.TearDownSuite(c)
    35  }
    36  
    37  func (s *DeleteCharmSuite) TestInit(c *gc.C) {
    38  	config := &DeleteCharmCommand{}
    39  	err := testing.InitCommand(config, []string{"--config", "/etc/charmd.conf", "--url", "cs:go"})
    40  	c.Assert(err, gc.IsNil)
    41  	c.Assert(config.ConfigPath, gc.Equals, "/etc/charmd.conf")
    42  	c.Assert(config.Url, gc.Equals, "cs:go")
    43  }
    44  
    45  func (s *DeleteCharmSuite) TestRun(c *gc.C) {
    46  	// Derive config file from test mongo port
    47  	confDir := c.MkDir()
    48  	f, err := os.Create(path.Join(confDir, "charmd.conf"))
    49  	c.Assert(err, gc.IsNil)
    50  	configPath := f.Name()
    51  	{
    52  		defer f.Close()
    53  		fmt.Fprintf(f, "mongo-url: %s\n", testing.MgoServer.Addr())
    54  	}
    55  	// Delete charm that does not exist, not found error.
    56  	config := &DeleteCharmCommand{}
    57  	out, err := testing.RunCommand(c, config,
    58  		[]string{"--config", configPath, "--url", "cs:unreleased/foo"})
    59  	fmt.Println(out)
    60  	c.Assert(err, gc.NotNil)
    61  	// Publish that charm now
    62  	url := charm.MustParseURL("cs:unreleased/foo")
    63  	{
    64  		s, err := store.Open(testing.MgoServer.Addr())
    65  		defer s.Close()
    66  		c.Assert(err, gc.IsNil)
    67  		pub, err := s.CharmPublisher([]*charm.URL{url}, "such-digest-much-unique")
    68  		c.Assert(err, gc.IsNil)
    69  		err = pub.Publish(testing.Charms.ClonedDir(c.MkDir(), "dummy"))
    70  		c.Assert(err, gc.IsNil)
    71  	}
    72  	// Delete charm, should now succeed
    73  	_, err = testing.RunCommand(c, config,
    74  		[]string{"--config", configPath, "--url", "cs:unreleased/foo"})
    75  	c.Assert(err, gc.IsNil)
    76  	c.Assert(config.Config, gc.NotNil)
    77  	// Confirm that the charm is gone
    78  	{
    79  		s, err := store.Open(testing.MgoServer.Addr())
    80  		defer s.Close()
    81  		c.Assert(err, gc.IsNil)
    82  		_, err = s.CharmInfo(url)
    83  		c.Assert(err, gc.NotNil)
    84  	}
    85  }