github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/caasbroker/fixture_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasbroker_test
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/juju/testing"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/caas"
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/config"
    15  )
    16  
    17  type fixture struct {
    18  	watcherErr   error
    19  	observerErrs []error
    20  	cloud        environs.CloudSpec
    21  	config       map[string]interface{}
    22  }
    23  
    24  func (fix *fixture) Run(c *gc.C, test func(*runContext)) {
    25  	context := &runContext{
    26  		cloud:  fix.cloud,
    27  		config: fix.config,
    28  	}
    29  	context.stub.SetErrors(fix.observerErrs...)
    30  	test(context)
    31  }
    32  
    33  type runContext struct {
    34  	mu     sync.Mutex
    35  	stub   testing.Stub
    36  	cloud  environs.CloudSpec
    37  	config map[string]interface{}
    38  }
    39  
    40  func (context *runContext) CloudSpec() (environs.CloudSpec, error) {
    41  	context.mu.Lock()
    42  	defer context.mu.Unlock()
    43  	context.stub.AddCall("CloudSpec")
    44  	if err := context.stub.NextErr(); err != nil {
    45  		return environs.CloudSpec{}, err
    46  	}
    47  	return context.cloud, nil
    48  }
    49  
    50  func (context *runContext) ModelConfig() (*config.Config, error) {
    51  	context.mu.Lock()
    52  	defer context.mu.Unlock()
    53  	context.stub.AddCall("Model")
    54  	if err := context.stub.NextErr(); err != nil {
    55  		return nil, err
    56  	}
    57  	return config.New(config.UseDefaults, context.config)
    58  }
    59  
    60  func (context *runContext) CheckCallNames(c *gc.C, names ...string) {
    61  	context.mu.Lock()
    62  	defer context.mu.Unlock()
    63  	context.stub.CheckCallNames(c, names...)
    64  }
    65  
    66  type mockBroker struct {
    67  	caas.Broker
    68  	testing.Stub
    69  	spec      environs.CloudSpec
    70  	namespace string
    71  	mu        sync.Mutex
    72  }
    73  
    74  func newMockBroker(args environs.OpenParams) (caas.Broker, error) {
    75  	return &mockBroker{spec: args.Cloud, namespace: args.Config.Name()}, nil
    76  }