github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmhostutil/container_linux_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package apmhostutil
    19  
    20  import (
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"github.com/waldiirawan/apm-agent-go/v2/model"
    29  )
    30  
    31  func TestCgroupContainerInfoDocker(t *testing.T) {
    32  	container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(`
    33  12:devices:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    34  11:hugetlb:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    35  10:memory:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    36  9:freezer:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    37  8:perf_event:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    38  7:blkio:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    39  6:pids:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    40  5:rdma:/
    41  4:cpuset:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    42  3:net_cls,net_prio:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    43  2:cpu,cpuacct:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    44  1:name=systemd:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76
    45  0::/system.slice/docker.service`[1:]))
    46  
    47  	assert.NoError(t, err)
    48  	assert.Nil(t, kubernetes)
    49  	assert.Equal(t, &model.Container{ID: "051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76"}, container)
    50  }
    51  
    52  func TestCgroupContainerInfoECS(t *testing.T) {
    53  	container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(`
    54  3:cpuacct:/ecs/eb9d3d0c-8936-42d7-80d8-f82b2f1a629e/7e9139716d9e5d762d22f9f877b87d1be8b1449ac912c025a984750c5dbff157
    55  `[1:]))
    56  
    57  	assert.NoError(t, err)
    58  	assert.Nil(t, kubernetes)
    59  	assert.Equal(t, &model.Container{ID: "7e9139716d9e5d762d22f9f877b87d1be8b1449ac912c025a984750c5dbff157"}, container)
    60  }
    61  
    62  func TestCgroupContainerInfoCloudFoundryGarden(t *testing.T) {
    63  	container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(`
    64  1:name=systemd:/system.slice/garden.service/garden/70eb4ce5-a065-4401-6990-88ed
    65  `[1:]))
    66  
    67  	assert.NoError(t, err)
    68  	assert.Nil(t, kubernetes)
    69  	assert.Equal(t, &model.Container{ID: "70eb4ce5-a065-4401-6990-88ed"}, container)
    70  }
    71  
    72  func TestUbuntuCgroup(t *testing.T) {
    73  	container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(`
    74  1:name=systemd:/user.slice/user-1000.slice/user@1000.service/apps.slice/apps-org.gnome.Terminal.slice/vte-spawn-75bc72bd-6642-4cf5-b62c-0674e11bfc84.scope
    75  `[1:]))
    76  
    77  	assert.NoError(t, err)
    78  	assert.Nil(t, kubernetes)
    79  	assert.Nil(t, container)
    80  }
    81  
    82  func TestCgroupContainerInfoNonContainer(t *testing.T) {
    83  	container, _, err := readCgroupContainerInfo(strings.NewReader(`
    84  12:devices:/user.slice
    85  11:hugetlb:/
    86  10:memory:/user.slice
    87  9:freezer:/
    88  8:perf_event:/
    89  7:blkio:/user.slice
    90  6:pids:/user.slice/user-1000.slice/session-2.scope
    91  5:rdma:/
    92  4:cpuset:/
    93  3:net_cls,net_prio:/
    94  2:cpu,cpuacct:/user.slice
    95  1:name=systemd:/user.slice/user-1000.slice/session-2.scope
    96  0::/user.slice/user-1000.slice/session-2.scope`[1:]))
    97  
    98  	assert.NoError(t, err)
    99  	assert.Nil(t, kubernetes)
   100  	assert.Nil(t, container)
   101  }
   102  
   103  func TestCgroupContainerInfoDockerSystemd(t *testing.T) {
   104  	container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(`
   105  1:name=systemd:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope
   106  `[1:]))
   107  
   108  	assert.NoError(t, err)
   109  	assert.Nil(t, kubernetes)
   110  	assert.Equal(t, &model.Container{ID: "cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411"}, container)
   111  }
   112  
   113  func TestCgroupContainerInfoECSFargate(t *testing.T) {
   114  	container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(`
   115  1:name=systemd:/ecs/03752a671e744971a862edcee6195646/03752a671e744971a862edcee6195646-4015103728
   116  `[1:]))
   117  
   118  	assert.NoError(t, err)
   119  	assert.Nil(t, kubernetes)
   120  	assert.Equal(t, &model.Container{ID: "03752a671e744971a862edcee6195646-4015103728"}, container)
   121  }
   122  
   123  func TestCgroupContainerInfoNonHex(t *testing.T) {
   124  	// Imaginary future format. We use the last part of the path,
   125  	// trimming legacy prefix/suffix, and check the expected
   126  	// length and runes used.
   127  	container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(`
   128  1:name=systemd:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76/not_hex
   129  `[1:]))
   130  
   131  	assert.NoError(t, err)
   132  	assert.Nil(t, kubernetes)
   133  	assert.Nil(t, container)
   134  }
   135  
   136  func TestCgroupContainerInfoKubernetes(t *testing.T) {
   137  	type testcase struct {
   138  		input            string
   139  		containerID      string
   140  		kubernetesPodUID string
   141  	}
   142  
   143  	testscases := []testcase{{
   144  		input:            "1:name=systemd:/kubepods/besteffort/pode9b90526-f47d-11e8-b2a5-080027b9f4fb/15aa6e53-b09a-40c7-8558-c6c31e36c88a",
   145  		containerID:      "15aa6e53-b09a-40c7-8558-c6c31e36c88a",
   146  		kubernetesPodUID: "e9b90526-f47d-11e8-b2a5-080027b9f4fb",
   147  	}, {
   148  		input:            "1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod90d81341_92de_11e7_8cf2_507b9d4141fa.slice/crio-2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63.scope",
   149  		containerID:      "2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63",
   150  		kubernetesPodUID: "90d81341-92de-11e7-8cf2-507b9d4141fa",
   151  	}, {
   152  		input:            "1:name=systemd:/system.slice/containerd.service/kubepods-burstable-podff49d0be_16b7_4a49_bb9e_8ec1f1f4e27f.slice:cri-containerd:0f99ad5f45163ed14ab8eaf92ed34bb4a631d007f8755a7d79be614bcb0df0ef",
   153  		containerID:      "0f99ad5f45163ed14ab8eaf92ed34bb4a631d007f8755a7d79be614bcb0df0ef",
   154  		kubernetesPodUID: "ff49d0be-16b7-4a49-bb9e-8ec1f1f4e27f",
   155  	}, {
   156  		input:            "9:freezer:/kubepods.slice/kubepods-pod22949dce_fd8b_11ea_8ede_98f2b32c645c.slice/docker-b15a5bdedd2e7645c3be271364324321b908314e4c77857bbfd32a041148c07f.scope",
   157  		containerID:      "b15a5bdedd2e7645c3be271364324321b908314e4c77857bbfd32a041148c07f",
   158  		kubernetesPodUID: "22949dce-fd8b-11ea-8ede-98f2b32c645c",
   159  	}, {
   160  		input:            "12:pids:/kubepods/kubepods/besteffort/pod0e886e9a-3879-45f9-b44d-86ef9df03224/244a65edefdffe31685c42317c9054e71dc1193048cf9459e2a4dd35cbc1dba4",
   161  		containerID:      "244a65edefdffe31685c42317c9054e71dc1193048cf9459e2a4dd35cbc1dba4",
   162  		kubernetesPodUID: "0e886e9a-3879-45f9-b44d-86ef9df03224",
   163  	}, {
   164  		input:            "10:cpuset:/kubepods/pod5eadac96-ab58-11ea-b82b-0242ac110009/7fe41c8a2d1da09420117894f11dd91f6c3a44dfeb7d125dc594bd53468861df",
   165  		containerID:      "7fe41c8a2d1da09420117894f11dd91f6c3a44dfeb7d125dc594bd53468861df",
   166  		kubernetesPodUID: "5eadac96-ab58-11ea-b82b-0242ac110009",
   167  	}}
   168  
   169  	hostname, err := os.Hostname()
   170  	require.NoError(t, err)
   171  
   172  	for _, testcase := range testscases {
   173  		container, kubernetes, err := readCgroupContainerInfo(strings.NewReader(testcase.input))
   174  		assert.NoError(t, err)
   175  		assert.Equal(t, &model.Container{ID: testcase.containerID}, container)
   176  		assert.Equal(t, &model.Kubernetes{
   177  			Pod: &model.KubernetesPod{
   178  				UID:  testcase.kubernetesPodUID,
   179  				Name: hostname,
   180  			},
   181  		}, kubernetes)
   182  	}
   183  }