|
1 | 1 | package container |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/binary" |
4 | 5 | "github.com/docker/docker/client" |
5 | 6 | "github.com/stretchr/testify/assert" |
6 | 7 | "testing" |
@@ -65,3 +66,77 @@ func TestCountCPUSet(t *testing.T) { |
65 | 66 | }) |
66 | 67 | } |
67 | 68 | } |
| 69 | + |
| 70 | +func TestParsePortBindingHostIP(t *testing.T) { |
| 71 | + tCases := map[string]struct { |
| 72 | + hostIP string |
| 73 | + parsedHostIP uint32 |
| 74 | + successExpected bool |
| 75 | + }{ |
| 76 | + "127.0.0.1": { |
| 77 | + hostIP: "127.0.0.1", |
| 78 | + parsedHostIP: binary.BigEndian.Uint32([]byte{127, 0, 0, 1}), |
| 79 | + successExpected: true, |
| 80 | + }, |
| 81 | + "Wrong literal": { |
| 82 | + hostIP: "Wrong literal", |
| 83 | + parsedHostIP: 0, |
| 84 | + successExpected: false, |
| 85 | + }, |
| 86 | + "IPv6 address": { |
| 87 | + hostIP: "fe80::1", |
| 88 | + parsedHostIP: 0, |
| 89 | + successExpected: false, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for name, tc := range tCases { |
| 94 | + t.Run(name, func(t *testing.T) { |
| 95 | + if !tc.successExpected { |
| 96 | + _, err := parsePortBindingHostIP(tc.hostIP) |
| 97 | + assert.Error(t, err) |
| 98 | + } else { |
| 99 | + parsedHostIP, err := parsePortBindingHostIP(tc.hostIP) |
| 100 | + assert.NoError(t, err) |
| 101 | + assert.Equal(t, tc.parsedHostIP, parsedHostIP) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestParsePortBindingHostPort(t *testing.T) { |
| 108 | + tCases := map[string]struct { |
| 109 | + hostPort string |
| 110 | + parsedHostPort uint16 |
| 111 | + successExpected bool |
| 112 | + }{ |
| 113 | + "1000": { |
| 114 | + hostPort: "1000", |
| 115 | + parsedHostPort: 1000, |
| 116 | + successExpected: true, |
| 117 | + }, |
| 118 | + "Wrong literal": { |
| 119 | + hostPort: "Wrong literal", |
| 120 | + parsedHostPort: 0, |
| 121 | + successExpected: false, |
| 122 | + }, |
| 123 | + "Out of range port": { |
| 124 | + hostPort: "65536", |
| 125 | + parsedHostPort: 0, |
| 126 | + successExpected: false, |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + for name, tc := range tCases { |
| 131 | + t.Run(name, func(t *testing.T) { |
| 132 | + if !tc.successExpected { |
| 133 | + _, err := parsePortBindingHostPort(tc.hostPort) |
| 134 | + assert.Error(t, err) |
| 135 | + } else { |
| 136 | + parsedHostPort, err := parsePortBindingHostPort(tc.hostPort) |
| 137 | + assert.NoError(t, err) |
| 138 | + assert.Equal(t, tc.parsedHostPort, parsedHostPort) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments