github.com/google/cadvisor@v0.49.1/container/crio/handler_test.go (about)

     1  // Copyright 2017 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 crio
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/google/cadvisor/container"
    24  	"github.com/google/cadvisor/fs"
    25  	info "github.com/google/cadvisor/info/v1"
    26  )
    27  
    28  func TestHandler(t *testing.T) {
    29  	as := assert.New(t)
    30  	type testCase struct {
    31  		client               CrioClient
    32  		name                 string
    33  		machineInfoFactory   info.MachineInfoFactory
    34  		fsInfo               fs.FsInfo
    35  		storageDriver        storageDriver
    36  		storageDir           string
    37  		cgroupSubsystems     map[string]string
    38  		inHostNamespace      bool
    39  		metadataEnvAllowList []string
    40  		includedMetrics      container.MetricSet
    41  
    42  		hasErr         bool
    43  		errContains    string
    44  		checkReference *info.ContainerReference
    45  	}
    46  	for _, ts := range []testCase{
    47  		{
    48  			mockCrioClient(Info{}, nil, fmt.Errorf("no client returned")),
    49  			"/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/crio-81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
    50  			nil,
    51  			nil,
    52  			"",
    53  			"",
    54  			nil,
    55  			false,
    56  			nil,
    57  			nil,
    58  
    59  			true,
    60  			"no client returned",
    61  			nil,
    62  		},
    63  		{
    64  			mockCrioClient(Info{}, nil, nil),
    65  			"/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/crio-81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
    66  			nil,
    67  			nil,
    68  			"",
    69  			"",
    70  			nil,
    71  			false,
    72  			nil,
    73  			nil,
    74  
    75  			true,
    76  			"no container with id 81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
    77  			nil,
    78  		},
    79  		{
    80  			mockCrioClient(
    81  				Info{},
    82  				map[string]*ContainerInfo{"81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f": {Name: "test", Labels: map[string]string{"io.kubernetes.container.name": "POD"}}},
    83  				nil,
    84  			),
    85  			"/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/crio-81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
    86  			nil,
    87  			nil,
    88  			"",
    89  			"",
    90  			nil,
    91  			false,
    92  			nil,
    93  			nil,
    94  
    95  			false,
    96  			"",
    97  			&info.ContainerReference{
    98  				Id:        "81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
    99  				Name:      "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/crio-81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
   100  				Aliases:   []string{"test", "81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f"},
   101  				Namespace: CrioNamespace,
   102  			},
   103  		},
   104  	} {
   105  		handler, err := newCrioContainerHandler(ts.client, ts.name, ts.machineInfoFactory, ts.fsInfo, ts.storageDriver, ts.storageDir, ts.cgroupSubsystems, ts.inHostNamespace, ts.metadataEnvAllowList, ts.includedMetrics)
   106  		if ts.hasErr {
   107  			as.NotNil(err)
   108  			if ts.errContains != "" {
   109  				as.Contains(err.Error(), ts.errContains)
   110  			}
   111  		}
   112  		if ts.checkReference != nil {
   113  			cr, err := handler.ContainerReference()
   114  			as.Nil(err)
   115  			as.Equal(*ts.checkReference, cr)
   116  		}
   117  	}
   118  }