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

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package application_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/cmd/cmdtesting"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/names.v2"
    14  
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/cmd/juju/application"
    17  	"github.com/juju/juju/core/constraints"
    18  	"github.com/juju/juju/jujuclient"
    19  	_ "github.com/juju/juju/provider/dummy"
    20  	jujutesting "github.com/juju/juju/testing"
    21  )
    22  
    23  type ShowSuite struct {
    24  	jujutesting.FakeJujuXDGDataHomeSuite
    25  	store *jujuclient.MemStore
    26  
    27  	mockAPI *mockShowAPI
    28  }
    29  
    30  var _ = gc.Suite(&ShowSuite{})
    31  
    32  func (s *ShowSuite) SetUpTest(c *gc.C) {
    33  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    34  
    35  	s.store = jujuclient.NewMemStore()
    36  	s.store.CurrentControllerName = "testing"
    37  	s.store.Controllers["testing"] = jujuclient.ControllerDetails{}
    38  	s.store.Models["testing"] = &jujuclient.ControllerModels{
    39  		Models: map[string]jujuclient.ModelDetails{
    40  			"admin/controller": {},
    41  		},
    42  		CurrentModel: "admin/controller",
    43  	}
    44  	s.store.Accounts["testing"] = jujuclient.AccountDetails{
    45  		User: "admin",
    46  	}
    47  
    48  	s.mockAPI = &mockShowAPI{
    49  		version:              9,
    50  		applicationsInfoFunc: func([]names.ApplicationTag) ([]params.ApplicationInfoResult, error) { return nil, nil },
    51  	}
    52  }
    53  
    54  func (s *ShowSuite) runShow(c *gc.C, args ...string) (*cmd.Context, error) {
    55  	return cmdtesting.RunCommand(c, application.NewShowCommandForTest(s.mockAPI, s.store), args...)
    56  }
    57  
    58  type showTest struct {
    59  	args   []string
    60  	err    string
    61  	stdout string
    62  	stderr string
    63  }
    64  
    65  func (s *ShowSuite) assertRunShow(c *gc.C, t showTest) {
    66  	context, err := s.runShow(c, t.args...)
    67  	if t.err == "" {
    68  		c.Assert(err, jc.ErrorIsNil)
    69  	} else {
    70  		c.Assert(err, gc.ErrorMatches, t.err)
    71  	}
    72  	c.Assert(cmdtesting.Stdout(context), gc.Equals, t.stdout)
    73  	c.Assert(cmdtesting.Stderr(context), gc.Equals, t.stderr)
    74  }
    75  
    76  func (s *ShowSuite) TestShowNoArguments(c *gc.C) {
    77  	msg := "an application name must be supplied"
    78  	s.assertRunShow(c, showTest{
    79  		err:    fmt.Sprintf("%v", msg),
    80  		stderr: fmt.Sprintf("ERROR %v\n", msg),
    81  	})
    82  }
    83  
    84  func (s *ShowSuite) TestShowInvalidName(c *gc.C) {
    85  	msg := "application name so-42-far-not-good not valid"
    86  	s.assertRunShow(c, showTest{
    87  		args:   []string{"so-42-far-not-good"},
    88  		err:    fmt.Sprintf("%v", msg),
    89  		stderr: fmt.Sprintf("ERROR %v\n", msg),
    90  	})
    91  }
    92  
    93  func (s *ShowSuite) TestShowInvalidValidNames(c *gc.C) {
    94  	msg := "application name so-42-far-not-good not valid"
    95  	s.assertRunShow(c, showTest{
    96  		args:   []string{"so-42-far-not-good", "wordpress"},
    97  		err:    fmt.Sprintf("%v", msg),
    98  		stderr: fmt.Sprintf("ERROR %v\n", msg),
    99  	})
   100  }
   101  
   102  func (s *ShowSuite) TestShowInvalidNames(c *gc.C) {
   103  	msg := "application names so-42-far-not-good, oo/42 not valid"
   104  	s.assertRunShow(c, showTest{
   105  		args:   []string{"so-42-far-not-good", "oo/42"},
   106  		err:    fmt.Sprintf("%v", msg),
   107  		stderr: fmt.Sprintf("ERROR %v\n", msg),
   108  	})
   109  }
   110  
   111  func (s *ShowSuite) TestShowInvalidAndValidNames(c *gc.C) {
   112  	msg := "application names so-42-far-not-good, oo/42 not valid"
   113  	s.assertRunShow(c, showTest{
   114  		args:   []string{"so-42-far-not-good", "wordpress", "oo/42"},
   115  		err:    fmt.Sprintf("%v", msg),
   116  		stderr: fmt.Sprintf("ERROR %v\n", msg),
   117  	})
   118  }
   119  
   120  func (s *ShowSuite) TestShowUnsupported(c *gc.C) {
   121  	s.mockAPI.version = 8
   122  	s.assertRunShow(c, showTest{
   123  		args: []string{"wordpress"},
   124  		err:  "show applications on API server version 8 not supported",
   125  	})
   126  }
   127  
   128  func (s *ShowSuite) TestShowApiError(c *gc.C) {
   129  	s.mockAPI.applicationsInfoFunc = func([]names.ApplicationTag) ([]params.ApplicationInfoResult, error) {
   130  		return []params.ApplicationInfoResult{
   131  			{Error: &params.Error{Message: "boom"}},
   132  		}, nil
   133  	}
   134  	msg := "boom"
   135  	s.assertRunShow(c, showTest{
   136  		args: []string{"wordpress"},
   137  		err:  fmt.Sprintf("%v", msg),
   138  	})
   139  }
   140  
   141  func (s *ShowSuite) createTestApplicationInfo(name string, suffix string) *params.ApplicationInfo {
   142  	app := fmt.Sprintf("%v%v", name, suffix)
   143  	return &params.ApplicationInfo{
   144  		Tag:         fmt.Sprintf("application-%v", app),
   145  		Charm:       fmt.Sprintf("charm-%v", app),
   146  		Series:      "quantal",
   147  		Channel:     "development",
   148  		Constraints: constraints.MustParse("arch=amd64 mem=4G cores=1 root-disk=8G"),
   149  		Principal:   true,
   150  		EndpointBindings: map[string]string{
   151  			"juju-info": "myspace",
   152  		},
   153  	}
   154  }
   155  
   156  func (s *ShowSuite) TestShow(c *gc.C) {
   157  	s.mockAPI.applicationsInfoFunc = func([]names.ApplicationTag) ([]params.ApplicationInfoResult, error) {
   158  		return []params.ApplicationInfoResult{
   159  			{Result: s.createTestApplicationInfo("wordpress", "")},
   160  		}, nil
   161  	}
   162  	s.assertRunShow(c, showTest{
   163  		args: []string{"wordpress"},
   164  		stdout: `
   165  wordpress:
   166    charm: charm-wordpress
   167    series: quantal
   168    channel: development
   169    constraints:
   170      arch: amd64
   171      cores: 1
   172      mem: 4096
   173      root-disk: 8192
   174    principal: true
   175    exposed: false
   176    remote: false
   177    endpoint-bindings:
   178      juju-info: myspace
   179  `[1:],
   180  	})
   181  }
   182  func (s *ShowSuite) TestShowJSON(c *gc.C) {
   183  	s.mockAPI.applicationsInfoFunc = func([]names.ApplicationTag) ([]params.ApplicationInfoResult, error) {
   184  		return []params.ApplicationInfoResult{
   185  			{Result: s.createTestApplicationInfo("wordpress", "")},
   186  		}, nil
   187  	}
   188  	s.assertRunShow(c, showTest{
   189  		args:   []string{"wordpress", "--format", "json"},
   190  		stdout: "{\"wordpress\":{\"charm\":\"charm-wordpress\",\"series\":\"quantal\",\"channel\":\"development\",\"constraints\":{\"arch\":\"amd64\",\"cores\":1,\"mem\":4096,\"root-disk\":8192},\"principal\":true,\"exposed\":false,\"remote\":false,\"endpoint-bindings\":{\"juju-info\":\"myspace\"}}}\n",
   191  	})
   192  }
   193  
   194  func (s *ShowSuite) TestShowMix(c *gc.C) {
   195  	s.mockAPI.applicationsInfoFunc = func([]names.ApplicationTag) ([]params.ApplicationInfoResult, error) {
   196  		return []params.ApplicationInfoResult{
   197  			{Result: s.createTestApplicationInfo("wordpress", "")},
   198  			{Error: &params.Error{Message: "boom"}},
   199  		}, nil
   200  	}
   201  	s.assertRunShow(c, showTest{
   202  		args: []string{"wordpress", "logging"},
   203  		err:  "boom",
   204  	})
   205  }
   206  
   207  func (s *ShowSuite) TestShowMany(c *gc.C) {
   208  	s.mockAPI.applicationsInfoFunc = func([]names.ApplicationTag) ([]params.ApplicationInfoResult, error) {
   209  		return []params.ApplicationInfoResult{
   210  			{Result: s.createTestApplicationInfo("wordpress", "")},
   211  			{Result: s.createTestApplicationInfo("logging", "")},
   212  		}, nil
   213  	}
   214  	s.assertRunShow(c, showTest{
   215  		args: []string{"wordpress", "logging"},
   216  		stdout: `
   217  logging:
   218    charm: charm-logging
   219    series: quantal
   220    channel: development
   221    constraints:
   222      arch: amd64
   223      cores: 1
   224      mem: 4096
   225      root-disk: 8192
   226    principal: true
   227    exposed: false
   228    remote: false
   229    endpoint-bindings:
   230      juju-info: myspace
   231  wordpress:
   232    charm: charm-wordpress
   233    series: quantal
   234    channel: development
   235    constraints:
   236      arch: amd64
   237      cores: 1
   238      mem: 4096
   239      root-disk: 8192
   240    principal: true
   241    exposed: false
   242    remote: false
   243    endpoint-bindings:
   244      juju-info: myspace
   245  `[1:],
   246  	})
   247  }
   248  
   249  type mockShowAPI struct {
   250  	version              int
   251  	applicationsInfoFunc func([]names.ApplicationTag) ([]params.ApplicationInfoResult, error)
   252  }
   253  
   254  func (s mockShowAPI) Close() error {
   255  	return nil
   256  }
   257  
   258  func (s mockShowAPI) BestAPIVersion() int {
   259  	return s.version
   260  }
   261  
   262  func (s mockShowAPI) ApplicationsInfo(tags []names.ApplicationTag) ([]params.ApplicationInfoResult, error) {
   263  	return s.applicationsInfoFunc(tags)
   264  }