本文整理汇总了Golang中github.com/docker/docker/pkg/stringid.GenerateRandomID函数的典型用法代码示例。如果您正苦于以下问题:Golang GenerateRandomID函数的具体用法?Golang GenerateRandomID怎么用?Golang GenerateRandomID使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GenerateRandomID函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DriverBenchDiffApplyN
// DriverBenchDiffApplyN benchmarks calls to diff and apply together
func DriverBenchDiffApplyN(b *testing.B, fileCount int, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
if err := driver.Create(base, "", nil); err != nil {
b.Fatal(err)
}
if err := addManyFiles(driver, base, fileCount, 3); err != nil {
b.Fatal(err)
}
if err := driver.Create(upper, base, nil); err != nil {
b.Fatal(err)
}
if err := addManyFiles(driver, upper, fileCount, 6); err != nil {
b.Fatal(err)
}
diffSize, err := driver.DiffSize(upper, "")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.StopTimer()
for i := 0; i < b.N; i++ {
diff := stringid.GenerateRandomID()
if err := driver.Create(diff, base, nil); err != nil {
b.Fatal(err)
}
if err := checkManyFiles(driver, diff, fileCount, 3); err != nil {
b.Fatal(err)
}
b.StartTimer()
arch, err := driver.Diff(upper, "")
if err != nil {
b.Fatal(err)
}
applyDiffSize, err := driver.ApplyDiff(diff, "", arch)
if err != nil {
b.Fatal(err)
}
b.StopTimer()
arch.Close()
if applyDiffSize != diffSize {
// TODO: enforce this
//b.Fatalf("Apply diff size different, got %d, expected %s", applyDiffSize, diffSize)
}
if err := checkManyFiles(driver, diff, fileCount, 6); err != nil {
b.Fatal(err)
}
}
}
示例2: DriverBenchDiffN
// DriverBenchDiffN benchmarks calls to diff on two layers with
// a provided number of files on the lower and upper layers.
func DriverBenchDiffN(b *testing.B, bottom, top int, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
if err := driver.Create(base, "", "", nil); err != nil {
b.Fatal(err)
}
if err := addManyFiles(driver, base, bottom, 3); err != nil {
b.Fatal(err)
}
if err := driver.Create(upper, base, "", nil); err != nil {
b.Fatal(err)
}
if err := addManyFiles(driver, upper, top, 6); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
arch, err := driver.Diff(upper, "")
if err != nil {
b.Fatal(err)
}
_, err = io.Copy(ioutil.Discard, arch)
if err != nil {
b.Fatalf("Error copying archive: %s", err)
}
arch.Close()
}
}
示例3: TestByParent
func TestByParent(t *testing.T) {
archive1, _ := fakeTar()
archive2, _ := fakeTar()
archive3, _ := fakeTar()
graph, _ := tempGraph(t)
defer nukeGraph(graph)
parentImage := &image.Image{
ID: stringid.GenerateRandomID(),
Comment: "parent",
Created: time.Now(),
Parent: "",
}
childImage1 := &image.Image{
ID: stringid.GenerateRandomID(),
Comment: "child1",
Created: time.Now(),
Parent: parentImage.ID,
}
childImage2 := &image.Image{
ID: stringid.GenerateRandomID(),
Comment: "child2",
Created: time.Now(),
Parent: parentImage.ID,
}
_ = graph.Register(parentImage, archive1)
_ = graph.Register(childImage1, archive2)
_ = graph.Register(childImage2, archive3)
byParent := graph.ByParent()
numChildren := len(byParent[parentImage.ID])
if numChildren != 2 {
t.Fatalf("Expected 2 children, found %d", numChildren)
}
}
示例4: DriverTestDiffApply
// DriverTestDiffApply tests diffing and applying produces the same layer
func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
defer PutDriver(t)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
if err := driver.Create(base, "", "", nil); err != nil {
t.Fatal(err)
}
if err := addManyFiles(driver, base, fileCount, 3); err != nil {
t.Fatal(err)
}
if err := driver.Create(upper, base, "", nil); err != nil {
t.Fatal(err)
}
if err := addManyFiles(driver, upper, fileCount, 6); err != nil {
t.Fatal(err)
}
diffSize, err := driver.DiffSize(upper, "")
if err != nil {
t.Fatal(err)
}
diff := stringid.GenerateRandomID()
if err := driver.Create(diff, base, "", nil); err != nil {
t.Fatal(err)
}
if err := checkManyFiles(driver, diff, fileCount, 3); err != nil {
t.Fatal(err)
}
arch, err := driver.Diff(upper, base)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(nil)
if _, err := buf.ReadFrom(arch); err != nil {
t.Fatal(err)
}
if err := arch.Close(); err != nil {
t.Fatal(err)
}
applyDiffSize, err := driver.ApplyDiff(diff, base, bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatal(err)
}
if applyDiffSize != diffSize {
t.Fatalf("Apply diff size different, got %d, expected %d", applyDiffSize, diffSize)
}
if err := checkManyFiles(driver, diff, fileCount, 6); err != nil {
t.Fatal(err)
}
}
示例5: DriverTestChanges
// DriverTestChanges tests computed changes on a layer matches changes made
func DriverTestChanges(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
defer PutDriver(t)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
if err := driver.Create(base, "", "", nil); err != nil {
t.Fatal(err)
}
if err := addManyFiles(driver, base, 20, 3); err != nil {
t.Fatal(err)
}
if err := driver.Create(upper, base, "", nil); err != nil {
t.Fatal(err)
}
expectedChanges, err := changeManyFiles(driver, upper, 20, 6)
if err != nil {
t.Fatal(err)
}
changes, err := driver.Changes(upper, base)
if err != nil {
t.Fatal(err)
}
if err = checkChanges(expectedChanges, changes); err != nil {
t.Fatal(err)
}
}
示例6: New
// New creates a new instance of network controller.
func New(cfgOptions ...config.Option) (NetworkController, error) {
var cfg *config.Config
if len(cfgOptions) > 0 {
cfg = &config.Config{}
cfg.ProcessOptions(cfgOptions...)
}
c := &controller{
id: stringid.GenerateRandomID(),
cfg: cfg,
networks: networkTable{},
sandboxes: sandboxTable{},
drivers: driverTable{}}
if err := initDrivers(c); err != nil {
return nil, err
}
if cfg != nil {
if err := c.initDataStore(); err != nil {
// Failing to initalize datastore is a bad situation to be in.
// But it cannot fail creating the Controller
log.Debugf("Failed to Initialize Datastore due to %v. Operating in non-clustered mode", err)
}
if err := c.initDiscovery(); err != nil {
// Failing to initalize discovery is a bad situation to be in.
// But it cannot fail creating the Controller
log.Debugf("Failed to Initialize Discovery : %v", err)
}
}
if err := c.startExternalKeyListener(); err != nil {
return nil, err
}
return c, nil
}
示例7: TestRenameCheckNames
func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
newName := "new_name" + stringid.GenerateRandomID()
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err := inspectField(newName, "Name")
if err != nil {
c.Fatal(err)
}
if name != "/"+newName {
c.Fatal("Failed to rename container ")
}
name, err = inspectField("first_name", "Name")
if err == nil && !strings.Contains(err.Error(), "No such image or container: first_name") {
c.Fatal(err)
}
}
示例8: runScramble
func runScramble(cmd *cobra.Command, args []string) {
globalFlags(cmd)
t1 := time.Now()
dir, err := ioutil.ReadDir(filepath.Join(graphdir, "graph"))
if err != nil {
logrus.Fatalf("Error reading graph dir: %s", err)
}
var ids = []string{}
for _, v := range dir {
id := v.Name()
if len(id) != 64 {
logrus.Debugf("Skipping: %s", v.Name())
continue
}
cacheDir, err := getCacheDir(id)
if err != nil {
if err == ErrNeedMigration {
logrus.Debugf("%s not migrated", id)
}
logrus.Fatalf("Error getting image IDs: %s", err)
}
if _, err := os.Stat(cacheDir); err != nil {
if os.IsNotExist(err) {
logrus.Debugf("Skipping, missing cache dir: %s", id)
continue
}
logrus.Fatalf("Error checking cache dir %s: %s", cacheDir, err)
}
ids = append(ids, id)
}
updates := map[string]string{}
fileUpdates := []string{
filepath.Join(graphdir, fmt.Sprintf("repositories-%s", driver)),
}
for _, id := range ids {
fmt.Fprintf(cmd.Out(), "Scrambling %s\n", id)
newID := stringid.GenerateRandomID()
updates[id] = newID
oldPath := filepath.Join(graphdir, "graph", id)
newPath := filepath.Join(graphdir, "graph", newID)
if err := os.Rename(oldPath, newPath); err != nil {
logrus.Errorf("Error renaming %s to %s: %s", oldPath, newPath, err)
continue
}
updates[id] = newID
fileUpdates = append(fileUpdates, filepath.Join(graphdir, "graph", newID, "json"))
}
updateReferences(updates, fileUpdates)
logrus.Debugf("Ran scramble in %s", time.Since(t1).String())
}
示例9: New
// New creates a new instance of network controller.
func New(cfgOptions ...config.Option) (NetworkController, error) {
c := &controller{
id: stringid.GenerateRandomID(),
cfg: config.ParseConfigOptions(cfgOptions...),
sandboxes: sandboxTable{},
svcRecords: make(map[string]svcInfo),
serviceBindings: make(map[serviceKey]*service),
agentInitDone: make(chan struct{}),
}
if err := c.initStores(); err != nil {
return nil, err
}
drvRegistry, err := drvregistry.New(c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope), c.RegisterDriver, nil)
if err != nil {
return nil, err
}
for _, i := range getInitializers() {
var dcfg map[string]interface{}
// External plugins don't need config passed through daemon. They can
// bootstrap themselves
if i.ntype != "remote" {
dcfg = c.makeDriverConfig(i.ntype)
}
if err := drvRegistry.AddDriver(i.ntype, i.fn, dcfg); err != nil {
return nil, err
}
}
c.drvRegistry = drvRegistry
if c.cfg != nil && c.cfg.Cluster.Watcher != nil {
if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil {
// Failing to initialize discovery is a bad situation to be in.
// But it cannot fail creating the Controller
log.Errorf("Failed to Initialize Discovery : %v", err)
}
}
c.WalkNetworks(populateSpecial)
// Reserve pools first before doing cleanup. Otherwise the
// cleanups of endpoint/network and sandbox below will
// generate many unnecessary warnings
c.reservePools()
// Cleanup resources
c.sandboxCleanup(c.cfg.ActiveSandboxes)
c.cleanupLocalEndpoints()
c.networkCleanup()
if err := c.startExternalKeyListener(); err != nil {
return nil, err
}
return c, nil
}
示例10: CreateVolume
// CreateVolume creates a volume in the cluster
func (c *Cluster) CreateVolume(request *dockerclient.VolumeCreateRequest) (*cluster.Volume, error) {
var (
wg sync.WaitGroup
volume *cluster.Volume
err error
)
if request.Name == "" {
request.Name = stringid.GenerateRandomID()
}
c.RLock()
for _, e := range c.engines {
wg.Add(1)
go func(engine *cluster.Engine) {
defer wg.Done()
v, er := engine.CreateVolume(request)
if v != nil {
volume = v
err = nil
}
if er != nil && volume == nil {
err = er
}
}(e)
}
c.RUnlock()
wg.Wait()
return volume, err
}
示例11: NewSession
// NewSession creates a new session
// TODO(tiborvass): remove authConfig param once registry client v2 is vendored
func NewSession(client *http.Client, authConfig *cliconfig.AuthConfig, endpoint *Endpoint) (r *Session, err error) {
r = &Session{
authConfig: authConfig,
client: client,
indexEndpoint: endpoint,
id: stringid.GenerateRandomID(),
}
var alwaysSetBasicAuth bool
// If we're working with a standalone private registry over HTTPS, send Basic Auth headers
// alongside all our requests.
if endpoint.VersionString(1) != IndexServer && endpoint.URL.Scheme == "https" {
info, err := endpoint.Ping()
if err != nil {
return nil, err
}
if info.Standalone && authConfig != nil {
logrus.Debugf("Endpoint %s is eligible for private registry. Enabling decorator.", endpoint.String())
alwaysSetBasicAuth = true
}
}
// Annotate the transport unconditionally so that v2 can
// properly fallback on v1 when an image is not found.
client.Transport = AuthTransport(client.Transport, authConfig, alwaysSetBasicAuth)
jar, err := cookiejar.New(nil)
if err != nil {
return nil, errors.New("cookiejar.New is not supposed to return an error")
}
client.Jar = jar
return r, nil
}
示例12: DriverTestDeepLayerRead
// DriverTestDeepLayerRead reads a file from a lower layer under a given number of layers
func DriverTestDeepLayerRead(t testing.TB, layerCount int, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
defer PutDriver(t)
base := stringid.GenerateRandomID()
if err := driver.Create(base, "", "", nil); err != nil {
t.Fatal(err)
}
content := []byte("test content")
if err := addFile(driver, base, "testfile.txt", content); err != nil {
t.Fatal(err)
}
topLayer, err := addManyLayers(driver, base, layerCount)
if err != nil {
t.Fatal(err)
}
err = checkManyLayers(driver, topLayer, layerCount)
if err != nil {
t.Fatal(err)
}
if err := checkFile(driver, topLayer, "testfile.txt", content); err != nil {
t.Fatal(err)
}
}
示例13: NewTask
// NewTask fucntion creates a task
func NewTask(config *cluster.ContainerConfig, name string, timeout time.Duration) (*Task, error) {
id := stringid.TruncateID(stringid.GenerateRandomID())
if name != "" {
id = name + "." + id
}
// save the name in labels as the mesos containerizer will override it
config.Labels[cluster.SwarmLabelNamespace+".mesos.name"] = name
// FIXME: once Mesos changes merged no need to save the task id to know which container we launched
config.Labels[cluster.SwarmLabelNamespace+".mesos.task"] = id
task := &Task{
config: config,
container: make(chan *cluster.Container),
Error: make(chan error),
updates: make(chan *mesosproto.TaskStatus),
}
task.Name = &name
task.TaskId = &mesosproto.TaskID{Value: &id}
task.Labels = &mesosproto.Labels{Labels: []*mesosproto.Label{{Key: proto.String("SWARM_CONTAINER_NAME"), Value: &name}}}
go task.suicide(timeout)
return task, nil
}
示例14: DriverBenchDeepLayerDiff
// DriverBenchDeepLayerDiff benchmarks calls to diff on top of a given number of layers.
func DriverBenchDeepLayerDiff(b *testing.B, layerCount int, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)
base := stringid.GenerateRandomID()
if err := driver.Create(base, "", "", nil); err != nil {
b.Fatal(err)
}
if err := addFiles(driver, base, 50); err != nil {
b.Fatal(err)
}
topLayer, err := addManyLayers(driver, base, layerCount)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
arch, err := driver.Diff(topLayer, "")
if err != nil {
b.Fatal(err)
}
_, err = io.Copy(ioutil.Discard, arch)
if err != nil {
b.Fatalf("Error copying archive: %s", err)
}
arch.Close()
}
}
示例15: TestRegister
func TestRegister(t *testing.T) {
graph, _ := tempGraph(t)
defer nukeGraph(graph)
archive, err := fakeTar()
if err != nil {
t.Fatal(err)
}
image := &image.Image{
ID: stringid.GenerateRandomID(),
Comment: "testing",
Created: time.Now(),
}
err = graph.Register(v1ImageDescriptor{image}, archive)
if err != nil {
t.Fatal(err)
}
images := graph.Map()
if l := len(images); l != 1 {
t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
}
if resultImg, err := graph.Get(image.ID); err != nil {
t.Fatal(err)
} else {
if resultImg.ID != image.ID {
t.Fatalf("Wrong image ID. Should be '%s', not '%s'", image.ID, resultImg.ID)
}
if resultImg.Comment != image.Comment {
t.Fatalf("Wrong image comment. Should be '%s', not '%s'", image.Comment, resultImg.Comment)
}
}
}