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