github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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  	Err         error
    35  
    36  	// callbacks useful for testing
    37  	BeforeCallback func()
    38  	DoneCallback   func()
    39  }
    40  
    41  // NewMockHandler returns a new MockHandler.
    42  func NewMockHandler() *MockHandler {
    43  	return &MockHandler{}
    44  }
    45  
    46  // Before satisfies hookstate.Handler.Before
    47  func (h *MockHandler) Before() error {
    48  	if h.BeforeCallback != nil {
    49  		h.BeforeCallback()
    50  	}
    51  	h.BeforeCalled = true
    52  	if h.BeforeError {
    53  		return fmt.Errorf("Before failed at user request")
    54  	}
    55  	return nil
    56  }
    57  
    58  // Done satisfies hookstate.Handler.Done
    59  func (h *MockHandler) Done() error {
    60  	if h.DoneCallback != nil {
    61  		h.DoneCallback()
    62  	}
    63  	h.DoneCalled = true
    64  	if h.DoneError {
    65  		return fmt.Errorf("Done failed at user request")
    66  	}
    67  	return nil
    68  }
    69  
    70  // Error satisfies hookstate.Handler.Error
    71  func (h *MockHandler) Error(err error) error {
    72  	h.Err = err
    73  	h.ErrorCalled = true
    74  	if h.ErrorError {
    75  		return fmt.Errorf("Error failed at user request")
    76  	}
    77  	return nil
    78  }