k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gencred/pkg/certificate/certificate_test.go (about)

     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 certificate
    18  
    19  import (
    20  	"testing"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/client-go/kubernetes"
    25  	k8sFake "k8s.io/client-go/kubernetes/fake"
    26  )
    27  
    28  func TestCreateClusterCertificateCredentials(t *testing.T) {
    29  	tests := []struct {
    30  		name         string
    31  		createClient func() kubernetes.Interface
    32  		expected     bool
    33  	}{
    34  		{
    35  			name: "create cluster certificate success",
    36  			createClient: func() kubernetes.Interface {
    37  				return k8sFake.NewSimpleClientset(&corev1.Secret{
    38  					TypeMeta: metav1.TypeMeta{},
    39  					ObjectMeta: metav1.ObjectMeta{
    40  						Namespace: metav1.NamespaceSystem,
    41  					},
    42  					Data: map[string][]uint8{corev1.ServiceAccountRootCAKey: {1, 2, 3}},
    43  				})
    44  			},
    45  			expected: true,
    46  		},
    47  		{
    48  			name: "create cluster certificate fail",
    49  			createClient: func() kubernetes.Interface {
    50  				return k8sFake.NewSimpleClientset()
    51  			},
    52  			expected: false,
    53  		},
    54  	}
    55  
    56  	for _, test := range tests {
    57  		t.Run(test.name, func(t *testing.T) {
    58  			client := test.createClient()
    59  			_, _, _, err := CreateClusterCertificateCredentials(client)
    60  			success := err == nil
    61  
    62  			if success != test.expected {
    63  				t.Fatalf("Expected %v, but got result %v: %v", test.expected, success, err)
    64  			}
    65  		})
    66  	}
    67  }