-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 Add golang client API with test suite
- Loading branch information
Showing
8 changed files
with
300 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
|
||
./edgevpn api & | ||
|
||
GO111MODULE=off go get github.com/onsi/ginkgo/ginkgo | ||
GO111MODULE=off go get github.com/onsi/gomega/... | ||
|
||
TEST_INSTANCE="http://localhost:8080" ginkgo -r api/client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package client_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestClient(t *testing.T) { | ||
if testInstance == "" { | ||
fmt.Println("a testing instance has to be defined with TEST_INSTANCE") | ||
os.Exit(1) | ||
} | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Client Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package client_test | ||
|
||
import ( | ||
"math/rand" | ||
"os" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/mudler/edgevpn/api/client" | ||
) | ||
|
||
var testInstance = os.Getenv("TEST_INSTANCE") | ||
|
||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
func randStringBytes(n int) string { | ||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = letterBytes[rand.Intn(len(letterBytes))] | ||
} | ||
return string(b) | ||
} | ||
|
||
var _ = Describe("Client", func() { | ||
c := NewClient(WithHost(testInstance)) | ||
|
||
// Start the test suite only if we have some machines connected | ||
BeforeSuite(func() { | ||
Eventually(func() (int, error) { | ||
m, err := c.Machines() | ||
return len(m), err | ||
}, 100*time.Second, 1*time.Second).Should(BeNumerically(">=", 0)) | ||
}) | ||
|
||
Context("Operates blockchain", func() { | ||
var testBucket string | ||
|
||
AfterEach(func() { | ||
Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).Should(ContainElement(testBucket)) | ||
err := c.DeleteBucket(testBucket) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).ShouldNot(ContainElement(testBucket)) | ||
}) | ||
|
||
BeforeEach(func() { | ||
testBucket = randStringBytes(10) | ||
}) | ||
|
||
It("Puts string data", func() { | ||
err := c.Put(testBucket, "foo", "bar") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Eventually(c.GetBuckets, 100*time.Second, 1*time.Second).Should(ContainElement(testBucket)) | ||
Eventually(func() ([]string, error) { return c.GetBucketKeys(testBucket) }, 100*time.Second, 1*time.Second).Should(ContainElement("foo")) | ||
|
||
Eventually(func() (string, error) { | ||
resp, err := c.GetBucketKey(testBucket, "foo") | ||
if err == nil { | ||
var r string | ||
resp.Unmarshal(&r) | ||
return r, nil | ||
} | ||
return "", err | ||
}, 100*time.Second, 1*time.Second).Should(Equal("bar")) | ||
|
||
m, err := c.Ledger() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(len(m) > 0).To(BeTrue()) | ||
}) | ||
|
||
It("Puts random data", func() { | ||
err := c.Put(testBucket, "foo2", struct{ Foo string }{Foo: "bar"}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Eventually(func() (string, error) { | ||
resp, err := c.GetBucketKey(testBucket, "foo2") | ||
if err == nil { | ||
var r struct{ Foo string } | ||
resp.Unmarshal(&r) | ||
return r.Foo, nil | ||
} | ||
|
||
return "", err | ||
}, 100*time.Second, 1*time.Second).Should(Equal("bar")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.