github.com/google/cadvisor@v0.49.1/container/containerd/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 // Handler for containerd containers. 16 package containerd 17 18 import ( 19 "testing" 20 21 "github.com/containerd/typeurl" 22 specs "github.com/opencontainers/runtime-spec/specs-go" 23 "github.com/stretchr/testify/assert" 24 25 "github.com/google/cadvisor/container" 26 "github.com/google/cadvisor/container/containerd/containers" 27 "github.com/google/cadvisor/fs" 28 info "github.com/google/cadvisor/info/v1" 29 ) 30 31 func init() { 32 typeurl.Register(&specs.Spec{}, "types.contianerd.io/opencontainers/runtime-spec", "v1", "Spec") 33 } 34 35 type mockedMachineInfo struct{} 36 37 func (m *mockedMachineInfo) GetMachineInfo() (*info.MachineInfo, error) { 38 return &info.MachineInfo{}, nil 39 } 40 41 func (m *mockedMachineInfo) GetVersionInfo() (*info.VersionInfo, error) { 42 return &info.VersionInfo{}, nil 43 } 44 45 func TestHandler(t *testing.T) { 46 as := assert.New(t) 47 type testCase struct { 48 client ContainerdClient 49 name string 50 machineInfoFactory info.MachineInfoFactory 51 fsInfo fs.FsInfo 52 cgroupSubsystems map[string]string 53 inHostNamespace bool 54 metadataEnvAllowList []string 55 includedMetrics container.MetricSet 56 57 hasErr bool 58 errContains string 59 checkReference *info.ContainerReference 60 checkEnvVars map[string]string 61 } 62 testContainers := make(map[string]*containers.Container) 63 testContainer := &containers.Container{ 64 ID: "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 65 Labels: map[string]string{"io.cri-containerd.kind": "sandbox"}, 66 } 67 spec := &specs.Spec{Root: &specs.Root{Path: "/test/"}, Process: &specs.Process{Env: []string{"TEST_REGION=FRA", "TEST_ZONE=A", "HELLO=WORLD"}}} 68 testContainer.Spec, _ = typeurl.MarshalAny(spec) 69 testContainers["40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"] = testContainer 70 for _, ts := range []testCase{ 71 { 72 mockcontainerdClient(nil, nil), 73 "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 74 nil, 75 nil, 76 nil, 77 false, 78 nil, 79 nil, 80 true, 81 "unable to find container \"40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9\"", 82 nil, 83 nil, 84 }, 85 { 86 mockcontainerdClient(testContainers, nil), 87 "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 88 &mockedMachineInfo{}, 89 nil, 90 nil, 91 false, 92 nil, 93 nil, 94 false, 95 "", 96 &info.ContainerReference{ 97 Id: "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 98 Name: "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 99 Aliases: []string{"40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"}, 100 Namespace: k8sContainerdNamespace, 101 }, 102 map[string]string{}, 103 }, 104 { 105 mockcontainerdClient(testContainers, nil), 106 "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 107 &mockedMachineInfo{}, 108 nil, 109 nil, 110 false, 111 []string{"TEST"}, 112 nil, 113 false, 114 "", 115 &info.ContainerReference{ 116 Id: "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 117 Name: "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", 118 Aliases: []string{"40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"}, 119 Namespace: k8sContainerdNamespace, 120 }, 121 map[string]string{"TEST_REGION": "FRA", "TEST_ZONE": "A"}, 122 }, 123 } { 124 handler, err := newContainerdContainerHandler(ts.client, ts.name, ts.machineInfoFactory, ts.fsInfo, ts.cgroupSubsystems, ts.inHostNamespace, ts.metadataEnvAllowList, ts.includedMetrics) 125 if ts.hasErr { 126 as.NotNil(err) 127 if ts.errContains != "" { 128 as.Contains(err.Error(), ts.errContains) 129 } 130 } 131 if ts.checkReference != nil { 132 cr, err := handler.ContainerReference() 133 as.Nil(err) 134 as.Equal(*ts.checkReference, cr) 135 } 136 if ts.checkEnvVars != nil { 137 sp, err := handler.GetSpec() 138 as.Nil(err) 139 as.Equal(ts.checkEnvVars, sp.Envs) 140 } 141 } 142 }