github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/environs/tools/marshal_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package tools_test
     5  
     6  import (
     7  	"encoding/json"
     8  	"time"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/environs/tools"
    14  )
    15  
    16  var _ = gc.Suite(&marshalSuite{})
    17  
    18  type marshalSuite struct {
    19  	streamMetadata map[string][]*tools.ToolsMetadata
    20  }
    21  
    22  func (s *marshalSuite) SetUpTest(c *gc.C) {
    23  	s.streamMetadata = map[string][]*tools.ToolsMetadata{
    24  		"released": releasedToolMetadataForTesting,
    25  		"proposed": proposedToolMetadataForTesting,
    26  	}
    27  }
    28  
    29  func (s *marshalSuite) TestLargeNumber(c *gc.C) {
    30  	metadata := map[string][]*tools.ToolsMetadata{
    31  		"released": {
    32  			{
    33  				Release:  "ubuntu",
    34  				Version:  "1.2.3.4",
    35  				Arch:     "arm",
    36  				Size:     9223372036854775807,
    37  				Path:     "/somewhere/over/the/rainbow.tar.gz",
    38  				FileType: "tar.gz",
    39  			}},
    40  	}
    41  	_, _, products, err := tools.MarshalToolsMetadataJSON(metadata, time.Now())
    42  	c.Assert(err, jc.ErrorIsNil)
    43  	c.Assert(string(products["released"]), jc.Contains, `"size": 9223372036854775807`)
    44  }
    45  
    46  var expectedIndex = `{
    47      "index": {
    48          "com.ubuntu.juju:proposed:agents": {
    49              "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    50              "format": "products:1.0",
    51              "datatype": "content-download",
    52              "path": "streams/v1/com.ubuntu.juju-proposed-agents.json",
    53              "products": [
    54                  "com.ubuntu.juju:ubuntu:arm64",
    55                  "com.ubuntu.juju:ubuntu:ppc64el"            
    56              ]
    57          },
    58          "com.ubuntu.juju:released:agents": {
    59              "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    60              "format": "products:1.0",
    61              "datatype": "content-download",
    62              "path": "streams/v1/com.ubuntu.juju-released-agents.json",
    63              "products": [
    64                  "com.ubuntu.juju:ubuntu:amd64",
    65                  "com.ubuntu.juju:ubuntu:arm"
    66              ]
    67          }
    68      },
    69      "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    70      "format": "index:1.0"
    71  }`
    72  
    73  var expectedLegacyIndex = `{
    74      "index": {
    75          "com.ubuntu.juju:released:agents": {
    76              "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    77              "format": "products:1.0",
    78              "datatype": "content-download",
    79              "path": "streams/v1/com.ubuntu.juju-released-agents.json",
    80              "products": [
    81                  "com.ubuntu.juju:ubuntu:amd64",
    82                  "com.ubuntu.juju:ubuntu:arm"
    83              ]
    84          }
    85      },
    86      "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
    87      "format": "index:1.0"
    88  }`
    89  
    90  var expectedReleasedProducts = `{
    91      "products": {
    92          "com.ubuntu.juju:ubuntu:amd64": {
    93              "version": "4.3.2.1",
    94              "arch": "amd64",
    95              "versions": {
    96                  "19700101": {
    97                      "items": {
    98                          "4.3.2.1-ubuntu-amd64": {
    99                              "release": "ubuntu",
   100                              "version": "4.3.2.1",
   101                              "arch": "amd64",
   102                              "size": 0,
   103                              "path": "whatever.tar.gz",
   104                              "ftype": "tar.gz",
   105                              "sha256": "afb14e65c794464e378def12cbad6a96f9186d69"
   106                          }
   107                      }
   108                  }
   109              }
   110          },
   111          "com.ubuntu.juju:ubuntu:arm": {
   112              "version": "1.2.3.4",
   113              "arch": "arm",
   114              "versions": {
   115                  "19700101": {
   116                      "items": {
   117                          "1.2.3.4-ubuntu-arm": {
   118                              "release": "ubuntu",
   119                              "version": "1.2.3.4",
   120                              "arch": "arm",
   121                              "size": 9223372036854775807,
   122                              "path": "/somewhere/over/the/rainbow.tar.gz",
   123                              "ftype": "tar.gz",
   124                              "sha256": ""
   125                          }
   126                      }
   127                  }
   128              }
   129          }
   130      },
   131      "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
   132      "format": "products:1.0",
   133      "content_id": "com.ubuntu.juju:released:agents"
   134  }`
   135  
   136  var expectedProposedProducts = `{
   137      "products": {
   138          "com.ubuntu.juju:ubuntu:arm64": {
   139              "version": "1.2-beta1",
   140              "arch": "arm64",
   141              "versions": {
   142                  "19700101": {
   143                      "items": {
   144                          "1.2-beta1-ubuntu-arm64": {
   145                              "release": "ubuntu",
   146                              "version": "1.2-beta1",
   147                              "arch": "arm64",
   148                              "size": 42,
   149                              "path": "gotham.tar.gz",
   150                              "ftype": "tar.gz",
   151                              "sha256": ""
   152                          }
   153                      }
   154                  }
   155              }
   156          },
   157          "com.ubuntu.juju:ubuntu:ppc64el": {
   158              "version": "1.2-alpha1",
   159              "arch": "ppc64el",
   160              "versions": {
   161                  "19700101": {
   162                      "items": {
   163                          "1.2-alpha1-ubuntu-ppc64el": {
   164                              "release": "ubuntu",
   165                              "version": "1.2-alpha1",
   166                              "arch": "ppc64el",
   167                              "size": 9223372036854775807,
   168                              "path": "/funkytown.tar.gz",
   169                              "ftype": "tar.gz",
   170                              "sha256": ""
   171                          }
   172                      }
   173                  }
   174              }
   175          }
   176      },
   177      "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
   178      "format": "products:1.0",
   179      "content_id": "com.ubuntu.juju:proposed:agents"
   180  }`
   181  
   182  var releasedToolMetadataForTesting = []*tools.ToolsMetadata{
   183  	{
   184  		Release:  "ubuntu",
   185  		Version:  "1.2.3.4",
   186  		Arch:     "arm",
   187  		Size:     9223372036854775807,
   188  		Path:     "/somewhere/over/the/rainbow.tar.gz",
   189  		FileType: "tar.gz",
   190  	},
   191  	{
   192  		Release:  "ubuntu",
   193  		Version:  "4.3.2.1",
   194  		Arch:     "amd64",
   195  		Path:     "whatever.tar.gz",
   196  		FileType: "tar.gz",
   197  		SHA256:   "afb14e65c794464e378def12cbad6a96f9186d69",
   198  	},
   199  	{
   200  		Release:  "xuanhuaceratops",
   201  		Version:  "4.3.2.1",
   202  		Arch:     "amd64",
   203  		Size:     42,
   204  		Path:     "dinodance.tar.gz",
   205  		FileType: "tar.gz",
   206  	},
   207  	{
   208  		Release:  "xuanhanosaurus",
   209  		Version:  "5.4.3.2",
   210  		Arch:     "amd64",
   211  		Size:     42,
   212  		Path:     "dinodisco.tar.gz",
   213  		FileType: "tar.gz",
   214  	},
   215  }
   216  
   217  var proposedToolMetadataForTesting = []*tools.ToolsMetadata{
   218  	{
   219  		Release:  "ubuntu",
   220  		Version:  "1.2-alpha1",
   221  		Arch:     "ppc64el",
   222  		Size:     9223372036854775807,
   223  		Path:     "/funkytown.tar.gz",
   224  		FileType: "tar.gz",
   225  	},
   226  	{
   227  		Release:  "ubuntu",
   228  		Version:  "1.2-beta1",
   229  		Arch:     "arm64",
   230  		Size:     42,
   231  		Path:     "gotham.tar.gz",
   232  		FileType: "tar.gz",
   233  	},
   234  	{
   235  		Release:  "xuanhuaceratops",
   236  		Version:  "4.3.2.1",
   237  		Arch:     "amd64",
   238  		Size:     42,
   239  		Path:     "dinodance.tar.gz",
   240  		FileType: "tar.gz",
   241  	},
   242  	{
   243  		Release:  "xuanhanosaurus",
   244  		Version:  "5.4.3.2",
   245  		Arch:     "amd64",
   246  		Size:     42,
   247  		Path:     "dinodisco.tar.gz",
   248  		FileType: "tar.gz",
   249  	},
   250  }
   251  
   252  func (s *marshalSuite) TestMarshalIndex(c *gc.C) {
   253  	index, legacyIndex, err := tools.MarshalToolsMetadataIndexJSON(s.streamMetadata, time.Unix(0, 0).UTC())
   254  	c.Assert(err, jc.ErrorIsNil)
   255  	assertIndex(c, index, expectedIndex)
   256  	assertIndex(c, legacyIndex, expectedLegacyIndex)
   257  }
   258  
   259  func assertIndex(c *gc.C, obtainedIndex []byte, expectedIndex string) {
   260  	// Unmarshall into objects so an order independent comparison can be done.
   261  	var obtained interface{}
   262  	err := json.Unmarshal(obtainedIndex, &obtained)
   263  	c.Assert(err, jc.ErrorIsNil)
   264  	var expected interface{}
   265  	err = json.Unmarshal([]byte(expectedIndex), &expected)
   266  	c.Assert(err, jc.ErrorIsNil)
   267  	c.Assert(obtained, jc.DeepEquals, expected)
   268  }
   269  
   270  func (s *marshalSuite) TestMarshalProducts(c *gc.C) {
   271  	products, err := tools.MarshalToolsMetadataProductsJSON(s.streamMetadata, time.Unix(0, 0).UTC())
   272  	c.Assert(err, jc.ErrorIsNil)
   273  	assertProducts(c, products)
   274  }
   275  
   276  func assertProducts(c *gc.C, obtainedProducts map[string][]byte) {
   277  	c.Assert(obtainedProducts, gc.HasLen, 2)
   278  	c.Assert(string(obtainedProducts["released"]), gc.Equals, expectedReleasedProducts)
   279  	c.Assert(string(obtainedProducts["proposed"]), gc.Equals, expectedProposedProducts)
   280  }
   281  
   282  func (s *marshalSuite) TestMarshal(c *gc.C) {
   283  	index, legacyIndex, products, err := tools.MarshalToolsMetadataJSON(s.streamMetadata, time.Unix(0, 0).UTC())
   284  	c.Assert(err, jc.ErrorIsNil)
   285  	assertIndex(c, index, expectedIndex)
   286  	assertIndex(c, legacyIndex, expectedLegacyIndex)
   287  	assertProducts(c, products)
   288  }
   289  
   290  func (s *marshalSuite) TestMarshalNoReleaseStream(c *gc.C) {
   291  	metadata := s.streamMetadata
   292  	delete(metadata, "released")
   293  	index, legacyIndex, products, err := tools.MarshalToolsMetadataJSON(s.streamMetadata, time.Unix(0, 0).UTC())
   294  	c.Assert(err, jc.ErrorIsNil)
   295  	c.Assert(legacyIndex, gc.IsNil)
   296  	c.Assert(index, gc.NotNil)
   297  	c.Assert(products, gc.NotNil)
   298  }