services_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  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 services_test
  14. import (
  15. "context"
  16. "io/ioutil"
  17. "net/http"
  18. "time"
  19. "github.com/ipfs/go-log"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. "github.com/mudler/edgevpn/pkg/blockchain"
  23. "github.com/mudler/edgevpn/pkg/logger"
  24. node "github.com/mudler/edgevpn/pkg/node"
  25. . "github.com/mudler/edgevpn/pkg/services"
  26. )
  27. func get(url string) string {
  28. client := &http.Client{
  29. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  30. return http.ErrUseLastResponse
  31. },
  32. Timeout: 1 * time.Second,
  33. }
  34. resp, err := client.Get(url)
  35. if err != nil {
  36. return ""
  37. }
  38. defer resp.Body.Close()
  39. body, err := ioutil.ReadAll(resp.Body)
  40. if err != nil {
  41. return ""
  42. }
  43. return string(body)
  44. }
  45. var _ = Describe("Expose services", func() {
  46. token := node.GenerateNewConnectionData().Base64()
  47. logg := logger.New(log.LevelFatal)
  48. l := node.Logger(logg)
  49. serviceUUID := "test"
  50. Context("Service sharing", func() {
  51. PIt("expose services and can connect to them", func() {
  52. ctx, cancel := context.WithCancel(context.Background())
  53. defer cancel()
  54. opts := RegisterService(logg, 5*time.Second, serviceUUID, "142.250.184.35:80")
  55. opts = append(opts, node.FromBase64(true, true, token, nil, nil), node.WithDiscoveryInterval(10*time.Second), node.WithStore(&blockchain.MemoryStore{}), l)
  56. e, _ := node.New(opts...)
  57. // First node expose a service
  58. // redirects to google:80
  59. e.Start(ctx)
  60. go func() {
  61. e2, _ := node.New(
  62. node.WithNetworkService(ConnectNetworkService(5*time.Second, serviceUUID, "127.0.0.1:9999")),
  63. node.WithDiscoveryInterval(10*time.Second),
  64. node.FromBase64(true, true, token, nil, nil), node.WithStore(&blockchain.MemoryStore{}), l)
  65. e2.Start(ctx)
  66. }()
  67. Eventually(func() string {
  68. return get("http://127.0.0.1:9999")
  69. }, 360*time.Second, 1*time.Second).Should(ContainSubstring("The document has moved"))
  70. })
  71. })
  72. })