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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resource_test
     5  
     6  import (
     7  	"bytes"
     8  	"time"
     9  
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	charmresource "gopkg.in/juju/charm.v6/resource"
    14  
    15  	resourcecmd "github.com/juju/juju/cmd/juju/resource"
    16  	"github.com/juju/juju/resource"
    17  )
    18  
    19  var _ = gc.Suite(&CharmTabularSuite{})
    20  
    21  type CharmTabularSuite struct {
    22  	testing.IsolationSuite
    23  }
    24  
    25  func (s *CharmTabularSuite) formatTabular(c *gc.C, value interface{}) string {
    26  	out := &bytes.Buffer{}
    27  	err := resourcecmd.FormatCharmTabular(out, value)
    28  	c.Assert(err, jc.ErrorIsNil)
    29  	return out.String()
    30  }
    31  
    32  func (s *CharmTabularSuite) TestFormatCharmTabularOkay(c *gc.C) {
    33  	res := charmRes(c, "spam", ".tgz", "...", "")
    34  	formatted := []resourcecmd.FormattedCharmResource{resourcecmd.FormatCharmResource(res)}
    35  
    36  	data := s.formatTabular(c, formatted)
    37  	c.Check(data, gc.Equals, `
    38  Resource  Revision
    39  spam      1
    40  `[1:])
    41  }
    42  
    43  func (s *CharmTabularSuite) TestFormatCharmTabularMinimal(c *gc.C) {
    44  	res := charmRes(c, "spam", "", "", "")
    45  	formatted := []resourcecmd.FormattedCharmResource{resourcecmd.FormatCharmResource(res)}
    46  
    47  	data := s.formatTabular(c, formatted)
    48  	c.Check(data, gc.Equals, `
    49  Resource  Revision
    50  spam      1
    51  `[1:])
    52  }
    53  
    54  func (s *CharmTabularSuite) TestFormatCharmTabularUpload(c *gc.C) {
    55  	res := charmRes(c, "spam", "", "", "")
    56  	res.Origin = charmresource.OriginUpload
    57  	formatted := []resourcecmd.FormattedCharmResource{resourcecmd.FormatCharmResource(res)}
    58  
    59  	data := s.formatTabular(c, formatted)
    60  	c.Check(data, gc.Equals, `
    61  Resource  Revision
    62  spam      1
    63  `[1:])
    64  }
    65  
    66  func (s *CharmTabularSuite) TestFormatCharmTabularMulti(c *gc.C) {
    67  	formatted := []resourcecmd.FormattedCharmResource{
    68  		resourcecmd.FormatCharmResource(charmRes(c, "spam", ".tgz", "spamspamspamspam", "")),
    69  		resourcecmd.FormatCharmResource(charmRes(c, "eggs", "", "...", "")),
    70  		resourcecmd.FormatCharmResource(charmRes(c, "somethingbig", ".zip", "", "")),
    71  		resourcecmd.FormatCharmResource(charmRes(c, "song", ".mp3", "your favorite", "")),
    72  		resourcecmd.FormatCharmResource(charmRes(c, "avatar", ".png", "your picture", "")),
    73  	}
    74  	formatted[1].Revision = 2
    75  
    76  	data := s.formatTabular(c, formatted)
    77  	c.Check(data, gc.Equals, `
    78  Resource      Revision
    79  avatar        1
    80  eggs          2
    81  somethingbig  1
    82  song          1
    83  spam          1
    84  `[1:])
    85  }
    86  
    87  func (s *CharmTabularSuite) TestFormatCharmTabularBadValue(c *gc.C) {
    88  	bogus := "should have been something else"
    89  	err := resourcecmd.FormatCharmTabular(nil, bogus)
    90  	c.Check(err, gc.ErrorMatches, `expected value of type .*`)
    91  }
    92  
    93  var _ = gc.Suite(&AppTabularSuite{})
    94  
    95  type AppTabularSuite struct {
    96  	testing.IsolationSuite
    97  }
    98  
    99  func (s *AppTabularSuite) formatTabular(c *gc.C, value interface{}) string {
   100  	out := &bytes.Buffer{}
   101  	err := resourcecmd.FormatAppTabular(out, value)
   102  	c.Assert(err, jc.ErrorIsNil)
   103  	return out.String()
   104  }
   105  
   106  func (s *AppTabularSuite) TestFormatApplicationOkay(c *gc.C) {
   107  	res := resource.Resource{
   108  
   109  		Resource: charmresource.Resource{
   110  			Meta: charmresource.Meta{
   111  				Name:        "openjdk",
   112  				Description: "the java runtime",
   113  			},
   114  			Origin:   charmresource.OriginStore,
   115  			Revision: 7,
   116  		},
   117  		Timestamp: time.Now(),
   118  	}
   119  
   120  	formatted := resourcecmd.FormattedApplicationInfo{
   121  		Resources: []resourcecmd.FormattedAppResource{resourcecmd.FormatAppResource(res)},
   122  	}
   123  
   124  	data := s.formatTabular(c, formatted)
   125  	c.Check(data, gc.Equals, `
   126  Resource  Supplied by  Revision
   127  openjdk   charmstore   7
   128  `[1:])
   129  }
   130  
   131  func (s *AppTabularSuite) TestFormatUnitOkay(c *gc.C) {
   132  	res := resource.Resource{
   133  
   134  		Resource: charmresource.Resource{
   135  			Meta: charmresource.Meta{
   136  				Name:        "openjdk",
   137  				Description: "the java runtime",
   138  			},
   139  			Origin:   charmresource.OriginStore,
   140  			Revision: 7,
   141  		},
   142  		Timestamp: time.Now(),
   143  	}
   144  
   145  	formatted := []resourcecmd.FormattedAppResource{
   146  		resourcecmd.FormatAppResource(res),
   147  	}
   148  
   149  	data := s.formatTabular(c, formatted)
   150  	c.Check(data, gc.Equals, `
   151  Resource  Revision
   152  openjdk   7
   153  `[1:])
   154  }
   155  
   156  func (s *AppTabularSuite) TestFormatSvcTabularMulti(c *gc.C) {
   157  	res := []resource.Resource{
   158  		{
   159  			Resource: charmresource.Resource{
   160  				Meta: charmresource.Meta{
   161  					Name:        "openjdk",
   162  					Description: "the java runtime",
   163  				},
   164  				Origin:   charmresource.OriginStore,
   165  				Revision: 7,
   166  			},
   167  		},
   168  		{
   169  			Resource: charmresource.Resource{
   170  				Meta: charmresource.Meta{
   171  					Name:        "website",
   172  					Description: "your website data",
   173  					Type:        charmresource.TypeFile,
   174  				},
   175  				Origin: charmresource.OriginUpload,
   176  			},
   177  		},
   178  		{
   179  			Resource: charmresource.Resource{
   180  				Meta: charmresource.Meta{
   181  					Name:        "openjdk2",
   182  					Description: "another java runtime",
   183  				},
   184  				Origin:   charmresource.OriginStore,
   185  				Revision: 8,
   186  			},
   187  			Timestamp: time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC),
   188  		},
   189  		{
   190  			Resource: charmresource.Resource{
   191  				Meta: charmresource.Meta{
   192  					Name:        "website2",
   193  					Description: "your website data",
   194  				},
   195  				Origin: charmresource.OriginUpload,
   196  			},
   197  			Username:  "Bill User",
   198  			Timestamp: time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC),
   199  		},
   200  	}
   201  
   202  	charmResources := []charmresource.Resource{
   203  		{
   204  			// This resource has a higher revision than the corresponding one
   205  			// above.
   206  			Meta: charmresource.Meta{
   207  				Name:        "openjdk",
   208  				Description: "the java runtime",
   209  			},
   210  			Revision: 10,
   211  			Origin:   charmresource.OriginStore,
   212  		},
   213  		{
   214  			// This resource is the same revision as the corresponding one
   215  			// above.
   216  			Meta: charmresource.Meta{
   217  				Name:        "openjdk2",
   218  				Description: "your website data",
   219  				Type:        charmresource.TypeFile,
   220  				Path:        "foobar",
   221  			},
   222  			Revision: 8,
   223  			Origin:   charmresource.OriginStore,
   224  		},
   225  		{
   226  			// This resource has been overridden by an uploaded resource above,
   227  			// so we won't show it as an available update.
   228  			Meta: charmresource.Meta{
   229  				Name:        "website2",
   230  				Description: "your website data",
   231  			},
   232  			Revision: 99,
   233  			Origin:   charmresource.OriginStore,
   234  		},
   235  		{
   236  			Meta: charmresource.Meta{
   237  				Name:        "website",
   238  				Description: "your website data",
   239  				Type:        charmresource.TypeFile,
   240  			},
   241  		},
   242  	}
   243  
   244  	formatted, err := resourcecmd.FormatApplicationResources(resource.ApplicationResources{
   245  		Resources:           res,
   246  		CharmStoreResources: charmResources,
   247  	})
   248  	c.Assert(err, jc.ErrorIsNil)
   249  
   250  	data := s.formatTabular(c, formatted)
   251  	// Notes: sorted by name, then by revision, newest first.
   252  	c.Check(data, gc.Equals, `
   253  Resource  Supplied by  Revision
   254  openjdk   charmstore   7
   255  openjdk2  charmstore   8
   256  website   upload       -
   257  website2  Bill User    2012-12-12T12:12
   258  
   259  [Updates Available]
   260  Resource  Revision
   261  openjdk   10
   262  `[1:])
   263  }
   264  
   265  func (s *AppTabularSuite) TestFormatSvcTabularBadValue(c *gc.C) {
   266  	bogus := "should have been something else"
   267  	err := resourcecmd.FormatAppTabular(nil, bogus)
   268  	c.Check(err, gc.ErrorMatches, `unexpected type for data: string`)
   269  }
   270  
   271  func (s *AppTabularSuite) TestFormatApplicationDetailsOkay(c *gc.C) {
   272  	res := charmRes(c, "spam", ".tgz", "...", "")
   273  	updates := []resourcecmd.FormattedCharmResource{resourcecmd.FormatCharmResource(res)}
   274  
   275  	data := resourcecmd.FormattedApplicationDetails{
   276  		Resources: []resourcecmd.FormattedDetailResource{
   277  			{
   278  				UnitID:      "svc/10",
   279  				UnitNumber:  10,
   280  				Unit:        fakeFmtSvcRes("data", "1"),
   281  				Expected:    fakeFmtSvcRes("data", "1"),
   282  				Progress:    17,
   283  				RevProgress: "combRev1 (fetching: 17%)",
   284  			},
   285  			{
   286  				UnitID:      "svc/5",
   287  				UnitNumber:  5,
   288  				Unit:        fakeFmtSvcRes("config", "2"),
   289  				Expected:    fakeFmtSvcRes("config", "3"),
   290  				RevProgress: "combRev3",
   291  			},
   292  		},
   293  		Updates: updates,
   294  	}
   295  
   296  	output := s.formatTabular(c, data)
   297  	c.Assert(output, gc.Equals, `
   298  Unit    Resource  Revision  Expected
   299  svc/5   config    combRev2  combRev3
   300  svc/10  data      combRev1  combRev1 (fetching: 17%)
   301  
   302  [Updates Available]
   303  Resource  Revision
   304  spam      1
   305  `[1:])
   306  }
   307  
   308  func (s *AppTabularSuite) TestFormatUnitDetailsOkay(c *gc.C) {
   309  	data := resourcecmd.FormattedUnitDetails{
   310  		{
   311  			UnitID:      "svc/10",
   312  			UnitNumber:  10,
   313  			Unit:        fakeFmtSvcRes("data", "1"),
   314  			Expected:    fakeFmtSvcRes("data", "1"),
   315  			RevProgress: "combRev1",
   316  		},
   317  		{
   318  			UnitID:      "svc/10",
   319  			UnitNumber:  10,
   320  			Unit:        fakeFmtSvcRes("config", "2"),
   321  			Expected:    fakeFmtSvcRes("config", "3"),
   322  			Progress:    91,
   323  			RevProgress: "combRev3 (fetching: 91%)",
   324  		},
   325  	}
   326  
   327  	output := s.formatTabular(c, data)
   328  	c.Assert(output, gc.Equals, `
   329  Resource  Revision  Expected
   330  config    combRev2  combRev3 (fetching: 91%)
   331  data      combRev1  combRev1
   332  `[1:])
   333  }
   334  
   335  func fakeFmtSvcRes(name, suffix string) resourcecmd.FormattedAppResource {
   336  	return resourcecmd.FormattedAppResource{
   337  		ID:               "ID" + suffix,
   338  		ApplicationID:    "svc",
   339  		Name:             name,
   340  		Type:             "Type" + suffix,
   341  		Path:             "Path + suffix",
   342  		Description:      "Desc" + suffix,
   343  		Revision:         "1",
   344  		Fingerprint:      "Fingerprint" + suffix,
   345  		Size:             100,
   346  		Origin:           "Origin" + suffix,
   347  		Used:             true,
   348  		Timestamp:        time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC),
   349  		Username:         "Username" + suffix,
   350  		CombinedRevision: "combRev" + suffix,
   351  		UsedYesNo:        "usedYesNo" + suffix,
   352  		CombinedOrigin:   "combOrig" + suffix,
   353  	}
   354  }