api_test.go 2.3 KB

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