services_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright © 2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package services_test
  16. import (
  17. "context"
  18. "io/ioutil"
  19. "net/http"
  20. "time"
  21. "github.com/ipfs/go-log"
  22. . "github.com/onsi/ginkgo/v2"
  23. . "github.com/onsi/gomega"
  24. "github.com/mudler/edgevpn/pkg/blockchain"
  25. "github.com/mudler/edgevpn/pkg/logger"
  26. node "github.com/mudler/edgevpn/pkg/node"
  27. . "github.com/mudler/edgevpn/pkg/services"
  28. )
  29. func get(url string) string {
  30. client := &http.Client{
  31. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  32. return http.ErrUseLastResponse
  33. },
  34. Timeout: 1 * time.Second,
  35. }
  36. resp, err := client.Get(url)
  37. if err != nil {
  38. return ""
  39. }
  40. defer resp.Body.Close()
  41. body, err := ioutil.ReadAll(resp.Body)
  42. if err != nil {
  43. return ""
  44. }
  45. return string(body)
  46. }
  47. var _ = Describe("Expose services", func() {
  48. token := node.GenerateNewConnectionData().Base64()
  49. logg := logger.New(log.LevelFatal)
  50. l := node.Logger(logg)
  51. serviceUUID := "test"
  52. e2, _ := node.New(
  53. node.WithNetworkService(ConnectNetworkService(5*time.Second, serviceUUID, "127.0.0.1:9999")),
  54. node.WithDiscoveryInterval(10*time.Second),
  55. node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
  56. Context("Service sharing", func() {
  57. It("expose services and can connect to them", func() {
  58. ctx, cancel := context.WithCancel(context.Background())
  59. defer cancel()
  60. opts := RegisterService(logg, 5*time.Second, serviceUUID, "142.250.184.35:80")
  61. opts = append(opts, node.FromBase64(true, true, token), node.WithDiscoveryInterval(10*time.Second), node.WithStore(&blockchain.MemoryStore{}), l)
  62. e, _ := node.New(opts...)
  63. // First node expose a service
  64. // redirects to google:80
  65. e.Start(ctx)
  66. go e2.Start(ctx)
  67. Eventually(func() string {
  68. return get("http://127.0.0.1:9999")
  69. }, 60*time.Second, 1*time.Second).Should(ContainSubstring("The document has moved"))
  70. })
  71. })
  72. })