github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/overlord/patch/patch6_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/overlord/patch"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  )
    33  
    34  type patch6Suite struct{}
    35  
    36  var _ = Suite(&patch6Suite{})
    37  
    38  var statePatch5JSON = []byte(`
    39  {
    40  	"last-task-id": 999,
    41  	"last-change-id": 99,
    42  
    43  	"data": {
    44  		"patch-level": 5,
    45  		"snaps": {
    46  			"a": {
    47  				"sequence": [{"name": "", "revision": "2"}],
    48                                  "flags": 1,
    49  				"current": "2"},
    50  			"b": {
    51  				"sequence": [{"name": "b", "revision": "2"}],
    52                                  "flags": 2,
    53  				"current": "2"},
    54  			"c": {
    55  				"sequence": [{"name": "c", "revision": "2"}],
    56                                  "flags": 4,
    57  				"current": "2"}
    58  		}
    59  	},
    60  	"changes": {
    61  		"1": {
    62  			"id": "1",
    63  			"kind": "install-snap",
    64  			"summary": "install a snap",
    65  			"status": 0,
    66  			"data": {"snap-names": ["a"]},
    67  			"task-ids": ["11","12"]
    68  		},
    69  		"2": {
    70  			"id": "2",
    71  			"kind": "install-snap",
    72  			"summary": "install b snap",
    73  			"status": 0,
    74  			"data": {"snap-names": ["b"]},
    75  			"task-ids": ["11","12"]
    76  		},
    77  		"3": {
    78  			"id": "3",
    79  			"kind": "revert-snap",
    80  			"summary": "revert c snap",
    81  			"status": 0,
    82  			"data": {"snap-names": ["c"]},
    83  			"task-ids": ["21","22"]
    84  		}
    85  	},
    86  	"tasks": {
    87                  "11": {
    88                          "id": "11",
    89                          "change": "1",
    90                          "kind": "download-snap",
    91                          "summary": "Download snap a from channel edge",
    92                          "status": 4,
    93                          "data": {"snap-setup": {
    94                                  "channel": "edge",
    95                                  "flags": 1
    96                          }},
    97                          "halt-tasks": ["12"]
    98                  },
    99                  "12": {"id": "12", "change": "1", "kind": "some-other-task"},
   100                  "21": {
   101                          "id": "21",
   102                          "change": "2",
   103                          "kind": "download-snap",
   104                          "summary": "Download snap b from channel beta",
   105                          "status": 4,
   106                          "data": {"snap-setup": {
   107                                  "channel": "beta",
   108                                  "flags": 2
   109                          }},
   110                          "halt-tasks": ["22"]
   111                  },
   112                  "22": {"id": "22", "change": "2", "kind": "some-other-task"},
   113                  "31": {
   114                          "id": "31",
   115                          "change": "3",
   116                          "kind": "prepare-snap",
   117                          "summary": "Prepare snap c",
   118                          "status": 4,
   119                          "data": {"snap-setup": {
   120                                  "channel": "stable",
   121                                  "flags": 1073741828
   122                          }},
   123                          "halt-tasks": ["32"]
   124                  },
   125                  "32": {"id": "32", "change": "3", "kind": "some-other-task"}
   126  	}
   127  }
   128  `)
   129  
   130  func (s *patch6Suite) SetUpTest(c *C) {
   131  	dirs.SetRootDir(c.MkDir())
   132  
   133  	err := os.MkdirAll(filepath.Dir(dirs.SnapStateFile), 0755)
   134  	c.Assert(err, IsNil)
   135  	err = ioutil.WriteFile(dirs.SnapStateFile, statePatch5JSON, 0644)
   136  	c.Assert(err, IsNil)
   137  }
   138  
   139  func (s *patch6Suite) TestPatch6(c *C) {
   140  	restorer := patch.MockLevel(6, 1)
   141  	defer restorer()
   142  
   143  	r, err := os.Open(dirs.SnapStateFile)
   144  	c.Assert(err, IsNil)
   145  	defer r.Close()
   146  	st, err := state.ReadState(nil, r)
   147  	c.Assert(err, IsNil)
   148  
   149  	func() {
   150  		st.Lock()
   151  		defer st.Unlock()
   152  
   153  		stateMap, err := patch.Patch4StateMap(st)
   154  		c.Assert(err, IsNil)
   155  		c.Check(int(stateMap["a"].Flags), Equals, 1)
   156  		c.Check(int(stateMap["b"].Flags), Equals, 2)
   157  		c.Check(int(stateMap["c"].Flags), Equals, 4)
   158  	}()
   159  
   160  	c.Assert(patch.Apply(st), IsNil)
   161  
   162  	st.Lock()
   163  	defer st.Unlock()
   164  
   165  	stateMap, err := patch.Patch6StateMap(st)
   166  	c.Assert(err, IsNil)
   167  
   168  	c.Check(stateMap["a"].DevMode, Equals, true)
   169  	c.Check(stateMap["a"].TryMode, Equals, false)
   170  	c.Check(stateMap["a"].JailMode, Equals, false)
   171  
   172  	c.Check(stateMap["b"].DevMode, Equals, false)
   173  	c.Check(stateMap["b"].TryMode, Equals, true)
   174  	c.Check(stateMap["b"].JailMode, Equals, false)
   175  
   176  	c.Check(stateMap["c"].DevMode, Equals, false)
   177  	c.Check(stateMap["c"].TryMode, Equals, false)
   178  	c.Check(stateMap["c"].JailMode, Equals, true)
   179  
   180  	for _, task := range st.Tasks() {
   181  		snapsup, err := patch.Patch6SnapSetup(task)
   182  		if err == state.ErrNoState {
   183  			continue
   184  		}
   185  		c.Assert(err, IsNil)
   186  
   187  		var snaps []string
   188  		c.Assert(task.Change().Get("snap-names", &snaps), IsNil)
   189  		c.Assert(snaps, HasLen, 1)
   190  
   191  		switch snaps[0] {
   192  		case "a":
   193  			c.Check(snapsup.DevMode, Equals, true, Commentf("a"))
   194  			c.Check(snapsup.TryMode, Equals, false, Commentf("a"))
   195  			c.Check(snapsup.JailMode, Equals, false, Commentf("a"))
   196  			c.Check(snapsup.Revert, Equals, false, Commentf("a"))
   197  		case "b":
   198  			c.Check(snapsup.DevMode, Equals, false, Commentf("b"))
   199  			c.Check(snapsup.TryMode, Equals, true, Commentf("b"))
   200  			c.Check(snapsup.JailMode, Equals, false, Commentf("b"))
   201  			c.Check(snapsup.Revert, Equals, false, Commentf("b"))
   202  		case "c":
   203  			c.Check(snapsup.DevMode, Equals, false, Commentf("c"))
   204  			c.Check(snapsup.TryMode, Equals, false, Commentf("c"))
   205  			c.Check(snapsup.JailMode, Equals, true, Commentf("c"))
   206  			c.Check(snapsup.Revert, Equals, true, Commentf("c"))
   207  		}
   208  	}
   209  }