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

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package credentialvalidator_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  	"gopkg.in/juju/worker.v1"
    13  
    14  	"github.com/juju/juju/api/base"
    15  	"github.com/juju/juju/core/watcher"
    16  	"github.com/juju/juju/core/watcher/watchertest"
    17  	coretesting "github.com/juju/juju/testing"
    18  	"github.com/juju/juju/worker/credentialvalidator"
    19  )
    20  
    21  // mockFacade implements credentialvalidator.Facade for use in the tests.
    22  type mockFacade struct {
    23  	*testing.Stub
    24  	credential *base.StoredCredential
    25  	exists     bool
    26  
    27  	watcher      *watchertest.MockNotifyWatcher
    28  	modelWatcher *watchertest.MockNotifyWatcher
    29  }
    30  
    31  func (m *mockFacade) setupModelHasNoCredential() {
    32  	m.credential.CloudCredential = ""
    33  	m.exists = false
    34  	m.watcher = nil
    35  }
    36  
    37  // ModelCredential is part of the credentialvalidator.Facade interface.
    38  func (m *mockFacade) ModelCredential() (base.StoredCredential, bool, error) {
    39  	m.AddCall("ModelCredential")
    40  	if err := m.NextErr(); err != nil {
    41  		return base.StoredCredential{}, false, err
    42  	}
    43  	return *m.credential, m.exists, nil
    44  }
    45  
    46  // WatchCredential is part of the credentialvalidator.Facade interface.
    47  func (mock *mockFacade) WatchCredential(id string) (watcher.NotifyWatcher, error) {
    48  	mock.AddCall("WatchCredential", id)
    49  	if err := mock.NextErr(); err != nil {
    50  		return nil, err
    51  	}
    52  	return mock.watcher, nil
    53  }
    54  
    55  // WatchModelCredential is part of the credentialvalidator.Facade interface.
    56  func (mock *mockFacade) WatchModelCredential() (watcher.NotifyWatcher, error) {
    57  	mock.AddCall("WatchModelCredential")
    58  	if err := mock.NextErr(); err != nil {
    59  		return nil, err
    60  	}
    61  	return mock.modelWatcher, nil
    62  }
    63  
    64  // credentialTag is the credential tag we're using in the tests.
    65  // needs to fit fmt.Sprintf("%s/%s/%s", cloudName, userName, credentialName)
    66  var credentialTag = names.NewCloudCredentialTag("cloud/user/credential").String()
    67  
    68  // panicFacade is a NewFacade that should not be called.
    69  func panicFacade(base.APICaller) (credentialvalidator.Facade, error) {
    70  	panic("panicFacade")
    71  }
    72  
    73  // panicWorker is a NewWorker that should not be called.
    74  func panicWorker(credentialvalidator.Config) (worker.Worker, error) {
    75  	panic("panicWorker")
    76  }
    77  
    78  // validConfig returns a minimal config stuffed with dummy objects that
    79  // will explode when used.
    80  func validConfig() credentialvalidator.Config {
    81  	return credentialvalidator.Config{
    82  		Facade: struct{ credentialvalidator.Facade }{},
    83  	}
    84  }
    85  
    86  // checkNotValid checks that the supplied credentialvalidator.Config fails to
    87  // Validate, and cannot be used to construct a credentialvalidator.Worker.
    88  func checkNotValid(c *gc.C, config credentialvalidator.Config, expect string) {
    89  	check := func(err error) {
    90  		c.Check(err, gc.ErrorMatches, expect)
    91  		c.Check(err, jc.Satisfies, errors.IsNotValid)
    92  	}
    93  
    94  	err := config.Validate()
    95  	check(err)
    96  
    97  	worker, err := credentialvalidator.NewWorker(config)
    98  	c.Check(worker, gc.IsNil)
    99  	check(err)
   100  }
   101  
   102  // validManifoldConfig returns a minimal config stuffed with dummy objects
   103  // that will explode when used.
   104  func validManifoldConfig() credentialvalidator.ManifoldConfig {
   105  	return credentialvalidator.ManifoldConfig{
   106  		APICallerName: "api-caller",
   107  		NewFacade:     panicFacade,
   108  		NewWorker:     panicWorker,
   109  	}
   110  }
   111  
   112  // checkManifoldNotValid checks that the supplied ManifoldConfig creates
   113  // a manifold that cannot be started.
   114  func checkManifoldNotValid(c *gc.C, config credentialvalidator.ManifoldConfig, expect string) {
   115  	err := config.Validate()
   116  	c.Check(err, gc.ErrorMatches, expect)
   117  	c.Check(err, jc.Satisfies, errors.IsNotValid)
   118  }
   119  
   120  // stubCaller is a base.APICaller that only implements ModelTag.
   121  type stubCaller struct {
   122  	base.APICaller
   123  }
   124  
   125  // ModelTag is part of the base.APICaller interface.
   126  func (*stubCaller) ModelTag() (names.ModelTag, bool) {
   127  	return coretesting.ModelTag, true
   128  }