github.com/google/cadvisor@v0.49.1/cache/memory/memory_test.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package memory
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	info "github.com/google/cadvisor/info/v1"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  const containerName = "/container"
    28  
    29  var (
    30  	cInfo = info.ContainerInfo{
    31  		ContainerReference: info.ContainerReference{Name: containerName},
    32  	}
    33  	zero time.Time
    34  )
    35  
    36  // Make stats with the specified identifier.
    37  func makeStat(i int) *info.ContainerStats {
    38  	return &info.ContainerStats{
    39  		Timestamp: zero.Add(time.Duration(i) * time.Second),
    40  		Cpu: info.CpuStats{
    41  			LoadAverage: int32(i),
    42  		},
    43  	}
    44  }
    45  
    46  func getRecentStats(t *testing.T, memoryCache *InMemoryCache, numStats int) []*info.ContainerStats {
    47  	stats, err := memoryCache.RecentStats(containerName, zero, zero, numStats)
    48  	require.Nil(t, err)
    49  	return stats
    50  }
    51  
    52  func TestAddStats(t *testing.T) {
    53  	memoryCache := New(60*time.Second, nil)
    54  
    55  	assert := assert.New(t)
    56  	assert.Nil(memoryCache.AddStats(&cInfo, makeStat(0)))
    57  	assert.Nil(memoryCache.AddStats(&cInfo, makeStat(1)))
    58  	assert.Nil(memoryCache.AddStats(&cInfo, makeStat(2)))
    59  	assert.Nil(memoryCache.AddStats(&cInfo, makeStat(0)))
    60  	cInfo2 := info.ContainerInfo{
    61  		ContainerReference: info.ContainerReference{
    62  			Name: "/container2",
    63  		},
    64  	}
    65  	assert.Nil(memoryCache.AddStats(&cInfo2, makeStat(0)))
    66  	assert.Nil(memoryCache.AddStats(&cInfo2, makeStat(1)))
    67  }
    68  
    69  func TestRecentStatsNoRecentStats(t *testing.T) {
    70  	memoryCache := makeWithStats(t, 0)
    71  
    72  	_, err := memoryCache.RecentStats(containerName, zero, zero, 60)
    73  	assert.NotNil(t, err)
    74  }
    75  
    76  // Make an instance of InMemoryCache with n stats.
    77  func makeWithStats(t *testing.T, n int) *InMemoryCache {
    78  	memoryCache := New(60*time.Second, nil)
    79  
    80  	for i := 0; i < n; i++ {
    81  		assert.NoError(t, memoryCache.AddStats(&cInfo, makeStat(i)))
    82  	}
    83  	return memoryCache
    84  }
    85  
    86  func TestRecentStatsGetZeroStats(t *testing.T) {
    87  	memoryCache := makeWithStats(t, 10)
    88  
    89  	assert.Len(t, getRecentStats(t, memoryCache, 0), 0)
    90  }
    91  
    92  func TestRecentStatsGetSomeStats(t *testing.T) {
    93  	memoryCache := makeWithStats(t, 10)
    94  
    95  	assert.Len(t, getRecentStats(t, memoryCache, 5), 5)
    96  }
    97  
    98  func TestRecentStatsGetAllStats(t *testing.T) {
    99  	memoryCache := makeWithStats(t, 10)
   100  
   101  	assert.Len(t, getRecentStats(t, memoryCache, -1), 10)
   102  }