github.com/juju/charmrepo/v7@v7.0.1/csclient/params/helpers_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the LGPLv3, see LICENCE file for details.
     3  
     4  package params_test // import "github.com/juju/charmrepo/v7/csclient/params"
     5  
     6  import (
     7  	"github.com/juju/charm/v9/resource"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/charmrepo/v7/csclient/params"
    13  )
    14  
    15  const fingerprint = "123456789012345678901234567890123456789012345678"
    16  
    17  type HelpersSuite struct {
    18  	testing.IsolationSuite
    19  }
    20  
    21  var _ = gc.Suite(&HelpersSuite{})
    22  
    23  func (HelpersSuite) TestResource2API(c *gc.C) {
    24  	fp, err := resource.NewFingerprint([]byte(fingerprint))
    25  	c.Assert(err, jc.ErrorIsNil)
    26  	res := resource.Resource{
    27  		Meta: resource.Meta{
    28  			Name:        "spam",
    29  			Type:        resource.TypeFile,
    30  			Path:        "spam.tgz",
    31  			Description: "you need it",
    32  		},
    33  		Origin:      resource.OriginUpload,
    34  		Revision:    0,
    35  		Fingerprint: fp,
    36  		Size:        10,
    37  	}
    38  	err = res.Validate()
    39  	c.Assert(err, jc.ErrorIsNil)
    40  	apiInfo := params.Resource2API(res)
    41  
    42  	c.Check(apiInfo, jc.DeepEquals, params.Resource{
    43  		Name:        "spam",
    44  		Type:        "file",
    45  		Path:        "spam.tgz",
    46  		Description: "you need it",
    47  		Revision:    0,
    48  		Fingerprint: []byte(fingerprint),
    49  		Size:        10,
    50  	})
    51  }
    52  
    53  func (HelpersSuite) TestAPI2ResourceFull(c *gc.C) {
    54  	res, err := params.API2Resource(params.Resource{
    55  		Name:        "spam",
    56  		Type:        "file",
    57  		Path:        "spam.tgz",
    58  		Description: "you need it",
    59  		Revision:    0,
    60  		Fingerprint: []byte(fingerprint),
    61  		Size:        10,
    62  	})
    63  	c.Assert(err, jc.ErrorIsNil)
    64  
    65  	fp, err := resource.NewFingerprint([]byte(fingerprint))
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	expected := resource.Resource{
    68  		Meta: resource.Meta{
    69  			Name:        "spam",
    70  			Type:        resource.TypeFile,
    71  			Path:        "spam.tgz",
    72  			Description: "you need it",
    73  		},
    74  		Origin:      resource.OriginStore,
    75  		Revision:    0,
    76  		Fingerprint: fp,
    77  		Size:        10,
    78  	}
    79  	err = expected.Validate()
    80  	c.Assert(err, jc.ErrorIsNil)
    81  
    82  	c.Check(res, jc.DeepEquals, expected)
    83  }
    84  
    85  func (HelpersSuite) TestAPI2ResourceBasic(c *gc.C) {
    86  	res, err := params.API2Resource(params.Resource{
    87  		Name: "spam",
    88  		Type: "file",
    89  		Path: "spam.tgz",
    90  	})
    91  	c.Assert(err, jc.ErrorIsNil)
    92  
    93  	expected := resource.Resource{
    94  		Meta: resource.Meta{
    95  			Name:        "spam",
    96  			Type:        resource.TypeFile,
    97  			Path:        "spam.tgz",
    98  			Description: "",
    99  		},
   100  		Origin:      resource.OriginStore,
   101  		Revision:    0,
   102  		Fingerprint: resource.Fingerprint{},
   103  		Size:        0,
   104  	}
   105  	err = expected.Validate()
   106  	c.Assert(err, jc.ErrorIsNil)
   107  
   108  	c.Check(res, jc.DeepEquals, expected)
   109  }
   110  
   111  func (HelpersSuite) TestAPI2ResourceNegativeRevision(c *gc.C) {
   112  	_, err := params.API2Resource(params.Resource{
   113  		Name:        "spam",
   114  		Type:        "file",
   115  		Path:        "spam.tgz",
   116  		Fingerprint: []byte(fingerprint),
   117  		Size:        20,
   118  		Revision:    -1,
   119  	})
   120  
   121  	c.Check(err, gc.ErrorMatches, `bad revision: must be non-negative, got -1`)
   122  }
   123  
   124  func (HelpersSuite) TestAPI2ResourceBadType(c *gc.C) {
   125  	_, err := params.API2Resource(params.Resource{
   126  		Name:        "spam",
   127  		Type:        "<bogus>",
   128  		Path:        "spam.tgz",
   129  		Revision:    0,
   130  		Fingerprint: []byte(fingerprint),
   131  		Size:        10,
   132  	})
   133  
   134  	c.Check(err, gc.ErrorMatches, `unsupported resource type "<bogus>"`)
   135  }
   136  
   137  func (HelpersSuite) TestAPI2ResourceBadFingerprint(c *gc.C) {
   138  	_, err := params.API2Resource(params.Resource{
   139  		Name:        "spam",
   140  		Type:        "file",
   141  		Path:        "spam.tgz",
   142  		Revision:    0,
   143  		Fingerprint: []byte(fingerprint + "1"),
   144  		Size:        10,
   145  	})
   146  
   147  	c.Check(err, gc.ErrorMatches, `invalid fingerprint \(too big\)`)
   148  }
   149  
   150  func (HelpersSuite) TestAPI2ResourceEmptyFingerprintNoSize(c *gc.C) {
   151  	res, err := params.API2Resource(params.Resource{
   152  		Name:        "spam",
   153  		Type:        "file",
   154  		Path:        "spam.tgz",
   155  		Revision:    0,
   156  		Fingerprint: nil,
   157  		Size:        0,
   158  	})
   159  	c.Assert(err, jc.ErrorIsNil)
   160  
   161  	expected := resource.Resource{
   162  		Meta: resource.Meta{
   163  			Name: "spam",
   164  			Type: resource.TypeFile,
   165  			Path: "spam.tgz",
   166  		},
   167  		Origin:      resource.OriginStore,
   168  		Revision:    0,
   169  		Fingerprint: resource.Fingerprint{},
   170  		Size:        0,
   171  	}
   172  	err = expected.Validate()
   173  	c.Assert(err, jc.ErrorIsNil)
   174  
   175  	c.Check(res, jc.DeepEquals, expected)
   176  }
   177  
   178  func (HelpersSuite) TestAPI2ResourceEmptyFingerprintWithSize(c *gc.C) {
   179  	_, err := params.API2Resource(params.Resource{
   180  		Name:        "spam",
   181  		Type:        "file",
   182  		Path:        "spam.tgz",
   183  		Revision:    0,
   184  		Fingerprint: nil,
   185  		Size:        10,
   186  	})
   187  
   188  	c.Check(err, gc.ErrorMatches, `bad file info: missing fingerprint`)
   189  }
   190  
   191  func (HelpersSuite) TestAPI2ResourceValidateFailed(c *gc.C) {
   192  	_, err := params.API2Resource(params.Resource{
   193  		Name:        "",
   194  		Type:        "file",
   195  		Path:        "spam.tgz",
   196  		Revision:    0,
   197  		Fingerprint: []byte(fingerprint),
   198  		Size:        10,
   199  	})
   200  
   201  	c.Check(err, gc.ErrorMatches, `.*resource missing name`)
   202  }