github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/hookstate/repository_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 hookstate 21 22 import ( 23 "regexp" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/overlord/hookstate/hooktest" 28 "github.com/snapcore/snapd/overlord/state" 29 "github.com/snapcore/snapd/snap" 30 ) 31 32 type repositorySuite struct{} 33 34 var _ = Suite(&repositorySuite{}) 35 36 func (s *repositorySuite) TestAddHandlerGenerator(c *C) { 37 repository := newRepository() 38 39 var calledContext *Context 40 mockHandlerGenerator := func(context *Context) Handler { 41 calledContext = context 42 return hooktest.NewMockHandler() 43 } 44 45 // Verify that a handler generator can be added to the repository 46 repository.addHandlerGenerator(regexp.MustCompile("test-hook"), mockHandlerGenerator) 47 48 state := state.New(nil) 49 state.Lock() 50 task := state.NewTask("test-task", "my test task") 51 setup := &HookSetup{Snap: "test-snap", Revision: snap.R(1), Hook: "test-hook"} 52 context := &Context{task: task, setup: setup} 53 state.Unlock() 54 55 c.Assert(context, NotNil) 56 57 // Verify that the handler can be generated 58 handlers := repository.generateHandlers(context) 59 c.Check(handlers, HasLen, 1) 60 c.Check(calledContext, DeepEquals, context) 61 62 // Add another handler 63 repository.addHandlerGenerator(regexp.MustCompile(".*-hook"), mockHandlerGenerator) 64 65 // Verify that two handlers are generated for the test-hook, now 66 handlers = repository.generateHandlers(context) 67 c.Check(handlers, HasLen, 2) 68 c.Check(calledContext, DeepEquals, context) 69 }