labels_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package netlabel
  2. import (
  3. "testing"
  4. _ "github.com/docker/libnetwork/testutils"
  5. )
  6. var input = []struct {
  7. label string
  8. key string
  9. value string
  10. }{
  11. {"com.directory.person.name=joe", "com.directory.person.name", "joe"},
  12. {"com.directory.person.age=24", "com.directory.person.age", "24"},
  13. {"com.directory.person.address=1234 First st.", "com.directory.person.address", "1234 First st."},
  14. {"com.directory.person.friends=", "com.directory.person.friends", ""},
  15. {"com.directory.person.nickname=o=u=8", "com.directory.person.nickname", "o=u=8"},
  16. {"", "", ""},
  17. {"com.directory.person.student", "com.directory.person.student", ""},
  18. }
  19. func TestKeyValue(t *testing.T) {
  20. for _, i := range input {
  21. k, v := KeyValue(i.label)
  22. if k != i.key || v != i.value {
  23. t.Fatalf("unexpected: %s, %s", k, v)
  24. }
  25. }
  26. }
  27. func TestToMap(t *testing.T) {
  28. lista := make([]string, len(input))
  29. for ind, i := range input {
  30. lista[ind] = i.label
  31. }
  32. mappa := ToMap(lista)
  33. if len(mappa) != len(lista) {
  34. t.Fatalf("Incorrect map length. Expected %d. Got %d", len(lista), len(mappa))
  35. }
  36. for _, i := range input {
  37. if v, ok := mappa[i.key]; !ok || v != i.value {
  38. t.Fatalf("Cannot find key or value for key: %s", i.key)
  39. }
  40. }
  41. }
  42. func TestFromMap(t *testing.T) {
  43. var m map[string]string
  44. lbls := FromMap(m)
  45. if len(lbls) != 0 {
  46. t.Fatalf("unexpected lbls length")
  47. }
  48. m = make(map[string]string, 3)
  49. m["peso"] = "85"
  50. m["statura"] = "170"
  51. m["maschio"] = ""
  52. lbls = FromMap(m)
  53. if len(lbls) != 3 {
  54. t.Fatalf("unexpected lbls length")
  55. }
  56. for _, l := range lbls {
  57. switch l {
  58. case "peso=85":
  59. case "statura=170":
  60. case "maschio":
  61. default:
  62. t.Fatalf("unexpected label: %s", l)
  63. }
  64. }
  65. }