github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/cmd/output_tabular_test.go (about)

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