github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/containercenter/container_center_file_discover_test.go (about)

     1  // Copyright 2021 iLogtail Authors
     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  //go:build !windows
    16  // +build !windows
    17  
    18  package containercenter
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"runtime"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"github.com/alibaba/ilogtail/pkg/helper"
    31  	"github.com/alibaba/ilogtail/pkg/logger"
    32  )
    33  
    34  func init() {
    35  	logger.InitTestLogger(logger.OptionDebugLevel, logger.OptionOpenConsole)
    36  
    37  }
    38  
    39  var staticDockerConfig = `[
    40  {
    41      "ID" : "123abc",
    42      "Name" : "xxx_info",
    43  	"Created": "2022-09-16T12:55:14.930245185+08:00",
    44  	"State": {
    45  		"Pid": 999999999908,
    46  		"Status": "running"
    47  	},
    48      "HostName" : "app-online",
    49      "IP" : "192.168.1.1",
    50      "Image" : "centos:latest",
    51      "LogPath" : "/var/lib/docker/xxxx/0.log",
    52      "Labels" : {
    53  		"a" : "b",
    54  		"io.kubernetes.container.name": "node-exporter",
    55          "io.kubernetes.pod.name": "node-exporter-5q796",
    56          "io.kubernetes.pod.namespace": "arms-prom",
    57          "io.kubernetes.pod.uid": "dd43f0eb-1194-11ea-aa77-3e20fcedfa8f"
    58      },
    59  	"LogType" : "json-file",
    60  	"UpperDir" : "/apsarapangu/disk12/docker/overlay/b6ff04a15c7ec040b3ef0857cb091d1c74de27d4d5daf32884a842055e9fbb6d/upper",
    61      "Env" : {
    62  		"x" : "y",
    63  		"xxx" : "yyyy"
    64      },
    65      "Mounts" : [
    66          {
    67              "Source": "/home/admin/logs",
    68              "Destination" : "/home/admin/logs",
    69              "Driver" : "host"
    70          },
    71          {
    72              "Source": "/var/lib/docker",
    73              "Destination" : "/docker",
    74              "Driver" : "host"
    75          }
    76      ]
    77  }
    78  ]
    79  `
    80  
    81  var staticDockerConfig2 = `[
    82  {
    83      "ID" : "123abc-2",
    84      "Name" : "xxx_info",
    85  	"Created": "2022-09-16T12:56:14.000000000+08:00",
    86  	"State": {
    87  		"Pid": 999999999909,
    88  		"Status": "running"
    89  	},
    90      "Image" : "centos:latest",
    91      "LogPath" : "/var/lib/docker/xxxx/1.log",
    92      "Labels" : {
    93  		"a" : "b",
    94  		"io.kubernetes.container.name": "node-exporter",
    95          "io.kubernetes.pod.name": "node-exporter-5q796",
    96          "io.kubernetes.pod.namespace": "arms-prom",
    97          "io.kubernetes.pod.uid": "dd43f0eb-1194-11ea-aa77-3e20fcedfa8f"
    98      },
    99  	"LogType" : "json-file",
   100  	"UpperDir" : "/apsarapangu/disk12/docker/overlay/b6ff04a15c7ec040b3ef0857cb091d1c74de27d4d5daf32884a842055e9fbb6d/upper",
   101      "Env" : {
   102  		"x" : "y",
   103  		"xxx" : "yyyy"
   104      },
   105      "Mounts" : [
   106          {
   107              "Source": "/home/admin/logs",
   108              "Destination" : "/home/admin/logs",
   109              "Driver" : "host"
   110          },
   111          {
   112              "Source": "/var/lib/docker",
   113              "Destination" : "/docker",
   114              "Driver" : "host"
   115          }
   116      ]
   117  }
   118  ]
   119  `
   120  
   121  var staticECIConfig = `[
   122  	{
   123  			"ID": "111",
   124  			"Name": "container1",
   125  			"Image": "image1",
   126  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container1/0.log",
   127  			"Labels": {
   128  					"io.kubernetes.container.name": "container1",
   129  					"io.kubernetes.pod.name": "podTest",
   130  					"io.kubernetes.pod.namespace": "prod",
   131  					"io.kubernetes.pod.uid": "podUidTest"
   132  			},
   133  			"LogType": "json-file",
   134  			"Env": {
   135  					"KUBERNETES_PORT": "tcp://192.168.0.1:443",
   136  					"KUBERNETES_PORT_443_TCP": "tcp://192.168.0.1:443",
   137  					"KUBERNETES_PORT_443_TCP_ADDR": "192.168.0.1",
   138  					"KUBERNETES_PORT_443_TCP_PORT": "443",
   139  					"KUBERNETES_PORT_443_TCP_PROTO": "tcp",
   140  					"KUBERNETES_SERVICE_HOST": "10.244.197.20",
   141  					"KUBERNETES_SERVICE_PORT": "6443",
   142  					"KUBERNETES_SERVICE_PORT_HTTPS": "443"
   143  			},
   144  			"Mounts": [
   145  				{
   146  					"Source": "/home/admin/logs",
   147  					"Destination" : "/home/admin/logs",
   148  					"Driver" : "host"
   149  				},
   150  				{
   151  					"Source": "/var/lib/docker",
   152  					"Destination" : "/docker",
   153  					"Driver" : "host"
   154  				}
   155  			],
   156  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/111/rootfs",
   157  			"Created": "2023-06-01T10:09:32.336427588+08:00",
   158  			"State": {
   159  					"Pid": 1001,
   160  					"Status": "running"
   161  			}
   162  	},
   163  	{
   164  			"ID": "222",
   165  			"Name": "container2",
   166  			"Image": "image2",
   167  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container2/0.log",
   168  			"Labels": {
   169  					"io.kubernetes.container.name": "container2",
   170  					"io.kubernetes.pod.name": "podTest",
   171  					"io.kubernetes.pod.namespace": "prod",
   172  					"io.kubernetes.pod.uid": "podUidTest"
   173  			},
   174  			"LogType": "json-file",
   175  			"Env": {
   176  					"KUBERNETES_PORT": "tcp://192.168.0.1:443",
   177  					"KUBERNETES_PORT_443_TCP": "tcp://192.168.0.1:443",
   178  					"KUBERNETES_PORT_443_TCP_ADDR": "192.168.0.1",
   179  					"KUBERNETES_PORT_443_TCP_PORT": "443",
   180  					"KUBERNETES_PORT_443_TCP_PROTO": "tcp",
   181  					"KUBERNETES_SERVICE_HOST": "10.244.197.20",
   182  					"KUBERNETES_SERVICE_PORT": "6443",
   183  					"KUBERNETES_SERVICE_PORT_HTTPS": "443"
   184  			},
   185  			"Mounts": [
   186  				{
   187  					"Source": "/home/admin/logs",
   188  					"Destination" : "/home/admin/logs",
   189  					"Driver" : "host"
   190  				},
   191  				{
   192  					"Source": "/var/lib/docker",
   193  					"Destination" : "/docker",
   194  					"Driver" : "host"
   195  				}
   196  			],
   197  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/222/rootfs",
   198  			"Created": "2023-06-01T10:09:33.221371944+08:00",
   199  			"State": {
   200  					"Pid": 1102,
   201  					"Status": "running"
   202  			}
   203  	},
   204  	{
   205  			"ID": "333",
   206  			"Name": "container3",
   207  			"Image": "image3",
   208  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container3/0.log",
   209  			"Labels": {
   210  					"io.kubernetes.container.name": "container3",
   211  					"io.kubernetes.pod.name": "podTest",
   212  					"io.kubernetes.pod.namespace": "prod",
   213  					"io.kubernetes.pod.uid": "podUidTest"
   214  			},
   215  			"LogType": "json-file",
   216  			"Env": {
   217  					"KUBERNETES_PORT": "tcp://192.168.0.1:443",
   218  					"KUBERNETES_PORT_443_TCP": "tcp://192.168.0.1:443",
   219  					"KUBERNETES_PORT_443_TCP_ADDR": "192.168.0.1",
   220  					"KUBERNETES_PORT_443_TCP_PORT": "443",
   221  					"KUBERNETES_PORT_443_TCP_PROTO": "tcp",
   222  					"KUBERNETES_SERVICE_HOST": "10.244.197.20",
   223  					"KUBERNETES_SERVICE_PORT": "6443",
   224  					"KUBERNETES_SERVICE_PORT_HTTPS": "443",
   225  					"POD_NAME": "",
   226  					"POD_NAMESPACE": "",
   227  					"aliyun_log_crd_user_defined_id": "k8s-group-xxx"
   228  			},
   229  			"Mounts": [
   230  				{
   231  					"Source": "/home/admin/logs",
   232  					"Destination" : "/home/admin/logs",
   233  					"Driver" : "host"
   234  				},
   235  				{
   236  					"Source": "/var/lib/docker",
   237  					"Destination" : "/docker",
   238  					"Driver" : "host"
   239  				}
   240  			],
   241  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/333/rootfs",
   242  			"Created": "2023-06-01T10:09:34.22551185+08:00",
   243  			"State": {
   244  					"Pid": 1181,
   245  					"Status": "running"
   246  			}
   247  	},
   248  	{
   249  			"ID": "444",
   250  			"Name": "container4",
   251  			"Image": "image4",
   252  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container4/0.log",
   253  			"Labels": {
   254  					"io.kubernetes.container.name": "container4",
   255  					"io.kubernetes.pod.name": "podTest",
   256  					"io.kubernetes.pod.namespace": "prod",
   257  					"io.kubernetes.pod.uid": "podUidTest"
   258  			},
   259  			"LogType": "json-file",
   260  			"Env": {
   261  					"POD_NAME": "",
   262  					"POD_NAMESPACE": ""
   263  			},
   264  			"Mounts": [
   265  				{
   266  					"Source": "/home/admin/logs",
   267  					"Destination" : "/home/admin/logs",
   268  					"Driver" : "host"
   269  				},
   270  				{
   271  					"Source": "/var/lib/docker",
   272  					"Destination" : "/docker",
   273  					"Driver" : "host"
   274  				}
   275  			],
   276  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/444/rootfs",
   277  			"Created": "2023-06-01T10:10:08.442675217+08:00",
   278  			"State": {
   279  					"Pid": 1965,
   280  					"Status": "running"
   281  			}
   282  	},
   283  	{
   284  			"ID": "555",
   285  			"Name": "container5",
   286  			"Image": "image5",
   287  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container5/0.log",
   288  			"Labels": {
   289  					"io.kubernetes.container.name": "container5",
   290  					"io.kubernetes.pod.name": "podTest",
   291  					"io.kubernetes.pod.namespace": "prod",
   292  					"io.kubernetes.pod.uid": "podUidTest"
   293  			},
   294  			"LogType": "json-file",
   295  			"Env": {
   296  					"POD_NAME": "",
   297  					"POD_NAMESPACE": ""
   298  			},
   299  			"Mounts": [
   300  				{
   301  					"Source": "/home/admin/logs",
   302  					"Destination" : "/home/admin/logs",
   303  					"Driver" : "host"
   304  				},
   305  				{
   306  					"Source": "/var/lib/docker",
   307  					"Destination" : "/docker",
   308  					"Driver" : "host"
   309  				}
   310  			],
   311  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/555/rootfs",
   312  			"Created": "2023-06-01T10:10:08.77858586+08:00",
   313  			"State": {
   314  					"Pid": 2007,
   315  					"Status": "running"
   316  			}
   317  	},
   318  	{
   319  			"ID": "podUidTest",
   320  			"Name": "POD",
   321  			"Image": "pause:latest",
   322  			"LogPath": "/var/log/pods/prod_podTest_podUidTest",
   323  			"Labels": {
   324  					"app": "appTest",
   325  					"attribute": "attTest",
   326  					"deployment": "deploymentTest",
   327  					"environment": "prod",
   328  					"io.kubernetes.pod.name": "podTest",
   329  					"io.kubernetes.pod.namespace": "prod",
   330  					"io.kubernetes.pod.uid": "podUidTest",
   331  					"log": "logTest",
   332  					"pod-template-hash": "xxx"
   333  			},
   334  			"LogType": "json-file",
   335  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/null/rootfs",
   336  			"Created": "0001-01-01T00:00:00Z",
   337  			"State": {
   338  					"Pid": 0,
   339  					"Status": ""
   340  			}
   341  	},
   342  	{
   343  			"ID": "666",
   344  			"Name": "container7",
   345  			"Image": "acr.baijia.com/uqun/container7:2022102001",
   346  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container7/0.log",
   347  			"Labels": {
   348  					"io.kubernetes.container.name": "container7",
   349  					"io.kubernetes.pod.name": "podTest",
   350  					"io.kubernetes.pod.namespace": "prod",
   351  					"io.kubernetes.pod.uid": "podUidTest"
   352  			},
   353  			"LogType": "json-file",
   354  			"Env": {
   355  				"POD_NAME": "",
   356  				"POD_NAMESPACE": ""
   357  			},
   358  			"Mounts": [
   359  				{
   360  					"Source": "/home/admin/logs",
   361  					"Destination" : "/home/admin/logs",
   362  					"Driver" : "host"
   363  				},
   364  				{
   365  					"Source": "/var/lib/docker",
   366  					"Destination" : "/docker",
   367  					"Driver" : "host"
   368  				}
   369  			],
   370  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/666/rootfs",
   371  			"Created": "2023-06-01T10:09:29.382815361+08:00",
   372  			"State": {
   373  					"Pid": 694,
   374  					"Status": "running"
   375  			}
   376  	},
   377  	{
   378  			"ID": "777",
   379  			"Name": "container8",
   380  			"Image": "acr.baijia.com/uqun/container8:1.7.0",
   381  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container8/0.log",
   382  			"Labels": {
   383  					"io.kubernetes.container.name": "container8",
   384  					"io.kubernetes.pod.name": "podTest",
   385  					"io.kubernetes.pod.namespace": "prod",
   386  					"io.kubernetes.pod.uid": "podUidTest"
   387  			},
   388  			"LogType": "json-file",
   389  			"Env": {
   390  				"POD_NAME": "",
   391  				"POD_NAMESPACE": ""
   392  			},
   393  			"Mounts": [
   394  					{
   395  							"Source": "/var/lib/kubelet/pods/podUidTest/volumes/kubernetes.io~empty-dir/container8-volume",
   396  							"Destination": "/ArmsAgent",
   397  							"Driver": ""
   398  					}
   399  			],
   400  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/e916e0ba011ab5bf825ba24ffba7e3e5dedd2073e29222dd6238eb874cc27ddd/rootfs",
   401  			"Created": "2023-06-01T10:09:30.213522454+08:00",
   402  			"State": {
   403  					"Pid": 872,
   404  					"Status": "running"
   405  			}
   406  	}
   407  ]`
   408  
   409  var staticECIConfig2 = `[
   410  	{
   411  			"ID": "111",
   412  			"Name": "container1",
   413  			"Image": "image1",
   414  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container1/0.log",
   415  			"Labels": {
   416  					"io.kubernetes.container.name": "container1",
   417  					"io.kubernetes.pod.name": "podTest",
   418  					"io.kubernetes.pod.namespace": "prod",
   419  					"io.kubernetes.pod.uid": "podUidTest"
   420  			},
   421  			"LogType": "json-file",
   422  			"Env": {
   423  					"KUBERNETES_PORT": "tcp://192.168.0.1:443",
   424  					"KUBERNETES_PORT_443_TCP": "tcp://192.168.0.1:443",
   425  					"KUBERNETES_PORT_443_TCP_ADDR": "192.168.0.1",
   426  					"KUBERNETES_PORT_443_TCP_PORT": "443",
   427  					"KUBERNETES_PORT_443_TCP_PROTO": "tcp",
   428  					"KUBERNETES_SERVICE_HOST": "10.244.197.20",
   429  					"KUBERNETES_SERVICE_PORT": "6443",
   430  					"KUBERNETES_SERVICE_PORT_HTTPS": "443"
   431  			},
   432  			"Mounts": [
   433  				{
   434  					"Source": "/home/admin/logs",
   435  					"Destination" : "/home/admin/logs",
   436  					"Driver" : "host"
   437  				},
   438  				{
   439  					"Source": "/var/lib/docker",
   440  					"Destination" : "/docker",
   441  					"Driver" : "host"
   442  				}
   443  			],
   444  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/111/rootfs",
   445  			"Created": "2023-06-01T10:09:32.336427588+08:00",
   446  			"State": {
   447  					"Pid": 1001,
   448  					"Status": "running"
   449  			}
   450  	},
   451  	{
   452  			"ID": "222",
   453  			"Name": "container2",
   454  			"Image": "image2",
   455  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container2/0.log",
   456  			"Labels": {
   457  					"io.kubernetes.container.name": "container2",
   458  					"io.kubernetes.pod.name": "podTest",
   459  					"io.kubernetes.pod.namespace": "prod",
   460  					"io.kubernetes.pod.uid": "podUidTest"
   461  			},
   462  			"LogType": "json-file",
   463  			"Env": {
   464  					"KUBERNETES_PORT": "tcp://192.168.0.1:443",
   465  					"KUBERNETES_PORT_443_TCP": "tcp://192.168.0.1:443",
   466  					"KUBERNETES_PORT_443_TCP_ADDR": "192.168.0.1",
   467  					"KUBERNETES_PORT_443_TCP_PORT": "443",
   468  					"KUBERNETES_PORT_443_TCP_PROTO": "tcp",
   469  					"KUBERNETES_SERVICE_HOST": "10.244.197.20",
   470  					"KUBERNETES_SERVICE_PORT": "6443",
   471  					"KUBERNETES_SERVICE_PORT_HTTPS": "443"
   472  			},
   473  			"Mounts": [
   474  				{
   475  					"Source": "/home/admin/logs",
   476  					"Destination" : "/home/admin/logs",
   477  					"Driver" : "host"
   478  				},
   479  				{
   480  					"Source": "/var/lib/docker",
   481  					"Destination" : "/docker",
   482  					"Driver" : "host"
   483  				}
   484  			],
   485  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/222/rootfs",
   486  			"Created": "2023-06-01T10:09:33.221371944+08:00",
   487  			"State": {
   488  					"Pid": 1102,
   489  					"Status": "running"
   490  			}
   491  	},
   492  	{
   493  			"ID": "333",
   494  			"Name": "container3",
   495  			"Image": "image3",
   496  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container3/0.log",
   497  			"Labels": {
   498  					"io.kubernetes.container.name": "container3",
   499  					"io.kubernetes.pod.name": "podTest",
   500  					"io.kubernetes.pod.namespace": "prod",
   501  					"io.kubernetes.pod.uid": "podUidTest"
   502  			},
   503  			"LogType": "json-file",
   504  			"Env": {
   505  					"KUBERNETES_PORT": "tcp://192.168.0.1:443",
   506  					"KUBERNETES_PORT_443_TCP": "tcp://192.168.0.1:443",
   507  					"KUBERNETES_PORT_443_TCP_ADDR": "192.168.0.1",
   508  					"KUBERNETES_PORT_443_TCP_PORT": "443",
   509  					"KUBERNETES_PORT_443_TCP_PROTO": "tcp",
   510  					"KUBERNETES_SERVICE_HOST": "10.244.197.20",
   511  					"KUBERNETES_SERVICE_PORT": "6443",
   512  					"KUBERNETES_SERVICE_PORT_HTTPS": "443",
   513  					"POD_NAME": "",
   514  					"POD_NAMESPACE": "",
   515  					"aliyun_log_crd_user_defined_id": "k8s-group-xxx"
   516  			},
   517  			"Mounts": [
   518  				{
   519  					"Source": "/home/admin/logs",
   520  					"Destination" : "/home/admin/logs",
   521  					"Driver" : "host"
   522  				},
   523  				{
   524  					"Source": "/var/lib/docker",
   525  					"Destination" : "/docker",
   526  					"Driver" : "host"
   527  				}
   528  			],
   529  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/333/rootfs",
   530  			"Created": "2023-06-01T10:09:34.22551185+08:00",
   531  			"State": {
   532  					"Pid": 1181,
   533  					"Status": "running"
   534  			}
   535  	},
   536  	{
   537  			"ID": "444",
   538  			"Name": "container4",
   539  			"Image": "image4",
   540  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container4/0.log",
   541  			"Labels": {
   542  					"io.kubernetes.container.name": "container4",
   543  					"io.kubernetes.pod.name": "podTest",
   544  					"io.kubernetes.pod.namespace": "prod",
   545  					"io.kubernetes.pod.uid": "podUidTest"
   546  			},
   547  			"LogType": "json-file",
   548  			"Env": {
   549  					"POD_NAME": "",
   550  					"POD_NAMESPACE": ""
   551  			},
   552  			"Mounts": [
   553  				{
   554  					"Source": "/home/admin/logs",
   555  					"Destination" : "/home/admin/logs",
   556  					"Driver" : "host"
   557  				},
   558  				{
   559  					"Source": "/var/lib/docker",
   560  					"Destination" : "/docker",
   561  					"Driver" : "host"
   562  				}
   563  			],
   564  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/444/rootfs",
   565  			"Created": "2023-06-01T10:10:08.442675217+08:00",
   566  			"State": {
   567  					"Pid": 1965,
   568  					"Status": "running"
   569  			}
   570  	},
   571  	{
   572  			"ID": "555",
   573  			"Name": "container5",
   574  			"Image": "image5",
   575  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container5/0.log",
   576  			"Labels": {
   577  					"io.kubernetes.container.name": "container5",
   578  					"io.kubernetes.pod.name": "podTest",
   579  					"io.kubernetes.pod.namespace": "prod",
   580  					"io.kubernetes.pod.uid": "podUidTest"
   581  			},
   582  			"LogType": "json-file",
   583  			"Env": {
   584  					"POD_NAME": "",
   585  					"POD_NAMESPACE": ""
   586  			},
   587  			"Mounts": [
   588  				{
   589  					"Source": "/home/admin/logs",
   590  					"Destination" : "/home/admin/logs",
   591  					"Driver" : "host"
   592  				},
   593  				{
   594  					"Source": "/var/lib/docker",
   595  					"Destination" : "/docker",
   596  					"Driver" : "host"
   597  				}
   598  			],
   599  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/555/rootfs",
   600  			"Created": "2023-06-01T10:10:08.77858586+08:00",
   601  			"State": {
   602  					"Pid": 2007,
   603  					"Status": "running"
   604  			}
   605  	},
   606  	{
   607  			"ID": "podUidTest",
   608  			"Name": "POD",
   609  			"Image": "pause:latest",
   610  			"LogPath": "/var/log/pods/prod_podTest_podUidTest",
   611  			"Labels": {
   612  					"app": "appTest",
   613  					"attribute": "attTest",
   614  					"deployment": "deploymentTest",
   615  					"environment": "prod",
   616  					"io.kubernetes.pod.name": "podTest",
   617  					"io.kubernetes.pod.namespace": "prod",
   618  					"io.kubernetes.pod.uid": "podUidTest",
   619  					"log": "logTest",
   620  					"pod-template-hash": "xxx",
   621  					"custom": "xxx"
   622  			},
   623  			"LogType": "json-file",
   624  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/null/rootfs",
   625  			"Created": "0001-01-01T00:00:00Z",
   626  			"State": {
   627  					"Pid": 0,
   628  					"Status": ""
   629  			}
   630  	},
   631  	{
   632  			"ID": "666",
   633  			"Name": "container7",
   634  			"Image": "acr.baijia.com/uqun/container7:2022102001",
   635  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container7/0.log",
   636  			"Labels": {
   637  					"io.kubernetes.container.name": "container7",
   638  					"io.kubernetes.pod.name": "podTest",
   639  					"io.kubernetes.pod.namespace": "prod",
   640  					"io.kubernetes.pod.uid": "podUidTest"
   641  			},
   642  			"LogType": "json-file",
   643  			"Env": {
   644  				"POD_NAME": "",
   645  				"POD_NAMESPACE": ""
   646  			},
   647  			"Mounts": [
   648  				{
   649  					"Source": "/home/admin/logs",
   650  					"Destination" : "/home/admin/logs",
   651  					"Driver" : "host"
   652  				},
   653  				{
   654  					"Source": "/var/lib/docker",
   655  					"Destination" : "/docker",
   656  					"Driver" : "host"
   657  				}
   658  			],
   659  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/666/rootfs",
   660  			"Created": "2023-06-01T10:09:29.382815361+08:00",
   661  			"State": {
   662  					"Pid": 694,
   663  					"Status": "running"
   664  			}
   665  	},
   666  	{
   667  			"ID": "777",
   668  			"Name": "container8",
   669  			"Image": "acr.baijia.com/uqun/container8:1.7.0",
   670  			"LogPath": "/var/log/pods/prod_podTest_podUidTest/container8/0.log",
   671  			"Labels": {
   672  					"io.kubernetes.container.name": "container8",
   673  					"io.kubernetes.pod.name": "podTest",
   674  					"io.kubernetes.pod.namespace": "prod",
   675  					"io.kubernetes.pod.uid": "podUidTest"
   676  			},
   677  			"LogType": "json-file",
   678  			"Env": {
   679  				"POD_NAME": "",
   680  				"POD_NAMESPACE": ""
   681  			},
   682  			"Mounts": [
   683  					{
   684  							"Source": "/var/lib/kubelet/pods/podUidTest/volumes/kubernetes.io~empty-dir/container8-volume",
   685  							"Destination": "/ArmsAgent",
   686  							"Driver": ""
   687  					}
   688  			],
   689  			"UpperDir": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/e916e0ba011ab5bf825ba24ffba7e3e5dedd2073e29222dd6238eb874cc27ddd/rootfs",
   690  			"Created": "2023-06-01T10:09:30.213522454+08:00",
   691  			"State": {
   692  					"Pid": 872,
   693  					"Status": "running"
   694  			}
   695  	}
   696  ]`
   697  
   698  func TestTryReadStaticContainerInfo(t *testing.T) {
   699  	defer os.Remove("./static_container.json")
   700  	defer os.Unsetenv(staticContainerInfoPathEnvKey)
   701  	os.WriteFile("./static_container.json", []byte(staticDockerConfig), os.ModePerm)
   702  	os.Setenv(staticContainerInfoPathEnvKey, "./static_container.json")
   703  	containerInfo, removedIDs, changed, err := tryReadStaticContainerInfo()
   704  	require.Nil(t, err)
   705  	require.Len(t, containerInfo, 1)
   706  	require.Empty(t, removedIDs)
   707  	require.True(t, changed)
   708  	info := containerInfo[0]
   709  	require.Equal(t, "123abc", info.ID)
   710  	require.Equal(t, "xxx_info", info.Name)
   711  	require.Equal(t, "2022-09-16T12:55:14.930245185+08:00", info.Created)
   712  	require.Equal(t, "/var/lib/docker/xxxx/0.log", info.LogPath)
   713  	require.Equal(t, "b", info.Config.Labels["a"])
   714  	require.Equal(t, "centos:latest", info.Config.Image)
   715  	require.Contains(t, info.Config.Env, "x=y")
   716  	require.Equal(t, "app-online", info.Config.Hostname)
   717  	require.Equal(t, "json-file", info.HostConfig.LogConfig.Type)
   718  	require.Equal(t, "/apsarapangu/disk12/docker/overlay/b6ff04a15c7ec040b3ef0857cb091d1c74de27d4d5daf32884a842055e9fbb6d/upper", info.GraphDriver.Data["UpperDir"])
   719  	require.Equal(t, "192.168.1.1", info.NetworkSettings.IPAddress)
   720  	if runtime.GOOS == "linux" {
   721  		require.Equal(t, ContainerStatusExited, info.State.Status)
   722  	} else {
   723  		require.Equal(t, ContainerStatusRunning, info.State.Status)
   724  	}
   725  	require.Equal(t, 999999999908, info.State.Pid)
   726  
   727  	os.WriteFile("./static_container.json", []byte(staticDockerConfig2), os.ModePerm)
   728  
   729  	containerInfo, removedIDs, changed, err = tryReadStaticContainerInfo()
   730  	require.Nil(t, err)
   731  	require.Len(t, containerInfo, 1)
   732  	require.Len(t, removedIDs, 1)
   733  	require.True(t, changed)
   734  
   735  	require.Equal(t, "123abc", removedIDs[0])
   736  	info = containerInfo[0]
   737  	require.Equal(t, "123abc-2", info.ID)
   738  	require.Equal(t, "xxx_info", info.Name)
   739  	require.Equal(t, "2022-09-16T12:56:14+08:00", info.Created)
   740  	require.Equal(t, "/var/lib/docker/xxxx/1.log", info.LogPath)
   741  	require.Equal(t, 999999999909, info.State.Pid)
   742  
   743  	containerInfo, removedIDs, changed, err = tryReadStaticContainerInfo()
   744  	require.Nil(t, err)
   745  	require.Len(t, containerInfo, 1)
   746  	require.Len(t, removedIDs, 0)
   747  	require.False(t, changed)
   748  }
   749  
   750  func TestLoadStaticContainerConfig(t *testing.T) {
   751  	resetContainerCenter()
   752  	defer os.Remove("./static_container.json")
   753  	defer os.Unsetenv(staticContainerInfoPathEnvKey)
   754  	os.WriteFile("./static_container.json", []byte(staticDockerConfig), os.ModePerm)
   755  	os.Setenv(staticContainerInfoPathEnvKey, "./static_container.json")
   756  	instance := getContainerCenterInstance()
   757  	allInfo := instance.containerMap
   758  	require.Equal(t, 1, len(allInfo))
   759  	for id, info := range allInfo {
   760  		require.Equal(t, "123abc", id)
   761  		require.Equal(t, "192.168.1.1", info.ContainerIP)
   762  	}
   763  }
   764  
   765  func TestLoadStaticContainerConfigTwice(t *testing.T) {
   766  	resetContainerCenter()
   767  	defer os.Remove("./static_container.json")
   768  	defer os.Unsetenv(staticContainerInfoPathEnvKey)
   769  	os.WriteFile("./static_container.json", []byte(staticECIConfig), os.ModePerm)
   770  	os.Setenv(staticContainerInfoPathEnvKey, "./static_container.json")
   771  	instance := getContainerCenterInstance()
   772  	allInfo := instance.containerMap
   773  	require.Equal(t, 8, len(allInfo))
   774  	for _, info := range allInfo {
   775  		require.Equal(t, 6, len(info.K8SInfo.Labels))
   776  	}
   777  
   778  	os.Remove("./static_container.json")
   779  	os.WriteFile("./static_container.json", []byte(staticECIConfig2), os.ModePerm)
   780  
   781  	time.Sleep(defaultContextTimeout)
   782  
   783  	allInfo = instance.containerMap
   784  	require.Equal(t, 8, len(allInfo))
   785  	for _, info := range allInfo {
   786  		require.Equal(t, 7, len(info.K8SInfo.Labels))
   787  	}
   788  }
   789  
   790  func checkSameDevInode(t *testing.T, oldname, newname string) {
   791  	logNameStat, _ := os.Stat(oldname)
   792  	logStat0, _ := os.Stat(newname)
   793  	logNameOSStat := helper.GetOSState(logNameStat)
   794  	logOSStat0 := helper.GetOSState(logStat0)
   795  	if logOSStat0.Device != logNameOSStat.Device ||
   796  		logOSStat0.Inode != logNameOSStat.Inode ||
   797  		logOSStat0.ModifyTime != logNameOSStat.ModifyTime ||
   798  		logOSStat0.Size != logNameOSStat.Size {
   799  		t.Errorf("now same os stat %s %s \n %+v %+v \n", oldname, newname, logOSStat0, logNameOSStat)
   800  	}
   801  }
   802  
   803  func TestScanContainerdFilesAndReLink(t *testing.T) {
   804  	dir, _ := os.Getwd()
   805  	defer func() {
   806  		_ = os.Remove(filepath.Join(dir, "0.log"))
   807  		_ = os.Remove(filepath.Join(dir, "99.log"))
   808  		_ = os.Remove(filepath.Join(dir, "100.log"))
   809  		_ = os.Remove(filepath.Join(dir, "101.log"))
   810  	}()
   811  	fmt.Printf("working dir : %s \n", dir)
   812  
   813  	logName := filepath.Join(dir, "stdout.log")
   814  	go scanContainerdFilesAndReLink(logName)
   815  
   816  	time.Sleep(time.Second)
   817  	os.WriteFile(filepath.Join(dir, "0.log"), []byte("abc"), os.ModePerm)
   818  	checkSameDevInode(t, logName, filepath.Join(dir, "0.log"))
   819  
   820  	os.WriteFile(filepath.Join(dir, "99.log"), []byte("abcdef"), os.ModePerm)
   821  	time.Sleep(time.Second * 2)
   822  	checkSameDevInode(t, logName, filepath.Join(dir, "99.log"))
   823  
   824  	os.WriteFile(filepath.Join(dir, "100.log"), []byte("abcde"), os.ModePerm)
   825  	time.Sleep(time.Second * 2)
   826  	checkSameDevInode(t, logName, filepath.Join(dir, "100.log"))
   827  
   828  	os.WriteFile(filepath.Join(dir, "101.log"), []byte("abcdefg"), os.ModePerm)
   829  	time.Sleep(time.Second * 2)
   830  	checkSameDevInode(t, logName, filepath.Join(dir, "101.log"))
   831  }