github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/patch/patch2_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/snapstate" 32 "github.com/snapcore/snapd/overlord/state" 33 "github.com/snapcore/snapd/snap" 34 ) 35 36 type patch2Suite struct{} 37 38 var _ = Suite(&patch2Suite{}) 39 40 var statePatch2JSON = []byte(` 41 { 42 "data": { 43 "patch-level": 1, 44 "snaps": { 45 "foo": { 46 "sequence": [{ 47 "name": "", 48 "revision": "x1" 49 }, { 50 "name": "", 51 "revision": "x2" 52 }], 53 "current": "x2" 54 }, 55 "bar": { 56 "candidate": { 57 "name": "", 58 "revision": "x1", 59 "snapid": "mysnapid" 60 } 61 } 62 } 63 }, 64 "changes": { 65 "1": { 66 "id": "1", 67 "kind": "some-change", 68 "summary": "summary-1", 69 "status": 0, 70 "task-ids": ["1"] 71 }, 72 "2": { 73 "id": "2", 74 "kind": "some-other-change", 75 "summary": "summary-2", 76 "status": 0, 77 "task-ids": ["2"] 78 } 79 }, 80 "tasks": { 81 "1": { 82 "id": "1", 83 "kind": "something", 84 "summary": "meep", 85 "status": 4, 86 "data": { 87 "snap-setup": { 88 "name": "foo", 89 "revision": "x3" 90 } 91 }, 92 "halt-tasks": [ 93 "7" 94 ], 95 "change": "1" 96 }, 97 "2": { 98 "id": "2", 99 "kind": "something-else", 100 "summary": "meep", 101 "status": 4, 102 "data": { 103 "snap-setup": { 104 "name": "bar", 105 "revision": "26" 106 } 107 }, 108 "halt-tasks": [ 109 "3" 110 ], 111 "change": "2" 112 } 113 } 114 } 115 `) 116 117 func (s *patch2Suite) SetUpTest(c *C) { 118 dirs.SetRootDir(c.MkDir()) 119 120 err := os.MkdirAll(filepath.Dir(dirs.SnapStateFile), 0755) 121 c.Assert(err, IsNil) 122 err = ioutil.WriteFile(dirs.SnapStateFile, statePatch2JSON, 0644) 123 c.Assert(err, IsNil) 124 } 125 126 func (s *patch2Suite) TestPatch2(c *C) { 127 restorer := patch.MockLevel(2, 1) 128 defer restorer() 129 130 r, err := os.Open(dirs.SnapStateFile) 131 c.Assert(err, IsNil) 132 defer r.Close() 133 st, err := state.ReadState(nil, r) 134 c.Assert(err, IsNil) 135 136 // go from patch level 1 -> 2 137 err = patch.Apply(st) 138 c.Assert(err, IsNil) 139 140 st.Lock() 141 defer st.Unlock() 142 143 // our mocks are correct 144 c.Assert(st.Changes(), HasLen, 2) 145 c.Assert(st.Tasks(), HasLen, 2) 146 147 var snapsup snapstate.SnapSetup 148 // transition of: 149 // - SnapSetup.{Name,Revision} -> SnapSetup.SideInfo.{RealName,Revision} 150 t := st.Task("1") 151 err = t.Get("snap-setup", &snapsup) 152 c.Assert(err, IsNil) 153 c.Assert(snapsup.SideInfo, DeepEquals, &snap.SideInfo{ 154 RealName: "foo", 155 Revision: snap.R("x3"), 156 }) 157 158 // transition of: 159 // - SnapState.Sequence is backfilled with "RealName" (if missing) 160 var snapst snapstate.SnapState 161 err = snapstate.Get(st, "foo", &snapst) 162 c.Assert(err, IsNil) 163 c.Check(snapst.Sequence[0].RealName, Equals, "foo") 164 c.Check(snapst.Sequence[1].RealName, Equals, "foo") 165 166 // transition of: 167 // - Candidate for "bar" -> tasks SnapSetup.SideInfo 168 t = st.Task("2") 169 err = t.Get("snap-setup", &snapsup) 170 c.Assert(err, IsNil) 171 c.Assert(snapsup.SideInfo, DeepEquals, &snap.SideInfo{ 172 RealName: "bar", 173 Revision: snap.R("x1"), 174 }) 175 176 // FIXME: bar is now empty and should no longer be there? 177 err = snapstate.Get(st, "bar", &snapst) 178 c.Assert(err, IsNil) 179 }