Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #856 from rbradford/memory-datastore
Browse files Browse the repository at this point in the history
Use a memory datastore for datastore tests
  • Loading branch information
Tim Pepper authored Dec 2, 2016
2 parents 53b4b8a + 41b92dc commit 96ab9bb
Show file tree
Hide file tree
Showing 5 changed files with 607 additions and 310 deletions.
11 changes: 9 additions & 2 deletions ciao-controller/internal/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (

// Config contains configuration information for the datastore.
type Config struct {
DBBackend persistentStore
PersistentURI string
TransientURI string
InitTablesPath string
Expand Down Expand Up @@ -82,6 +83,7 @@ type attachment struct {
}

type persistentStore interface {
init(config Config) error
disconnect()

// interfaces related to logging
Expand Down Expand Up @@ -208,8 +210,13 @@ func (ds *Datastore) initExternalIPs() {
// files if this is the first time the database has been
// created. The datastore caches are also filled.
func (ds *Datastore) Init(config Config) error {
// init persistentStore first...
ps, err := getPersistentStore(config)
ps := config.DBBackend

if ps == nil {
ps = &sqliteDB{}
}

err := ps.init(config)
if err != nil {
return err
}
Expand Down
108 changes: 11 additions & 97 deletions ciao-controller/internal/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ func TestDeleteInstanceResources(t *testing.T) {
resourcesBefore[r.Rname] = r.Usage
}

time.Sleep(1 * time.Second)

err = ds.DeleteInstance(instance.ID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -347,8 +345,6 @@ func TestDeleteInstanceNetwork(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)

err = ds.DeleteInstance(instance.ID)
if err != nil {
t.Fatal(err)
Expand All @@ -373,8 +369,6 @@ func TestDeleteInstanceNetwork(t *testing.T) {
t.Fatal("IP Address not released from cache")
}

time.Sleep(1 * time.Second)

// clear tenant from cache
ds.tenantsLock.Lock()
delete(ds.tenants, tenant.ID)
Expand Down Expand Up @@ -457,7 +451,6 @@ func TestGetAllInstancesFromTenant(t *testing.T) {
// if we don't get 10 eventually, the test will timeout and fail
instances, err := ds.GetAllInstancesFromTenant(tenant.ID)
for len(instances) < 10 {
time.Sleep(1 * time.Second)
instances, err = ds.GetAllInstancesFromTenant(tenant.ID)
}

Expand All @@ -480,7 +473,6 @@ func TestGetAllInstancesByNode(t *testing.T) {
retry := 5
for len(newInstances) < len(instances) && retry > 0 {
retry--
time.Sleep(1 * time.Second)
newInstances, err = ds.GetAllInstancesByNode(stat.NodeUUID)
if err != nil {
t.Fatal(err)
Expand All @@ -501,7 +493,6 @@ func TestGetInstance(t *testing.T) {
}

for instance == nil {
time.Sleep(1 * time.Second)
instance, err = ds.GetInstance(instances[0].ID)
if err != nil && err != sql.ErrNoRows {
t.Fatal(err)
Expand Down Expand Up @@ -573,8 +564,6 @@ func TestHandleStats(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)

// check instance stats recorded
for i := range stats {
id := stats[i].InstanceUUID
Expand Down Expand Up @@ -649,8 +638,6 @@ func TestGetInstanceLastStats(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)

serverStats := ds.GetInstanceLastStats(stat.NodeUUID)

if len(serverStats.Servers) != len(instances) {
Expand Down Expand Up @@ -714,8 +701,6 @@ func TestGetNodeLastStats(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)

computeNodes := ds.GetNodeLastStats()

// how many compute Nodes should be here? If we want to
Expand All @@ -725,7 +710,7 @@ func TestGetNodeLastStats(t *testing.T) {
}
}

func TestGetBatchFrameStatistics(t *testing.T) {
func createTestFrameTraces(label string) []payloads.FrameTrace {
var nodes []payloads.SSNTPNode
for i := 0; i < 3; i++ {
node := payloads.SSNTPNode{
Expand All @@ -740,7 +725,7 @@ func TestGetBatchFrameStatistics(t *testing.T) {
var frames []payloads.FrameTrace
for i := 0; i < 3; i++ {
stat := payloads.FrameTrace{
Label: "batch_frame_test",
Label: label,
Type: "type",
Operand: "operand",
StartTimestamp: time.Now().Format(time.RFC3339Nano),
Expand All @@ -749,9 +734,12 @@ func TestGetBatchFrameStatistics(t *testing.T) {
}
frames = append(frames, stat)
}
return frames
}

func TestGetBatchFrameStatistics(t *testing.T) {
trace := payloads.Trace{
Frames: frames,
Frames: createTestFrameTraces("batch_frame_test"),
}

err := ds.HandleTraceReport(trace)
Expand All @@ -766,32 +754,8 @@ func TestGetBatchFrameStatistics(t *testing.T) {
}

func TestGetBatchFrameSummary(t *testing.T) {
var nodes []payloads.SSNTPNode
for i := 0; i < 3; i++ {
node := payloads.SSNTPNode{
SSNTPUUID: uuid.Generate().String(),
SSNTPRole: "test",
TxTimestamp: time.Now().Format(time.RFC3339Nano),
RxTimestamp: time.Now().Format(time.RFC3339Nano),
}
nodes = append(nodes, node)
}

var frames []payloads.FrameTrace
for i := 0; i < 3; i++ {
stat := payloads.FrameTrace{
Label: "batch_summary_test",
Type: "type",
Operand: "operand",
StartTimestamp: time.Now().Format(time.RFC3339Nano),
EndTimestamp: time.Now().Format(time.RFC3339Nano),
Nodes: nodes,
}
frames = append(frames, stat)
}

trace := payloads.Trace{
Frames: frames,
Frames: createTestFrameTraces("batch_summary_test"),
}

err := ds.HandleTraceReport(trace)
Expand Down Expand Up @@ -839,25 +803,7 @@ func TestClearLog(t *testing.T) {
}

func TestAddFrameStat(t *testing.T) {
var nodes []payloads.SSNTPNode
for i := 0; i < 3; i++ {
node := payloads.SSNTPNode{
SSNTPUUID: uuid.Generate().String(),
SSNTPRole: "test",
TxTimestamp: time.Now().Format(time.RFC3339Nano),
RxTimestamp: time.Now().Format(time.RFC3339Nano),
}
nodes = append(nodes, node)
}

stat := payloads.FrameTrace{
Label: "test",
Type: "type",
Operand: "operand",
StartTimestamp: time.Now().Format(time.RFC3339Nano),
EndTimestamp: time.Now().Format(time.RFC3339Nano),
Nodes: nodes,
}
stat := createTestFrameTraces("test")[0]
err := ds.db.addFrameStat(stat)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -951,8 +897,6 @@ func TestAllocateTenantIP(t *testing.T) {
t.Fatal("IP Address not claimed in cache")
}

time.Sleep(5 * time.Second)

// clear out cache
ds.tenantsLock.Lock()
delete(ds.tenants, tenant.ID)
Expand Down Expand Up @@ -1110,32 +1054,8 @@ func TestAddCNCIIP(t *testing.T) {
}

func TestHandleTraceReport(t *testing.T) {
var nodes []payloads.SSNTPNode
for i := 0; i < 3; i++ {
node := payloads.SSNTPNode{
SSNTPUUID: uuid.Generate().String(),
SSNTPRole: "test",
TxTimestamp: time.Now().Format(time.RFC3339Nano),
RxTimestamp: time.Now().Format(time.RFC3339Nano),
}
nodes = append(nodes, node)
}

var frames []payloads.FrameTrace
for i := 0; i < 3; i++ {
stat := payloads.FrameTrace{
Label: "test",
Type: "type",
Operand: "operand",
StartTimestamp: time.Now().Format(time.RFC3339Nano),
EndTimestamp: time.Now().Format(time.RFC3339Nano),
Nodes: nodes,
}
frames = append(frames, stat)
}

trace := payloads.Trace{
Frames: frames,
Frames: createTestFrameTraces("test"),
}

err := ds.HandleTraceReport(trace)
Expand Down Expand Up @@ -1192,8 +1112,6 @@ func TestReleaseTenantIP(t *testing.T) {
t.Fatal("IP Address not marked Used")
}

time.Sleep(1 * time.Second)

err = ds.ReleaseTenantIP(tenant.ID, ip.String())
if err != nil {
t.Fatal(err)
Expand All @@ -1210,8 +1128,6 @@ func TestReleaseTenantIP(t *testing.T) {
t.Fatal("IP Address not released from cache")
}

time.Sleep(1 * time.Second)

// clear tenant from cache
ds.tenantsLock.Lock()
delete(ds.tenants, tenant.ID)
Expand Down Expand Up @@ -1307,7 +1223,6 @@ func TestRestartFailure(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)
reason := payloads.RestartNoInstance

err = ds.RestartFailure(instance.ID, reason)
Expand All @@ -1332,7 +1247,6 @@ func TestStopFailure(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)
reason := payloads.StopNoInstance

err = ds.StopFailure(instance.ID, reason)
Expand All @@ -1357,8 +1271,6 @@ func TestStartFailureFullCloud(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)

tenantBefore, err := ds.GetTenant(tenant.ID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -2825,6 +2737,7 @@ func TestMain(m *testing.M) {
ds = new(Datastore)

dsConfig := Config{
DBBackend: &MemoryDB{},
PersistentURI: "file:memdb1?mode=memory&cache=shared",
TransientURI: "file:memdb2?mode=memory&cache=shared",
InitTablesPath: *tablesInitPath,
Expand All @@ -2833,6 +2746,7 @@ func TestMain(m *testing.M) {

err := ds.Init(dsConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}

Expand Down
Loading

0 comments on commit 96ab9bb

Please sign in to comment.