diff --git a/cli/cmd/context/create.go b/cli/cmd/context/create.go index c04ecf9e8..121d25a2d 100644 --- a/cli/cmd/context/create.go +++ b/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,30 +66,10 @@ func createCommand() *cobra.Command { } func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType string) error { - var description string - var contextData interface{} - - switch contextType { - case "aci": - cs, err := client.GetCloudService(ctx, "aci") - if err != nil { - return errors.Wrap(err, "cannot connect to backend") - } - params := map[string]string{ - "aciSubscriptionId": opts.aciSubscriptionID, - "aciResourceGroup": opts.aciResourceGroup, - "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{} + contextData, description, err := getContextData(ctx, contextType, opts) + if err != nil { + return nil } - s := store.ContextStore(ctx) return s.Create( name, @@ -99,3 +78,26 @@ func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType 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 nil, "", errors.Wrap(err, "cannot connect to ACI backend") + } + params := map[string]string{ + "aciSubscriptionId": opts.aciSubscriptionID, + "aciResourceGroup": opts.aciResourceGroup, + "aciLocation": opts.aciLocation, + "description": opts.description, + } + 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)) + } +} diff --git a/cli/cmd/context/create_test.go b/cli/cmd/context/create_test.go new file mode 100644 index 000000000..2d9c06a35 --- /dev/null +++ b/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)) +}