server6_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. //go:build integration
  5. // +build integration
  6. package e2e_test
  7. import (
  8. "fmt"
  9. "log"
  10. "net"
  11. "runtime"
  12. "testing"
  13. "github.com/insomniacslk/dhcp/dhcpv6"
  14. "github.com/insomniacslk/dhcp/dhcpv6/client6"
  15. "github.com/insomniacslk/dhcp/iana"
  16. "github.com/stretchr/testify/require"
  17. "github.com/vishvananda/netns"
  18. "github.com/coredhcp/coredhcp/config"
  19. "github.com/coredhcp/coredhcp/plugins"
  20. "github.com/coredhcp/coredhcp/server"
  21. // Plugins
  22. "github.com/coredhcp/coredhcp/plugins/file"
  23. "github.com/coredhcp/coredhcp/plugins/serverid"
  24. )
  25. var serverConfig = config.Config{
  26. Server6: &config.ServerConfig{
  27. Addresses: []net.UDPAddr{
  28. {
  29. IP: net.ParseIP("ff02::1:2"),
  30. Port: dhcpv6.DefaultServerPort,
  31. Zone: "cdhcp_srv",
  32. },
  33. },
  34. Plugins: []config.PluginConfig{
  35. {Name: "server_id", Args: []string{"LL", "11:22:33:44:55:66"}},
  36. {Name: "file", Args: []string{"./leases-dhcpv6-test.txt"}},
  37. },
  38. },
  39. }
  40. // This function *must* be run in its own routine
  41. // For now this assumes ns are created outside.
  42. // TODO: dynamically create NS and interfaces directly in the test program
  43. func runServer(readyCh chan<- struct{}, nsName string, desiredPlugins []*plugins.Plugin) {
  44. runtime.LockOSThread()
  45. defer runtime.UnlockOSThread()
  46. ns, err := netns.GetFromName(nsName)
  47. if err != nil {
  48. log.Panicf("Netns `%s` not set up: %v", nsName, err)
  49. }
  50. if err := netns.Set(ns); err != nil {
  51. log.Panicf("Failed to switch to netns `%s`: %v", nsName, err)
  52. }
  53. // register plugins
  54. for _, pl := range desiredPlugins {
  55. if err := plugins.RegisterPlugin(pl); err != nil {
  56. log.Panicf("Failed to register plugin `%s`: %v", pl.Name, err)
  57. }
  58. }
  59. // start DHCP server
  60. srv, err := server.Start(&serverConfig)
  61. if err != nil {
  62. log.Panicf("Server could not start: %v", err)
  63. }
  64. readyCh <- struct{}{}
  65. if err := srv.Wait(); err != nil {
  66. log.Panicf("Server errored during run: %v", err)
  67. }
  68. }
  69. // runInNs will execute the provided cmd in the namespace nsName.
  70. // It returns the error status of the cmd. Errors in NS management will panic
  71. func runClient6(nsName, iface string, modifiers ...dhcpv6.Modifier) error {
  72. runtime.LockOSThread()
  73. defer runtime.UnlockOSThread()
  74. backupNS, err := netns.Get()
  75. if err != nil {
  76. panic("Could not save handle to original NS")
  77. }
  78. ns, err := netns.GetFromName(nsName)
  79. if err != nil {
  80. panic("netns not set up")
  81. }
  82. if err := netns.Set(ns); err != nil {
  83. panic(fmt.Sprintf("Couldn't switch to test NS: %v", err))
  84. }
  85. client := client6.NewClient()
  86. _, cErr := client.Exchange(iface, modifiers...)
  87. if netns.Set(backupNS) != nil {
  88. panic("couldn't switch back to original NS")
  89. }
  90. return cErr
  91. }
  92. // TestDora creates a server and attempts to connect to it
  93. func TestDora(t *testing.T) {
  94. readyCh := make(chan struct{}, 1)
  95. go runServer(readyCh,
  96. "coredhcp-direct-upper",
  97. []*plugins.Plugin{
  98. &serverid.Plugin, &file.Plugin,
  99. },
  100. )
  101. // wait for server to be ready before sending DHCP request
  102. <-readyCh
  103. mac, err := net.ParseMAC("de:ad:be:ef:00:00")
  104. if err != nil {
  105. panic(err)
  106. }
  107. require.NoError(t, runClient6(
  108. "coredhcp-direct-lower", "cdhcp_cli",
  109. dhcpv6.WithClientID(&dhcpv6.DUIDLL{
  110. HWType: iana.HWTypeEthernet,
  111. LinkLayerAddr: mac,
  112. }),
  113. ))
  114. }