From b7c2b8111f1cdc4fbf31c89dde8164871962b7a8 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Tue, 6 Oct 2015 11:53:39 -0700 Subject: [PATCH] Add netlable. KeyValue() and ToMap() Signed-off-by: Alessandro Boch --- libnetwork/netlabel/labels.go | 27 +++++++++++- libnetwork/netlabel/labels_test.go | 69 ++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 libnetwork/netlabel/labels_test.go diff --git a/libnetwork/netlabel/labels.go b/libnetwork/netlabel/labels.go index dd30fbe5ae..5a9ff79998 100644 --- a/libnetwork/netlabel/labels.go +++ b/libnetwork/netlabel/labels.go @@ -1,6 +1,9 @@ package netlabel -import "strings" +import ( + "fmt" + "strings" +) const ( // Prefix constant marks the reserved label space for libnetwork @@ -22,7 +25,7 @@ const ( // MacAddress constant represents Mac Address config of a Container MacAddress = Prefix + ".endpoint.macaddress" - // ExposedPorts constant represents exposedports of a Container + // ExposedPorts constant represents the container's Exposed Ports ExposedPorts = Prefix + ".endpoint.exposedports" //EnableIPv6 constant represents enabling IPV6 at network level @@ -86,3 +89,23 @@ func Value(label string) string { return kv[1] } + +// KeyValue decomposes the label in the (key,value) pair +func KeyValue(label string) (string, string, error) { + kv := strings.SplitN(label, "=", 2) + if len(kv) != 2 { + return "", "", fmt.Errorf("invalid label: %s", label) + } + return kv[0], kv[1], nil +} + +// ToMap converts a list of labels in amap of (key,value) pairs +func ToMap(labels []string) map[string]string { + m := make(map[string]string, len(labels)) + for _, l := range labels { + if k, v, err := KeyValue(l); err == nil { + m[k] = v + } + } + return m +} diff --git a/libnetwork/netlabel/labels_test.go b/libnetwork/netlabel/labels_test.go new file mode 100644 index 0000000000..e000f6e4f9 --- /dev/null +++ b/libnetwork/netlabel/labels_test.go @@ -0,0 +1,69 @@ +package netlabel + +import ( + "testing" + + _ "github.com/docker/libnetwork/testutils" +) + +func TestKeyValue(t *testing.T) { + input := []struct { + label string + key string + value string + good bool + }{ + {"name=joe", "name", "joe", true}, + {"age=24", "age", "24", true}, + {"address:1234 First st.", "", "", false}, + {"girlfriend=", "girlfriend", "", true}, + {"nickname=o=u=8", "nickname", "o=u=8", true}, + {"", "", "", false}, + } + + for _, i := range input { + k, v, err := KeyValue(i.label) + if k != i.key || v != i.value || i.good != (err == nil) { + t.Fatalf("unexpected: %s, %s, %v", k, v, err) + } + } +} + +func TestToMap(t *testing.T) { + input := []struct { + label string + key string + value string + good bool + }{ + {"name=joe", "name", "joe", true}, + {"age=24", "age", "24", true}, + {"address:1234 First st.", "", "", false}, + {"girlfriend=", "girlfriend", "", true}, + {"nickname=o=u=8", "nickname", "o=u=8", true}, + {"", "", "", false}, + } + + lista := make([]string, len(input)) + for ind, i := range input { + lista[ind] = i.label + } + + mappa := ToMap(lista) + + if len(mappa) != len(lista)-2 { + t.Fatalf("Incorrect map length. Expected %d. Got %d", len(lista), len(mappa)) + } + + for _, i := range input { + if i.good { + if v, ok := mappa[i.key]; !ok || v != i.value { + t.Fatalf("Cannot find key or value for key: %s", i.key) + } + } else { + if _, ok := mappa[i.key]; ok { + t.Fatalf("Found invalid key in map: %s", i.key) + } + } + } +}