mirror of
https://github.com/status-im/consul.git
synced 2025-01-15 08:14:54 +00:00
consul: Adding tests for endpoint method
This commit is contained in:
parent
3e9dc6d8b6
commit
fc5110405c
@ -14,8 +14,8 @@ type KVS struct {
|
|||||||
|
|
||||||
// Apply is used to apply a KVS request to the data store. This should
|
// Apply is used to apply a KVS request to the data store. This should
|
||||||
// only be used for operations that modify the data
|
// only be used for operations that modify the data
|
||||||
func (c *Catalog) Apply(args *structs.KVSRequest, reply *bool) error {
|
func (k *KVS) Apply(args *structs.KVSRequest, reply *bool) error {
|
||||||
if done, err := c.srv.forward("KVS.Apply", args.Datacenter, args, reply); done {
|
if done, err := k.srv.forward("KVS.Apply", args.Datacenter, args, reply); done {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer metrics.MeasureSince([]string{"consul", "kvs", "apply"}, time.Now())
|
defer metrics.MeasureSince([]string{"consul", "kvs", "apply"}, time.Now())
|
||||||
@ -26,9 +26,9 @@ func (c *Catalog) Apply(args *structs.KVSRequest, reply *bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply the update
|
// Apply the update
|
||||||
resp, err := c.srv.raftApply(structs.KVSRequestType, args)
|
resp, err := k.srv.raftApply(structs.KVSRequestType, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.srv.logger.Printf("[ERR] consul.kvs: Apply failed: %v", err)
|
k.srv.logger.Printf("[ERR] consul.kvs: Apply failed: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if respErr, ok := resp.(error); ok {
|
if respErr, ok := resp.(error); ok {
|
||||||
|
65
consul/kvs_endpoint_test.go
Normal file
65
consul/kvs_endpoint_test.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package consul
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hashicorp/consul/consul/structs"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestKVS_Apply(t *testing.T) {
|
||||||
|
dir1, s1 := testServer(t)
|
||||||
|
defer os.RemoveAll(dir1)
|
||||||
|
defer s1.Shutdown()
|
||||||
|
client := rpcClient(t, s1)
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
// Wait for leader
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
arg := structs.KVSRequest{
|
||||||
|
Datacenter: "dc1",
|
||||||
|
Op: structs.KVSSet,
|
||||||
|
DirEnt: structs.DirEntry{
|
||||||
|
Key: "test",
|
||||||
|
Flags: 42,
|
||||||
|
Value: []byte("test"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
var out bool
|
||||||
|
if err := client.Call("KVS.Apply", &arg, &out); err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
state := s1.fsm.State()
|
||||||
|
_, d, err := state.KVSGet("test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
if d == nil {
|
||||||
|
t.Fatalf("should not be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do a check and set
|
||||||
|
arg.Op = structs.KVSCAS
|
||||||
|
arg.DirEnt.ModifyIndex = d.ModifyIndex
|
||||||
|
arg.DirEnt.Flags = 43
|
||||||
|
if err := client.Call("KVS.Apply", &arg, &out); err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check this was applied
|
||||||
|
if out != true {
|
||||||
|
t.Fatalf("bad: %v", out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
_, d, err = state.KVSGet("test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
if d.Flags != 43 {
|
||||||
|
t.Fatalf("bad: %v", d)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user