client_test.go 2.3 KB

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