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