github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/sysadvisor/test/metacache_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"io/ioutil"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  
    27  	"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/options"
    28  	"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache"
    29  	"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types"
    30  	"github.com/kubewharf/katalyst-core/pkg/config"
    31  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric"
    32  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    33  	metricspool "github.com/kubewharf/katalyst-core/pkg/metrics/metrics-pool"
    34  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    35  )
    36  
    37  func generateMachineConfig(t *testing.T) *config.Configuration {
    38  	testConfiguration, err := options.NewOptions().Config()
    39  	require.NoError(t, err)
    40  	require.NotNil(t, testConfiguration)
    41  
    42  	tmpStateDir, err := ioutil.TempDir("", "sys-advisor-test")
    43  	require.NoError(t, err)
    44  	testConfiguration.GenericSysAdvisorConfiguration.StateFileDirectory = tmpStateDir
    45  
    46  	return testConfiguration
    47  }
    48  
    49  func newTestMetaCache(t *testing.T) *metacache.MetaCacheImp {
    50  	metaCache, err := metacache.NewMetaCacheImp(generateMachineConfig(t), metricspool.DummyMetricsEmitterPool{}, metric.NewFakeMetricsFetcher(metrics.DummyMetrics{}))
    51  	require.NoError(t, err)
    52  	require.NotNil(t, metaCache)
    53  	return metaCache
    54  }
    55  
    56  func TestContainer(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	metaCache := newTestMetaCache(t)
    60  
    61  	err := metaCache.SetContainerInfo("pod-0", "container-0", &types.ContainerInfo{})
    62  	assert.Nil(t, err)
    63  	err = metaCache.SetContainerInfo("pod-1", "container-1", &types.ContainerInfo{})
    64  	assert.Nil(t, err)
    65  
    66  	_, ok := metaCache.GetContainerInfo("pod-0", "container-0")
    67  	assert.True(t, ok)
    68  	_, ok = metaCache.GetContainerInfo("pod-1", "container-1")
    69  	assert.True(t, ok)
    70  
    71  	err = metaCache.RemovePod("pod-0")
    72  	assert.Nil(t, err)
    73  	err = metaCache.DeleteContainer("pod-1", "container-1")
    74  	assert.Nil(t, err)
    75  
    76  	_, ok = metaCache.GetContainerInfo("pod-0", "container-0")
    77  	assert.False(t, ok)
    78  	_, ok = metaCache.GetContainerInfo("pod-1", "container-1")
    79  	assert.False(t, ok)
    80  }
    81  
    82  func TestPool(t *testing.T) {
    83  	t.Parallel()
    84  
    85  	general.Infof("ready to start %v", "test pool")
    86  
    87  	metaCache := newTestMetaCache(t)
    88  
    89  	log := general.LoggerWithPrefix("advisor", general.LoggingPKGFull)
    90  	log.Infof("started %v", "test pool")
    91  
    92  	err := metaCache.SetPoolInfo("pool-0", &types.PoolInfo{})
    93  	assert.Nil(t, err)
    94  	err = metaCache.SetPoolInfo("pool-1", &types.PoolInfo{})
    95  	assert.Nil(t, err)
    96  	err = metaCache.SetPoolInfo("pool-2", &types.PoolInfo{})
    97  	assert.Nil(t, err)
    98  
    99  	_, ok := metaCache.GetPoolInfo("pool-0")
   100  	assert.True(t, ok)
   101  	_, ok = metaCache.GetPoolInfo("pool-1")
   102  	assert.True(t, ok)
   103  	_, ok = metaCache.GetPoolInfo("pool-2")
   104  	assert.True(t, ok)
   105  
   106  	err = metaCache.DeletePool("pool-0")
   107  	assert.Nil(t, err)
   108  	err = metaCache.GCPoolEntries(sets.NewString("pool-2"))
   109  	assert.Nil(t, err)
   110  
   111  	_, ok = metaCache.GetPoolInfo("pool-0")
   112  	assert.False(t, ok)
   113  	_, ok = metaCache.GetPoolInfo("pool-1")
   114  	assert.False(t, ok)
   115  	_, ok = metaCache.GetPoolInfo("pool-2")
   116  	assert.True(t, ok)
   117  }