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