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

     1  // Copyright 2022 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 helper
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/alibaba/ilogtail/pkg"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestNewmanagerMeta(t *testing.T) {
    28  	genFunc := func(prefix string, start, end int) string {
    29  		var res []string
    30  		for i := start; i <= end; i++ {
    31  			res = append(res, fmt.Sprintf("%s_%d", prefix, i))
    32  		}
    33  		return strings.Join(res, ",")
    34  	}
    35  	meta := NewmanagerMeta("telegraf")
    36  	for i := 0; i < 8; i++ {
    37  		prjNum := i / 4
    38  		logNum := i / 2
    39  		cfgNum := i
    40  		meta.Add(fmt.Sprintf("p_%d", prjNum), fmt.Sprintf("l_%d", logNum), fmt.Sprintf("c_%d", cfgNum))
    41  		assert.Equal(t, meta.ctx.Value(pkg.LogTailMeta).(*pkg.LogtailContextMeta).GetAlarm().Project, genFunc("p", 0, prjNum))
    42  		assert.Equal(t, meta.ctx.Value(pkg.LogTailMeta).(*pkg.LogtailContextMeta).GetAlarm().Logstore, genFunc("l", 0, logNum))
    43  	}
    44  
    45  	for i := 7; i >= 0; i-- {
    46  		prjNum := i / 4
    47  		logNum := i / 2
    48  		cfgNum := i
    49  		meta.Delete(fmt.Sprintf("p_%d", prjNum), fmt.Sprintf("l_%d", logNum), fmt.Sprintf("c_%d", cfgNum))
    50  		cfgNum--
    51  		if i%2 == 0 {
    52  			logNum--
    53  		}
    54  		if i%4 == 0 {
    55  			prjNum--
    56  		}
    57  		if prjNum == -1 && logNum == -1 && cfgNum == -1 {
    58  			assert.Equal(t, meta.ctx.Value(pkg.LogTailMeta).(*pkg.LogtailContextMeta).GetAlarm().Project, "")
    59  			assert.Equal(t, meta.ctx.Value(pkg.LogTailMeta).(*pkg.LogtailContextMeta).GetAlarm().Logstore, "")
    60  		} else {
    61  			assert.Equal(t, meta.ctx.Value(pkg.LogTailMeta).(*pkg.LogtailContextMeta).GetAlarm().Project, genFunc("p", 0, prjNum))
    62  			assert.Equal(t, meta.ctx.Value(pkg.LogTailMeta).(*pkg.LogtailContextMeta).GetAlarm().Logstore, genFunc("l", 0, logNum))
    63  
    64  		}
    65  	}
    66  }