github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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  	"github.com/snapcore/snapd/overlord/hookstate/hooktest"
    27  
    28  	. "gopkg.in/check.v1"
    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  	c.Check(s.mockHandler.Error(err), IsNil)
    81  	c.Check(s.mockHandler.ErrorCalled, Equals, true)
    82  	c.Check(s.mockHandler.Err, Equals, err)
    83  }
    84  
    85  func (s *hooktestSuite) TestErrorError(c *C) {
    86  	s.mockHandler.ErrorError = true
    87  	err := fmt.Errorf("test error")
    88  	c.Check(s.mockHandler.Error(err), NotNil)
    89  	c.Check(s.mockHandler.ErrorCalled, Equals, true)
    90  	c.Check(s.mockHandler.Err, Equals, err)
    91  }