api_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright © 2021 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 api_test
  16. import (
  17. "context"
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "net/http"
  22. "os"
  23. "path/filepath"
  24. "time"
  25. "github.com/ipfs/go-log"
  26. . "github.com/mudler/edgevpn/api"
  27. client "github.com/mudler/edgevpn/api/client"
  28. "github.com/mudler/edgevpn/pkg/blockchain"
  29. "github.com/mudler/edgevpn/pkg/logger"
  30. "github.com/mudler/edgevpn/pkg/node"
  31. . "github.com/onsi/ginkgo/v2"
  32. . "github.com/onsi/gomega"
  33. )
  34. var _ = Describe("API", func() {
  35. Context("Binds on socket", func() {
  36. It("sets data to the API", func() {
  37. d, _ := ioutil.TempDir("", "xxx")
  38. defer os.RemoveAll(d)
  39. os.MkdirAll(d, os.ModePerm)
  40. socket := filepath.Join(d, "socket")
  41. c := client.NewClient(client.WithHost("http://unix"), client.WithHTTPClient(&http.Client{
  42. Transport: &http.Transport{
  43. DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
  44. return net.Dial("unix", socket)
  45. },
  46. },
  47. }))
  48. token := node.GenerateNewConnectionData().Base64()
  49. ctx, cancel := context.WithCancel(context.Background())
  50. defer cancel()
  51. l := node.Logger(logger.New(log.LevelFatal))
  52. e, _ := node.New(node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
  53. e.Start(ctx)
  54. go func() {
  55. err := API(ctx, fmt.Sprintf("unix://%s", socket), 10*time.Second, 20*time.Second, e)
  56. Expect(err).ToNot(HaveOccurred())
  57. }()
  58. Eventually(func() error {
  59. return c.Put("b", "f", "bar")
  60. }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred())
  61. Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).Should(ContainElement("b"))
  62. Eventually(func() string {
  63. d, err := c.GetBucketKey("b", "f")
  64. if err != nil {
  65. fmt.Println(err)
  66. }
  67. var s string
  68. d.Unmarshal(&s)
  69. return s
  70. }, 10*time.Second, 1*time.Second).Should(Equal("bar"))
  71. })
  72. })
  73. })