firewall_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package gce
  14. import "testing"
  15. func TestIsPortsSubset(t *testing.T) {
  16. tc := map[string]struct {
  17. required []string
  18. coverage []string
  19. expectErr bool
  20. }{
  21. "Single port coverage": {
  22. required: []string{"tcp/50"},
  23. coverage: []string{"tcp/50", "tcp/60", "tcp/70"},
  24. },
  25. "Port range coverage": {
  26. required: []string{"tcp/50"},
  27. coverage: []string{"tcp/20-30", "tcp/45-60"},
  28. },
  29. "Multiple Port range coverage": {
  30. required: []string{"tcp/50", "tcp/29", "tcp/46"},
  31. coverage: []string{"tcp/20-30", "tcp/45-60"},
  32. },
  33. "Not covered": {
  34. required: []string{"tcp/50"},
  35. coverage: []string{"udp/50", "tcp/49", "tcp/51-60"},
  36. expectErr: true,
  37. },
  38. }
  39. for name, c := range tc {
  40. t.Run(name, func(t *testing.T) {
  41. gotErr := isPortsSubset(c.required, c.coverage)
  42. if c.expectErr != (gotErr != nil) {
  43. t.Errorf("isPortsSubset(%v, %v) = %v, wanted err? %v", c.required, c.coverage, gotErr, c.expectErr)
  44. }
  45. })
  46. }
  47. }