client_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. err := c.Put(testBucket, "foo", "bar")
  46. Expect(err).ToNot(HaveOccurred())
  47. Eventually(func() string {
  48. s, _ := c.Summary()
  49. return s.NodeID
  50. }, 100*time.Second, 1*time.Second).ShouldNot(BeEmpty())
  51. })
  52. It("Puts string data", func() {
  53. err := c.Put(testBucket, "foo", "bar")
  54. Expect(err).ToNot(HaveOccurred())
  55. Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).Should(ContainElement(testBucket))
  56. Eventually(func() ([]string, error) { return c.GetBucketKeys(testBucket) }, 100*time.Second, 1*time.Second).Should(ContainElement("foo"))
  57. Eventually(func() (string, error) {
  58. resp, err := c.GetBucketKey(testBucket, "foo")
  59. if err == nil {
  60. var r string
  61. resp.Unmarshal(&r)
  62. return r, nil
  63. }
  64. return "", err
  65. }, 100*time.Second, 1*time.Second).Should(Equal("bar"))
  66. m, err := c.Ledger()
  67. Expect(err).ToNot(HaveOccurred())
  68. Expect(len(m) > 0).To(BeTrue())
  69. })
  70. It("Puts random data", func() {
  71. err := c.Put(testBucket, "foo2", struct{ Foo string }{Foo: "bar"})
  72. Expect(err).ToNot(HaveOccurred())
  73. Eventually(func() (string, error) {
  74. resp, err := c.GetBucketKey(testBucket, "foo2")
  75. if err == nil {
  76. var r struct{ Foo string }
  77. resp.Unmarshal(&r)
  78. return r.Foo, nil
  79. }
  80. return "", err
  81. }, 100*time.Second, 1*time.Second).Should(Equal("bar"))
  82. })
  83. })
  84. })