caps_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package capabilities // import "github.com/docker/docker/pkg/capabilities"
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestMatch(t *testing.T) {
  7. set := Set{
  8. "foo": struct{}{},
  9. "bar": struct{}{},
  10. }
  11. type testcase struct {
  12. caps [][]string
  13. expected []string
  14. }
  15. testcases := []testcase{
  16. // matches
  17. {
  18. caps: [][]string{{}},
  19. expected: []string{},
  20. },
  21. {
  22. caps: [][]string{{"foo"}},
  23. expected: []string{"foo"},
  24. },
  25. {
  26. caps: [][]string{{"bar"}, {"foo"}},
  27. expected: []string{"bar"},
  28. },
  29. {
  30. caps: [][]string{{"foo", "bar"}},
  31. expected: []string{"foo", "bar"},
  32. },
  33. {
  34. caps: [][]string{{"qux"}, {"foo"}},
  35. expected: []string{"foo"},
  36. },
  37. {
  38. caps: [][]string{{"foo", "bar"}, {"baz"}, {"bar"}},
  39. expected: []string{"foo", "bar"},
  40. },
  41. // non matches
  42. {caps: nil},
  43. {caps: [][]string{}},
  44. {caps: [][]string{{"qux"}}},
  45. {caps: [][]string{{"foo", "bar", "qux"}}},
  46. {caps: [][]string{{"qux"}, {"baz"}}},
  47. {caps: [][]string{{"foo", "baz"}}},
  48. }
  49. for _, m := range testcases {
  50. t.Run(fmt.Sprintf("%v", m.caps), func(t *testing.T) {
  51. selected := set.Match(m.caps)
  52. if m.expected == nil || selected == nil {
  53. if m.expected == nil && selected == nil {
  54. return
  55. }
  56. t.Fatalf("selected = %v, expected = %v", selected, m.expected)
  57. }
  58. if len(selected) != len(m.expected) {
  59. t.Fatalf("len(selected) = %d, len(expected) = %d", len(selected), len(m.expected))
  60. }
  61. for i, s := range selected {
  62. if m.expected[i] != s {
  63. t.Fatalf("selected[%d] = %s, expected[%d] = %s", i, s, i, m.expected[i])
  64. }
  65. }
  66. })
  67. }
  68. }