github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/testing/utils.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"path/filepath"
     8  	"runtime"
     9  
    10  	"github.com/juju/errors"
    11  	"github.com/juju/utils/set"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/names.v2"
    14  
    15  	"github.com/juju/juju/core/leadership"
    16  	"github.com/juju/juju/storage"
    17  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    18  )
    19  
    20  type fops interface {
    21  	// MkDir provides the functionality of gc.C.MkDir().
    22  	MkDir() string
    23  }
    24  
    25  // RealPaths implements Paths for tests that do touch the filesystem.
    26  type RealPaths struct {
    27  	tools         string
    28  	charm         string
    29  	socket        string
    30  	metricsspool  string
    31  	componentDirs map[string]string
    32  	fops          fops
    33  }
    34  
    35  func osDependentSockPath(c *gc.C) string {
    36  	sockPath := filepath.Join(c.MkDir(), "test.sock")
    37  	if runtime.GOOS == "windows" {
    38  		return `\\.\pipe` + sockPath[2:]
    39  	}
    40  	return sockPath
    41  }
    42  
    43  func NewRealPaths(c *gc.C) RealPaths {
    44  	return RealPaths{
    45  		tools:         c.MkDir(),
    46  		charm:         c.MkDir(),
    47  		socket:        osDependentSockPath(c),
    48  		metricsspool:  c.MkDir(),
    49  		componentDirs: make(map[string]string),
    50  		fops:          c,
    51  	}
    52  }
    53  
    54  func (p RealPaths) GetMetricsSpoolDir() string {
    55  	return p.metricsspool
    56  }
    57  
    58  func (p RealPaths) GetToolsDir() string {
    59  	return p.tools
    60  }
    61  
    62  func (p RealPaths) GetCharmDir() string {
    63  	return p.charm
    64  }
    65  
    66  func (p RealPaths) GetJujucSocket() string {
    67  	return p.socket
    68  }
    69  
    70  func (p RealPaths) ComponentDir(name string) string {
    71  	if dirname, ok := p.componentDirs[name]; ok {
    72  		return dirname
    73  	}
    74  	p.componentDirs[name] = filepath.Join(p.fops.MkDir(), name)
    75  	return p.componentDirs[name]
    76  }
    77  
    78  type StorageContextAccessor struct {
    79  	CStorage map[names.StorageTag]*ContextStorage
    80  }
    81  
    82  func (s *StorageContextAccessor) StorageTags() ([]names.StorageTag, error) {
    83  	tags := set.NewTags()
    84  	for tag := range s.CStorage {
    85  		tags.Add(tag)
    86  	}
    87  	storageTags := make([]names.StorageTag, len(tags))
    88  	for i, tag := range tags.SortedValues() {
    89  		storageTags[i] = tag.(names.StorageTag)
    90  	}
    91  	return storageTags, nil
    92  }
    93  
    94  func (s *StorageContextAccessor) Storage(tag names.StorageTag) (jujuc.ContextStorageAttachment, error) {
    95  	storage, ok := s.CStorage[tag]
    96  	if !ok {
    97  		return nil, errors.NotFoundf("storage")
    98  	}
    99  	return storage, nil
   100  }
   101  
   102  type ContextStorage struct {
   103  	CTag      names.StorageTag
   104  	CKind     storage.StorageKind
   105  	CLocation string
   106  }
   107  
   108  func (c *ContextStorage) Tag() names.StorageTag {
   109  	return c.CTag
   110  }
   111  
   112  func (c *ContextStorage) Kind() storage.StorageKind {
   113  	return c.CKind
   114  }
   115  
   116  func (c *ContextStorage) Location() string {
   117  	return c.CLocation
   118  }
   119  
   120  type FakeTracker struct {
   121  	leadership.Tracker
   122  }
   123  
   124  func (FakeTracker) ApplicationName() string {
   125  	return "application-name"
   126  }