k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/core/componentstatus/rest_test.go (about)

     1  /*
     2  Copyright 2015 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 componentstatus
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  	metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
    27  	"k8s.io/apimachinery/pkg/fields"
    28  	"k8s.io/apimachinery/pkg/labels"
    29  
    30  	"net/http"
    31  	"time"
    32  
    33  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    34  	genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
    35  	api "k8s.io/kubernetes/pkg/apis/core"
    36  	"k8s.io/kubernetes/pkg/probe"
    37  )
    38  
    39  type fakeHttpProber struct {
    40  	result probe.Result
    41  	body   string
    42  	err    error
    43  }
    44  
    45  func (f *fakeHttpProber) Probe(*http.Request, time.Duration) (probe.Result, string, error) {
    46  	return f.result, f.body, f.err
    47  }
    48  
    49  type testResponse struct {
    50  	result probe.Result
    51  	data   string
    52  	err    error
    53  }
    54  
    55  func NewTestREST(resp testResponse) *REST {
    56  	prober := &fakeHttpProber{
    57  		result: resp.result,
    58  		body:   resp.data,
    59  		err:    resp.err,
    60  	}
    61  	return &REST{
    62  		GetServersToValidate: func() map[string]Server {
    63  			return map[string]Server{
    64  				"test1": &HttpServer{Addr: "testserver1", Port: 8000, Path: "/healthz", Prober: prober},
    65  			}
    66  		},
    67  	}
    68  }
    69  
    70  func createTestStatus(name string, status api.ConditionStatus, msg string, err string) *api.ComponentStatus {
    71  	retVal := &api.ComponentStatus{
    72  		Conditions: []api.ComponentCondition{
    73  			{Type: api.ComponentHealthy, Status: status, Message: msg, Error: err},
    74  		},
    75  	}
    76  	retVal.Name = name
    77  	return retVal
    78  }
    79  
    80  func TestList_NoError(t *testing.T) {
    81  	r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
    82  	got, err := r.List(genericapirequest.NewContext(), nil)
    83  	if err != nil {
    84  		t.Fatalf("Unexpected error: %v", err)
    85  	}
    86  	expect := &api.ComponentStatusList{
    87  		Items: []api.ComponentStatus{*(createTestStatus("test1", api.ConditionTrue, "ok", ""))},
    88  	}
    89  	if e, a := expect, got; !reflect.DeepEqual(e, a) {
    90  		t.Errorf("Got unexpected object. Diff: %s", cmp.Diff(e, a))
    91  	}
    92  }
    93  
    94  func TestList_WithLabelSelectors(t *testing.T) {
    95  	r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
    96  	opts := metainternalversion.ListOptions{
    97  		LabelSelector: labels.SelectorFromSet(map[string]string{
    98  			"testLabel": "testValue",
    99  		}),
   100  	}
   101  	got, err := r.List(genericapirequest.NewContext(), &opts)
   102  	if err != nil {
   103  		t.Fatalf("Unexpected error: %v", err)
   104  	}
   105  	expect := &api.ComponentStatusList{
   106  		Items: []api.ComponentStatus{},
   107  	}
   108  	if e, a := expect, got; !reflect.DeepEqual(e, a) {
   109  		t.Errorf("Got unexpected object. Diff: %s", cmp.Diff(e, a))
   110  	}
   111  }
   112  
   113  func TestList_WithFieldSelectors(t *testing.T) {
   114  	r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
   115  	opts := metainternalversion.ListOptions{
   116  		FieldSelector: fields.SelectorFromSet(map[string]string{
   117  			"testField": "testValue",
   118  		}),
   119  	}
   120  	got, err := r.List(genericapirequest.NewContext(), &opts)
   121  	if err != nil {
   122  		t.Fatalf("Unexpected error: %v", err)
   123  	}
   124  	expect := &api.ComponentStatusList{
   125  		Items: []api.ComponentStatus{},
   126  	}
   127  	if e, a := expect, got; !reflect.DeepEqual(e, a) {
   128  		t.Errorf("Got unexpected object. Diff: %s", cmp.Diff(e, a))
   129  	}
   130  }
   131  
   132  func TestList_FailedCheck(t *testing.T) {
   133  	r := NewTestREST(testResponse{result: probe.Failure, data: ""})
   134  	got, err := r.List(genericapirequest.NewContext(), nil)
   135  	if err != nil {
   136  		t.Fatalf("Unexpected error: %v", err)
   137  	}
   138  	expect := &api.ComponentStatusList{
   139  		Items: []api.ComponentStatus{
   140  			*(createTestStatus("test1", api.ConditionFalse, "", ""))},
   141  	}
   142  	if e, a := expect, got; !reflect.DeepEqual(e, a) {
   143  		t.Errorf("Got unexpected object. Diff: %s", cmp.Diff(e, a))
   144  	}
   145  }
   146  
   147  func TestList_UnknownError(t *testing.T) {
   148  	r := NewTestREST(testResponse{result: probe.Unknown, data: "", err: fmt.Errorf("fizzbuzz error")})
   149  	got, err := r.List(genericapirequest.NewContext(), nil)
   150  	if err != nil {
   151  		t.Fatalf("Unexpected error: %v", err)
   152  	}
   153  	expect := &api.ComponentStatusList{
   154  		Items: []api.ComponentStatus{
   155  			*(createTestStatus("test1", api.ConditionUnknown, "", "fizzbuzz error"))},
   156  	}
   157  	if e, a := expect, got; !reflect.DeepEqual(e, a) {
   158  		t.Errorf("Got unexpected object. Diff: %s", cmp.Diff(e, a))
   159  	}
   160  }
   161  
   162  func TestGet_NoError(t *testing.T) {
   163  	r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
   164  	got, err := r.Get(genericapirequest.NewContext(), "test1", &metav1.GetOptions{})
   165  	if err != nil {
   166  		t.Fatalf("Unexpected error: %v", err)
   167  	}
   168  	expect := createTestStatus("test1", api.ConditionTrue, "ok", "")
   169  	if e, a := expect, got; !reflect.DeepEqual(e, a) {
   170  		t.Errorf("Got unexpected object. Diff: %s", cmp.Diff(e, a))
   171  	}
   172  }
   173  
   174  func TestGet_BadName(t *testing.T) {
   175  	r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
   176  	_, err := r.Get(genericapirequest.NewContext(), "invalidname", &metav1.GetOptions{})
   177  	if err == nil {
   178  		t.Fatalf("Expected error, but did not get one")
   179  	}
   180  	if !strings.Contains(err.Error(), "Component not found: invalidname") {
   181  		t.Fatalf("Got unexpected error: %v", err)
   182  	}
   183  }