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

     1  // Copyright 2023 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  package containercenter
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/docker/docker/api/types"
    22  	"github.com/docker/docker/api/types/container"
    23  	"github.com/stretchr/testify/suite"
    24  
    25  	"github.com/alibaba/ilogtail/pkg/flags"
    26  	"github.com/alibaba/ilogtail/pkg/logger"
    27  )
    28  
    29  func TestContainerExport(t *testing.T) {
    30  	suite.Run(t, new(containerExportTestSuite))
    31  }
    32  
    33  func (s *containerExportTestSuite) TestCastContainerDetail() {
    34  
    35  	envSet := make(map[string]struct{})
    36  	labelSet := make(map[string]struct{})
    37  	k8sLabelSet := make(map[string]struct{})
    38  
    39  	envSet["testEnv1"] = struct{}{}
    40  	labelSet["testLebel1"] = struct{}{}
    41  
    42  	info := mockDockerInfoDetail("testConfig", []string{"testEnv1=stdout"})
    43  	cInfo := CastContainerDetail(info, envSet, labelSet, k8sLabelSet)
    44  
    45  	s.Equal(1, len(cInfo.Env))
    46  	s.Equal(0, len(cInfo.ContainerLabels))
    47  }
    48  
    49  func (s *containerExportTestSuite) TestGetAllContainerIncludeEnvAndLabelToRecord() {
    50  	envSet := make(map[string]struct{})
    51  	labelSet := make(map[string]struct{})
    52  	k8sLabelSet := make(map[string]struct{})
    53  
    54  	diffEnvSet := make(map[string]struct{})
    55  	diffLabelSet := make(map[string]struct{})
    56  	diffK8sLabelSet := make(map[string]struct{})
    57  
    58  	envSet["testEnv1"] = struct{}{}
    59  	labelSet["testLabel1"] = struct{}{}
    60  	diffEnvSet["testEnv1"] = struct{}{}
    61  
    62  	info := mockDockerInfoDetail("testConfig", []string{"testEnv1=stdout"})
    63  	cMap := GetContainerMap()
    64  	cMap["test"] = info
    65  
    66  	result := GetAllContainerIncludeEnvAndLabelToRecord(envSet, labelSet, k8sLabelSet, diffEnvSet, diffLabelSet, diffK8sLabelSet)
    67  	s.Equal(1, len(result))
    68  }
    69  
    70  func (s *containerExportTestSuite) TestGetAllContainerToRecord() {
    71  	envSet := make(map[string]struct{})
    72  	labelSet := make(map[string]struct{})
    73  	k8sLabelSet := make(map[string]struct{})
    74  
    75  	envSet["testEnv1"] = struct{}{}
    76  	labelSet["testLabel1"] = struct{}{}
    77  
    78  	info := mockDockerInfoDetail("testConfig", []string{"testEnv1=stdout"})
    79  	cMap := GetContainerMap()
    80  	cMap["test"] = info
    81  
    82  	result := GetAllContainerToRecord(envSet, labelSet, k8sLabelSet, make(map[string]struct{}))
    83  	s.Equal(1, len(result))
    84  }
    85  
    86  type containerExportTestSuite struct {
    87  	suite.Suite
    88  }
    89  
    90  func (s *containerExportTestSuite) BeforeTest(suiteName, testName string) {
    91  	logger.Infof(context.Background(), "========== %s %s test start ========================", suiteName, testName)
    92  }
    93  
    94  func (s *containerExportTestSuite) AfterTest(suiteName, testName string) {
    95  	logger.Infof(context.Background(), "========== %s %s test end =======================", suiteName, testName)
    96  
    97  }
    98  
    99  func mockDockerInfoDetail(containerName string, envList []string) *DockerInfoDetail {
   100  	dockerInfo := types.ContainerJSON{
   101  		ContainerJSONBase: &types.ContainerJSONBase{
   102  			Name: containerName,
   103  			ID:   "test",
   104  		},
   105  	}
   106  	dockerInfo.Config = &container.Config{}
   107  	dockerInfo.Config.Env = envList
   108  	return CreateContainerInfoDetail(dockerInfo, *flags.LogConfigPrefix, false)
   109  }