github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/undertaker/mock_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package undertaker_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/environs"
    13  	"github.com/juju/juju/status"
    14  	"github.com/juju/juju/watcher"
    15  	"github.com/juju/juju/worker"
    16  	"github.com/juju/juju/worker/undertaker"
    17  	"github.com/juju/juju/worker/workertest"
    18  )
    19  
    20  type mockFacade struct {
    21  	stub *testing.Stub
    22  	info params.UndertakerModelInfoResult
    23  }
    24  
    25  func (mock *mockFacade) ModelInfo() (params.UndertakerModelInfoResult, error) {
    26  	mock.stub.AddCall("ModelInfo")
    27  	if err := mock.stub.NextErr(); err != nil {
    28  		return params.UndertakerModelInfoResult{}, err
    29  	}
    30  	return mock.info, nil
    31  }
    32  
    33  func (mock *mockFacade) WatchModelResources() (watcher.NotifyWatcher, error) {
    34  	mock.stub.AddCall("WatchModelResources")
    35  	if err := mock.stub.NextErr(); err != nil {
    36  		return nil, err
    37  	}
    38  	const count = 5
    39  	changes := make(chan struct{}, count)
    40  	for i := 0; i < count; i++ {
    41  		changes <- struct{}{}
    42  	}
    43  	return &mockWatcher{
    44  		Worker:  workertest.NewErrorWorker(nil),
    45  		changes: changes,
    46  	}, nil
    47  }
    48  
    49  func (mock *mockFacade) ProcessDyingModel() error {
    50  	mock.stub.AddCall("ProcessDyingModel")
    51  	return mock.stub.NextErr()
    52  }
    53  
    54  func (mock *mockFacade) SetStatus(status status.Status, info string, data map[string]interface{}) error {
    55  	mock.stub.MethodCall(mock, "SetStatus", status, info, data)
    56  	return mock.stub.NextErr()
    57  }
    58  
    59  func (mock *mockFacade) RemoveModel() error {
    60  	mock.stub.AddCall("RemoveModel")
    61  	return mock.stub.NextErr()
    62  }
    63  
    64  type mockEnviron struct {
    65  	environs.Environ
    66  	stub *testing.Stub
    67  }
    68  
    69  func (mock *mockEnviron) Destroy() error {
    70  	mock.stub.AddCall("Destroy")
    71  	return mock.stub.NextErr()
    72  }
    73  
    74  type mockWatcher struct {
    75  	worker.Worker
    76  	changes chan struct{}
    77  }
    78  
    79  func (mock *mockWatcher) Changes() watcher.NotifyChannel {
    80  	return mock.changes
    81  }
    82  
    83  type fixture struct {
    84  	info   params.UndertakerModelInfoResult
    85  	errors []error
    86  	dirty  bool
    87  }
    88  
    89  func (fix fixture) cleanup(c *gc.C, w worker.Worker) {
    90  	if fix.dirty {
    91  		workertest.DirtyKill(c, w)
    92  	} else {
    93  		workertest.CleanKill(c, w)
    94  	}
    95  }
    96  
    97  func (fix fixture) run(c *gc.C, test func(worker.Worker)) *testing.Stub {
    98  	stub := &testing.Stub{}
    99  	environ := &mockEnviron{
   100  		stub: stub,
   101  	}
   102  	facade := &mockFacade{
   103  		stub: stub,
   104  		info: fix.info,
   105  	}
   106  	stub.SetErrors(fix.errors...)
   107  	w, err := undertaker.NewUndertaker(undertaker.Config{
   108  		Facade:  facade,
   109  		Environ: environ,
   110  	})
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	defer fix.cleanup(c, w)
   113  	test(w)
   114  	return stub
   115  }