github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/config"
    11  	"github.com/juju/juju/instance"
    12  	"github.com/juju/juju/mongo"
    13  	"github.com/juju/juju/service/common"
    14  	svctesting "github.com/juju/juju/service/common/testing"
    15  )
    16  
    17  var (
    18  	CheckIfRoot          = &checkIfRoot
    19  	CheckLocalPort       = &checkLocalPort
    20  	DetectPackageProxies = &detectPackageProxies
    21  	ExecuteCloudConfig   = &executeCloudConfig
    22  	Provider             = providerInstance
    23  	UserCurrent          = &userCurrent
    24  )
    25  
    26  // CheckConfigNamespace checks the result of the namespace call on the
    27  // localConfig.
    28  func CheckConfigNamespace(c *gc.C, cfg *config.Config, expected string) {
    29  	env, err := providerInstance.Open(cfg)
    30  	c.Assert(err, jc.ErrorIsNil)
    31  	namespace := env.(*localEnviron).config.namespace()
    32  	c.Assert(namespace, gc.Equals, expected)
    33  }
    34  
    35  // CreateDirs calls createDirs on the localEnviron.
    36  func CreateDirs(c *gc.C, cfg *config.Config) error {
    37  	env, err := providerInstance.Open(cfg)
    38  	c.Assert(err, jc.ErrorIsNil)
    39  	return env.(*localEnviron).config.createDirs()
    40  }
    41  
    42  // CheckDirs returns the list of directories to check for permissions in the test.
    43  func CheckDirs(c *gc.C, cfg *config.Config) []string {
    44  	localConfig, err := providerInstance.newConfig(cfg)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	return []string{
    47  		localConfig.rootDir(),
    48  		localConfig.storageDir(),
    49  		localConfig.mongoDir(),
    50  	}
    51  }
    52  
    53  // ContainerAndBridge returns the "container" and "network-bridge"
    54  // settings as seen by the local provider.
    55  func ContainerAndBridge(c *gc.C, cfg *config.Config) (string, string) {
    56  	localConfig, err := providerInstance.newConfig(cfg)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	return string(localConfig.container()), localConfig.networkBridge()
    59  }
    60  
    61  // MockAddressForInterface replaces the getAddressForInterface with a function
    62  // that returns a constant localhost ip address.
    63  func MockAddressForInterface() func() {
    64  	return testing.PatchValue(&getAddressForInterface, func(name string) (string, error) {
    65  		logger.Debugf("getAddressForInterface called for %s", name)
    66  		return "127.0.0.1", nil
    67  	})
    68  }
    69  
    70  type mockInstance struct {
    71  	id                string
    72  	instance.Instance // stub out other methods with panics
    73  }
    74  
    75  func (inst *mockInstance) Id() instance.Id {
    76  	return instance.Id(inst.id)
    77  }
    78  
    79  func PatchServices(patchValue func(interface{}, interface{}), data *svctesting.FakeServiceData) {
    80  	patchValue(&mongoRemoveService, func(namespace string) error {
    81  		data.AddCall("RemoveService", namespace)
    82  		data.SetStatus(mongo.ServiceName(namespace), "")
    83  		return data.NextErr()
    84  	})
    85  	patchValue(&discoverService, func(name string) (agentService, error) {
    86  		return NewService(name, common.Conf{}, data), nil
    87  	})
    88  }
    89  
    90  func NewService(name string, conf common.Conf, data *svctesting.FakeServiceData) *svctesting.FakeService {
    91  	svc := svctesting.NewFakeService(name, conf)
    92  	svc.FakeServiceData = data
    93  	return svc
    94  }