github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/tools/lxdclient/testing_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxdclient
     7  
     8  import (
     9  	"crypto/x509"
    10  
    11  	"github.com/juju/errors"
    12  	"github.com/juju/testing"
    13  	"github.com/lxc/lxd"
    14  	"github.com/lxc/lxd/shared"
    15  	gc "gopkg.in/check.v1"
    16  )
    17  
    18  type BaseSuite struct {
    19  	testing.IsolationSuite
    20  
    21  	Stub   *testing.Stub
    22  	Client *stubClient
    23  	Cert   *Cert
    24  }
    25  
    26  func (s *BaseSuite) SetUpTest(c *gc.C) {
    27  	s.IsolationSuite.SetUpTest(c)
    28  
    29  	s.Stub = &testing.Stub{}
    30  	s.Client = &stubClient{stub: s.Stub}
    31  	s.Cert = &Cert{
    32  		Name:    "some cert",
    33  		CertPEM: []byte("<a valid PEM-encoded x.509 cert>"),
    34  		KeyPEM:  []byte("<a valid PEM-encoded x.509 key>"),
    35  	}
    36  }
    37  
    38  type stubClient struct {
    39  	stub *testing.Stub
    40  
    41  	Instance   *shared.ContainerState
    42  	Instances  []shared.ContainerInfo
    43  	ReturnCode int
    44  	Response   *lxd.Response
    45  	Aliases    map[string]string
    46  }
    47  
    48  func (s *stubClient) WaitForSuccess(waitURL string) error {
    49  	s.stub.AddCall("WaitForSuccess", waitURL)
    50  	if err := s.stub.NextErr(); err != nil {
    51  		return errors.Trace(err)
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func (s *stubClient) SetServerConfig(key string, value string) (*lxd.Response, error) {
    58  	s.stub.AddCall("SetServerConfig", key, value)
    59  	if err := s.stub.NextErr(); err != nil {
    60  		return nil, errors.Trace(err)
    61  	}
    62  
    63  	return s.Response, nil
    64  }
    65  
    66  func (s *stubClient) CertificateAdd(cert *x509.Certificate, name string) error {
    67  	s.stub.AddCall("CertificateAdd", cert, name)
    68  	if err := s.stub.NextErr(); err != nil {
    69  		return errors.Trace(err)
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func (s *stubClient) ContainerState(name string) (*shared.ContainerState, error) {
    76  	s.stub.AddCall("ContainerState", name)
    77  	if err := s.stub.NextErr(); err != nil {
    78  		return nil, errors.Trace(err)
    79  	}
    80  
    81  	return s.Instance, nil
    82  }
    83  
    84  func (s *stubClient) ListContainers() ([]shared.ContainerInfo, error) {
    85  	s.stub.AddCall("ListContainers")
    86  	if err := s.stub.NextErr(); err != nil {
    87  		return nil, errors.Trace(err)
    88  	}
    89  
    90  	return s.Instances, nil
    91  }
    92  
    93  func (s *stubClient) GetAlias(alias string) string {
    94  	s.stub.AddCall("GetAlias", alias)
    95  	if err := s.stub.NextErr(); err != nil {
    96  		return ""
    97  	}
    98  	return s.Aliases[alias]
    99  }
   100  
   101  func (s *stubClient) Init(name, remote, image string, profiles *[]string, ephem bool) (*lxd.Response, error) {
   102  	s.stub.AddCall("AddInstance", name, remote, image, profiles, ephem)
   103  	if err := s.stub.NextErr(); err != nil {
   104  		return nil, errors.Trace(err)
   105  	}
   106  
   107  	return s.Response, nil
   108  }
   109  
   110  func (s *stubClient) Delete(name string) (*lxd.Response, error) {
   111  	s.stub.AddCall("Delete", name)
   112  	if err := s.stub.NextErr(); err != nil {
   113  		return nil, errors.Trace(err)
   114  	}
   115  
   116  	return s.Response, nil
   117  }
   118  
   119  func (s *stubClient) Action(name string, action shared.ContainerAction, timeout int, force bool, stateful bool) (*lxd.Response, error) {
   120  	s.stub.AddCall("Action", name, action, timeout, force, stateful)
   121  	if err := s.stub.NextErr(); err != nil {
   122  		return nil, errors.Trace(err)
   123  	}
   124  
   125  	return s.Response, nil
   126  }
   127  
   128  func (s *stubClient) SetContainerConfig(name, key, value string) error {
   129  	s.stub.AddCall("SetContainerConfig", name, key, value)
   130  	if err := s.stub.NextErr(); err != nil {
   131  		return errors.Trace(err)
   132  	}
   133  
   134  	return nil
   135  }