client_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 client_test
  16. import (
  17. "math/rand"
  18. "time"
  19. . "github.com/onsi/ginkgo/v2"
  20. . "github.com/onsi/gomega"
  21. . "github.com/mudler/edgevpn/api/client"
  22. )
  23. const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  24. func randStringBytes(n int) string {
  25. b := make([]byte, n)
  26. for i := range b {
  27. b[i] = letterBytes[rand.Intn(len(letterBytes))]
  28. }
  29. return string(b)
  30. }
  31. var _ = Describe("Client", func() {
  32. c := NewClient(WithHost(testInstance))
  33. Context("Operates blockchain", func() {
  34. var testBucket string
  35. AfterEach(func() {
  36. Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).Should(ContainElement(testBucket))
  37. err := c.DeleteBucket(testBucket)
  38. Expect(err).ToNot(HaveOccurred())
  39. Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).ShouldNot(ContainElement(testBucket))
  40. })
  41. BeforeEach(func() {
  42. testBucket = randStringBytes(10)
  43. })
  44. It("Summary returns some info", func() {
  45. Eventually(func() string {
  46. s, _ := c.Summary()
  47. return s.NodeID
  48. }, 100*time.Second, 1*time.Second).ShouldNot(BeEmpty())
  49. })
  50. It("Puts string data", func() {
  51. err := c.Put(testBucket, "foo", "bar")
  52. Expect(err).ToNot(HaveOccurred())
  53. Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).Should(ContainElement(testBucket))
  54. Eventually(func() ([]string, error) { return c.GetBucketKeys(testBucket) }, 100*time.Second, 1*time.Second).Should(ContainElement("foo"))
  55. Eventually(func() (string, error) {
  56. resp, err := c.GetBucketKey(testBucket, "foo")
  57. if err == nil {
  58. var r string
  59. resp.Unmarshal(&r)
  60. return r, nil
  61. }
  62. return "", err
  63. }, 100*time.Second, 1*time.Second).Should(Equal("bar"))
  64. m, err := c.Ledger()
  65. Expect(err).ToNot(HaveOccurred())
  66. Expect(len(m) > 0).To(BeTrue())
  67. })
  68. It("Puts random data", func() {
  69. err := c.Put(testBucket, "foo2", struct{ Foo string }{Foo: "bar"})
  70. Expect(err).ToNot(HaveOccurred())
  71. Eventually(func() (string, error) {
  72. resp, err := c.GetBucketKey(testBucket, "foo2")
  73. if err == nil {
  74. var r struct{ Foo string }
  75. resp.Unmarshal(&r)
  76. return r.Foo, nil
  77. }
  78. return "", err
  79. }, 100*time.Second, 1*time.Second).Should(Equal("bar"))
  80. })
  81. })
  82. })