github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/crossmodel/show_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package crossmodel_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/charm.v6"
    13  
    14  	"github.com/juju/juju/cmd/juju/crossmodel"
    15  	jujucrossmodel "github.com/juju/juju/core/crossmodel"
    16  )
    17  
    18  type showSuite struct {
    19  	BaseCrossModelSuite
    20  	mockAPI *mockShowAPI
    21  }
    22  
    23  var _ = gc.Suite(&showSuite{})
    24  
    25  func (s *showSuite) SetUpTest(c *gc.C) {
    26  	s.BaseCrossModelSuite.SetUpTest(c)
    27  
    28  	s.mockAPI = &mockShowAPI{
    29  		desc: "IBM DB2 Express Server Edition is an entry level database system",
    30  	}
    31  }
    32  
    33  func (s *showSuite) runShow(c *gc.C, args ...string) (*cmd.Context, error) {
    34  	return cmdtesting.RunCommand(c, crossmodel.NewShowEndpointsCommandForTest(s.store, s.mockAPI), args...)
    35  }
    36  
    37  func (s *showSuite) TestShowNoUrl(c *gc.C) {
    38  	s.assertShowError(c, nil, ".*must specify endpoint URL.*")
    39  }
    40  
    41  func (s *showSuite) TestShowApiError(c *gc.C) {
    42  	s.mockAPI.msg = "fail"
    43  	s.assertShowError(c, []string{"fred/model.db2"}, ".*fail.*")
    44  }
    45  
    46  func (s *showSuite) TestShowURLError(c *gc.C) {
    47  	s.assertShowError(c, []string{"fred/model.foo/db2"}, "application offer URL has invalid form.*")
    48  }
    49  
    50  func (s *showSuite) TestShowNameOnly(c *gc.C) {
    51  	s.assertShowYaml(c, "db2")
    52  }
    53  
    54  func (s *showSuite) TestShowYaml(c *gc.C) {
    55  	s.assertShowYaml(c, "fred/model.db2")
    56  }
    57  
    58  func (s *showSuite) assertShowYaml(c *gc.C, arg string) {
    59  	s.assertShow(
    60  		c,
    61  		[]string{arg, "--format", "yaml"},
    62  		`
    63  test-master:fred/model.db2:
    64    description: IBM DB2 Express Server Edition is an entry level database system
    65    access: consume
    66    endpoints:
    67      db2:
    68        interface: http
    69        role: requirer
    70      log:
    71        interface: http
    72        role: provider
    73    users:
    74      bob:
    75        display-name: Bob
    76        access: consume
    77  `[1:],
    78  	)
    79  }
    80  
    81  func (s *showSuite) TestShowTabular(c *gc.C) {
    82  	s.assertShow(
    83  		c,
    84  		[]string{"fred/model.db2", "--format", "tabular"},
    85  		`
    86  Store        URL             Access   Description                                 Endpoint  Interface  Role
    87  test-master  fred/model.db2  consume  IBM DB2 Express Server Edition is an entry  db2       http       requirer
    88                                        level database system                       log       http       provider
    89  
    90  `[1:],
    91  	)
    92  }
    93  
    94  func (s *showSuite) TestShowDifferentController(c *gc.C) {
    95  	s.mockAPI.controllerName = "different"
    96  	s.assertShow(
    97  		c,
    98  		[]string{"different:fred/model.db2", "--format", "tabular"},
    99  		`
   100  Store      URL             Access   Description                                 Endpoint  Interface  Role
   101  different  fred/model.db2  consume  IBM DB2 Express Server Edition is an entry  db2       http       requirer
   102                                      level database system                       log       http       provider
   103  
   104  `[1:],
   105  	)
   106  }
   107  
   108  func (s *showSuite) TestShowTabularExactly180Desc(c *gc.C) {
   109  	s.mockAPI.desc = s.mockAPI.desc + s.mockAPI.desc + s.mockAPI.desc[:52]
   110  	s.assertShow(
   111  		c,
   112  		[]string{"fred/model.db2", "--format", "tabular"},
   113  		`
   114  Store        URL             Access   Description                                   Endpoint  Interface  Role
   115  test-master  fred/model.db2  consume  IBM DB2 Express Server Edition is an entry    db2       http       requirer
   116                                        level database systemIBM DB2 Express Server   log       http       provider
   117                                        Edition is an entry level database systemIBM                       
   118                                        DB2 Express Server Edition is an entry level                       
   119                                        dat                                                                
   120  
   121  `[1:],
   122  	)
   123  }
   124  
   125  func (s *showSuite) TestShowTabularMoreThan180Desc(c *gc.C) {
   126  	s.mockAPI.desc = s.mockAPI.desc + s.mockAPI.desc + s.mockAPI.desc
   127  	s.assertShow(
   128  		c,
   129  		[]string{"fred/model.db2", "--format", "tabular"},
   130  		`
   131  Store        URL             Access   Description                                   Endpoint  Interface  Role
   132  test-master  fred/model.db2  consume  IBM DB2 Express Server Edition is an entry    db2       http       requirer
   133                                        level database systemIBM DB2 Express Server   log       http       provider
   134                                        Edition is an entry level database systemIBM                       
   135                                        DB2 Express Server Edition is an entry level                       
   136                                        ...                                                                
   137  
   138  `[1:],
   139  	)
   140  }
   141  
   142  func (s *showSuite) assertShow(c *gc.C, args []string, expected string) {
   143  	context, err := s.runShow(c, args...)
   144  	c.Assert(err, jc.ErrorIsNil)
   145  
   146  	obtained := cmdtesting.Stdout(context)
   147  	c.Assert(obtained, gc.Matches, expected)
   148  }
   149  
   150  func (s *showSuite) assertShowError(c *gc.C, args []string, expected string) {
   151  	_, err := s.runShow(c, args...)
   152  	c.Assert(err, gc.ErrorMatches, expected)
   153  }
   154  
   155  type mockShowAPI struct {
   156  	controllerName string
   157  	msg, desc      string
   158  }
   159  
   160  func (s mockShowAPI) Close() error {
   161  	return nil
   162  }
   163  
   164  func (s mockShowAPI) ApplicationOffer(url string) (*jujucrossmodel.ApplicationOfferDetails, error) {
   165  	if s.msg != "" {
   166  		return nil, errors.New(s.msg)
   167  	}
   168  
   169  	offerURL := "fred/model.db2"
   170  	if s.controllerName != "" {
   171  		offerURL = s.controllerName + ":" + offerURL
   172  	}
   173  	return &jujucrossmodel.ApplicationOfferDetails{
   174  		OfferName:              "hosted-db2",
   175  		OfferURL:               offerURL,
   176  		ApplicationDescription: s.desc,
   177  		Endpoints: []charm.Relation{
   178  			{Name: "log", Interface: "http", Role: charm.RoleProvider},
   179  			{Name: "db2", Interface: "http", Role: charm.RoleRequirer},
   180  		},
   181  		Users: []jujucrossmodel.OfferUserDetails{{
   182  			UserName: "bob", DisplayName: "Bob", Access: "consume",
   183  		}},
   184  	}, nil
   185  }