diff --git a/Makefile b/Makefile index 320fd81f7..5c5f4373d 100644 --- a/Makefile +++ b/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: diff --git a/azure/convert/ports_test.go b/azure/convert/ports_test.go new file mode 100644 index 000000000..7c8e3a8e3 --- /dev/null +++ b/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) + }) + } +} diff --git a/cli/cmd/ps.go b/cli/cmd/ps.go index 7b4c234f5..562b691e7 100644 --- a/cli/cmd/ps.go +++ b/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" ) diff --git a/cli/formatter/container.go b/cli/formatter/container.go index 1d2d182a2..7b62056b7 100644 --- a/cli/formatter/container.go +++ b/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])