plugin_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. package staticroute
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSetup4(t *testing.T) {
  10. assert.Empty(t, routes)
  11. var err error
  12. // no args
  13. _, err = setup4()
  14. if assert.Error(t, err) {
  15. assert.Equal(t, "need at least one static route", err.Error())
  16. }
  17. // invalid arg
  18. _, err = setup4("foo")
  19. if assert.Error(t, err) {
  20. assert.Equal(t, "expected a destination/gateway pair, got: foo", err.Error())
  21. }
  22. // invalid destination
  23. _, err = setup4("foo,")
  24. if assert.Error(t, err) {
  25. assert.Equal(t, "expected a destination subnet, got: foo", err.Error())
  26. }
  27. // invalid gateway
  28. _, err = setup4("10.0.0.0/8,foo")
  29. if assert.Error(t, err) {
  30. assert.Equal(t, "expected a gateway address, got: foo", err.Error())
  31. }
  32. // valid route
  33. _, err = setup4("10.0.0.0/8,192.168.1.1")
  34. if assert.NoError(t, err) {
  35. if assert.Equal(t, 1, len(routes)) {
  36. assert.Equal(t, "10.0.0.0/8", routes[0].Dest.String())
  37. assert.Equal(t, "192.168.1.1", routes[0].Router.String())
  38. }
  39. }
  40. // multiple valid routes
  41. _, err = setup4("10.0.0.0/8,192.168.1.1", "192.168.2.0/24,192.168.1.100")
  42. if assert.NoError(t, err) {
  43. if assert.Equal(t, 2, len(routes)) {
  44. assert.Equal(t, "10.0.0.0/8", routes[0].Dest.String())
  45. assert.Equal(t, "192.168.1.1", routes[0].Router.String())
  46. assert.Equal(t, "192.168.2.0/24", routes[1].Dest.String())
  47. assert.Equal(t, "192.168.1.100", routes[1].Router.String())
  48. }
  49. }
  50. }