services_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. Context("Service sharing", func() {
  53. PIt("expose services and can connect to them", func() {
  54. ctx, cancel := context.WithCancel(context.Background())
  55. defer cancel()
  56. opts := RegisterService(logg, 5*time.Second, serviceUUID, "142.250.184.35:80")
  57. opts = append(opts, node.FromBase64(true, true, token), node.WithDiscoveryInterval(10*time.Second), node.WithStore(&blockchain.MemoryStore{}), l)
  58. e, _ := node.New(opts...)
  59. // First node expose a service
  60. // redirects to google:80
  61. e.Start(ctx)
  62. go func() {
  63. e2, _ := node.New(
  64. node.WithNetworkService(ConnectNetworkService(5*time.Second, serviceUUID, "127.0.0.1:9999")),
  65. node.WithDiscoveryInterval(10*time.Second),
  66. node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
  67. e2.Start(ctx)
  68. }()
  69. Eventually(func() string {
  70. return get("http://127.0.0.1:9999")
  71. }, 360*time.Second, 1*time.Second).Should(ContainSubstring("The document has moved"))
  72. })
  73. })
  74. })