github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sources/kubelet/kubelet_client_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 kubelet
    16  
    17  import (
    18  	"net/http/httptest"
    19  	"testing"
    20  	"time"
    21  
    22  	cadvisor_api "github.com/google/cadvisor/info/v1"
    23  	jsoniter "github.com/json-iterator/go"
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  	util "k8s.io/client-go/util/testing"
    27  )
    28  
    29  func checkContainer(t *testing.T, expected cadvisor_api.ContainerInfo, actual cadvisor_api.ContainerInfo) {
    30  	assert.True(t, actual.Spec.Eq(&expected.Spec))
    31  	for i, stat := range actual.Stats {
    32  		assert.True(t, stat.Eq(expected.Stats[i]))
    33  	}
    34  }
    35  
    36  func TestAllContainers(t *testing.T) {
    37  	rootContainer := cadvisor_api.ContainerInfo{
    38  		ContainerReference: cadvisor_api.ContainerReference{
    39  			Name: "/",
    40  		},
    41  		Spec: cadvisor_api.ContainerSpec{
    42  			CreationTime: time.Now(),
    43  			HasCpu:       true,
    44  			HasMemory:    true,
    45  		},
    46  		Stats: []*cadvisor_api.ContainerStats{
    47  			{
    48  				Timestamp: time.Now(),
    49  			},
    50  		},
    51  	}
    52  
    53  	subcontainer := cadvisor_api.ContainerInfo{
    54  		ContainerReference: cadvisor_api.ContainerReference{
    55  			Name: "/sub",
    56  		},
    57  		Spec: cadvisor_api.ContainerSpec{
    58  			CreationTime: time.Now(),
    59  			HasCpu:       true,
    60  			HasMemory:    true,
    61  		},
    62  		Stats: []*cadvisor_api.ContainerStats{
    63  			{
    64  				Timestamp: time.Now(),
    65  			},
    66  		},
    67  	}
    68  	response := map[string]cadvisor_api.ContainerInfo{
    69  		rootContainer.Name: {
    70  			ContainerReference: cadvisor_api.ContainerReference{
    71  				Name: rootContainer.Name,
    72  			},
    73  			Spec: rootContainer.Spec,
    74  			Stats: []*cadvisor_api.ContainerStats{
    75  				rootContainer.Stats[0],
    76  			},
    77  		},
    78  		subcontainer.Name: {
    79  			ContainerReference: cadvisor_api.ContainerReference{
    80  				Name: subcontainer.Name,
    81  			},
    82  			Spec: subcontainer.Spec,
    83  			Stats: []*cadvisor_api.ContainerStats{
    84  				subcontainer.Stats[0],
    85  			},
    86  		},
    87  	}
    88  	data, err := jsoniter.ConfigFastest.Marshal(&response)
    89  	require.NoError(t, err)
    90  	handler := util.FakeHandler{
    91  		StatusCode:   200,
    92  		RequestBody:  "",
    93  		ResponseBody: string(data),
    94  		T:            t,
    95  	}
    96  	server := httptest.NewServer(&handler)
    97  	defer server.Close()
    98  	kubeletClient := KubeletClient{}
    99  	containers, err := kubeletClient.getAllContainers(server.URL, time.Now(), time.Now().Add(time.Minute))
   100  	require.NoError(t, err)
   101  	require.Len(t, containers, 2)
   102  	checkContainer(t, rootContainer, containers[0])
   103  	checkContainer(t, subcontainer, containers[1])
   104  }