k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gencred/pkg/serviceaccount/serviceaccount_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 serviceaccount
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	authenticationv1 "k8s.io/api/authentication/v1"
    24  	authorizationv1 "k8s.io/api/authorization/v1"
    25  	corev1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/client-go/kubernetes"
    29  	k8sFake "k8s.io/client-go/kubernetes/fake"
    30  	k8sTesting "k8s.io/client-go/testing"
    31  )
    32  
    33  func TestCreateClusterServiceAccountCredentials(t *testing.T) {
    34  	tests := []struct {
    35  		name         string
    36  		createClient func() kubernetes.Interface
    37  		expected     bool
    38  	}{
    39  		{
    40  			name: "create cluster service account success",
    41  			createClient: func() kubernetes.Interface {
    42  				var client kubernetes.Interface = &k8sFake.Clientset{}
    43  
    44  				client.(*k8sFake.Clientset).Fake.AddReactor("get", "configmaps", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
    45  					r := &corev1.ConfigMap{
    46  						ObjectMeta: metav1.ObjectMeta{
    47  							Name:      "kube-root-ca.crt",
    48  							Namespace: corev1.NamespaceDefault,
    49  						},
    50  						Data: map[string]string{
    51  							"ca.crt": "ca",
    52  						},
    53  					}
    54  					return true, r, nil
    55  				})
    56  
    57  				client.(*k8sFake.Clientset).Fake.AddReactor("get", "serviceaccounts", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
    58  					r := &corev1.ServiceAccount{
    59  						ObjectMeta: metav1.ObjectMeta{
    60  							Name:      serviceAccountName,
    61  							Namespace: corev1.NamespaceDefault,
    62  						},
    63  						Secrets: []corev1.ObjectReference{{Name: "secret-abc"}},
    64  					}
    65  					return true, r, nil
    66  				})
    67  
    68  				client.(*k8sFake.Clientset).Fake.AddReactor("create", "selfsubjectaccessreviews", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
    69  					r := &authorizationv1.SelfSubjectAccessReview{
    70  						Status: authorizationv1.SubjectAccessReviewStatus{
    71  							Allowed: true,
    72  							Reason:  "I am a test!",
    73  						},
    74  					}
    75  					return true, r, nil
    76  				})
    77  
    78  				client.(*k8sFake.Clientset).Fake.AddReactor("create", "serviceaccounts/token", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
    79  					r := &authenticationv1.TokenRequest{
    80  						Status: authenticationv1.TokenRequestStatus{
    81  							Token: "abc",
    82  						},
    83  					}
    84  					return true, r, nil
    85  				})
    86  
    87  				return client
    88  			},
    89  			expected: true,
    90  		},
    91  		{
    92  			name: "create cluster service account fail",
    93  			createClient: func() kubernetes.Interface {
    94  				return k8sFake.NewSimpleClientset()
    95  			},
    96  			expected: false,
    97  		},
    98  	}
    99  
   100  	for _, test := range tests {
   101  		t.Run(test.name, func(t *testing.T) {
   102  			client := test.createClient()
   103  			_, _, err := CreateClusterServiceAccountCredentials(client, metav1.Duration{Duration: 2 * 24 * time.Hour})
   104  			success := err == nil
   105  
   106  			if success != test.expected {
   107  				t.Fatalf("Expected %v, but got result %v: %v", test.expected, success, err)
   108  			}
   109  		})
   110  	}
   111  }