...

Source file src/k8s.io/kubernetes/pkg/kubelet/pluginmanager/metrics/metrics_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/pluginmanager/metrics

     1  /*
     2  Copyright 2019 The Kubernetes 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 metrics
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
    24  )
    25  
    26  func TestMetricCollection(t *testing.T) {
    27  	dsw := cache.NewDesiredStateOfWorld()
    28  	asw := cache.NewActualStateOfWorld()
    29  	fakePlugin := cache.PluginInfo{
    30  		SocketPath: fmt.Sprintf("fake/path/plugin.sock"),
    31  	}
    32  	// Add one plugin to DesiredStateOfWorld
    33  	err := dsw.AddOrUpdatePlugin(fakePlugin.SocketPath)
    34  	if err != nil {
    35  		t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
    36  	}
    37  
    38  	// Add one plugin to ActualStateOfWorld
    39  	err = asw.AddPlugin(fakePlugin)
    40  	if err != nil {
    41  		t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
    42  	}
    43  
    44  	metricCollector := &totalPluginsCollector{asw: asw, dsw: dsw}
    45  
    46  	// Check if getPluginCount returns correct data
    47  	count := metricCollector.getPluginCount()
    48  	if len(count) != 2 {
    49  		t.Errorf("getPluginCount failed. Expected <2> states, got <%d>", len(count))
    50  	}
    51  
    52  	dswCount, ok := count["desired_state_of_world"]
    53  	if !ok {
    54  		t.Errorf("getPluginCount failed. Expected <desired_state_of_world>, got nothing")
    55  	}
    56  
    57  	fakePluginCount := dswCount["fake/path/plugin.sock"]
    58  	if fakePluginCount != 1 {
    59  		t.Errorf("getPluginCount failed. Expected <1> fake/path/plugin.sock in DesiredStateOfWorld, got <%d>",
    60  			fakePluginCount)
    61  	}
    62  
    63  	aswCount, ok := count["actual_state_of_world"]
    64  	if !ok {
    65  		t.Errorf("getPluginCount failed. Expected <actual_state_of_world>, got nothing")
    66  	}
    67  
    68  	fakePluginCount = aswCount["fake/path/plugin.sock"]
    69  	if fakePluginCount != 1 {
    70  		t.Errorf("getPluginCount failed. Expected <1> fake/path/plugin.sock in ActualStateOfWorld, got <%d>",
    71  			fakePluginCount)
    72  	}
    73  }
    74  

View as plain text