2019-10-15 14:35:45 +03: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 14:35:45 +03: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-24 00:54:55 +03:00
|
|
|
wkv := NewStore(
|
2019-10-24 01:12:45 +03:00
|
|
|
Token(apiToken),
|
|
|
|
Account(accountID),
|
2019-10-24 00:31:36 +03:00
|
|
|
Namespace(kvID),
|
2020-03-02 21:14:25 +03:00
|
|
|
CacheTTL(60000000000),
|
2019-10-15 14:35:45 +03:00
|
|
|
)
|
|
|
|
|
2019-10-24 00:05:39 +03:00
|
|
|
records, err := wkv.List()
|
2019-10-15 14:35:45 +03:00
|
|
|
if err != nil {
|
2019-10-24 00:05:39 +03:00
|
|
|
t.Fatalf("List: %s\n", err.Error())
|
2019-10-23 19:26:34 +03:00
|
|
|
} else {
|
2019-10-24 00:05:39 +03:00
|
|
|
t.Log("Listed " + strconv.Itoa(len(records)) + " records")
|
2019-10-15 14:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 01:23:14 +03: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 14:35:45 +03: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 19:26:34 +03: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 14:35:45 +03:00
|
|
|
err = wkv.Delete(randomK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Delete: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|