mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add netlable. KeyValue() and ToMap()
Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
parent
14afa765a9
commit
b7c2b8111f
2 changed files with 94 additions and 2 deletions
|
@ -1,6 +1,9 @@
|
||||||
package netlabel
|
package netlabel
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Prefix constant marks the reserved label space for libnetwork
|
// Prefix constant marks the reserved label space for libnetwork
|
||||||
|
@ -22,7 +25,7 @@ const (
|
||||||
// MacAddress constant represents Mac Address config of a Container
|
// MacAddress constant represents Mac Address config of a Container
|
||||||
MacAddress = Prefix + ".endpoint.macaddress"
|
MacAddress = Prefix + ".endpoint.macaddress"
|
||||||
|
|
||||||
// ExposedPorts constant represents exposedports of a Container
|
// ExposedPorts constant represents the container's Exposed Ports
|
||||||
ExposedPorts = Prefix + ".endpoint.exposedports"
|
ExposedPorts = Prefix + ".endpoint.exposedports"
|
||||||
|
|
||||||
//EnableIPv6 constant represents enabling IPV6 at network level
|
//EnableIPv6 constant represents enabling IPV6 at network level
|
||||||
|
@ -86,3 +89,23 @@ func Value(label string) string {
|
||||||
|
|
||||||
return kv[1]
|
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
|
||||||
|
}
|
||||||
|
|
69
libnetwork/netlabel/labels_test.go
Normal file
69
libnetwork/netlabel/labels_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue