...

Source file src/k8s.io/kubectl/pkg/drain/default_test.go

Documentation: k8s.io/kubectl/pkg/drain

     1  /*
     2  Copyright 2021 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 drain
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/client-go/kubernetes/fake"
    26  )
    27  
    28  func TestRunCordonOrUncordon(t *testing.T) {
    29  	nilContextError := fmt.Errorf("RunCordonOrUncordon error: drainer.Ctx can't be nil")
    30  	nilClientError := fmt.Errorf("RunCordonOrUncordon error: drainer.Client can't be nil")
    31  	tests := []struct {
    32  		description   string
    33  		drainer       *Helper
    34  		node          *corev1.Node
    35  		desired       bool
    36  		expectedError *error
    37  	}{
    38  		{
    39  			description: "nil context object",
    40  			drainer: &Helper{
    41  				Client: fake.NewSimpleClientset(),
    42  			},
    43  			desired:       true,
    44  			expectedError: &nilContextError,
    45  		},
    46  		{
    47  			description: "nil client object",
    48  			drainer: &Helper{
    49  				Ctx: context.TODO(),
    50  			},
    51  			desired:       true,
    52  			expectedError: &nilClientError,
    53  		},
    54  	}
    55  
    56  	for _, test := range tests {
    57  		test := test
    58  		t.Run(test.description, func(t *testing.T) {
    59  			err := RunCordonOrUncordon(test.drainer, test.node, test.desired)
    60  			if test.expectedError == nil {
    61  				if err != nil {
    62  					t.Fatalf("%s: did not expect error, got err=%s", test.description, err.Error())
    63  				}
    64  			} else if err.Error() != (*test.expectedError).Error() {
    65  				t.Fatalf("%s: the error does not match expected error, got err=%s, expected err=%s", test.description, err, *test.expectedError)
    66  			}
    67  		})
    68  	}
    69  }
    70  

View as plain text