client_test.go 2.6 KB

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