github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/listplans/list_plans_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package listplans_test
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  	"time"
    10  
    11  	"github.com/juju/cmd/cmdtesting"
    12  	api "github.com/juju/romulus/api/plan"
    13  	wireformat "github.com/juju/romulus/wireformat/plan"
    14  	"github.com/juju/testing"
    15  	jc "github.com/juju/testing/checkers"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	"github.com/juju/juju/cmd/juju/romulus/listplans"
    19  	coretesting "github.com/juju/juju/testing"
    20  )
    21  
    22  var (
    23  	testPlan1 = `
    24      description:
    25          text: |
    26              Lorem ipsum dolor sit amet,
    27              consectetur adipiscing elit.
    28              Nunc pretium purus nec magna faucibus, sed eleifend dui fermentum. Nulla nec ornare lorem, sed imperdiet turpis. Nam auctor quis massa et commodo. Maecenas in magna erat. Duis non iaculis risus, a malesuada quam. Sed quis commodo sapien. Suspendisse laoreet diam eu interdum tristique. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
    29              Donec eu nunc quis eros fermentum porta non ut justo. Donec ut tempus sapien. Suspendisse bibendum fermentum eros, id feugiat justo elementum quis. Quisque vel volutpat risus. Aenean pellentesque ultrices consequat. Maecenas luctus, augue vitae ullamcorper vulputate, purus ligula accumsan diam, ut efficitur diam tellus ac nibh. Cras eros ligula, mattis in ex quis, porta efficitur quam. Donec porta, est ut interdum blandit, enim est elementum sapien, quis congue orci dui et nulla. Maecenas vehicula malesuada vehicula. Phasellus sapien ante, semper eu ornare sed, vulputate id nunc. Maecenas in orci mollis, sagittis lorem quis, ultrices metus. Integer molestie tempor augue, pulvinar blandit sapien ultricies eget.
    30              Fusce sed tellus sit amet tortor mollis pellentesque. Nulla tempus sem tellus, vitae tempor ipsum scelerisque eu. Cras tempor, tellus nec pretium egestas, felis massa luctus velit, vitae feugiat nunc velit ac tellus. Maecenas quis nisi diam. Sed pulvinar suscipit nibh sit amet cursus. Ut sem orci, consequat id pretium id, lacinia id nisl. Maecenas id quam at nisi eleifend porta. Vestibulum at ligula arcu. Quisque tincidunt pulvinar egestas. Ut suscipit ornare ligula a fermentum. Morbi ante justo, condimentum ut risus vitae, molestie elementum elit. Curabitur malesuada commodo diam sed ultrices. Vestibulum tincidunt turpis at ultricies fermentum. Morbi ipsum felis, laoreet quis risus id, ornare elementum urna. Morbi ultrices porttitor pulvinar. Maecenas facilisis velit sit amet tellus feugiat iaculis.
    31      metrics:
    32        pings:
    33          unit:
    34            transform: max
    35            period: hour
    36            gaps: zero
    37  `
    38  	testPlan2 = `
    39      metrics:
    40        pongs:
    41          unit:
    42            transform: max
    43            period: hour
    44            gaps: zero
    45  `
    46  )
    47  
    48  type ListPlansCommandSuite struct {
    49  	coretesting.FakeJujuXDGDataHomeSuite
    50  	mockAPI *mockapi
    51  	stub    *testing.Stub
    52  }
    53  
    54  var _ = gc.Suite(&ListPlansCommandSuite{})
    55  
    56  func (s *ListPlansCommandSuite) SetUpTest(c *gc.C) {
    57  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    58  	s.stub = &testing.Stub{}
    59  	s.mockAPI = newMockAPI(s.stub)
    60  	s.PatchValue(listplans.NewClient, listplans.APIClientFnc(s.mockAPI))
    61  }
    62  
    63  func (s *ListPlansCommandSuite) TestTabularOutput(c *gc.C) {
    64  	listPlans := &listplans.ListPlansCommand{
    65  		CharmResolver: &mockCharmResolver{
    66  			ResolvedURL: "series/some-charm-url",
    67  			Stub:        s.stub,
    68  		},
    69  	}
    70  	ctx, err := cmdtesting.RunCommand(c, listPlans, "some-charm-url")
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals,
    73  		`PLAN             	PRICE	DESCRIPTION                                       
    74  bob/test-plan-1  	     	Lorem ipsum dolor sit amet,                       
    75                   	     	consectetur adipiscing elit.                      
    76                   	     	Nunc pretium purus nec magna faucibus, sed        
    77                   	     	eleifend dui fermentum. Nulla nec ornare lorem,   
    78                   	     	sed imperdiet turpis. Nam auctor quis massa et    
    79                   	     	commodo. Maecenas in magna erat. Duis non iaculis 
    80                   	     	risus, a malesuada quam. Sed quis commodo sapien. 
    81                   	     	Suspendisse laoreet diam eu interdum tristique.   
    82                   	     	Class aptent taciti sociosqu ad litora torquent   
    83                   	     	per conubia nostra, per inceptos himenaeos.       
    84                   	     	Donec eu nunc quis eros fermentum porta non ut    
    85                   	     	justo. Donec ut tempus sapien. Suspendisse        
    86                   	     	bibendum fermentum eros, id feugiat justo         
    87                   	     	elementum quis. Quisque vel volutpat risus. Aenean
    88                   	     	pellentesque ultrices consequat. Maecenas luctus, 
    89                   	     	augue vitae ullamcorper vulputate, purus ligula   
    90                   	     	accumsan diam, ut efficitur diam tellus ac nibh.  
    91                   	     	Cras eros ligula, mattis in ex quis, porta        
    92                   	     	efficitur quam. Donec porta, est ut interdum      
    93                   	     	blandit, enim est elementum sapien, quis congue   
    94                   	     	orci dui et nulla. Maecenas vehicula malesuada    
    95                   	     	vehicula. Phasellus sapien ante, semper eu ornare 
    96                   	     	sed, vulputate id nunc. Maecenas in orci mollis,  
    97                   	     	sagittis lorem quis, ultrices metus. Integer      
    98                   	     	molestie tempor augue, pulvinar blandit sapien    
    99                   	     	ultricies eget.                                   
   100                   	     	Fusce sed tellus sit amet tortor mollis           
   101                   	     	pellentesque. Nulla tempus sem tellus, vitae      
   102                   	     	tempor ipsum scelerisque eu. Cras tempor, tellus  
   103                   	     	nec pretium egestas, felis massa luctus velit,    
   104                   	     	vitae feugiat nunc velit ac tellus. Maecenas quis 
   105                   	     	nisi diam. Sed pulvinar suscipit nibh sit amet    
   106                   	     	cursus. Ut sem orci, consequat id pretium id,     
   107                   	     	lacinia id nisl. Maecenas id quam at nisi eleifend
   108                   	     	porta. Vestibulum at ligula arcu. Quisque         
   109                   	     	tincidunt pulvinar egestas. Ut suscipit ornare    
   110                   	     	ligula a fermentum. Morbi ante justo, condimentum 
   111                   	     	ut risus vitae, molestie elementum elit. Curabitur
   112                   	     	malesuada commodo diam sed ultrices. Vestibulum   
   113                   	     	tincidunt turpis at ultricies fermentum. Morbi    
   114                   	     	ipsum felis, laoreet quis risus id, ornare        
   115                   	     	elementum urna. Morbi ultrices porttitor pulvinar.
   116                   	     	Maecenas facilisis velit sit amet tellus feugiat  
   117                   	     	iaculis.                                          
   118                   	     	                                                  
   119  carol/test-plan-2	     	                                                  
   120  `)
   121  }
   122  
   123  func (s *ListPlansCommandSuite) TestGetCommands(c *gc.C) {
   124  	tests := []struct {
   125  		about            string
   126  		args             []string
   127  		err              string
   128  		resolvedCharmURL string
   129  		apiCall          []interface{}
   130  	}{{
   131  		about:            "charm url is resolved",
   132  		args:             []string{"some-charm-url"},
   133  		resolvedCharmURL: "series/some-charm-url-1",
   134  		apiCall:          []interface{}{"series/some-charm-url-1"},
   135  	}, {
   136  		about:   "everything works - default format",
   137  		args:    []string{"some-charm-url"},
   138  		apiCall: []interface{}{"some-charm-url"},
   139  	}, {
   140  		about:   "everything works - yaml",
   141  		args:    []string{"some-charm-url", "--format", "yaml"},
   142  		apiCall: []interface{}{"some-charm-url"},
   143  	}, {
   144  		about:   "everything works - smart",
   145  		args:    []string{"some-charm-url", "--format", "smart"},
   146  		apiCall: []interface{}{"some-charm-url"},
   147  	}, {
   148  		about:   "everything works - json",
   149  		args:    []string{"some-charm-url", "--format", "json"},
   150  		apiCall: []interface{}{"some-charm-url"},
   151  	}, {
   152  		about:   "everything works - summary",
   153  		args:    []string{"some-charm-url", "--format", "summary"},
   154  		apiCall: []interface{}{"some-charm-url"},
   155  	}, {
   156  		about:   "everything works - tabular",
   157  		args:    []string{"some-charm-url", "--format", "tabular"},
   158  		apiCall: []interface{}{"some-charm-url"},
   159  	}, {
   160  		about:   "missing argument",
   161  		args:    []string{},
   162  		err:     `missing arguments`,
   163  		apiCall: []interface{}{},
   164  	}, {
   165  		about:   "unknown arguments",
   166  		args:    []string{"some-charm-url", "extra", "arguments"},
   167  		err:     `unknown command line arguments: extra,arguments`,
   168  		apiCall: []interface{}{},
   169  	},
   170  	}
   171  
   172  	for i, t := range tests {
   173  		c.Logf("Running test %d %s", i, t.about)
   174  		s.mockAPI.reset()
   175  
   176  		listPlans := &listplans.ListPlansCommand{
   177  			CharmResolver: &mockCharmResolver{
   178  				ResolvedURL: t.resolvedCharmURL,
   179  				Stub:        s.stub,
   180  			},
   181  		}
   182  		_, err := cmdtesting.RunCommand(c, listPlans, t.args...)
   183  		if t.err != "" {
   184  			c.Assert(err, gc.ErrorMatches, t.err)
   185  		} else {
   186  			c.Assert(err, jc.ErrorIsNil)
   187  			s.mockAPI.CheckCall(c, 0, "Resolve", t.args[0])
   188  			s.mockAPI.CheckCall(c, 1, "GetAssociatedPlans", t.apiCall...)
   189  		}
   190  	}
   191  }
   192  
   193  // mockapi mocks the plan service api
   194  type mockapi struct {
   195  	*testing.Stub
   196  	api.Client
   197  }
   198  
   199  func newMockAPI(s *testing.Stub) *mockapi {
   200  	return &mockapi{Stub: s}
   201  }
   202  
   203  // Get implements the Get function of the api.PlanClient interface.
   204  // TODO (domas) : fix once querying by charm url is in place
   205  func (m *mockapi) GetAssociatedPlans(charmURL string) ([]wireformat.Plan, error) {
   206  	m.AddCall("GetAssociatedPlans", charmURL)
   207  	p1 := wireformat.Plan{
   208  		URL:        "bob/test-plan-1",
   209  		Definition: testPlan1,
   210  		CreatedOn:  time.Date(2015, 0, 0, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
   211  	}
   212  	p2 := wireformat.Plan{
   213  		URL:        "carol/test-plan-2",
   214  		Definition: testPlan2,
   215  		CreatedOn:  time.Date(2015, 0, 0, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
   216  	}
   217  	return []wireformat.Plan{p1, p2}, m.NextErr()
   218  }
   219  
   220  func (m *mockapi) reset() {
   221  	m.ResetCalls()
   222  }
   223  
   224  // mockCharmResolver is a mock implementation of cmd.CharmResolver.
   225  type mockCharmResolver struct {
   226  	*testing.Stub
   227  	ResolvedURL string
   228  }
   229  
   230  // Resolve implements cmd.CharmResolver.
   231  func (r *mockCharmResolver) Resolve(_ func(*url.URL) error, _ *http.Client, charmURL string) (string, error) {
   232  	r.AddCall("Resolve", charmURL)
   233  	if r.ResolvedURL != "" {
   234  		return r.ResolvedURL, r.NextErr()
   235  	}
   236  	return charmURL, r.NextErr()
   237  }