github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/instance/placement_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package instance_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/instance"
    11  )
    12  
    13  type PlacementSuite struct{}
    14  
    15  var _ = gc.Suite(&PlacementSuite{})
    16  
    17  func (s *PlacementSuite) TestParsePlacement(c *gc.C) {
    18  	parsePlacementTests := []struct {
    19  		arg                          string
    20  		expectScope, expectDirective string
    21  		err                          string
    22  	}{{
    23  		arg: "",
    24  	}, {
    25  		arg:             "0",
    26  		expectScope:     instance.MachineScope,
    27  		expectDirective: "0",
    28  	}, {
    29  		arg:             "0/lxd/0",
    30  		expectScope:     instance.MachineScope,
    31  		expectDirective: "0/lxd/0",
    32  	}, {
    33  		arg: "#:x",
    34  		err: `invalid value "x" for "#" scope: expected machine-id`,
    35  	}, {
    36  		arg: "lxd:x",
    37  		err: `invalid value "x" for "lxd" scope: expected machine-id`,
    38  	}, {
    39  		arg: "kvm:x",
    40  		err: `invalid value "x" for "kvm" scope: expected machine-id`,
    41  	}, {
    42  		arg:             "kvm:123",
    43  		expectScope:     string(instance.KVM),
    44  		expectDirective: "123",
    45  	}, {
    46  		arg:         "lxd",
    47  		expectScope: string(instance.LXD),
    48  	}, {
    49  		arg: "non-standard",
    50  		err: "placement scope missing",
    51  	}, {
    52  		arg: ":non-standard",
    53  		err: "placement scope missing",
    54  	}, {
    55  		arg:             "non:standard",
    56  		expectScope:     "non",
    57  		expectDirective: "standard",
    58  	}}
    59  
    60  	for i, t := range parsePlacementTests {
    61  		c.Logf("test %d: %s", i, t.arg)
    62  		p, err := instance.ParsePlacement(t.arg)
    63  		if t.err != "" {
    64  			c.Assert(err, gc.ErrorMatches, t.err)
    65  		} else {
    66  			c.Assert(err, jc.ErrorIsNil)
    67  		}
    68  		if t.expectScope == "" && t.expectDirective == "" {
    69  			c.Assert(p, gc.IsNil)
    70  		} else {
    71  			c.Assert(p, gc.DeepEquals, &instance.Placement{
    72  				Scope:     t.expectScope,
    73  				Directive: t.expectDirective,
    74  			})
    75  		}
    76  	}
    77  }