Browse code

Display empty string instead of <nil> when IP opt is nil.

Fixes #13878.

Signed-off-by: Eric-Olivier Lamey <eo@lamey.me>

Eric-Olivier Lamey authored on 2015/06/11 16:53:39
Showing 2 changed files
... ...
@@ -27,5 +27,8 @@ func (o *IpOpt) Set(val string) error {
27 27
 }
28 28
 
29 29
 func (o *IpOpt) String() string {
30
+	if *o.IP == nil {
31
+		return ""
32
+	}
30 33
 	return o.IP.String()
31 34
 }
... ...
@@ -2,6 +2,7 @@ package opts
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"net"
5 6
 	"strings"
6 7
 	"testing"
7 8
 )
... ...
@@ -179,6 +180,18 @@ func TestValidateExtraHosts(t *testing.T) {
179 179
 	}
180 180
 }
181 181
 
182
+func TestIpOptString(t *testing.T) {
183
+	addresses := []string{"", "0.0.0.0"}
184
+	var ip net.IP
185
+
186
+	for _, address := range addresses {
187
+		stringAddress := NewIpOpt(&ip, address).String()
188
+		if stringAddress != address {
189
+			t.Fatalf("IpOpt string should be `%s`, not `%s`", address, stringAddress)
190
+		}
191
+	}
192
+}
193
+
182 194
 func logOptsValidator(val string) (string, error) {
183 195
 	allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
184 196
 	vals := strings.Split(val, "=")