api_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 api_test
  14. import (
  15. "context"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "time"
  21. "github.com/ipfs/go-log"
  22. . "github.com/mudler/edgevpn/api"
  23. client "github.com/mudler/edgevpn/api/client"
  24. "github.com/mudler/edgevpn/pkg/blockchain"
  25. "github.com/mudler/edgevpn/pkg/logger"
  26. "github.com/mudler/edgevpn/pkg/node"
  27. . "github.com/onsi/ginkgo/v2"
  28. . "github.com/onsi/gomega"
  29. )
  30. var _ = Describe("API", func() {
  31. Context("Binds on socket", func() {
  32. It("sets data to the API", func() {
  33. d, _ := ioutil.TempDir("", "xxx")
  34. defer os.RemoveAll(d)
  35. os.MkdirAll(d, os.ModePerm)
  36. socket := filepath.Join(d, "socket")
  37. c := client.NewClient(client.WithHost("unix://" + socket))
  38. token := node.GenerateNewConnectionData().Base64()
  39. ctx, cancel := context.WithCancel(context.Background())
  40. defer cancel()
  41. l := node.Logger(logger.New(log.LevelFatal))
  42. e, _ := node.New(node.FromBase64(true, true, token, nil, nil), node.WithStore(&blockchain.MemoryStore{}), l)
  43. e.Start(ctx)
  44. e2, _ := node.New(node.FromBase64(true, true, token, nil, nil), node.WithStore(&blockchain.MemoryStore{}), l)
  45. e2.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. })