github.com/google/cadvisor@v0.49.1/container/containerd/factory_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 containerd
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/containerd/typeurl"
    21  	"github.com/google/cadvisor/container/containerd/containers"
    22  	specs "github.com/opencontainers/runtime-spec/specs-go"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestIsContainerName(t *testing.T) {
    27  	tests := []struct {
    28  		name     string
    29  		expected bool
    30  	}{
    31  		{
    32  			name:     "/system.slice/run-containerd-io.containerd.runtime.v1.linux-k8s.io-14ae50f1d3ada102aec3ab00168fdafb2dc0986d79ca9e8d5b75581fa89e9fea-rootfs.mount",
    33  			expected: false,
    34  		},
    35  		{
    36  			name:     "/kubepods/besteffort/podd76e26fba3bf2bfd215eb29011d55250/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
    37  			expected: true,
    38  		},
    39  	}
    40  	for _, test := range tests {
    41  		if actual := isContainerName(test.name); actual != test.expected {
    42  			t.Errorf("%s: expected: %v, actual: %v", test.name, test.expected, actual)
    43  		}
    44  	}
    45  }
    46  
    47  func TestCanHandleAndAccept(t *testing.T) {
    48  	as := assert.New(t)
    49  	testContainers := make(map[string]*containers.Container)
    50  	testContainer := &containers.Container{
    51  		ID:     "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
    52  		Labels: map[string]string{"io.cri-containerd.kind": "sandbox"},
    53  	}
    54  	spec := &specs.Spec{Root: &specs.Root{Path: "/test/"}, Process: &specs.Process{}}
    55  	testContainer.Spec, _ = typeurl.MarshalAny(spec)
    56  	testContainers["40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"] = testContainer
    57  
    58  	f := &containerdFactory{
    59  		client:             mockcontainerdClient(testContainers, nil),
    60  		cgroupSubsystems:   nil,
    61  		fsInfo:             nil,
    62  		machineInfoFactory: nil,
    63  		includedMetrics:    nil,
    64  	}
    65  	for k, v := range map[string]bool{
    66  		"/kubepods/besteffort/podd76e26fba3bf2bfd215eb29011d55250/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9":                        true,
    67  		"/system.slice/run-containerd-io.containerd.runtime.v1.linux-k8s.io-14ae50f1d3ada102aec3ab00168fdafb2dc0986d79ca9e8d5b75581fa89e9fea-rootfs.mount": false,
    68  	} {
    69  		b1, b2, err := f.CanHandleAndAccept(k)
    70  		as.Nil(err)
    71  		as.Equal(b1, v)
    72  		as.Equal(b2, v)
    73  	}
    74  }