launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/names/unit_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package names_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	"launchpad.net/juju-core/names"
    12  )
    13  
    14  type unitSuite struct{}
    15  
    16  var _ = gc.Suite(&unitSuite{})
    17  
    18  func (s *unitSuite) TestUnitTag(c *gc.C) {
    19  	c.Assert(names.UnitTag("wordpress/2"), gc.Equals, "unit-wordpress-2")
    20  }
    21  
    22  var unitNameTests = []struct {
    23  	pattern string
    24  	valid   bool
    25  	service string
    26  }{
    27  	{pattern: "wordpress/42", valid: true, service: "wordpress"},
    28  	{pattern: "rabbitmq-server/123", valid: true, service: "rabbitmq-server"},
    29  	{pattern: "foo", valid: false},
    30  	{pattern: "foo/", valid: false},
    31  	{pattern: "bar/foo", valid: false},
    32  	{pattern: "20/20", valid: false},
    33  	{pattern: "foo-55", valid: false},
    34  	{pattern: "foo-bar/123", valid: true, service: "foo-bar"},
    35  	{pattern: "foo-bar/123/", valid: false},
    36  	{pattern: "foo-bar/123-not", valid: false},
    37  }
    38  
    39  func (s *unitSuite) TestUnitNameFormats(c *gc.C) {
    40  	for i, test := range unitNameTests {
    41  		c.Logf("test %d: %q", i, test.pattern)
    42  		c.Assert(names.IsUnit(test.pattern), gc.Equals, test.valid)
    43  	}
    44  }
    45  
    46  func (s *unitSuite) TestInvalidUnitTagFormats(c *gc.C) {
    47  	for i, test := range unitNameTests {
    48  		if !test.valid {
    49  			c.Logf("test %d: %q", i, test.pattern)
    50  			expect := fmt.Sprintf("%q is not a valid unit name", test.pattern)
    51  			testUnitTag := func() { names.UnitTag(test.pattern) }
    52  			c.Assert(testUnitTag, gc.PanicMatches, expect)
    53  		}
    54  	}
    55  }
    56  
    57  func (s *serviceSuite) TestUnitService(c *gc.C) {
    58  	for i, test := range unitNameTests {
    59  		c.Logf("test %d: %q", i, test.pattern)
    60  		if !test.valid {
    61  			expect := fmt.Sprintf("%q is not a valid unit name", test.pattern)
    62  			testFunc := func() { names.UnitService(test.pattern) }
    63  			c.Assert(testFunc, gc.PanicMatches, expect)
    64  		} else {
    65  			c.Assert(names.UnitService(test.pattern), gc.Equals, test.service)
    66  		}
    67  	}
    68  }