github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/patch/patch3_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 patch3Suite struct{} 35 36 var _ = Suite(&patch3Suite{}) 37 38 var statePatch3JSON = []byte(` 39 { 40 "last-task-id": 999, 41 "last-change-id": 99, 42 43 "data": { 44 "patch-level": 2 45 }, 46 "changes": { 47 "1": { 48 "id": "1", 49 "kind": "some-change", 50 "summary": "summary-1", 51 "status": 0, 52 "task-ids": ["1"] 53 } 54 }, 55 "tasks": { 56 "1": { 57 "id": "1", 58 "kind": "link-snap", 59 "summary": "meep", 60 "status": 2, 61 "halt-tasks": [ 62 "7" 63 ], 64 "change": "1" 65 }, 66 "2": { 67 "id": "2", 68 "kind": "unlink-snap", 69 "summary": "meep", 70 "status": 2, 71 "halt-tasks": [ 72 "3" 73 ], 74 "change": "1" 75 }, 76 "3": { 77 "id": "3", 78 "kind": "unrelated", 79 "summary": "meep", 80 "status": 4, 81 "halt-tasks": [ 82 "3" 83 ], 84 "change": "1" 85 }, 86 "4": { 87 "id": "4", 88 "kind": "unlink-current-snap", 89 "summary": "meep", 90 "status": 2, 91 "halt-tasks": [ 92 "3" 93 ], 94 "change": "1" 95 } 96 } 97 } 98 `) 99 100 func (s *patch3Suite) SetUpTest(c *C) { 101 dirs.SetRootDir(c.MkDir()) 102 103 err := os.MkdirAll(filepath.Dir(dirs.SnapStateFile), 0755) 104 c.Assert(err, IsNil) 105 err = ioutil.WriteFile(dirs.SnapStateFile, statePatch3JSON, 0644) 106 c.Assert(err, IsNil) 107 } 108 109 func (s *patch3Suite) TestPatch3(c *C) { 110 restorer := patch.MockLevel(3, 1) 111 defer restorer() 112 113 r, err := os.Open(dirs.SnapStateFile) 114 c.Assert(err, IsNil) 115 defer r.Close() 116 st, err := state.ReadState(nil, r) 117 c.Assert(err, IsNil) 118 119 // our mocks are correct 120 st.Lock() 121 c.Assert(st.Tasks(), HasLen, 4) 122 st.Unlock() 123 124 // go from patch level 2 -> 3 125 err = patch.Apply(st) 126 c.Assert(err, IsNil) 127 128 st.Lock() 129 defer st.Unlock() 130 131 // we got two more tasks 132 c.Assert(st.Tasks(), HasLen, 7) 133 for _, t := range st.Tasks() { 134 switch t.Kind() { 135 case "start-snap-services": 136 ht := t.WaitTasks() 137 c.Check(ht, HasLen, 1) 138 c.Check(ht[0].Kind(), Equals, "link-snap") 139 case "unlink-snap": 140 ht := t.WaitTasks() 141 c.Check(ht, HasLen, 1) 142 c.Check(ht[0].Kind(), Equals, "stop-snap-services") 143 case "unlink-current-snap": 144 ht := t.WaitTasks() 145 c.Check(ht, HasLen, 1) 146 c.Check(ht[0].Kind(), Equals, "stop-snap-services") 147 } 148 } 149 }