github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/hookstate/hooktest/handler_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 hooktest_test
    21  
    22  import (
    23  	"fmt"
    24  	"testing"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/overlord/hookstate/hooktest"
    29  )
    30  
    31  func Test(t *testing.T) { TestingT(t) }
    32  
    33  type hooktestSuite struct {
    34  	mockHandler *hooktest.MockHandler
    35  }
    36  
    37  var _ = Suite(&hooktestSuite{})
    38  
    39  func (s *hooktestSuite) SetUpTest(c *C) {
    40  	s.mockHandler = hooktest.NewMockHandler()
    41  }
    42  
    43  func (s *hooktestSuite) TestBefore(c *C) {
    44  	var callbackCalled = false
    45  	s.mockHandler.BeforeCallback = func() {
    46  		callbackCalled = true
    47  	}
    48  	c.Check(s.mockHandler.BeforeCalled, Equals, false)
    49  	c.Check(s.mockHandler.Before(), IsNil)
    50  	c.Check(s.mockHandler.BeforeCalled, Equals, true)
    51  	c.Check(callbackCalled, Equals, true)
    52  }
    53  
    54  func (s *hooktestSuite) TestBeforeError(c *C) {
    55  	s.mockHandler.BeforeError = true
    56  	c.Check(s.mockHandler.Before(), NotNil)
    57  	c.Check(s.mockHandler.BeforeCalled, Equals, true)
    58  }
    59  
    60  func (s *hooktestSuite) TestDone(c *C) {
    61  	var callbackCalled = false
    62  	s.mockHandler.DoneCallback = func() {
    63  		callbackCalled = true
    64  	}
    65  	c.Check(s.mockHandler.DoneCalled, Equals, false)
    66  	c.Check(s.mockHandler.Done(), IsNil)
    67  	c.Check(s.mockHandler.DoneCalled, Equals, true)
    68  	c.Check(callbackCalled, Equals, true)
    69  }
    70  
    71  func (s *hooktestSuite) TestDoneError(c *C) {
    72  	s.mockHandler.DoneError = true
    73  	c.Check(s.mockHandler.Done(), NotNil)
    74  	c.Check(s.mockHandler.DoneCalled, Equals, true)
    75  }
    76  
    77  func (s *hooktestSuite) TestError(c *C) {
    78  	err := fmt.Errorf("test error")
    79  	c.Check(s.mockHandler.ErrorCalled, Equals, false)
    80  	ignore, herr := s.mockHandler.Error(err)
    81  	c.Check(ignore, Equals, false)
    82  	c.Check(herr, IsNil)
    83  	c.Check(s.mockHandler.ErrorCalled, Equals, true)
    84  	c.Check(s.mockHandler.Err, Equals, err)
    85  }
    86  
    87  func (s *hooktestSuite) TestErrorError(c *C) {
    88  	s.mockHandler.ErrorError = true
    89  	err := fmt.Errorf("test error")
    90  	ignore, herr := s.mockHandler.Error(err)
    91  	c.Check(ignore, Equals, false)
    92  	c.Check(herr, NotNil)
    93  	c.Check(s.mockHandler.ErrorCalled, Equals, true)
    94  	c.Check(s.mockHandler.Err, Equals, err)
    95  }
    96  
    97  func (s *hooktestSuite) TestIgnoreError(c *C) {
    98  	s.mockHandler.IgnoreOriginalErr = true
    99  	err := fmt.Errorf("test error")
   100  	ignore, herr := s.mockHandler.Error(err)
   101  	c.Check(ignore, Equals, true)
   102  	c.Check(herr, IsNil)
   103  	c.Check(s.mockHandler.ErrorCalled, Equals, true)
   104  	c.Check(s.mockHandler.Err, Equals, err)
   105  }