mirror of
https://github.com/docker/docs.git
synced 2026-11-06 12:54:24 +03:00
Merge pull request #1606 from hairyhenderson/verify-swarm-discovery-flag-1604
Verifying the --swarm-discovery flag
This commit is contained in:
@@ -3,6 +3,7 @@ package commands
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/docker/machine/log"
|
||||
|
||||
@@ -35,6 +36,10 @@ func cmdCreate(c *cli.Context) {
|
||||
log.Fatal("You must specify a machine name")
|
||||
}
|
||||
|
||||
if err := validateSwarmDiscovery(c.String("swarm-discovery")); err != nil {
|
||||
log.Fatalf("Error parsing swarm discovery: %s", err)
|
||||
}
|
||||
|
||||
certInfo := getCertPathInfo(c)
|
||||
|
||||
if err := setupCertificates(
|
||||
@@ -118,3 +123,20 @@ func trimDriverFlags(driver string, cmds []cli.Command) ([]cli.Command, error) {
|
||||
|
||||
return filteredCmds, nil
|
||||
}
|
||||
|
||||
func validateSwarmDiscovery(discovery string) error {
|
||||
if discovery == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
matched, err := regexp.MatchString(`[^:]*://.*`, discovery)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if matched {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("Swarm Discovery URL was in the wrong format: %s", discovery)
|
||||
}
|
||||
|
||||
@@ -1 +1,22 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateSwarmDiscoveryErrorsGivenInvalidURL(t *testing.T) {
|
||||
err := validateSwarmDiscovery("foo")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestValidateSwarmDiscoveryAcceptsEmptyString(t *testing.T) {
|
||||
err := validateSwarmDiscovery("")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestValidateSwarmDiscoveryAcceptsValidFormat(t *testing.T) {
|
||||
err := validateSwarmDiscovery("token://deadbeefcafe")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user