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