github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/assertstate/validation_set_tracking_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 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 assertstate_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/overlord/assertstate" 26 "github.com/snapcore/snapd/overlord/state" 27 ) 28 29 type validationSetTrackingSuite struct { 30 st *state.State 31 } 32 33 var _ = Suite(&validationSetTrackingSuite{}) 34 35 func (s *validationSetTrackingSuite) SetUpTest(c *C) { 36 s.st = state.New(nil) 37 } 38 39 func (s *validationSetTrackingSuite) TestUpdate(c *C) { 40 s.st.Lock() 41 defer s.st.Unlock() 42 43 all, err := assertstate.ValidationSets(s.st) 44 c.Assert(err, IsNil) 45 c.Assert(all, HasLen, 0) 46 47 tr := assertstate.ValidationSetTracking{ 48 AccountID: "foo", 49 Name: "bar", 50 Mode: assertstate.Enforce, 51 PinnedAt: 1, 52 Current: 2, 53 } 54 assertstate.UpdateValidationSet(s.st, &tr) 55 56 all, err = assertstate.ValidationSets(s.st) 57 c.Assert(err, IsNil) 58 c.Assert(all, HasLen, 1) 59 for k, v := range all { 60 c.Check(k, Equals, "foo/bar") 61 c.Check(v, DeepEquals, &assertstate.ValidationSetTracking{AccountID: "foo", Name: "bar", Mode: assertstate.Enforce, PinnedAt: 1, Current: 2}) 62 } 63 64 tr = assertstate.ValidationSetTracking{ 65 AccountID: "foo", 66 Name: "bar", 67 Mode: assertstate.Monitor, 68 PinnedAt: 2, 69 Current: 3, 70 } 71 assertstate.UpdateValidationSet(s.st, &tr) 72 73 all, err = assertstate.ValidationSets(s.st) 74 c.Assert(err, IsNil) 75 c.Assert(all, HasLen, 1) 76 for k, v := range all { 77 c.Check(k, Equals, "foo/bar") 78 c.Check(v, DeepEquals, &assertstate.ValidationSetTracking{AccountID: "foo", Name: "bar", Mode: assertstate.Monitor, PinnedAt: 2, Current: 3}) 79 } 80 81 tr = assertstate.ValidationSetTracking{ 82 AccountID: "foo", 83 Name: "baz", 84 Mode: assertstate.Enforce, 85 Current: 3, 86 } 87 assertstate.UpdateValidationSet(s.st, &tr) 88 89 all, err = assertstate.ValidationSets(s.st) 90 c.Assert(err, IsNil) 91 c.Assert(all, HasLen, 2) 92 93 var gotFirst, gotSecond bool 94 for k, v := range all { 95 if k == "foo/bar" { 96 gotFirst = true 97 c.Check(v, DeepEquals, &assertstate.ValidationSetTracking{AccountID: "foo", Name: "bar", Mode: assertstate.Monitor, PinnedAt: 2, Current: 3}) 98 } else { 99 gotSecond = true 100 c.Check(k, Equals, "foo/baz") 101 c.Check(v, DeepEquals, &assertstate.ValidationSetTracking{AccountID: "foo", Name: "baz", Mode: assertstate.Enforce, PinnedAt: 0, Current: 3}) 102 } 103 } 104 c.Check(gotFirst, Equals, true) 105 c.Check(gotSecond, Equals, true) 106 } 107 108 func (s *validationSetTrackingSuite) TestDelete(c *C) { 109 s.st.Lock() 110 defer s.st.Unlock() 111 112 // delete non-existing one is fine 113 assertstate.DeleteValidationSet(s.st, "foo", "bar") 114 all, err := assertstate.ValidationSets(s.st) 115 c.Assert(err, IsNil) 116 c.Assert(all, HasLen, 0) 117 118 tr := assertstate.ValidationSetTracking{ 119 AccountID: "foo", 120 Name: "bar", 121 Mode: assertstate.Monitor, 122 } 123 assertstate.UpdateValidationSet(s.st, &tr) 124 125 all, err = assertstate.ValidationSets(s.st) 126 c.Assert(err, IsNil) 127 c.Assert(all, HasLen, 1) 128 129 // deletes existing one 130 assertstate.DeleteValidationSet(s.st, "foo", "bar") 131 all, err = assertstate.ValidationSets(s.st) 132 c.Assert(err, IsNil) 133 c.Assert(all, HasLen, 0) 134 } 135 136 func (s *validationSetTrackingSuite) TestGet(c *C) { 137 s.st.Lock() 138 defer s.st.Unlock() 139 140 err := assertstate.GetValidationSet(s.st, "foo", "bar", nil) 141 c.Assert(err, ErrorMatches, `internal error: tr is nil`) 142 143 tr := assertstate.ValidationSetTracking{ 144 AccountID: "foo", 145 Name: "bar", 146 Mode: assertstate.Enforce, 147 Current: 3, 148 } 149 assertstate.UpdateValidationSet(s.st, &tr) 150 151 var res assertstate.ValidationSetTracking 152 err = assertstate.GetValidationSet(s.st, "foo", "bar", &res) 153 c.Assert(err, IsNil) 154 c.Check(res, DeepEquals, tr) 155 156 // non-existing 157 err = assertstate.GetValidationSet(s.st, "foo", "baz", &res) 158 c.Assert(err, Equals, state.ErrNoState) 159 }