client_test.go 2.7 KB

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