add //go:build directives to prevent downgrading to go1.16 language
This is a follow-up to 0e73168b7e6d1d029d76d05b843b1aaec46739a8
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]; https://go.dev/doc/toolchain#:~:text=The%20go%20line%20for,file%20to%20Go%201.22
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-14 07:51:57 -05:00
|
|
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
|
2024-11-12 06:32:30 -05:00
|
|
|
//go:build go1.22
|
add //go:build directives to prevent downgrading to go1.16 language
This is a follow-up to 0e73168b7e6d1d029d76d05b843b1aaec46739a8
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]; https://go.dev/doc/toolchain#:~:text=The%20go%20line%20for,file%20to%20Go%201.22
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-14 07:51:57 -05:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-09-13 03:01:31 -04:00
|
|
|
"encoding/json"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-07-10 09:51:07 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2024-07-03 09:35:44 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerPsContext(t *testing.T) {
|
|
|
|
containerID := stringid.GenerateRandomID()
|
|
|
|
unix := time.Now().Add(-65 * time.Second).Unix()
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
var ctx ContainerContext
|
2016-09-08 13:11:39 -04:00
|
|
|
cases := []struct {
|
2024-07-03 09:35:44 -04:00
|
|
|
container container.Summary
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc bool
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
2024-07-03 09:43:47 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{ID: containerID},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: stringid.TruncateID(containerID),
|
|
|
|
call: ctx.ID,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{ID: containerID},
|
2024-07-03 09:43:47 -04:00
|
|
|
expValue: containerID,
|
|
|
|
call: ctx.ID,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Names: []string{"/foobar_baz"}},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "foobar_baz",
|
|
|
|
call: ctx.Names,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Image: "ubuntu"},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "ubuntu",
|
|
|
|
call: ctx.Image,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Image: "verylongimagename"},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "verylongimagename",
|
|
|
|
call: ctx.Image,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Image: "verylongimagename"},
|
2024-07-03 09:43:47 -04:00
|
|
|
expValue: "verylongimagename",
|
|
|
|
call: ctx.Image,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{
|
2022-09-29 11:21:51 -04:00
|
|
|
Image: "a5a665ff33eced1e0803148700880edab4",
|
|
|
|
ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "a5a665ff33ec",
|
|
|
|
call: ctx.Image,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-09-29 11:21:51 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{
|
2022-09-29 11:21:51 -04:00
|
|
|
Image: "a5a665ff33eced1e0803148700880edab4",
|
|
|
|
ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expValue: "a5a665ff33eced1e0803148700880edab4",
|
|
|
|
call: ctx.Image,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Image: ""},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "<no image>",
|
|
|
|
call: ctx.Image,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Command: "sh -c 'ls -la'"},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: `"sh -c 'ls -la'"`,
|
|
|
|
call: ctx.Command,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Created: unix},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: time.Unix(unix, 0).String(),
|
|
|
|
call: ctx.CreatedAt,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Ports: []container.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "8080/tcp",
|
|
|
|
call: ctx.Ports,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Status: "RUNNING"},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "RUNNING",
|
|
|
|
call: ctx.Status,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{SizeRw: 10},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "10B",
|
|
|
|
call: ctx.Size,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{SizeRw: 10, SizeRootFs: 20},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "10B (virtual 20B)",
|
|
|
|
call: ctx.Size,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
call: ctx.Labels,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Labels: map[string]string{"cpu": "6", "storage": "ssd"}},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "cpu=6,storage=ssd",
|
|
|
|
call: ctx.Labels,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{Created: unix},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "About a minute ago",
|
|
|
|
call: ctx.RunningFor,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{
|
|
|
|
Mounts: []container.MountPoint{
|
2024-07-03 09:43:47 -04:00
|
|
|
{
|
|
|
|
Name: "this-is-a-long-volume-name-and-will-be-truncated-if-trunc-is-set",
|
|
|
|
Driver: "local",
|
|
|
|
Source: "/a/path",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
trunc: true,
|
|
|
|
expValue: "this-is-a-long…",
|
|
|
|
call: ctx.Mounts,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{
|
|
|
|
Mounts: []container.MountPoint{
|
2024-07-03 09:43:47 -04:00
|
|
|
{
|
|
|
|
Driver: "local",
|
|
|
|
Source: "/a/path",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expValue: "/a/path",
|
|
|
|
call: ctx.Mounts,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
container: container.Summary{
|
|
|
|
Mounts: []container.MountPoint{
|
2024-07-03 09:43:47 -04:00
|
|
|
{
|
|
|
|
Name: "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203",
|
|
|
|
Driver: "local",
|
|
|
|
Source: "/a/path",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expValue: "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203",
|
|
|
|
call: ctx.Mounts,
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-12-24 10:30:23 -05:00
|
|
|
ctx = ContainerContext{c: c.container, trunc: c.trunc}
|
2016-09-08 13:11:39 -04:00
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
2020-07-10 09:51:07 -04:00
|
|
|
test.CompareMultipleValues(t, v, c.expValue)
|
2016-09-08 13:11:39 -04:00
|
|
|
} else if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 09:35:44 -04:00
|
|
|
c1 := container.Summary{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
|
2019-12-24 10:30:23 -05:00
|
|
|
ctx = ContainerContext{c: c1, trunc: true}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
sid := ctx.Label("com.docker.swarm.swarm-id")
|
|
|
|
node := ctx.Label("com.docker.swarm.node_name")
|
|
|
|
if sid != "33" {
|
|
|
|
t.Fatalf("Expected 33, was %s\n", sid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node != "ubuntu" {
|
|
|
|
t.Fatalf("Expected ubuntu, was %s\n", node)
|
|
|
|
}
|
|
|
|
|
2024-07-03 09:35:44 -04:00
|
|
|
c2 := container.Summary{}
|
2019-12-24 10:30:23 -05:00
|
|
|
ctx = ContainerContext{c: c2, trunc: true}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
label := ctx.Label("anything.really")
|
|
|
|
if label != "" {
|
|
|
|
t.Fatalf("Expected an empty string, was %s", label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerContextWrite(t *testing.T) {
|
|
|
|
unixTime := time.Now().AddDate(0, 0, -1).Unix()
|
|
|
|
expectedTime := time.Unix(unixTime, 0).String()
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
cases := []struct {
|
|
|
|
context Context
|
2016-09-08 13:11:39 -04:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Errors
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: "{{InvalidFunction}}"},
|
|
|
|
expected: `template parsing error: template: :1: function "InvalidFunction" not defined`,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: "{{nil}}"},
|
|
|
|
expected: `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
// Table Format
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("table", false, true)},
|
|
|
|
expected: `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
|
2020-08-28 17:00:21 -04:00
|
|
|
containerID1 ubuntu "" 24 hours ago foobar_baz 0B
|
|
|
|
containerID2 ubuntu "" 24 hours ago foobar_bar 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("table", false, false)},
|
|
|
|
expected: `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
2020-08-28 17:00:21 -04:00
|
|
|
containerID1 ubuntu "" 24 hours ago foobar_baz
|
|
|
|
containerID2 ubuntu "" 24 hours ago foobar_bar
|
2016-09-08 13:11:39 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("table {{.Image}}", false, false)},
|
|
|
|
expected: "IMAGE\nubuntu\nubuntu\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("table {{.Image}}", false, true)},
|
|
|
|
expected: "IMAGE\nubuntu\nubuntu\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("table {{.Image}}", true, false)},
|
|
|
|
expected: "containerID1\ncontainerID2\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("table", true, false)},
|
|
|
|
expected: "containerID1\ncontainerID2\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2019-07-12 07:10:08 -04:00
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("table {{.State}}", false, true)},
|
|
|
|
expected: "STATE\nrunning\nrunning\n",
|
2019-07-12 07:10:08 -04:00
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
// Raw Format
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("raw", false, false)},
|
|
|
|
expected: fmt.Sprintf(`container_id: containerID1
|
2016-09-08 13:11:39 -04:00
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2019-07-12 07:10:08 -04:00
|
|
|
state: running
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_baz
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
container_id: containerID2
|
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2019-07-12 07:10:08 -04:00
|
|
|
state: running
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_bar
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
`, expectedTime, expectedTime),
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("raw", false, true)},
|
|
|
|
expected: fmt.Sprintf(`container_id: containerID1
|
2016-09-08 13:11:39 -04:00
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2019-07-12 07:10:08 -04:00
|
|
|
state: running
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_baz
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2017-02-07 15:58:56 -05:00
|
|
|
size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
container_id: containerID2
|
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2019-07-12 07:10:08 -04:00
|
|
|
state: running
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_bar
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2017-02-07 15:58:56 -05:00
|
|
|
size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
`, expectedTime, expectedTime),
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("raw", true, false)},
|
|
|
|
expected: "container_id: containerID1\ncontainer_id: containerID2\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
// Custom Format
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: "{{.Image}}"},
|
|
|
|
expected: "ubuntu\nubuntu\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat("{{.Image}}", false, true)},
|
|
|
|
expected: "ubuntu\nubuntu\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2017-05-21 21:39:06 -04:00
|
|
|
// Special headers for customized table format
|
2017-02-03 23:23:00 -05:00
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)},
|
|
|
|
expected: string(golden.Get(t, "container-context-write-special-headers.golden")),
|
2017-02-03 23:23:00 -05:00
|
|
|
},
|
2019-12-24 11:32:44 -05:00
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{Format: NewContainerFormat(`table {{split .Image ":"}}`, false, false)},
|
|
|
|
expected: "IMAGE\n[ubuntu]\n[ubuntu]\n",
|
2019-12-24 11:32:44 -05:00
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 09:35:44 -04:00
|
|
|
containers := []container.Summary{
|
2020-08-28 17:00:21 -04:00
|
|
|
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unixTime, State: "running"},
|
|
|
|
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unixTime, State: "running"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(string(tc.context.Format), func(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
tc.context.Output = &out
|
|
|
|
err := ContainerWrite(tc.context, containers)
|
|
|
|
if err != nil {
|
|
|
|
assert.Error(t, err, tc.expected)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, out.String(), tc.expected)
|
|
|
|
}
|
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
|
|
|
out := bytes.NewBufferString("")
|
2024-07-03 09:35:44 -04:00
|
|
|
containers := []container.Summary{}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
cases := []struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
context Context
|
2016-09-08 13:11:39 -04:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: "{{.Image}}",
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: "table {{.Image}}",
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "IMAGE\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewContainerFormat("{{.Image}}", false, true),
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewContainerFormat("table {{.Image}}", false, true),
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "IMAGE\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: "table {{.Image}}\t{{.Size}}",
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "IMAGE SIZE\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:43:47 -04:00
|
|
|
context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true),
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "IMAGE SIZE\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(string(tc.context.Format), func(t *testing.T) {
|
|
|
|
err := ContainerWrite(tc.context, containers)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, out.String(), tc.expected)
|
|
|
|
// Clean buffer
|
|
|
|
out.Reset()
|
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 03:01:31 -04:00
|
|
|
|
|
|
|
func TestContainerContextWriteJSON(t *testing.T) {
|
|
|
|
unix := time.Now().Add(-65 * time.Second).Unix()
|
2024-07-03 09:35:44 -04:00
|
|
|
containers := []container.Summary{
|
2019-07-12 07:10:08 -04:00
|
|
|
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix, State: "running"},
|
|
|
|
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix, State: "running"},
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
expectedCreated := time.Unix(unix, 0).String()
|
2023-11-20 12:04:36 -05:00
|
|
|
expectedJSONs := []map[string]any{
|
2017-05-03 18:14:30 -04:00
|
|
|
{
|
|
|
|
"Command": "\"\"",
|
|
|
|
"CreatedAt": expectedCreated,
|
|
|
|
"ID": "containerID1",
|
|
|
|
"Image": "ubuntu",
|
|
|
|
"Labels": "",
|
|
|
|
"LocalVolumes": "0",
|
|
|
|
"Mounts": "",
|
|
|
|
"Names": "foobar_baz",
|
|
|
|
"Networks": "",
|
|
|
|
"Ports": "",
|
|
|
|
"RunningFor": "About a minute ago",
|
|
|
|
"Size": "0B",
|
2019-07-12 07:10:08 -04:00
|
|
|
"State": "running",
|
2017-05-03 18:14:30 -04:00
|
|
|
"Status": "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Command": "\"\"",
|
|
|
|
"CreatedAt": expectedCreated,
|
|
|
|
"ID": "containerID2",
|
|
|
|
"Image": "ubuntu",
|
|
|
|
"Labels": "",
|
|
|
|
"LocalVolumes": "0",
|
|
|
|
"Mounts": "",
|
|
|
|
"Names": "foobar_bar",
|
|
|
|
"Networks": "",
|
|
|
|
"Ports": "",
|
|
|
|
"RunningFor": "About a minute ago",
|
|
|
|
"Size": "0B",
|
2019-07-12 07:10:08 -04:00
|
|
|
"State": "running",
|
2017-05-03 18:14:30 -04:00
|
|
|
"Status": "",
|
|
|
|
},
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
err := ContainerWrite(Context{Format: "{{json .}}", Output: out}, containers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
2017-06-15 12:28:08 -04:00
|
|
|
msg := fmt.Sprintf("Output: line %d: %s", i, line)
|
2023-11-20 12:04:36 -05:00
|
|
|
var m map[string]any
|
2017-06-15 12:28:08 -04:00
|
|
|
err := json.Unmarshal([]byte(line), &m)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err, msg)
|
|
|
|
assert.Check(t, is.DeepEqual(expectedJSONs[i], m), msg)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerContextWriteJSONField(t *testing.T) {
|
2024-07-03 09:35:44 -04:00
|
|
|
containers := []container.Summary{
|
2016-09-13 03:01:31 -04:00
|
|
|
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu"},
|
|
|
|
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu"},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
err := ContainerWrite(Context{Format: "{{json .ID}}", Output: out}, containers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
2017-06-15 12:28:08 -04:00
|
|
|
msg := fmt.Sprintf("Output: line %d: %s", i, line)
|
2016-09-13 03:01:31 -04:00
|
|
|
var s string
|
2017-06-15 12:28:08 -04:00
|
|
|
err := json.Unmarshal([]byte(line), &s)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err, msg)
|
|
|
|
assert.Check(t, is.Equal(containers[i].ID, s), msg)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 11:10:27 -05:00
|
|
|
|
|
|
|
func TestContainerBackCompat(t *testing.T) {
|
2024-07-03 09:35:44 -04:00
|
|
|
containers := []container.Summary{{ID: "brewhaha"}}
|
2016-11-12 11:10:27 -05:00
|
|
|
cases := []string{
|
|
|
|
"ID",
|
|
|
|
"Names",
|
|
|
|
"Image",
|
|
|
|
"Command",
|
|
|
|
"CreatedAt",
|
|
|
|
"RunningFor",
|
|
|
|
"Ports",
|
|
|
|
"Status",
|
|
|
|
"Size",
|
|
|
|
"Labels",
|
|
|
|
"Mounts",
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", c)), Output: buf}
|
|
|
|
if err := ContainerWrite(ctx, containers); err != nil {
|
2016-11-17 01:17:40 -05:00
|
|
|
t.Logf("could not render template for field '%s': %v", c, err)
|
2016-11-12 11:10:27 -05:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
2017-06-23 10:24:43 -04:00
|
|
|
|
|
|
|
type ports struct {
|
2024-07-03 09:35:44 -04:00
|
|
|
ports []container.Port
|
2017-06-23 10:24:43 -04:00
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:29:49 -04:00
|
|
|
//nolint:lll
|
2017-06-23 10:24:43 -04:00
|
|
|
func TestDisplayablePorts(t *testing.T) {
|
|
|
|
cases := []ports{
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "9988/tcp",
|
2022-09-29 11:21:51 -04:00
|
|
|
},
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "9988/udp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
IP: "0.0.0.0",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "0.0.0.0:0->9988/tcp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
2024-08-20 11:31:07 -04:00
|
|
|
{
|
|
|
|
ports: []container.Port{
|
|
|
|
{
|
|
|
|
IP: "::",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "[::]:0->9988/tcp",
|
|
|
|
},
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "9988/tcp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "4.3.2.1:8899->9988/tcp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
2024-09-23 06:16:58 -04:00
|
|
|
{
|
|
|
|
ports: []container.Port{
|
|
|
|
{
|
|
|
|
IP: "::1",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "[::1]:8899->9988/tcp",
|
|
|
|
},
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "4.3.2.1:9988->9988/tcp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
2024-09-23 06:16:58 -04:00
|
|
|
{
|
|
|
|
ports: []container.Port{
|
|
|
|
{
|
|
|
|
IP: "::1",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "[::1]:9988->9988/tcp",
|
|
|
|
},
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "9988/udp, 9988/udp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 9998,
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 9999,
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "1.2.3.4:9998-9999->9998-9999/udp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
2024-09-23 06:16:58 -04:00
|
|
|
{
|
|
|
|
ports: []container.Port{
|
|
|
|
{
|
|
|
|
IP: "::1",
|
|
|
|
PublicPort: 9998,
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "::1",
|
|
|
|
PublicPort: 9999,
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "[::1]:9998-9999->9998-9999/udp",
|
|
|
|
},
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 8887,
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 8888,
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
2024-09-23 06:16:58 -04:00
|
|
|
{
|
|
|
|
ports: []container.Port{
|
|
|
|
{
|
|
|
|
IP: "::1",
|
|
|
|
PublicPort: 8887,
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "::1",
|
|
|
|
PublicPort: 8888,
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "[::1]:8887->9998/udp, [::1]:8888->9999/udp",
|
|
|
|
},
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "9998-9999/udp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 6677,
|
|
|
|
PublicPort: 7766,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "9988/udp, 1.2.3.4:7766->6677/tcp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 2233,
|
|
|
|
PublicPort: 3322,
|
|
|
|
Type: "tcp",
|
2024-09-23 06:16:58 -04:00
|
|
|
}, {
|
|
|
|
IP: "::1",
|
|
|
|
PrivatePort: 2233,
|
|
|
|
PublicPort: 3322,
|
|
|
|
Type: "tcp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
},
|
2024-09-23 06:16:58 -04:00
|
|
|
expected: "4.3.2.1:3322->2233/tcp, [::1]:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 6677,
|
|
|
|
PublicPort: 7766,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 2233,
|
|
|
|
PublicPort: 3322,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-03 09:35:44 -04:00
|
|
|
ports: []container.Port{
|
2017-06-23 10:24:43 -04:00
|
|
|
{
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "udp",
|
2017-06-13 23:42:58 -04:00
|
|
|
}, {
|
|
|
|
PrivatePort: 12345,
|
|
|
|
Type: "sctp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
},
|
2024-07-03 09:43:47 -04:00
|
|
|
expected: "80/tcp, 80/udp, 1024/tcp, 1024/udp, 12345/sctp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp",
|
2017-06-23 10:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range cases {
|
|
|
|
actual := DisplayablePorts(port.ports)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(port.expected, actual))
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
}
|