github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/network-get_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/testing"
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  )
    17  
    18  type NetworkGetSuite struct {
    19  	ContextSuite
    20  }
    21  
    22  var _ = gc.Suite(&NetworkGetSuite{})
    23  
    24  func (s *NetworkGetSuite) createCommand(c *gc.C) cmd.Command {
    25  	hctx := s.GetHookContext(c, -1, "")
    26  
    27  	presetBindings := make(map[string][]params.NetworkConfig)
    28  	presetBindings["known-relation"] = []params.NetworkConfig{
    29  		{Address: "10.10.0.23"},
    30  		{Address: "192.168.1.111"},
    31  	}
    32  	presetBindings["known-extra"] = []params.NetworkConfig{
    33  		{Address: "10.20.1.42"},
    34  		{Address: "fc00::1/64"},
    35  	}
    36  	presetBindings["valid-no-config"] = nil
    37  	// Simulate known but unspecified bindings.
    38  	presetBindings["known-unbound"] = []params.NetworkConfig{
    39  		{Address: "10.33.1.8"}, // Simulate preferred private address will be used for these.
    40  	}
    41  	hctx.info.NetworkInterface.BindingsToNetworkConfigs = presetBindings
    42  
    43  	com, err := jujuc.NewCommand(hctx, cmdString("network-get"))
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	return com
    46  }
    47  
    48  func (s *NetworkGetSuite) TestNetworkGet(c *gc.C) {
    49  	for i, t := range []struct {
    50  		summary  string
    51  		args     []string
    52  		code     int
    53  		out      string
    54  		checkctx func(*gc.C, *cmd.Context)
    55  	}{{
    56  		summary: "no arguments",
    57  		code:    2,
    58  		out:     `no arguments specified`,
    59  	}, {
    60  		summary: "empty binding name specified",
    61  		code:    2,
    62  		args:    []string{""},
    63  		out:     `no binding name specified`,
    64  	}, {
    65  		summary: "binding name given, no --primary-address given",
    66  		code:    2,
    67  		args:    []string{"foo"},
    68  		out:     `--primary-address is currently required`,
    69  	}, {
    70  		summary: "unknown binding given, with --primary-address",
    71  		args:    []string{"unknown", "--primary-address"},
    72  		code:    1,
    73  		out:     "insert server error for unknown binding here",
    74  	}, {
    75  		summary: "valid arguments, API server returns no config",
    76  		args:    []string{"valid-no-config", "--primary-address"},
    77  		code:    1,
    78  		out:     `no network config found for binding "valid-no-config"`,
    79  	}, {
    80  		summary: "explicitly bound, extra-binding name given with --primary-address",
    81  		args:    []string{"known-extra", "--primary-address"},
    82  		out:     "10.20.1.42",
    83  	}, {
    84  		summary: "explicitly bound relation name given with --primary-address",
    85  		args:    []string{"known-relation", "--primary-address"},
    86  		out:     "10.10.0.23",
    87  	}, {
    88  		summary: "implicitly bound binding name given with --primary-address",
    89  		args:    []string{"known-unbound", "--primary-address"},
    90  		out:     "10.33.1.8", // preferred private address used for unspecified bindings.
    91  	}} {
    92  		c.Logf("test %d: %s", i, t.summary)
    93  		com := s.createCommand(c)
    94  		ctx := testing.Context(c)
    95  		code := cmd.Main(com, ctx, t.args)
    96  		c.Check(code, gc.Equals, t.code)
    97  		if code == 0 {
    98  			c.Check(bufferString(ctx.Stderr), gc.Equals, "")
    99  			expect := t.out
   100  			if expect != "" {
   101  				expect = expect + "\n"
   102  			}
   103  			c.Check(bufferString(ctx.Stdout), gc.Equals, expect)
   104  		} else {
   105  			c.Check(bufferString(ctx.Stdout), gc.Equals, "")
   106  			expect := fmt.Sprintf(`(.|\n)*error: %s\n`, t.out)
   107  			c.Check(bufferString(ctx.Stderr), gc.Matches, expect)
   108  		}
   109  	}
   110  }
   111  
   112  func (s *NetworkGetSuite) TestHelp(c *gc.C) {
   113  
   114  	var helpTemplate = `
   115  Usage: network-get [options] <binding-name> --primary-address
   116  
   117  Summary:
   118  get network config
   119  
   120  Options:
   121  --format  (= smart)
   122      Specify output format (json|smart|yaml)
   123  -o, --output (= "")
   124      Specify an output file
   125  --primary-address  (= false)
   126      get the primary address for the binding
   127  
   128  Details:
   129  network-get returns the network config for a given binding name. The only
   130  supported flag for now is --primary-address, which is required and returns
   131  the IP address the local unit should advertise as its endpoint to its peers.
   132  `[1:]
   133  
   134  	com := s.createCommand(c)
   135  	ctx := testing.Context(c)
   136  	code := cmd.Main(com, ctx, []string{"--help"})
   137  	c.Check(code, gc.Equals, 0)
   138  
   139  	c.Check(bufferString(ctx.Stdout), gc.Equals, helpTemplate)
   140  	c.Check(bufferString(ctx.Stderr), gc.Equals, "")
   141  }