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