Переглянути джерело

Adding unit test on context conversion

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif 5 роки тому
батько
коміт
0720767d1e
2 змінених файлів з 117 додано та 2 видалено
  1. 6 2
      server/proxy/contexts.go
  2. 111 0
      server/proxy/contexts_test.go

+ 6 - 2
server/proxy/contexts.go

@@ -47,6 +47,10 @@ func (cp *contextsProxy) List(ctx context.Context, request *contextsv1.ListReque
 		return &contextsv1.ListResponse{}, err
 	}
 
+	return convertContexts(contexts, configFile.CurrentContext), nil
+}
+
+func convertContexts(contexts []*store.DockerContext, currentContext string) *contextsv1.ListResponse {
 	result := &contextsv1.ListResponse{}
 
 	for _, c := range contexts {
@@ -60,7 +64,7 @@ func (cp *contextsProxy) List(ctx context.Context, request *contextsv1.ListReque
 			Name:        c.Name,
 			ContextType: c.Type(),
 			Description: c.Metadata.Description,
-			Current:     c.Name == configFile.CurrentContext,
+			Current:     c.Name == currentContext,
 		}
 		switch c.Type() {
 		case store.DefaultContextType:
@@ -73,7 +77,7 @@ func (cp *contextsProxy) List(ctx context.Context, request *contextsv1.ListReque
 
 		result.Contexts = append(result.Contexts, &context)
 	}
-	return result, nil
+	return result
 }
 
 func getDockerEndpoint(endpoint interface{}) *contextsv1.Context_DockerEndpoint {

+ 111 - 0
server/proxy/contexts_test.go

@@ -0,0 +1,111 @@
+/*
+   Copyright 2020 Docker Compose CLI authors
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package proxy
+
+import (
+	"testing"
+
+	"gotest.tools/v3/assert"
+
+	"github.com/docker/compose-cli/context/store"
+	contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
+	"github.com/google/go-cmp/cmp/cmpopts"
+)
+
+func TestConvertContext(t *testing.T) {
+	contexts := []*store.DockerContext{
+		{
+			Name: store.DefaultContextName,
+			Metadata: store.ContextMetadata{
+				Description: "description 1",
+				Type:        store.DefaultContextType,
+			},
+			Endpoints: map[string]interface{}{
+				"docker": &store.Endpoint{
+					Host: "unix://var/run/docker.sock",
+				},
+			},
+		},
+		{
+			Name: "acicontext",
+			Metadata: store.ContextMetadata{
+				Description: "group1@eastus",
+				Type:        store.AciContextType,
+			},
+			Endpoints: map[string]interface{}{
+				"aci": &store.AciContext{
+					Location:       "eastus",
+					ResourceGroup:  "group1",
+					SubscriptionID: "Subscription id",
+				},
+			},
+		},
+		{
+			Name: "ecscontext",
+			Metadata: store.ContextMetadata{
+				Description: "ecs description",
+				Type:        store.EcsContextType,
+			},
+			Endpoints: map[string]interface{}{
+				"ecs": &store.EcsContext{
+					CredentialsFromEnv: false,
+					Profile:            "awsprofile",
+				},
+			},
+		},
+	}
+	converted := convertContexts(contexts, "acicontext")
+	expected := []*contextsv1.Context{
+		{
+			Name:        store.DefaultContextName,
+			Current:     false,
+			ContextType: store.DefaultContextType,
+			Description: "description 1",
+			Endpoint: &contextsv1.Context_DockerEndpoint{
+				DockerEndpoint: &contextsv1.DockerEndpoint{
+					Host: "unix://var/run/docker.sock",
+				},
+			},
+		},
+		{
+			Name:        "acicontext",
+			Current:     true,
+			ContextType: store.AciContextType,
+			Description: "group1@eastus",
+			Endpoint: &contextsv1.Context_AciEndpoint{
+				AciEndpoint: &contextsv1.AciEndpoint{
+					Region:         "eastus",
+					ResourceGroup:  "group1",
+					SubscriptionId: "Subscription id",
+				},
+			},
+		},
+		{
+			Name:        "ecscontext",
+			Current:     false,
+			ContextType: store.EcsContextType,
+			Description: "ecs description",
+			Endpoint: &contextsv1.Context_EcsEndpoint{
+				EcsEndpoint: &contextsv1.EcsEndpoint{
+					FromEnvironment: false,
+					Profile:         "awsprofile",
+				},
+			},
+		},
+	}
+	assert.DeepEqual(t, converted.Contexts, expected, cmpopts.IgnoreUnexported(contextsv1.Context{}, contextsv1.DockerEndpoint{}, contextsv1.AciEndpoint{}, contextsv1.EcsEndpoint{}))
+}