...

Source file src/k8s.io/kubernetes/pkg/kubelet/kuberuntime/instrumented_services_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/kuberuntime

     1  /*
     2  Copyright 2016 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 kuberuntime
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  	"net/http"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	compbasemetrics "k8s.io/component-base/metrics"
    29  	runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
    30  	"k8s.io/kubernetes/pkg/kubelet/metrics"
    31  )
    32  
    33  func TestRecordOperation(t *testing.T) {
    34  	// Use local registry
    35  	var registry = compbasemetrics.NewKubeRegistry()
    36  	var gather compbasemetrics.Gatherer = registry
    37  
    38  	registry.MustRegister(metrics.RuntimeOperations)
    39  	registry.MustRegister(metrics.RuntimeOperationsDuration)
    40  	registry.MustRegister(metrics.RuntimeOperationsErrors)
    41  
    42  	registry.Reset()
    43  
    44  	l, err := net.Listen("tcp", "127.0.0.1:0")
    45  	assert.NoError(t, err)
    46  	defer l.Close()
    47  
    48  	prometheusURL := "http://" + l.Addr().String() + "/metrics"
    49  	mux := http.NewServeMux()
    50  	handler := compbasemetrics.HandlerFor(gather, compbasemetrics.HandlerOpts{})
    51  	mux.Handle("/metrics", handler)
    52  	server := &http.Server{
    53  		Addr:    l.Addr().String(),
    54  		Handler: mux,
    55  	}
    56  	go func() {
    57  		server.Serve(l)
    58  	}()
    59  
    60  	recordOperation("create_container", time.Now())
    61  	runtimeOperationsCounterExpected := "kubelet_runtime_operations_total{operation_type=\"create_container\"} 1"
    62  	runtimeOperationsDurationExpected := "kubelet_runtime_operations_duration_seconds_count{operation_type=\"create_container\"} 1"
    63  
    64  	assert.HTTPBodyContains(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    65  		mux.ServeHTTP(w, r)
    66  	}), "GET", prometheusURL, nil, runtimeOperationsCounterExpected)
    67  
    68  	assert.HTTPBodyContains(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    69  		mux.ServeHTTP(w, r)
    70  	}), "GET", prometheusURL, nil, runtimeOperationsDurationExpected)
    71  }
    72  
    73  func TestInstrumentedVersion(t *testing.T) {
    74  	ctx := context.Background()
    75  	fakeRuntime, _, _, _ := createTestRuntimeManager()
    76  	irs := newInstrumentedRuntimeService(fakeRuntime)
    77  	vr, err := irs.Version(ctx, "1")
    78  	assert.NoError(t, err)
    79  	assert.Equal(t, kubeRuntimeAPIVersion, vr.Version)
    80  }
    81  
    82  func TestStatus(t *testing.T) {
    83  	ctx := context.Background()
    84  	fakeRuntime, _, _, _ := createTestRuntimeManager()
    85  	fakeRuntime.FakeStatus = &runtimeapi.RuntimeStatus{
    86  		Conditions: []*runtimeapi.RuntimeCondition{
    87  			{Type: runtimeapi.RuntimeReady, Status: false},
    88  			{Type: runtimeapi.NetworkReady, Status: true},
    89  		},
    90  	}
    91  	irs := newInstrumentedRuntimeService(fakeRuntime)
    92  	actural, err := irs.Status(ctx, false)
    93  	assert.NoError(t, err)
    94  	expected := &runtimeapi.RuntimeStatus{
    95  		Conditions: []*runtimeapi.RuntimeCondition{
    96  			{Type: runtimeapi.RuntimeReady, Status: false},
    97  			{Type: runtimeapi.NetworkReady, Status: true},
    98  		},
    99  	}
   100  	assert.Equal(t, expected, actural.Status)
   101  }
   102  

View as plain text