2019-10-15 12:35:45 +01:00
|
|
|
package cloudflare
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-10-15 12:35:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCloudflare(t *testing.T) {
|
|
|
|
apiToken, accountID := os.Getenv("CF_API_TOKEN"), os.Getenv("CF_ACCOUNT_ID")
|
|
|
|
kvID := os.Getenv("KV_NAMESPACE_ID")
|
|
|
|
if len(apiToken) == 0 || len(accountID) == 0 || len(kvID) == 0 {
|
|
|
|
t.Skip("No Cloudflare API keys available, skipping test")
|
|
|
|
}
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randomK := strconv.Itoa(rand.Int())
|
|
|
|
randomV := strconv.Itoa(rand.Int())
|
|
|
|
|
2019-10-23 22:54:55 +01:00
|
|
|
wkv := NewStore(
|
2019-10-23 23:12:45 +01:00
|
|
|
Token(apiToken),
|
|
|
|
Account(accountID),
|
2019-10-23 22:31:36 +01:00
|
|
|
Namespace(kvID),
|
2020-03-02 18:14:25 +00:00
|
|
|
CacheTTL(60000000000),
|
2019-10-15 12:35:45 +01:00
|
|
|
)
|
|
|
|
|
2019-10-23 22:05:39 +01:00
|
|
|
records, err := wkv.List()
|
2019-10-15 12:35:45 +01:00
|
|
|
if err != nil {
|
2019-10-23 22:05:39 +01:00
|
|
|
t.Fatalf("List: %s\n", err.Error())
|
2019-10-23 17:26:34 +01:00
|
|
|
} else {
|
2019-10-23 22:05:39 +01:00
|
|
|
t.Log("Listed " + strconv.Itoa(len(records)) + " records")
|
2019-10-15 12:35:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
err = wkv.Write(&store.Record{
|
|
|
|
Key: randomK,
|
|
|
|
Value: []byte(randomV),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Write: %s", err.Error())
|
|
|
|
}
|
|
|
|
err = wkv.Write(&store.Record{
|
|
|
|
Key: "expirationtest",
|
|
|
|
Value: []byte("This message will self destruct"),
|
|
|
|
Expiry: 75 * time.Second,
|
|
|
|
})
|
2019-10-15 12:35:45 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Write: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// This might be needed for cloudflare eventual consistency
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
|
|
|
|
r, err := wkv.Read(randomK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
if len(r) != 1 {
|
|
|
|
t.Errorf("Expected to read 1 key, got %d keys\n", len(r))
|
|
|
|
}
|
|
|
|
if string(r[0].Value) != randomV {
|
|
|
|
t.Errorf("Read: expected %s, got %s\n", randomK, string(r[0].Value))
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:26:34 +01:00
|
|
|
r, err = wkv.Read("expirationtest")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: expirationtest should still exist")
|
|
|
|
}
|
|
|
|
if r[0].Expiry == 0 {
|
|
|
|
t.Error("Expected r to have an expiry")
|
|
|
|
} else {
|
|
|
|
t.Log(r[0].Expiry)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
r, err = wkv.Read("expirationtest")
|
|
|
|
if err == nil && len(r) != 0 {
|
|
|
|
t.Error("Read: Managed to read expirationtest, but it should have expired")
|
|
|
|
t.Log(err, r[0].Key, string(r[0].Value), r[0].Expiry, len(r))
|
|
|
|
}
|
|
|
|
|
2019-10-15 12:35:45 +01:00
|
|
|
err = wkv.Delete(randomK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Delete: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|