-
-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathmounts.go
175 lines (147 loc) · 5.96 KB
/
mounts.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package testcontainers
import (
"errors"
"path/filepath"
)
const (
MountTypeBind MountType = iota // Deprecated: Use MountTypeVolume instead
MountTypeVolume
MountTypeTmpfs
MountTypePipe
MountTypeImage
)
var (
ErrDuplicateMountTarget = errors.New("duplicate mount target detected")
ErrInvalidBindMount = errors.New("invalid bind mount")
)
var (
_ ContainerMountSource = (*GenericBindMountSource)(nil) // Deprecated: use Files or HostConfigModifier in the ContainerRequest, or copy files container APIs to make containers portable across Docker environments
_ ContainerMountSource = (*GenericVolumeMountSource)(nil)
_ ContainerMountSource = (*GenericTmpfsMountSource)(nil)
_ ContainerMountSource = (*GenericImageMountSource)(nil)
)
type (
// ContainerMounts represents a collection of mounts for a container
ContainerMounts []ContainerMount
MountType uint
)
// ContainerMountSource is the base for all mount sources
type ContainerMountSource interface {
// Source will be used as Source field in the final mount
// this might either be a volume name, a host path or might be empty e.g. for Tmpfs
Source() string
// Type determines the final mount type
// possible options are limited by the Docker API
Type() MountType
}
// Deprecated: use Files or HostConfigModifier in the ContainerRequest, or copy files container APIs to make containers portable across Docker environments
// GenericBindMountSource implements ContainerMountSource and represents a bind mount
// Optionally mount.BindOptions might be added for advanced scenarios
type GenericBindMountSource struct {
// HostPath is the path mounted into the container
// the same host path might be mounted to multiple locations within a single container
HostPath string
}
// Deprecated: use Files or HostConfigModifier in the ContainerRequest, or copy files container APIs to make containers portable across Docker environments
func (s GenericBindMountSource) Source() string {
return s.HostPath
}
// Deprecated: use Files or HostConfigModifier in the ContainerRequest, or copy files container APIs to make containers portable across Docker environments
func (GenericBindMountSource) Type() MountType {
return MountTypeBind
}
// GenericVolumeMountSource implements ContainerMountSource and represents a volume mount
type GenericVolumeMountSource struct {
// Name refers to the name of the volume to be mounted
// the same volume might be mounted to multiple locations within a single container
Name string
}
func (s GenericVolumeMountSource) Source() string {
return s.Name
}
func (GenericVolumeMountSource) Type() MountType {
return MountTypeVolume
}
// GenericTmpfsMountSource implements ContainerMountSource and represents a TmpFS mount
// Optionally mount.TmpfsOptions might be added for advanced scenarios
type GenericTmpfsMountSource struct{}
func (s GenericTmpfsMountSource) Source() string {
return ""
}
func (GenericTmpfsMountSource) Type() MountType {
return MountTypeTmpfs
}
// ContainerMountTarget represents the target path within a container where the mount will be available
// Note that mount targets must be unique. It's not supported to mount different sources to the same target.
type ContainerMountTarget string
func (t ContainerMountTarget) Target() string {
return string(t)
}
// Deprecated: use Files or HostConfigModifier in the ContainerRequest, or copy files container APIs to make containers portable across Docker environments
// BindMount returns a new ContainerMount with a GenericBindMountSource as source
// This is a convenience method to cover typical use cases.
func BindMount(hostPath string, mountTarget ContainerMountTarget) ContainerMount {
return ContainerMount{
Source: GenericBindMountSource{HostPath: hostPath},
Target: mountTarget,
}
}
// VolumeMount returns a new ContainerMount with a GenericVolumeMountSource as source
// This is a convenience method to cover typical use cases.
func VolumeMount(volumeName string, mountTarget ContainerMountTarget) ContainerMount {
return ContainerMount{
Source: GenericVolumeMountSource{Name: volumeName},
Target: mountTarget,
}
}
// ImageMount returns a new ContainerMount with a GenericImageMountSource as source
// This is a convenience method to cover typical use cases.
func ImageMount(imageName string, subpath string, mountTarget ContainerMountTarget) ContainerMount {
return ContainerMount{
Source: NewGenericImageMountSource(imageName, subpath),
Target: mountTarget,
}
}
// Mounts returns a ContainerMounts to support a more fluent API
func Mounts(mounts ...ContainerMount) ContainerMounts {
return mounts
}
// ContainerMount models a mount into a container
type ContainerMount struct {
// Source is typically either a GenericVolumeMountSource, as BindMount is not supported by all Docker environments
Source ContainerMountSource
// Target is the path where the mount should be mounted within the container
Target ContainerMountTarget
// ReadOnly determines if the mount should be read-only
ReadOnly bool
}
// GenericImageMountSource implements ContainerMountSource and represents an image mount
type GenericImageMountSource struct {
// imageName refers to the name of the image to be mounted
// the same image might be mounted to multiple locations within a single container
imageName string
// subpath is the path within the image to be mounted
subpath string
}
// NewGenericImageMountSource creates a new GenericImageMountSource
func NewGenericImageMountSource(imageName string, subpath string) GenericImageMountSource {
return GenericImageMountSource{
imageName: imageName,
subpath: subpath,
}
}
// Source returns the name of the image to be mounted
func (s GenericImageMountSource) Source() string {
return s.imageName
}
// Type returns the type of the mount
func (GenericImageMountSource) Type() MountType {
return MountTypeImage
}
// Validate validates the source of the mount
func (s GenericImageMountSource) Validate() error {
if !filepath.IsLocal(s.subpath) {
return errors.New("image mount source must be a local path")
}
return nil
}