client_test.go 3.0 KB

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