github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/charm-admin/config_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/cmd"
    14  	"launchpad.net/juju-core/testing"
    15  	"launchpad.net/juju-core/testing/testbase"
    16  )
    17  
    18  type ConfigSuite struct {
    19  	testbase.LoggingSuite
    20  }
    21  
    22  var _ = gc.Suite(&ConfigSuite{})
    23  
    24  const testConfig = `
    25  mongo-url: localhost:23456
    26  foo: 1
    27  bar: false
    28  `
    29  
    30  func (s *ConfigSuite) SetUpSuite(c *gc.C) {
    31  	s.LoggingSuite.SetUpSuite(c)
    32  }
    33  
    34  func (s *ConfigSuite) TearDownSuite(c *gc.C) {
    35  	s.LoggingSuite.TearDownSuite(c)
    36  }
    37  
    38  type SomeConfigCommand struct {
    39  	ConfigCommand
    40  }
    41  
    42  func (c *SomeConfigCommand) Info() *cmd.Info {
    43  	return &cmd.Info{
    44  		Name:    "some-cmd",
    45  		Purpose: "something in particular that requires configuration",
    46  	}
    47  }
    48  
    49  func (c *SomeConfigCommand) Run(ctx *cmd.Context) error {
    50  	return c.ReadConfig(ctx)
    51  }
    52  
    53  func (s *ConfigSuite) TestReadConfig(c *gc.C) {
    54  	confDir := c.MkDir()
    55  	f, err := os.Create(path.Join(confDir, "charmd.conf"))
    56  	c.Assert(err, gc.IsNil)
    57  	cfgPath := f.Name()
    58  	{
    59  		defer f.Close()
    60  		fmt.Fprint(f, testConfig)
    61  	}
    62  
    63  	config := &SomeConfigCommand{}
    64  	args := []string{"--config", cfgPath}
    65  	err = testing.InitCommand(config, args)
    66  	c.Assert(err, gc.IsNil)
    67  	_, err = testing.RunCommand(c, config, args)
    68  	c.Assert(err, gc.IsNil)
    69  
    70  	c.Assert(config.Config, gc.NotNil)
    71  	c.Assert(config.Config.MongoURL, gc.Equals, "localhost:23456")
    72  }