github.com/google/cadvisor@v0.49.1/info/v1/container_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 v1
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  func TestStatsStartTime(t *testing.T) {
    23  	N := 10
    24  	stats := make([]*ContainerStats, 0, N)
    25  	ct := time.Now()
    26  	for i := 0; i < N; i++ {
    27  		s := &ContainerStats{
    28  			Timestamp: ct.Add(time.Duration(i) * time.Second),
    29  		}
    30  		stats = append(stats, s)
    31  	}
    32  	cinfo := &ContainerInfo{
    33  		ContainerReference: ContainerReference{
    34  			Name: "/some/container",
    35  		},
    36  		Stats: stats,
    37  	}
    38  	ref := ct.Add(time.Duration(N-1) * time.Second)
    39  	end := cinfo.StatsEndTime()
    40  
    41  	if !ref.Equal(end) {
    42  		t.Errorf("end time is %v; should be %v", end, ref)
    43  	}
    44  }
    45  
    46  func TestStatsEndTime(t *testing.T) {
    47  	N := 10
    48  	stats := make([]*ContainerStats, 0, N)
    49  	ct := time.Now()
    50  	for i := 0; i < N; i++ {
    51  		s := &ContainerStats{
    52  			Timestamp: ct.Add(time.Duration(i) * time.Second),
    53  		}
    54  		stats = append(stats, s)
    55  	}
    56  	cinfo := &ContainerInfo{
    57  		ContainerReference: ContainerReference{
    58  			Name: "/some/container",
    59  		},
    60  		Stats: stats,
    61  	}
    62  	ref := ct
    63  	start := cinfo.StatsStartTime()
    64  
    65  	if !ref.Equal(start) {
    66  		t.Errorf("start time is %v; should be %v", start, ref)
    67  	}
    68  }