github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/environs/imagemetadata/marshal_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package imagemetadata_test
     5  
     6  import (
     7  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/environs/imagemetadata"
    13  	"github.com/juju/juju/environs/simplestreams"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  var _ = gc.Suite(&marshalSuite{})
    18  
    19  type marshalSuite struct {
    20  	testing.BaseSuite
    21  }
    22  
    23  var expectedIndex = `{
    24      "index": {
    25          "com.ubuntu.cloud:custom": {
    26              "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    27              "format": "products:1.0",
    28              "datatype": "image-ids",
    29              "cloudname": "custom",
    30              "clouds": [
    31                  {
    32                      "region": "region",
    33                      "endpoint": "endpoint"
    34                  }
    35              ],
    36              "path": "streams/v1/com.ubuntu.cloud-released-imagemetadata.json",
    37              "products": [
    38                  "com.ubuntu.cloud:server:12.04:amd64",
    39                  "com.ubuntu.cloud:server:12.04:arm",
    40                  "com.ubuntu.cloud:server:13.10:arm"
    41              ]
    42          }
    43      },
    44      "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    45      "format": "index:1.0"
    46  }`
    47  
    48  var expectedProducts = `{
    49      "products": {
    50          "com.ubuntu.cloud:server:12.04:amd64": {
    51              "version": "12.04",
    52              "arch": "amd64",
    53              "versions": {
    54                  "19700101": {
    55                      "items": {
    56                          "abcd": {
    57                              "id": "abcd",
    58                              "root_store": "root",
    59                              "virt": "virt"
    60                          }
    61                      }
    62                  }
    63              }
    64          },
    65          "com.ubuntu.cloud:server:12.04:arm": {
    66              "version": "12.04",
    67              "arch": "arm",
    68              "versions": {
    69                  "19700101": {
    70                      "items": {
    71                          "5678": {
    72                              "id": "5678"
    73                          }
    74                      }
    75                  }
    76              }
    77          },
    78          "com.ubuntu.cloud:server:13.10:arm": {
    79              "version": "13.10",
    80              "arch": "arm",
    81              "versions": {
    82                  "19700101": {
    83                      "items": {
    84                          "1234": {
    85                              "id": "1234"
    86                          }
    87                      }
    88                  }
    89              }
    90          }
    91      },
    92      "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    93      "format": "products:1.0",
    94      "content_id": "com.ubuntu.cloud:custom"
    95  }`
    96  
    97  var imageMetadataForTesting = []*imagemetadata.ImageMetadata{
    98  	{
    99  		Id:      "1234",
   100  		Version: "13.10",
   101  		Arch:    "arm",
   102  	},
   103  	{
   104  		Id:      "5678",
   105  		Version: "12.04",
   106  		Arch:    "arm",
   107  	},
   108  	{
   109  		Id:       "abcd",
   110  		Version:  "12.04",
   111  		Arch:     "amd64",
   112  		VirtType: "virt",
   113  		Storage:  "root",
   114  	},
   115  }
   116  
   117  func (s *marshalSuite) TestMarshalIndex(c *gc.C) {
   118  	cloudSpec := []simplestreams.CloudSpec{{Region: "region", Endpoint: "endpoint"}}
   119  	index, err := imagemetadata.MarshalImageMetadataIndexJSON(imageMetadataForTesting, cloudSpec, time.Unix(0, 0).UTC())
   120  	c.Assert(err, jc.ErrorIsNil)
   121  	c.Assert(string(index), gc.Equals, expectedIndex)
   122  }
   123  
   124  func (s *marshalSuite) TestMarshalProducts(c *gc.C) {
   125  	products, err := imagemetadata.MarshalImageMetadataProductsJSON(imageMetadataForTesting, time.Unix(0, 0).UTC())
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Assert(string(products), gc.Equals, expectedProducts)
   128  }
   129  
   130  func (s *marshalSuite) TestMarshal(c *gc.C) {
   131  	cloudSpec := []simplestreams.CloudSpec{{Region: "region", Endpoint: "endpoint"}}
   132  	index, products, err := imagemetadata.MarshalImageMetadataJSON(imageMetadataForTesting, cloudSpec, time.Unix(0, 0).UTC())
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	c.Assert(string(index), gc.Equals, expectedIndex)
   135  	c.Assert(string(products), gc.Equals, expectedProducts)
   136  }