github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/lifeflag/util_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lifeflag_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/apiserver/common"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/state"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  // mockAuth implements common.Authorizer for the tests' convenience.
    17  type mockAuth struct {
    18  	common.Authorizer
    19  	modelManager bool
    20  }
    21  
    22  func (mock mockAuth) AuthModelManager() bool {
    23  	return mock.modelManager
    24  }
    25  
    26  // auth is a convenience constructor for a mockAuth.
    27  func auth(modelManager bool) common.Authorizer {
    28  	return mockAuth{modelManager: modelManager}
    29  }
    30  
    31  // mockBackend implements lifeflag.Backend for the tests' convenience.
    32  type mockBackend struct {
    33  	exist bool
    34  	watch bool
    35  }
    36  
    37  func (mock *mockBackend) ModelUUID() string {
    38  	return coretesting.ModelTag.Id()
    39  }
    40  
    41  func (mock *mockBackend) FindEntity(tag names.Tag) (state.Entity, error) {
    42  	if tag != coretesting.ModelTag {
    43  		panic("should never happen -- bad auth somewhere")
    44  	}
    45  	if !mock.exist {
    46  		return nil, errors.NotFoundf("model")
    47  	}
    48  	return &mockEntity{
    49  		watch: mock.watch,
    50  	}, nil
    51  }
    52  
    53  // mockEntity implements state.Entity for the tests' convenience.
    54  type mockEntity struct {
    55  	watch bool
    56  }
    57  
    58  func (mock *mockEntity) Tag() names.Tag {
    59  	return coretesting.ModelTag
    60  }
    61  
    62  func (mock *mockEntity) Life() state.Life {
    63  	return state.Dying
    64  }
    65  
    66  func (mock *mockEntity) Watch() state.NotifyWatcher {
    67  	changes := make(chan struct{}, 1)
    68  	if mock.watch {
    69  		changes <- struct{}{}
    70  	} else {
    71  		close(changes)
    72  	}
    73  	return &mockWatcher{changes: changes}
    74  }
    75  
    76  // mockWatcher implements state.NotifyWatcher for the tests' convenience.
    77  type mockWatcher struct {
    78  	state.NotifyWatcher
    79  	changes chan struct{}
    80  }
    81  
    82  func (mock *mockWatcher) Changes() <-chan struct{} {
    83  	return mock.changes
    84  }
    85  
    86  func (mock *mockWatcher) Err() error {
    87  	return errors.New("blammo")
    88  }
    89  
    90  // entities is a convenience constructor for params.Entities.
    91  func entities(tags ...string) params.Entities {
    92  	entities := params.Entities{Entities: make([]params.Entity, len(tags))}
    93  	for i, tag := range tags {
    94  		entities.Entities[i].Tag = tag
    95  	}
    96  	return entities
    97  }
    98  
    99  func modelEntity() params.Entities {
   100  	return entities(coretesting.ModelTag.String())
   101  }