github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/resource/resource_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  	"time"
     8  
     9  	"github.com/juju/errors"
    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  type ResourceSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&ResourceSuite{})
    23  
    24  func (ResourceSuite) TestValidateUploadUsed(c *gc.C) {
    25  	res := resource.Resource{
    26  		Resource:  newFullCharmResource(c, "spam"),
    27  		ID:        "a-service/spam",
    28  		ServiceID: "a-service",
    29  		Username:  "a-user",
    30  		Timestamp: time.Now(),
    31  	}
    32  
    33  	err := res.Validate()
    34  
    35  	c.Check(err, jc.ErrorIsNil)
    36  }
    37  
    38  func (ResourceSuite) TestValidateUploadNotUsed(c *gc.C) {
    39  	res := resource.Resource{
    40  		Resource:  newFullCharmResource(c, "spam"),
    41  		ID:        "a-service/spam",
    42  		ServiceID: "a-service",
    43  	}
    44  
    45  	err := res.Validate()
    46  
    47  	c.Check(err, jc.ErrorIsNil)
    48  }
    49  
    50  func (ResourceSuite) TestValidateUploadPending(c *gc.C) {
    51  	res := resource.Resource{
    52  		Resource:  newFullCharmResource(c, "spam"),
    53  		ID:        "a-service/spam",
    54  		PendingID: "some-unique-ID",
    55  		ServiceID: "a-service",
    56  		Username:  "a-user",
    57  		Timestamp: time.Now(),
    58  	}
    59  
    60  	err := res.Validate()
    61  
    62  	c.Check(err, jc.ErrorIsNil)
    63  }
    64  
    65  func (ResourceSuite) TestValidateZeroValue(c *gc.C) {
    66  	var res resource.Resource
    67  
    68  	err := res.Validate()
    69  
    70  	c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
    71  	c.Check(err, gc.ErrorMatches, `.*bad info.*`)
    72  }
    73  
    74  func (ResourceSuite) TestValidateBadInfo(c *gc.C) {
    75  	var charmRes charmresource.Resource
    76  	c.Assert(charmRes.Validate(), gc.NotNil)
    77  
    78  	res := resource.Resource{
    79  		Resource: charmRes,
    80  	}
    81  
    82  	err := res.Validate()
    83  
    84  	c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
    85  	c.Check(err, gc.ErrorMatches, `.*bad info.*`)
    86  }
    87  
    88  func (ResourceSuite) TestValidateMissingID(c *gc.C) {
    89  	res := resource.Resource{
    90  		Resource:  newFullCharmResource(c, "spam"),
    91  		ServiceID: "a-service",
    92  		Username:  "a-user",
    93  		Timestamp: time.Now(),
    94  	}
    95  
    96  	err := res.Validate()
    97  
    98  	c.Check(err, jc.ErrorIsNil)
    99  }
   100  
   101  func (ResourceSuite) TestValidateMissingServiceID(c *gc.C) {
   102  	res := resource.Resource{
   103  		Resource:  newFullCharmResource(c, "spam"),
   104  		ID:        "a-service/spam",
   105  		Username:  "a-user",
   106  		Timestamp: time.Now(),
   107  	}
   108  
   109  	err := res.Validate()
   110  
   111  	c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
   112  	c.Check(err, gc.ErrorMatches, `.*missing service ID.*`)
   113  }
   114  
   115  func (ResourceSuite) TestValidateMissingUsername(c *gc.C) {
   116  	res := resource.Resource{
   117  		Resource:  newFullCharmResource(c, "spam"),
   118  		ID:        "a-service/spam",
   119  		ServiceID: "a-service",
   120  		Username:  "",
   121  		Timestamp: time.Now(),
   122  	}
   123  
   124  	err := res.Validate()
   125  
   126  	c.Check(err, jc.ErrorIsNil)
   127  }
   128  
   129  func (ResourceSuite) TestValidateMissingTimestamp(c *gc.C) {
   130  	res := resource.Resource{
   131  		Resource:  newFullCharmResource(c, "spam"),
   132  		ID:        "a-service/spam",
   133  		ServiceID: "a-service",
   134  		Username:  "a-user",
   135  		Timestamp: time.Time{},
   136  	}
   137  
   138  	err := res.Validate()
   139  
   140  	c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
   141  	c.Check(err, gc.ErrorMatches, `.*missing timestamp.*`)
   142  }
   143  
   144  func (ResourceSuite) TestRevisionStringNone(c *gc.C) {
   145  	res := resource.Resource{
   146  		Resource: charmresource.Resource{
   147  			Meta: charmresource.Meta{
   148  				Name:        "foo",
   149  				Type:        charmresource.TypeFile,
   150  				Path:        "foo.tgz",
   151  				Description: "you need it",
   152  			},
   153  			Origin: charmresource.OriginUpload,
   154  		},
   155  		ServiceID: "svc",
   156  	}
   157  
   158  	err := res.Validate()
   159  	c.Check(err, jc.ErrorIsNil)
   160  
   161  	c.Check(res.RevisionString(), gc.Equals, "-")
   162  }
   163  
   164  func (ResourceSuite) TestRevisionStringTime(c *gc.C) {
   165  	res := resource.Resource{
   166  		Resource: charmresource.Resource{
   167  			Meta: charmresource.Meta{
   168  				Name:        "foo",
   169  				Type:        charmresource.TypeFile,
   170  				Path:        "foo.tgz",
   171  				Description: "you need it",
   172  			},
   173  			Origin: charmresource.OriginUpload,
   174  		},
   175  		ServiceID: "svc",
   176  		Username:  "a-user",
   177  		Timestamp: time.Date(2012, 7, 8, 15, 59, 5, 5, time.UTC),
   178  	}
   179  
   180  	err := res.Validate()
   181  	c.Check(err, jc.ErrorIsNil)
   182  
   183  	c.Check(res.RevisionString(), gc.Equals, "2012-07-08 15:59:05 +0000 UTC")
   184  }
   185  
   186  func (ResourceSuite) TestRevisionStringNumber(c *gc.C) {
   187  	res := resource.Resource{
   188  		Resource: charmresource.Resource{
   189  			Meta: charmresource.Meta{
   190  				Name:        "foo",
   191  				Type:        charmresource.TypeFile,
   192  				Path:        "foo.tgz",
   193  				Description: "you need it",
   194  			},
   195  			Origin:   charmresource.OriginStore,
   196  			Revision: 7,
   197  		},
   198  		ServiceID: "svc",
   199  		Username:  "a-user",
   200  		Timestamp: time.Date(2012, 7, 8, 15, 59, 5, 5, time.UTC),
   201  	}
   202  
   203  	err := res.Validate()
   204  	c.Check(err, jc.ErrorIsNil)
   205  
   206  	c.Check(res.RevisionString(), gc.Equals, "7")
   207  }
   208  
   209  func (s *ResourceSuite) TestAsMap(c *gc.C) {
   210  	spam := newStoreResource(c, "spam", "a-service", 2)
   211  	eggs := newStoreResource(c, "eggs", "a-service", 3)
   212  	resources := []resource.Resource{
   213  		spam,
   214  		eggs,
   215  	}
   216  
   217  	resMap := resource.AsMap(resources)
   218  
   219  	c.Check(resMap, jc.DeepEquals, map[string]resource.Resource{
   220  		"spam": spam,
   221  		"eggs": eggs,
   222  	})
   223  }