mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
Merge pull request #142 from chris-crone/fix-141
Set random container name if not set
This commit is contained in:
commit
0917acfb01
@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -24,10 +23,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
rand.Seed(time.Now().Unix())
|
|
||||||
}
|
|
||||||
|
|
||||||
//go login process, derived from code sample provided by MS at https://github.com/devigned/go-az-cli-stuff
|
//go login process, derived from code sample provided by MS at https://github.com/devigned/go-az-cli-stuff
|
||||||
const (
|
const (
|
||||||
authorizeFormat = "https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s&prompt=select_account&response_mode=query&scope=%s"
|
authorizeFormat = "https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s&prompt=select_account&response_mode=query&scope=%s"
|
||||||
|
@ -30,9 +30,7 @@ package run
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/namesgenerator"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/docker/api/cli/options/run"
|
"github.com/docker/api/cli/options/run"
|
||||||
@ -52,7 +50,7 @@ func Command() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
|
cmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
|
||||||
cmd.Flags().StringVar(&opts.Name, "name", getRandomName(), "Assign a name to the container")
|
cmd.Flags().StringVar(&opts.Name, "name", "", "Assign a name to the container")
|
||||||
cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
|
cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
|
||||||
cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: user:key@my_share:/absolute/path/to/target")
|
cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: user:key@my_share:/absolute/path/to/target")
|
||||||
|
|
||||||
@ -77,8 +75,3 @@ func runRun(ctx context.Context, image string, opts run.Opts) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRandomName() string {
|
|
||||||
// Azure supports hyphen but not underscore in names
|
|
||||||
return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
|
|
||||||
}
|
|
||||||
|
@ -1,23 +1,42 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2020 Docker Inc.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person
|
||||||
|
obtaining a copy of this software and associated documentation
|
||||||
|
files (the "Software"), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use, copy,
|
||||||
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED,
|
||||||
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE,
|
||||||
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||||
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
package run
|
package run
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"gotest.tools/v3/golden"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func TestHelp(t *testing.T) {
|
||||||
// AzureNameRegex is used to validate container names
|
var b bytes.Buffer
|
||||||
// Regex was taken from server side error:
|
c := Command()
|
||||||
// The container name must contain no more than 63 characters and must match the regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?' (e.g. 'my-name').
|
c.SetOutput(&b)
|
||||||
AzureNameRegex = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])")
|
_ = c.Help()
|
||||||
)
|
golden.Assert(t, b.String(), "run-help.golden")
|
||||||
|
|
||||||
// TestAzureRandomName ensures compliance with Azure naming requirements
|
|
||||||
func TestAzureRandomName(t *testing.T) {
|
|
||||||
n := getRandomName()
|
|
||||||
require.Less(t, len(n), 64)
|
|
||||||
require.Greater(t, len(n), 1)
|
|
||||||
require.Regexp(t, AzureNameRegex, n)
|
|
||||||
}
|
}
|
||||||
|
10
cli/cmd/run/testdata/run-help.golden
vendored
Normal file
10
cli/cmd/run/testdata/run-help.golden
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
Run a container
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
run [flags]
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
-l, --label stringArray Set meta data on a container
|
||||||
|
--name string Assign a name to the container
|
||||||
|
-p, --publish stringArray Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT
|
||||||
|
-v, --volume stringArray Volume. Ex: user:key@my_share:/absolute/path/to/target
|
@ -30,11 +30,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -69,6 +71,8 @@ func init() {
|
|||||||
if err := os.Setenv("PATH", fmt.Sprintf("%s:%s", os.Getenv("PATH"), path)); err != nil {
|
if err := os.Setenv("PATH", fmt.Sprintf("%s:%s", os.Getenv("PATH"), path)); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
// Seed random
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
|
|
||||||
func isOwnCommand(cmd *cobra.Command) bool {
|
func isOwnCommand(cmd *cobra.Command) bool {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/docker/pkg/namesgenerator"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
|
|
||||||
"github.com/docker/api/containers"
|
"github.com/docker/api/containers"
|
||||||
@ -20,6 +21,10 @@ type Opts struct {
|
|||||||
|
|
||||||
// ToContainerConfig convert run options to a container configuration
|
// ToContainerConfig convert run options to a container configuration
|
||||||
func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
|
func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
|
||||||
|
if r.Name == "" {
|
||||||
|
r.Name = getRandomName()
|
||||||
|
}
|
||||||
|
|
||||||
publish, err := r.toPorts()
|
publish, err := r.toPorts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return containers.ContainerConfig{}, err
|
return containers.ContainerConfig{}, err
|
||||||
@ -83,3 +88,8 @@ func toLabels(labels []string) (map[string]string, error) {
|
|||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRandomName() string {
|
||||||
|
// Azure supports hyphen but not underscore in names
|
||||||
|
return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package run
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -15,6 +16,21 @@ type RunOptsSuite struct {
|
|||||||
suite.Suite
|
suite.Suite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// AzureNameRegex is used to validate container names
|
||||||
|
// Regex was taken from server side error:
|
||||||
|
// The container name must contain no more than 63 characters and must match the regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?' (e.g. 'my-name').
|
||||||
|
AzureNameRegex = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])")
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestAzureRandomName ensures compliance with Azure naming requirements
|
||||||
|
func (s *RunOptsSuite) TestAzureRandomName() {
|
||||||
|
n := getRandomName()
|
||||||
|
require.Less(s.T(), len(n), 64)
|
||||||
|
require.Greater(s.T(), len(n), 1)
|
||||||
|
require.Regexp(s.T(), AzureNameRegex, n)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *RunOptsSuite) TestPortParse() {
|
func (s *RunOptsSuite) TestPortParse() {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
in string
|
in string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user