tested on multi-machine multi-container scenario

This commit is contained in:
Nan Deng 2014-07-02 14:24:45 -07:00
parent e4c439ce25
commit 795b451288
2 changed files with 47 additions and 1 deletions

View File

@ -16,6 +16,7 @@ package influxdb
import (
"fmt"
"math/rand"
"testing"
"time"
@ -25,7 +26,8 @@ import (
)
func runStorageTest(f func(storage.StorageDriver, *testing.T), t *testing.T) {
machineName := "mymachine"
// randomly generate a machine name to mimic multi-machine senario.
machineName := fmt.Sprintf("machine-%v-%v", time.Now(), rand.Int63())
tablename := "t"
database := "cadvisor"
username := "root"
@ -63,29 +65,39 @@ func runStorageTest(f func(storage.StorageDriver, *testing.T), t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer driver.Close()
// generate another container's data on same machine.
test.StorageDriverFillRandomStatsFunc("containerOnSameMachine", 100)(driver, t)
f(driver, t)
}
func TestSampleCpuUsage(t *testing.T) {
// we generates more than one container's data.
runStorageTest(test.StorageDriverFillRandomStatsFunc("otherContainer", 100), t)
runStorageTest(test.StorageDriverTestSampleCpuUsage, t)
}
func TestRetrievePartialRecentStats(t *testing.T) {
runStorageTest(test.StorageDriverFillRandomStatsFunc("otherContainer", 100), t)
runStorageTest(test.StorageDriverTestRetrievePartialRecentStats, t)
}
func TestSamplesWithoutSample(t *testing.T) {
runStorageTest(test.StorageDriverFillRandomStatsFunc("otherContainer", 100), t)
runStorageTest(test.StorageDriverTestSamplesWithoutSample, t)
}
func TestRetrieveAllRecentStats(t *testing.T) {
runStorageTest(test.StorageDriverFillRandomStatsFunc("otherContainer", 100), t)
runStorageTest(test.StorageDriverTestRetrieveAllRecentStats, t)
}
func TestNoRecentStats(t *testing.T) {
runStorageTest(test.StorageDriverFillRandomStatsFunc("otherContainer", 100), t)
runStorageTest(test.StorageDriverTestNoRecentStats, t)
}
func TestNoSamples(t *testing.T) {
runStorageTest(test.StorageDriverFillRandomStatsFunc("otherContainer", 100), t)
runStorageTest(test.StorageDriverTestNoSamples, t)
}

View File

@ -83,6 +83,40 @@ func samplesInTrace(samples []*info.ContainerStatsSample, cpuTrace, memTrace []u
}
}
// This function returns a function that will generate random stats and write
// them into the storage. The returned function will not close the driver of
// the call, which could be served as a building block to do other works
func StorageDriverFillRandomStatsFunc(
containerName string,
N int,
) func(driver storage.StorageDriver, t *testing.T) {
return func(driver storage.StorageDriver, t *testing.T) {
cpuTrace := make([]uint64, 0, N)
memTrace := make([]uint64, 0, N)
// We need N+1 observations to get N samples
for i := 0; i < N+1; i++ {
cpuTrace = append(cpuTrace, uint64(rand.Intn(1000)))
memTrace = append(memTrace, uint64(rand.Intn(1000)))
}
samplePeriod := 1 * time.Second
ref := info.ContainerReference{
Name: containerName,
}
trace := buildTrace(cpuTrace, memTrace, samplePeriod)
for _, stats := range trace {
err := driver.AddStats(ref, stats)
if err != nil {
t.Fatalf("unable to add stats: %v", err)
}
}
}
}
func StorageDriverTestSampleCpuUsage(driver storage.StorageDriver, t *testing.T) {
defer driver.Close()
N := 100