opts/mount: add tmpfs-specific options

added following options:

 * tmpfs-size
 * tmpfs-mode

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2016-11-08 05:32:21 +00:00 committed by Vincent Demeester
parent 17e9503bbb
commit 547dc2052c
2 changed files with 59 additions and 4 deletions

View File

@ -3,10 +3,12 @@ package opts
import ( import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings" "strings"
mounttypes "github.com/docker/docker/api/types/mount" mounttypes "github.com/docker/docker/api/types/mount"
"github.com/docker/go-units"
) )
// MountOpt is a Value type for parsing mounts // MountOpt is a Value type for parsing mounts
@ -43,6 +45,13 @@ func (m *MountOpt) Set(value string) error {
return mount.BindOptions return mount.BindOptions
} }
tmpfsOptions := func() *mounttypes.TmpfsOptions {
if mount.TmpfsOptions == nil {
mount.TmpfsOptions = new(mounttypes.TmpfsOptions)
}
return mount.TmpfsOptions
}
setValueOnMap := func(target map[string]string, value string) { setValueOnMap := func(target map[string]string, value string) {
parts := strings.SplitN(value, "=", 2) parts := strings.SplitN(value, "=", 2)
if len(parts) == 1 { if len(parts) == 1 {
@ -102,6 +111,18 @@ func (m *MountOpt) Set(value string) error {
volumeOptions().DriverConfig.Options = make(map[string]string) volumeOptions().DriverConfig.Options = make(map[string]string)
} }
setValueOnMap(volumeOptions().DriverConfig.Options, value) setValueOnMap(volumeOptions().DriverConfig.Options, value)
case "tmpfs-size":
sizeBytes, err := units.RAMInBytes(value)
if err != nil {
return fmt.Errorf("invalid value for %s: %s", key, value)
}
tmpfsOptions().SizeBytes = sizeBytes
case "tmpfs-mode":
ui64, err := strconv.ParseUint(value, 8, 32)
if err != nil {
return fmt.Errorf("invalid value for %s: %s", key, value)
}
tmpfsOptions().Mode = os.FileMode(ui64)
default: default:
return fmt.Errorf("unexpected key '%s' in '%s'", key, field) return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
} }
@ -115,11 +136,14 @@ func (m *MountOpt) Set(value string) error {
return fmt.Errorf("target is required") return fmt.Errorf("target is required")
} }
if mount.Type == mounttypes.TypeBind && mount.VolumeOptions != nil { if mount.VolumeOptions != nil && mount.Type != mounttypes.TypeVolume {
return fmt.Errorf("cannot mix 'volume-*' options with mount type '%s'", mounttypes.TypeBind) return fmt.Errorf("cannot mix 'volume-*' options with mount type '%s'", mount.Type)
} }
if mount.Type == mounttypes.TypeVolume && mount.BindOptions != nil { if mount.BindOptions != nil && mount.Type != mounttypes.TypeBind {
return fmt.Errorf("cannot mix 'bind-*' options with mount type '%s'", mounttypes.TypeVolume) return fmt.Errorf("cannot mix 'bind-*' options with mount type '%s'", mount.Type)
}
if mount.TmpfsOptions != nil && mount.Type != mounttypes.TypeTmpfs {
return fmt.Errorf("cannot mix 'tmpfs-*' options with mount type '%s'", mount.Type)
} }
m.values = append(m.values, mount) m.values = append(m.values, mount)

View File

@ -1,6 +1,7 @@
package opts package opts
import ( import (
"os"
"testing" "testing"
mounttypes "github.com/docker/docker/api/types/mount" mounttypes "github.com/docker/docker/api/types/mount"
@ -151,3 +152,33 @@ func TestMountOptTypeConflict(t *testing.T) {
assert.Error(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix") assert.Error(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix")
assert.Error(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix") assert.Error(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix")
} }
func TestMountOptSetTmpfsNoError(t *testing.T) {
for _, testcase := range []string{
// tests several aliases that should have same result.
"type=tmpfs,target=/target,tmpfs-size=1m,tmpfs-mode=0700",
"type=tmpfs,target=/target,tmpfs-size=1MB,tmpfs-mode=700",
} {
var mount MountOpt
assert.NilError(t, mount.Set(testcase))
mounts := mount.Value()
assert.Equal(t, len(mounts), 1)
assert.DeepEqual(t, mounts[0], mounttypes.Mount{
Type: mounttypes.TypeTmpfs,
Target: "/target",
TmpfsOptions: &mounttypes.TmpfsOptions{
SizeBytes: 1024 * 1024, // not 1000 * 1000
Mode: os.FileMode(0700),
},
})
}
}
func TestMountOptSetTmpfsError(t *testing.T) {
var m MountOpt
assert.Error(t, m.Set("type=tmpfs,target=/foo,tmpfs-size=foo"), "invalid value for tmpfs-size")
assert.Error(t, m.Set("type=tmpfs,target=/foo,tmpfs-mode=foo"), "invalid value for tmpfs-mode")
assert.Error(t, m.Set("type=tmpfs"), "target is required")
}