github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cloudconfig/instancecfg/instancecfg_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package instancecfg_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	"github.com/juju/version"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/cloudconfig/instancecfg"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/state/multiwatcher"
    14  	"github.com/juju/juju/testing"
    15  	coretools "github.com/juju/juju/tools"
    16  )
    17  
    18  type instancecfgSuite struct {
    19  	testing.BaseSuite
    20  }
    21  
    22  var _ = gc.Suite(&instancecfgSuite{})
    23  
    24  func (*instancecfgSuite) TestInstanceTagsController(c *gc.C) {
    25  	cfg := testing.CustomModelConfig(c, testing.Attrs{})
    26  	controllerJobs := []multiwatcher.MachineJob{multiwatcher.JobManageModel}
    27  	nonControllerJobs := []multiwatcher.MachineJob{multiwatcher.JobHostUnits}
    28  	testInstanceTags(c, cfg, controllerJobs, map[string]string{
    29  		"juju-model-uuid":      testing.ModelTag.Id(),
    30  		"juju-controller-uuid": testing.ControllerTag.Id(),
    31  		"juju-is-controller":   "true",
    32  	})
    33  	testInstanceTags(c, cfg, nonControllerJobs, map[string]string{
    34  		"juju-model-uuid":      testing.ModelTag.Id(),
    35  		"juju-controller-uuid": testing.ControllerTag.Id(),
    36  	})
    37  }
    38  
    39  func (*instancecfgSuite) TestInstanceTagsUserSpecified(c *gc.C) {
    40  	cfg := testing.CustomModelConfig(c, testing.Attrs{
    41  		"resource-tags": "a=b c=",
    42  	})
    43  	testInstanceTags(c, cfg, nil, map[string]string{
    44  		"juju-model-uuid":      testing.ModelTag.Id(),
    45  		"juju-controller-uuid": testing.ControllerTag.Id(),
    46  		"a": "b",
    47  		"c": "",
    48  	})
    49  }
    50  
    51  func testInstanceTags(c *gc.C, cfg *config.Config, jobs []multiwatcher.MachineJob, expectTags map[string]string) {
    52  	tags := instancecfg.InstanceTags(testing.ModelTag.Id(), testing.ControllerTag.Id(), cfg, jobs)
    53  	c.Assert(tags, jc.DeepEquals, expectTags)
    54  }
    55  
    56  func (*instancecfgSuite) TestAgentVersionZero(c *gc.C) {
    57  	var icfg instancecfg.InstanceConfig
    58  	c.Assert(icfg.AgentVersion(), gc.Equals, version.Binary{})
    59  }
    60  
    61  func (*instancecfgSuite) TestAgentVersion(c *gc.C) {
    62  	var icfg instancecfg.InstanceConfig
    63  	list := coretools.List{
    64  		&coretools.Tools{Version: version.MustParseBinary("2.3.4-trusty-amd64")},
    65  	}
    66  	err := icfg.SetTools(list)
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(icfg.AgentVersion(), gc.Equals, list[0].Version)
    69  }
    70  
    71  func (*instancecfgSuite) TestSetToolsSameVersions(c *gc.C) {
    72  	var icfg instancecfg.InstanceConfig
    73  	list := coretools.List{
    74  		&coretools.Tools{Version: version.MustParseBinary("2.3.4-trusty-amd64")},
    75  		&coretools.Tools{Version: version.MustParseBinary("2.3.4-trusty-amd64")},
    76  	}
    77  	err := icfg.SetTools(list)
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(icfg.ToolsList(), jc.DeepEquals, list)
    80  }
    81  
    82  func (*instancecfgSuite) TestSetToolsDifferentVersions(c *gc.C) {
    83  	var icfg instancecfg.InstanceConfig
    84  	list := coretools.List{
    85  		&coretools.Tools{Version: version.MustParseBinary("2.3.4-trusty-amd64")},
    86  		&coretools.Tools{Version: version.MustParseBinary("2.3.5-trusty-amd64")},
    87  	}
    88  	err := icfg.SetTools(list)
    89  	c.Assert(err, gc.ErrorMatches, `tools info mismatch.*2\.3\.4.*2\.3\.5.*`)
    90  	c.Assert(icfg.ToolsList(), gc.HasLen, 0)
    91  }
    92  
    93  func (*instancecfgSuite) TestJujuTools(c *gc.C) {
    94  	icfg := &instancecfg.InstanceConfig{
    95  		DataDir: "/path/to/datadir/",
    96  	}
    97  	err := icfg.SetTools(coretools.List{
    98  		&coretools.Tools{
    99  			Version: version.MustParseBinary("2.3.4-trusty-amd64"),
   100  			URL:     "/tools/2.3.4-trusty-amd64",
   101  		},
   102  	})
   103  	c.Assert(err, jc.ErrorIsNil)
   104  
   105  	c.Assert(icfg.JujuTools(), gc.Equals, "/path/to/datadir/tools/2.3.4-trusty-amd64")
   106  }
   107  
   108  func (*instancecfgSuite) TestGUITools(c *gc.C) {
   109  	icfg := &instancecfg.InstanceConfig{
   110  		DataDir: "/path/to/datadir/",
   111  	}
   112  	c.Assert(icfg.GUITools(), gc.Equals, "/path/to/datadir/gui")
   113  }