gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/patch/patch6_3_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package patch_test
    21  
    22  import (
    23  	"bytes"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/overlord/patch"
    29  	"github.com/snapcore/snapd/overlord/snapstate"
    30  	"github.com/snapcore/snapd/overlord/state"
    31  	"github.com/snapcore/snapd/snap"
    32  )
    33  
    34  type patch63Suite struct{}
    35  
    36  var _ = Suite(&patch63Suite{})
    37  
    38  var statePatch6_3JSON = []byte(`
    39  {
    40  	"data": {
    41  		"patch-level": 6,
    42  		"snaps": {
    43                    "local-install": {
    44  			"type": "app",
    45  			"sequence": [
    46  			  {
    47  				"name": "local-install",
    48                                  "snap-id": "",
    49  				"revision": "x1"
    50  			  },
    51  			  {
    52  				"name": "local-install",
    53  				"snap-id": "",
    54  				"revision": "x2"
    55  			  }
    56  			],
    57  			"active": true,
    58  			"current": "x2"
    59                    },
    60  		  "prefix-postfix-slashes": {
    61  			"type": "app",
    62  			"sequence": [
    63  			  {
    64  				"name": "prefix-postfix-slashes",
    65  				"snap-id": "Hswp9oOzj3b4mw8gcC00XtxWnKH9QiCQ",
    66  				"revision": "30",
    67                                  "channel": "edge",
    68                                  "title": "some-title"
    69  			  },
    70  			  {
    71  				"name": "prefix-postfix-slashes",
    72  				"snap-id": "Hswp9oOzj3b4mw8gcC00XtxWnKH9QiCQ",
    73  				"revision": "32",
    74                                  "channel": "edge",
    75                                  "title": "some-title"
    76  			  }
    77  			],
    78  			"active": true,
    79  			"current": "32",
    80  			"channel": "//edge//",
    81                          "user-id": 1
    82  		  },
    83  		  "one-prefix-slash": {
    84  			"type": "app",
    85  			"sequence": [
    86  			  {
    87  				"name": "white",
    88  				"snap-id": "one-prefix-slash-id",
    89  				"revision": "2"
    90  			  }
    91  			],
    92  			"active": true,
    93  			"current": "2",
    94  			"channel": "/stable"
    95  		  },
    96  		  "track-with-risk": {
    97  			"type": "app",
    98  			"sequence": [
    99  			  {
   100  				"name": "track-with-risk",
   101  				"snap-id": "track-with-snapid-id",
   102  				"revision": "3"
   103  			  }
   104  			],
   105  			"active": true,
   106  			"current": "3",
   107  			"channel": "latest/stable"
   108  		  },
   109  		  "track-with-risk-branch": {
   110  			"type": "app",
   111  			"sequence": [
   112  			  {
   113  				"name": "red",
   114  				"snap-id": "track-with-risk-branch-snapid-id",
   115  				"revision": "3"
   116  			  }
   117  			],
   118  			"active": true,
   119  			"current": "3",
   120  			"channel": "1.0/stable/branch"
   121  		  }
   122  		}
   123  	  },
   124           "changes": {
   125                         "6": {
   126                                 "id": "6",
   127                                 "kind": "auto-refresh",
   128                                 "summary": "...",
   129                                 "status": 0,
   130                                 "clean": true,
   131                                 "task-ids": ["1", "8"]
   132                         },
   133                         "9": {
   134                                 "id": "9",
   135                                 "kind": "install",
   136                                 "summary": "...",
   137                                 "status": 4,
   138                                 "clean": true,
   139                                 "task-ids": ["10"]
   140                         },
   141                         "99": {
   142                                 "id": "99",
   143                                 "kind": "install",
   144                                 "summary": "...",
   145                                 "status": 0,
   146                                 "clean": true,
   147                                 "task-ids": ["99"]
   148                         }
   149           },
   150           "tasks": {
   151                 "1": {
   152                         "id": "1",
   153                         "kind": "download-snap",
   154                         "summary": "...",
   155                         "status": 2,
   156                         "clean": true,
   157                         "data": {
   158                           "snap-setup": {
   159                                 "channel": "/stable",
   160                                 "type": "app",
   161                                 "is-auto-refresh": true,
   162                                 "snap-path": "/path",
   163                                 "download-info": {
   164                                   "download-url": "foo",
   165                                   "size": 1234,
   166                                   "sha3-384": "123456",
   167                                   "deltas": [
   168                                         {
   169                                           "from-revision": 10934,
   170                                           "to-revision": 10972,
   171                                           "format": "xdelta3",
   172                                           "download-url": "foo",
   173                                           "size": 16431136,
   174                                           "sha3-384": "1"
   175                                         }
   176                                   ]
   177                                 },
   178                                 "side-info": {
   179                                   "name": "other-snap",
   180                                   "snap-id": "other-snap-id",
   181                                   "revision": "1",
   182                                   "channel": "stable",
   183                                   "title": "other-snap"
   184                                 },
   185                                 "media": [
   186                                   {
   187                                         "type": "icon",
   188                                         "url": "a"
   189                                   },
   190                                   {
   191                                         "type": "screenshot",
   192                                         "url": "2"
   193                                   },
   194                                   {
   195                                         "type": "screenshot",
   196                                         "url": "3"
   197                                   },
   198                                   {
   199                                         "type": "screenshot",
   200                                         "url": "4"
   201                                   },
   202                                   {
   203                                         "type": "video",
   204                                         "url": "5"
   205                                   }
   206                                 ]
   207                           }
   208                         },
   209                         "change": "6"
   210                   },
   211                   "8": {
   212                         "id": "8",
   213                         "kind": "link-snap",
   214                         "summary": "",
   215                         "status": 2,
   216                         "data": {
   217                           "old-candidate-index": -1,
   218                           "old-channel": "/18/edge//",
   219                           "snap-setup-task": "1"
   220                        },
   221                        "change": "6"
   222                   },
   223                   "10": {
   224                         "id": "10",
   225                         "kind": "other",
   226                         "summary": "",
   227                         "status": 4,
   228                         "data": {
   229                           "old-channel": "/edge",
   230                           "snap-setup": {
   231                                 "channel": "/stable",
   232                                 "type": "app",
   233                                  "snap-path": "/path",
   234                                  "side-info": {
   235                                   "name": "some-snap",
   236                                   "snap-id": "some-snap-id",
   237                                   "revision": "1",
   238                                   "channel": "stable",
   239                                   "title": "snapd"
   240                                 }
   241                            }
   242                         },
   243                         "change": "9"
   244                   },
   245                   "99": {
   246                         "id": "99",
   247                         "kind": "prepare-snap",
   248                         "summary": "",
   249                         "status": 0,
   250                         "data": {
   251                           "snap-setup": {
   252                                 "type": "app",
   253                                 "snap-path": "/path",
   254                                 "side-info": {
   255                                   "name": "local-snap",
   256                                   "snap-id": "",
   257                                   "revision": "unset"
   258                                 }
   259                            }
   260                         },
   261                         "change": "99"
   262                   }
   263          }
   264  }`)
   265  
   266  func (s *patch63Suite) SetUpTest(c *C) {
   267  	dirs.SetRootDir(c.MkDir())
   268  	snap.MockSanitizePlugsSlots(func(*snap.Info) {})
   269  }
   270  
   271  func (s *patch63Suite) TestPatch63(c *C) {
   272  	restore1 := patch.MockLevel(6, 3)
   273  	defer restore1()
   274  
   275  	r := bytes.NewReader(statePatch6_3JSON)
   276  	st, err := state.ReadState(nil, r)
   277  	c.Assert(err, IsNil)
   278  
   279  	c.Assert(patch.Apply(st), IsNil)
   280  	st.Lock()
   281  	defer st.Unlock()
   282  
   283  	all, err := snapstate.All(st)
   284  	c.Assert(err, IsNil)
   285  	// our mocks are ok
   286  	c.Check(all, HasLen, 5)
   287  	c.Check(all["prefix-postfix-slashes"], NotNil)
   288  	// our patch changed this
   289  	c.Check(all["prefix-postfix-slashes"].TrackingChannel, Equals, "latest/edge")
   290  	// none of the other information has changed
   291  	c.Check(all["prefix-postfix-slashes"], DeepEquals, &snapstate.SnapState{
   292  		SnapType: "app",
   293  		Sequence: []*snap.SideInfo{
   294  			{
   295  				RealName:    "prefix-postfix-slashes",
   296  				SnapID:      "Hswp9oOzj3b4mw8gcC00XtxWnKH9QiCQ",
   297  				Revision:    snap.R(30),
   298  				Channel:     "edge",
   299  				EditedTitle: "some-title",
   300  			}, {
   301  				RealName:    "prefix-postfix-slashes",
   302  				SnapID:      "Hswp9oOzj3b4mw8gcC00XtxWnKH9QiCQ",
   303  				Revision:    snap.R(32),
   304  				Channel:     "edge",
   305  				EditedTitle: "some-title",
   306  			},
   307  		},
   308  		Active:          true,
   309  		Current:         snap.R(32),
   310  		TrackingChannel: "latest/edge",
   311  		UserID:          1,
   312  	})
   313  	// another transition
   314  	c.Check(all["one-prefix-slash"].TrackingChannel, Equals, "latest/stable")
   315  	// full
   316  	c.Check(all["track-with-risk"].TrackingChannel, Equals, "latest/stable")
   317  	// unchanged
   318  	c.Check(all["track-with-risk-branch"].TrackingChannel, Equals, "1.0/stable/branch")
   319  	// also unchanged
   320  	c.Check(all["local-install"].TrackingChannel, Equals, "")
   321  
   322  	// check tasks
   323  	task := st.Task("1")
   324  	c.Assert(task, NotNil)
   325  	// this was converted
   326  	var snapsup snapstate.SnapSetup
   327  	err = task.Get("snap-setup", &snapsup)
   328  	c.Assert(err, IsNil)
   329  	c.Check(snapsup.Channel, Equals, "latest/stable")
   330  
   331  	// sanity check that old stuff is untouched
   332  	c.Check(snapsup.Flags.IsAutoRefresh, Equals, true)
   333  	c.Assert(snapsup.Media, HasLen, 5)
   334  	c.Check(snapsup.Media[0].URL, Equals, "a")
   335  	c.Assert(snapsup.DownloadInfo, NotNil)
   336  	c.Check(snapsup.DownloadInfo.DownloadURL, Equals, "foo")
   337  	c.Check(snapsup.DownloadInfo.Deltas, HasLen, 1)
   338  
   339  	// old-channel data got updated
   340  	task = st.Task("8")
   341  	c.Assert(task, NotNil)
   342  	var oldCh string
   343  	c.Assert(task.Get("old-channel", &oldCh), IsNil)
   344  	c.Check(oldCh, Equals, "18/edge")
   345  
   346  	// task 10 not updated because the change is ready
   347  	task = st.Task("10")
   348  	c.Assert(task, NotNil)
   349  	c.Assert(task.Get("snap-setup", &snapsup), IsNil)
   350  	c.Check(snapsup.Channel, Equals, "/stable")
   351  	err = task.Get("old-channel", &oldCh)
   352  	c.Assert(err, IsNil)
   353  	c.Check(oldCh, Equals, "/edge")
   354  }