github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/hookstate/hooktest/handler.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 hooktest 21 22 import "fmt" 23 24 // MockHandler is a mock hookstate.Handler. 25 type MockHandler struct { 26 BeforeCalled bool 27 BeforeError bool 28 29 DoneCalled bool 30 DoneError bool 31 32 ErrorCalled bool 33 ErrorError bool 34 IgnoreOriginalErr bool 35 Err error 36 37 // callbacks useful for testing 38 BeforeCallback func() 39 DoneCallback func() 40 } 41 42 // NewMockHandler returns a new MockHandler. 43 func NewMockHandler() *MockHandler { 44 return &MockHandler{} 45 } 46 47 // Before satisfies hookstate.Handler.Before 48 func (h *MockHandler) Before() error { 49 if h.BeforeCallback != nil { 50 h.BeforeCallback() 51 } 52 h.BeforeCalled = true 53 if h.BeforeError { 54 return fmt.Errorf("Before failed at user request") 55 } 56 return nil 57 } 58 59 // Done satisfies hookstate.Handler.Done 60 func (h *MockHandler) Done() error { 61 if h.DoneCallback != nil { 62 h.DoneCallback() 63 } 64 h.DoneCalled = true 65 if h.DoneError { 66 return fmt.Errorf("Done failed at user request") 67 } 68 return nil 69 } 70 71 // Error satisfies hookstate.Handler.Error 72 func (h *MockHandler) Error(err error) (bool, error) { 73 h.Err = err 74 h.ErrorCalled = true 75 if h.ErrorError { 76 return false, fmt.Errorf("Error failed at user request") 77 } 78 return h.IgnoreOriginalErr, nil 79 }