From 795b4512882d7083e629b4e918faa399de45d0f9 Mon Sep 17 00:00:00 2001 From: Nan Deng Date: Wed, 2 Jul 2014 14:24:45 -0700 Subject: [PATCH] tested on multi-machine multi-container scenario --- storage/influxdb/influxdb_test.go | 14 ++++++++++++- storage/test/storagetests.go | 34 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/storage/influxdb/influxdb_test.go b/storage/influxdb/influxdb_test.go index ead4cecb..803e1842 100644 --- a/storage/influxdb/influxdb_test.go +++ b/storage/influxdb/influxdb_test.go @@ -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) } diff --git a/storage/test/storagetests.go b/storage/test/storagetests.go index 9c4ec0ae..cf4226a1 100644 --- a/storage/test/storagetests.go +++ b/storage/test/storagetests.go @@ -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