Parcourir la source

Do not allow context creation for unknown types

Guillaume Tardif il y a 5 ans
Parent
commit
4a4b62ded2
2 fichiers modifiés avec 57 ajouts et 21 suppressions
  1. 23 21
      cli/cmd/context/create.go
  2. 34 0
      cli/cmd/context/create_test.go

+ 23 - 21
cli/cmd/context/create.go

@@ -29,13 +29,12 @@ package context
 
 import (
 	"context"
+	"fmt"
 
 	"github.com/pkg/errors"
-
-	"github.com/docker/api/client"
-
 	"github.com/spf13/cobra"
 
+	"github.com/docker/api/client"
 	"github.com/docker/api/context/store"
 )
 
@@ -67,14 +66,25 @@ func createCommand() *cobra.Command {
 }
 
 func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType string) error {
-	var description string
-	var contextData interface{}
+	contextData, description, err := getContextData(ctx, contextType, opts)
+	if err != nil {
+		return nil
+	}
+	s := store.ContextStore(ctx)
+	return s.Create(
+		name,
+		contextType,
+		description,
+		contextData,
+	)
+}
 
+func getContextData(ctx context.Context, contextType string, opts AciCreateOpts) (interface{}, string, error) {
 	switch contextType {
 	case "aci":
 		cs, err := client.GetCloudService(ctx, "aci")
 		if err != nil {
-			return errors.Wrap(err, "cannot connect to backend")
+			return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
 		}
 		params := map[string]string{
 			"aciSubscriptionId": opts.aciSubscriptionID,
@@ -82,20 +92,12 @@ func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType
 			"aciLocation":       opts.aciLocation,
 			"description":       opts.description,
 		}
-		contextData, description, err = cs.CreateContextData(ctx, params)
-		if err != nil {
-			return errors.Wrap(err, "cannot create context")
-		}
-	default: // TODO: we need to implement different contexts for known backends
-		description = opts.description
-		contextData = store.ExampleContext{}
+		return cs.CreateContextData(ctx, params)
+	case "moby":
+		return store.MobyContext{}, opts.description, nil
+	case "example":
+		return store.ExampleContext{}, opts.description, nil
+	default:
+		return nil, "", errors.New(fmt.Sprintf("incorrect context type %s, must be one of (aci | moby | docker)", contextType))
 	}
-
-	s := store.ContextStore(ctx)
-	return s.Create(
-		name,
-		contextType,
-		description,
-		contextData,
-	)
 }

+ 34 - 0
cli/cmd/context/create_test.go

@@ -0,0 +1,34 @@
+package context
+
+import (
+	"context"
+	"testing"
+
+	"github.com/docker/api/context/store"
+
+	_ "github.com/docker/api/example"
+	"github.com/docker/api/tests/framework"
+	. "github.com/onsi/gomega"
+	"github.com/stretchr/testify/suite"
+)
+
+type PsSuite struct {
+	framework.CliSuite
+}
+
+func (sut *PsSuite) TestCreateContextDataMoby() {
+	data, description, err := getContextData(context.TODO(), "moby", AciCreateOpts{})
+	Expect(err).To(BeNil())
+	Expect(data).To(Equal(store.MobyContext{}))
+	Expect(description).To(Equal(""))
+}
+
+func (sut *PsSuite) TestErrorOnUnknownContextType() {
+	_, _, err := getContextData(context.TODO(), "foo", AciCreateOpts{})
+	Expect(err).To(MatchError("incorrect context type foo, must be one of (aci | moby | docker)"))
+}
+
+func TestPs(t *testing.T) {
+	RegisterTestingT(t)
+	suite.Run(t, new(PsSuite))
+}