Explorar el Código

Add ports convet tests

Djordje Lukic hace 5 años
padre
commit
6fd290e2b1
Se han modificado 4 ficheros con 96 adiciones y 6 borrados
  1. 1 1
      Makefile
  2. 89 0
      azure/convert/ports_test.go
  3. 1 2
      cli/cmd/ps.go
  4. 5 3
      cli/formatter/container.go

+ 1 - 1
Makefile

@@ -62,7 +62,7 @@ cache-clear: ## Clear the builder cache
 	@docker builder prune --force --filter type=exec.cachemount --filter=unused-for=24h
 
 lint: ## run linter(s)
-	docker run -t -v $(PWD):/app -w /app golangci/golangci-lint:v1.27-alpine golangci-lint run ./...
+	docker run --rm -t -v $(PWD):/app -w /app golangci/golangci-lint:v1.27-alpine golangci-lint run ./...
 
 help: ## Show help
 	@echo Please specify a build target. The choices are:

+ 89 - 0
azure/convert/ports_test.go

@@ -0,0 +1,89 @@
+package convert
+
+import (
+	"testing"
+
+	"github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
+	"github.com/Azure/go-autorest/autorest/to"
+	"github.com/stretchr/testify/assert"
+
+	"github.com/docker/api/containers"
+)
+
+func TestPortConvert(t *testing.T) {
+	expectedPorts := []containers.Port{
+		{
+			HostPort:      80,
+			ContainerPort: 80,
+			HostIP:        "10.0.0.1",
+			Protocol:      "tcp",
+		},
+	}
+	testCases := []struct {
+		name     string
+		ip       *containerinstance.IPAddress
+		ports    []containerinstance.ContainerPort
+		expected []containers.Port
+	}{
+		{
+			name: "convert port",
+			ip: &containerinstance.IPAddress{
+				IP: to.StringPtr("10.0.0.1"),
+			},
+			ports: []containerinstance.ContainerPort{
+				{
+					Protocol: "tcp",
+					Port:     to.Int32Ptr(80),
+				},
+			},
+			expected: expectedPorts,
+		},
+		{
+			name: "with nil ip",
+			ip:   nil,
+			ports: []containerinstance.ContainerPort{
+				{
+					Protocol: "tcp",
+					Port:     to.Int32Ptr(80),
+				},
+			},
+			expected: []containers.Port{
+				{
+					HostPort:      80,
+					ContainerPort: 80,
+					HostIP:        "",
+					Protocol:      "tcp",
+				},
+			},
+		},
+		{
+			name: "skip nil ports",
+			ip:   nil,
+			ports: []containerinstance.ContainerPort{
+				{
+					Protocol: "tcp",
+					Port:     to.Int32Ptr(80),
+				},
+				{
+					Protocol: "tcp",
+					Port:     nil,
+				},
+			},
+			expected: []containers.Port{
+				{
+					HostPort:      80,
+					ContainerPort: 80,
+					HostIP:        "",
+					Protocol:      "tcp",
+				},
+			},
+		},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.name, func(t *testing.T) {
+			ports := ToPorts(testCase.ip, testCase.ports)
+			assert.Equal(t, testCase.expected, ports)
+		})
+	}
+}

+ 1 - 2
cli/cmd/ps.go

@@ -6,11 +6,10 @@ import (
 	"os"
 	"text/tabwriter"
 
+	"github.com/docker/docker/pkg/stringid"
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 
-	"github.com/docker/docker/pkg/stringid"
-
 	"github.com/docker/api/cli/formatter"
 	"github.com/docker/api/client"
 )

+ 5 - 3
cli/formatter/container.go

@@ -17,9 +17,11 @@ type portGroup struct {
 // PortsString returns a human readable published ports
 func PortsString(ports []containers.Port) string {
 	groupMap := make(map[string]*portGroup)
-	var result []string
-	var hostMappings []string
-	var groupMapKeys []string
+	var (
+		result       []string
+		hostMappings []string
+		groupMapKeys []string
+	)
 
 	sort.Slice(ports, func(i int, j int) bool {
 		return comparePorts(ports[i], ports[j])