github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/local/export_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  package local
     4  
     5  import (
     6  	"github.com/juju/testing"
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/environs"
    11  	"github.com/juju/juju/environs/config"
    12  	"github.com/juju/juju/instance"
    13  )
    14  
    15  var (
    16  	CheckIfRoot        = &checkIfRoot
    17  	CheckLocalPort     = &checkLocalPort
    18  	DetectAptProxies   = &detectAptProxies
    19  	ExecuteCloudConfig = &executeCloudConfig
    20  	Provider           = providerInstance
    21  	UserCurrent        = &userCurrent
    22  )
    23  
    24  // ConfigNamespace returns the result of the namespace call on the
    25  // localConfig.
    26  func ConfigNamespace(cfg *config.Config) string {
    27  	env, _ := providerInstance.Open(cfg)
    28  	return env.(*localEnviron).config.namespace()
    29  }
    30  
    31  // CreateDirs calls createDirs on the localEnviron.
    32  func CreateDirs(c *gc.C, cfg *config.Config) error {
    33  	env, err := providerInstance.Open(cfg)
    34  	c.Assert(err, jc.ErrorIsNil)
    35  	return env.(*localEnviron).config.createDirs()
    36  }
    37  
    38  // CheckDirs returns the list of directories to check for permissions in the test.
    39  func CheckDirs(c *gc.C, cfg *config.Config) []string {
    40  	localConfig, err := providerInstance.newConfig(cfg)
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	return []string{
    43  		localConfig.rootDir(),
    44  		localConfig.storageDir(),
    45  		localConfig.mongoDir(),
    46  	}
    47  }
    48  
    49  // ContainerAndBridge returns the "container" and "network-bridge"
    50  // settings as seen by the local provider.
    51  func ContainerAndBridge(c *gc.C, cfg *config.Config) (string, string) {
    52  	localConfig, err := providerInstance.newConfig(cfg)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	return string(localConfig.container()), localConfig.networkBridge()
    55  }
    56  
    57  // MockAddressForInterface replaces the getAddressForInterface with a function
    58  // that returns a constant localhost ip address.
    59  func MockAddressForInterface() func() {
    60  	return testing.PatchValue(&getAddressForInterface, func(name string) (string, error) {
    61  		logger.Debugf("getAddressForInterface called for %s", name)
    62  		return "127.0.0.1", nil
    63  	})
    64  }
    65  
    66  type mockInstance struct {
    67  	id                string
    68  	instance.Instance // stub out other methods with panics
    69  }
    70  
    71  func (inst *mockInstance) Id() instance.Id {
    72  	return instance.Id(inst.id)
    73  }
    74  
    75  type startInstanceFunc func(*localEnviron, environs.StartInstanceParams) (instance.Instance, *instance.HardwareCharacteristics, error)
    76  
    77  func PatchCreateContainer(s *testing.CleanupSuite, c *gc.C, expectedURL string) startInstanceFunc {
    78  	mockFunc := func(_ *localEnviron, args environs.StartInstanceParams) (instance.Instance, *instance.HardwareCharacteristics, error) {
    79  		c.Assert(args.Tools, gc.HasLen, 1)
    80  		c.Assert(args.Tools[0].URL, gc.Equals, expectedURL)
    81  		return &mockInstance{id: "mock"}, nil, nil
    82  	}
    83  	s.PatchValue(&createContainer, mockFunc)
    84  	return mockFunc
    85  }