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

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperatorprovisioner_test
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/juju/juju/storage"
    10  	"github.com/juju/testing"
    11  	"github.com/juju/version"
    12  	"gopkg.in/juju/names.v2"
    13  	"gopkg.in/tomb.v2"
    14  
    15  	"github.com/juju/juju/agent"
    16  	apicaasprovisioner "github.com/juju/juju/api/caasoperatorprovisioner"
    17  	"github.com/juju/juju/apiserver/params"
    18  	"github.com/juju/juju/caas"
    19  	"github.com/juju/juju/core/life"
    20  	"github.com/juju/juju/core/watcher"
    21  	coretesting "github.com/juju/juju/testing"
    22  	"github.com/juju/juju/worker/caasoperatorprovisioner"
    23  )
    24  
    25  type mockProvisionerFacade struct {
    26  	mu   sync.Mutex
    27  	stub *testing.Stub
    28  	caasoperatorprovisioner.CAASProvisionerFacade
    29  	applicationsWatcher *mockStringsWatcher
    30  	apiWatcher          *mockNotifyWatcher
    31  	life                life.Value
    32  }
    33  
    34  func newMockProvisionerFacade(stub *testing.Stub) *mockProvisionerFacade {
    35  	return &mockProvisionerFacade{
    36  		stub:                stub,
    37  		applicationsWatcher: newMockStringsWatcher(),
    38  		apiWatcher:          newMockNotifyWatcher(),
    39  	}
    40  }
    41  
    42  func (m *mockProvisionerFacade) WatchApplications() (watcher.StringsWatcher, error) {
    43  	m.mu.Lock()
    44  	defer m.mu.Unlock()
    45  	m.stub.MethodCall(m, "WatchApplications")
    46  	if err := m.stub.NextErr(); err != nil {
    47  		return nil, err
    48  	}
    49  	return m.applicationsWatcher, nil
    50  }
    51  
    52  func (m *mockProvisionerFacade) OperatorProvisioningInfo() (apicaasprovisioner.OperatorProvisioningInfo, error) {
    53  	m.mu.Lock()
    54  	defer m.mu.Unlock()
    55  	m.stub.MethodCall(m, "OperatorProvisioningInfo")
    56  	if err := m.stub.NextErr(); err != nil {
    57  		return apicaasprovisioner.OperatorProvisioningInfo{}, err
    58  	}
    59  	return apicaasprovisioner.OperatorProvisioningInfo{
    60  		ImagePath:    "juju-operator-image",
    61  		Version:      version.MustParse("2.99.0"),
    62  		APIAddresses: []string{"10.0.0.1:17070", "192.18.1.1:17070"},
    63  		Tags:         map[string]string{"fred": "mary"},
    64  		CharmStorage: storage.KubernetesFilesystemParams{
    65  			Provider:     "kubernetes",
    66  			Size:         uint64(1024),
    67  			ResourceTags: map[string]string{"foo": "bar"},
    68  			Attributes:   map[string]interface{}{"key": "value"},
    69  		},
    70  	}, nil
    71  }
    72  
    73  func (m *mockProvisionerFacade) Life(entityName string) (life.Value, error) {
    74  	m.mu.Lock()
    75  	defer m.mu.Unlock()
    76  	m.stub.MethodCall(m, "Life", entityName)
    77  	if err := m.stub.NextErr(); err != nil {
    78  		return "", err
    79  	}
    80  	return m.life, nil
    81  }
    82  
    83  func (m *mockProvisionerFacade) SetPasswords(passwords []apicaasprovisioner.ApplicationPassword) (params.ErrorResults, error) {
    84  	m.mu.Lock()
    85  	defer m.mu.Unlock()
    86  	m.stub.MethodCall(m, "SetPasswords", passwords)
    87  	if err := m.stub.NextErr(); err != nil {
    88  		return params.ErrorResults{}, err
    89  	}
    90  	return params.ErrorResults{
    91  		Results: make([]params.ErrorResult, len(passwords)),
    92  	}, nil
    93  }
    94  
    95  type mockAgentConfig struct {
    96  	agent.Config
    97  }
    98  
    99  func (m *mockAgentConfig) Controller() names.ControllerTag {
   100  	return coretesting.ControllerTag
   101  }
   102  
   103  func (m *mockAgentConfig) DataDir() string {
   104  	return "/var/lib/juju"
   105  }
   106  
   107  func (m *mockAgentConfig) LogDir() string {
   108  	return "/var/log/juju"
   109  }
   110  
   111  func (m *mockAgentConfig) OldPassword() string {
   112  	return "old password"
   113  }
   114  
   115  func (m *mockAgentConfig) CACert() string {
   116  	return coretesting.CACert
   117  }
   118  
   119  type mockBroker struct {
   120  	testing.Stub
   121  	caas.Broker
   122  	operatorExists bool
   123  }
   124  
   125  func (m *mockBroker) EnsureOperator(appName, agentPath string, config *caas.OperatorConfig) error {
   126  	m.MethodCall(m, "EnsureOperator", appName, agentPath, config)
   127  	return m.NextErr()
   128  }
   129  
   130  func (m *mockBroker) OperatorExists(appName string) (bool, error) {
   131  	m.MethodCall(m, "OperatorExists", appName)
   132  	return m.operatorExists, m.NextErr()
   133  }
   134  
   135  func (m *mockBroker) DeleteOperator(appName string) error {
   136  	m.MethodCall(m, "DeleteOperator", appName)
   137  	return m.NextErr()
   138  }
   139  
   140  type mockWatcher struct {
   141  	testing.Stub
   142  	tomb.Tomb
   143  	mu         sync.Mutex
   144  	terminated bool
   145  }
   146  
   147  func (w *mockWatcher) killed() bool {
   148  	w.mu.Lock()
   149  	defer w.mu.Unlock()
   150  	return w.terminated
   151  }
   152  
   153  func (w *mockWatcher) Kill() {
   154  	w.MethodCall(w, "Kill")
   155  	w.Tomb.Kill(nil)
   156  	w.mu.Lock()
   157  	defer w.mu.Unlock()
   158  	w.terminated = true
   159  }
   160  
   161  func (w *mockWatcher) Stop() error {
   162  	w.MethodCall(w, "Stop")
   163  	if err := w.NextErr(); err != nil {
   164  		return err
   165  	}
   166  	w.Tomb.Kill(nil)
   167  	return w.Tomb.Wait()
   168  }
   169  
   170  type mockStringsWatcher struct {
   171  	mockWatcher
   172  	changes chan []string
   173  }
   174  
   175  func newMockStringsWatcher() *mockStringsWatcher {
   176  	w := &mockStringsWatcher{changes: make(chan []string, 5)}
   177  	w.Tomb.Go(func() error {
   178  		<-w.Tomb.Dying()
   179  		return nil
   180  	})
   181  	return w
   182  }
   183  
   184  func (w *mockStringsWatcher) Changes() watcher.StringsChannel {
   185  	return w.changes
   186  }
   187  
   188  type mockNotifyWatcher struct {
   189  	mockWatcher
   190  	changes chan struct{}
   191  }
   192  
   193  func newMockNotifyWatcher() *mockNotifyWatcher {
   194  	w := &mockNotifyWatcher{changes: make(chan struct{}, 5)}
   195  	w.Tomb.Go(func() error {
   196  		<-w.Tomb.Dying()
   197  		return nil
   198  	})
   199  	return w
   200  }
   201  
   202  func (m *mockNotifyWatcher) Changes() watcher.NotifyChannel {
   203  	return m.changes
   204  }