1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Update Godeps docker/pkg/common => /stringid

- pkg/common was renamed to pkg/stringid
- removed stale dep on libcontainer/utils

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2015-04-17 21:37:54 -07:00
parent 91eef607d0
commit 88eb07fb8d
8 changed files with 20 additions and 140 deletions

View file

@ -10,11 +10,6 @@
"Comment": "v0.6.4-12-g467d9d5", "Comment": "v0.6.4-12-g467d9d5",
"Rev": "467d9d55c2d2c17248441a8fc661561161f40d5e" "Rev": "467d9d55c2d2c17248441a8fc661561161f40d5e"
}, },
{
"ImportPath": "github.com/docker/docker/pkg/common",
"Comment": "v1.4.1-1379-g8e107a9",
"Rev": "8e107a93210c54f22ec1354d969c771b1abfbe05"
},
{ {
"ImportPath": "github.com/docker/docker/pkg/iptables", "ImportPath": "github.com/docker/docker/pkg/iptables",
"Comment": "v1.4.1-2492-ge690ad9", "Comment": "v1.4.1-2492-ge690ad9",
@ -36,9 +31,9 @@
"Rev": "e690ad92925a045344bde8d2d59d7a7f602dded6" "Rev": "e690ad92925a045344bde8d2d59d7a7f602dded6"
}, },
{ {
"ImportPath": "github.com/docker/libcontainer/utils", "ImportPath": "github.com/docker/docker/pkg/stringid",
"Comment": "v1.4.0-324-g88989e6", "Comment": "v1.4.1-2492-ge690ad9",
"Rev": "88989e66d3a1ab960deb37f3dd7f824d85e1b9bc" "Rev": "e690ad92925a045344bde8d2d59d7a7f602dded6"
}, },
{ {
"ImportPath": "github.com/vishvananda/netlink", "ImportPath": "github.com/vishvananda/netlink",

View file

@ -0,0 +1 @@
This package provides helper functions for dealing with string identifiers

View file

@ -1,4 +1,4 @@
package common package stringid
import ( import (
"crypto/rand" "crypto/rand"
@ -36,12 +36,3 @@ func GenerateRandomID() string {
return value return value
} }
} }
func RandomString() string {
id := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, id); err != nil {
panic(err) // This shouldn't happen
}
return hex.EncodeToString(id)
}

View file

@ -1,8 +1,14 @@
package common package stringid
import ( import "testing"
"testing"
) func TestGenerateRandomID(t *testing.T) {
id := GenerateRandomID()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestShortenId(t *testing.T) { func TestShortenId(t *testing.T) {
id := GenerateRandomID() id := GenerateRandomID()
@ -27,33 +33,3 @@ func TestShortenIdInvalid(t *testing.T) {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
} }
} }
func TestGenerateRandomID(t *testing.T) {
id := GenerateRandomID()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestRandomString(t *testing.T) {
id := RandomString()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestRandomStringUniqueness(t *testing.T) {
repeats := 25
set := make(map[string]struct{}, repeats)
for i := 0; i < repeats; i = i + 1 {
id := RandomString()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
if _, ok := set[id]; ok {
t.Fatalf("Random number is repeated")
}
set[id] = struct{}{}
}
}

View file

@ -1,68 +0,0 @@
package utils
import (
"crypto/rand"
"encoding/hex"
"io"
"io/ioutil"
"path/filepath"
"strconv"
"syscall"
)
const (
exitSignalOffset = 128
)
// GenerateRandomName returns a new name joined with a prefix. This size
// specified is used to truncate the randomly generated value
func GenerateRandomName(prefix string, size int) (string, error) {
id := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, id); err != nil {
return "", err
}
return prefix + hex.EncodeToString(id)[:size], nil
}
// ResolveRootfs ensures that the current working directory is
// not a symlink and returns the absolute path to the rootfs
func ResolveRootfs(uncleanRootfs string) (string, error) {
rootfs, err := filepath.Abs(uncleanRootfs)
if err != nil {
return "", err
}
return filepath.EvalSymlinks(rootfs)
}
func CloseExecFrom(minFd int) error {
fdList, err := ioutil.ReadDir("/proc/self/fd")
if err != nil {
return err
}
for _, fi := range fdList {
fd, err := strconv.Atoi(fi.Name())
if err != nil {
// ignore non-numeric file names
continue
}
if fd < minFd {
// ignore descriptors lower than our specified minimum
continue
}
// intentionally ignore errors from syscall.CloseOnExec
syscall.CloseOnExec(fd)
// the cases where this might fail are basically file descriptors that have already been closed (including and especially the one that was created when ioutil.ReadDir did the "opendir" syscall)
}
return nil
}
// ExitStatus returns the correct exit status for a process based on if it
// was signaled or existed cleanly.
func ExitStatus(status syscall.WaitStatus) int {
if status.Signaled() {
return exitSignalOffset + int(status.Signal())
}
return status.ExitStatus()
}

View file

@ -1,15 +0,0 @@
package utils
import "testing"
func TestGenerateName(t *testing.T) {
name, err := GenerateRandomName("veth", 5)
if err != nil {
t.Fatal(err)
}
expected := 5 + len("veth")
if len(name) != 5+len("veth") {
t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
}
}

View file

@ -41,7 +41,7 @@ package libnetwork
import ( import (
"sync" "sync"
"github.com/docker/docker/pkg/common" "github.com/docker/docker/pkg/stringid"
"github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/driverapi"
) )
@ -152,7 +152,7 @@ func (c *controller) NewNetwork(nd *NetworkDriver, name string, options interfac
network := &network{ network := &network{
name: name, name: name,
id: driverapi.UUID(common.GenerateRandomID()), id: driverapi.UUID(stringid.GenerateRandomID()),
ctrlr: c, ctrlr: c,
driver: nd} driver: nd}
network.endpoints = make(endpointTable) network.endpoints = make(endpointTable)
@ -224,7 +224,7 @@ func (n *network) Delete() error {
func (n *network) CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, *driverapi.SandboxInfo, error) { func (n *network) CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, *driverapi.SandboxInfo, error) {
ep := &endpoint{name: name} ep := &endpoint{name: name}
ep.id = driverapi.UUID(common.GenerateRandomID()) ep.id = driverapi.UUID(stringid.GenerateRandomID())
ep.network = n ep.network = n
d := n.driver.internalDriver d := n.driver.internalDriver

View file

@ -6,12 +6,12 @@ import (
"runtime" "runtime"
"testing" "testing"
"github.com/docker/libcontainer/utils" "github.com/docker/libnetwork/netutils"
"github.com/vishvananda/netns" "github.com/vishvananda/netns"
) )
func newKey(t *testing.T) (string, error) { func newKey(t *testing.T) (string, error) {
name, err := utils.GenerateRandomName("netns", 12) name, err := netutils.GenerateRandomName("netns", 12)
if err != nil { if err != nil {
return "", err return "", err
} }