github.com/kubewharf/katalyst-core@v0.5.3/pkg/client/control/vpa_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 control
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	apis "github.com/kubewharf/katalyst-api/pkg/apis/autoscaling/v1alpha1"
    27  	externalfake "github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned/fake"
    28  )
    29  
    30  func TestPatchVPA(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	oldvpa1 := &apis.KatalystVerticalPodAutoscaler{
    34  		ObjectMeta: metav1.ObjectMeta{
    35  			Name:      "vpa1",
    36  			Namespace: "default",
    37  		},
    38  	}
    39  	newvpa1 := &apis.KatalystVerticalPodAutoscaler{
    40  		ObjectMeta: metav1.ObjectMeta{
    41  			Name:      "vpa1",
    42  			Namespace: "default",
    43  			Annotations: map[string]string{
    44  				"a1": "a1",
    45  			},
    46  		},
    47  	}
    48  
    49  	for _, tc := range []struct {
    50  		name   string
    51  		oldVPA *apis.KatalystVerticalPodAutoscaler
    52  		newVPA *apis.KatalystVerticalPodAutoscaler
    53  	}{
    54  		{
    55  			name:   "add annotation",
    56  			oldVPA: oldvpa1,
    57  			newVPA: newvpa1,
    58  		},
    59  		{
    60  			name:   "delete annotation",
    61  			oldVPA: newvpa1,
    62  			newVPA: oldvpa1,
    63  		},
    64  	} {
    65  		tc := tc
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			t.Parallel()
    68  
    69  			internalClient := externalfake.NewSimpleClientset(tc.oldVPA)
    70  			updater := NewRealVPAUpdater(internalClient)
    71  			_, err := updater.PatchVPA(context.TODO(), tc.oldVPA, tc.newVPA)
    72  			assert.NoError(t, err)
    73  			vpa, err := internalClient.AutoscalingV1alpha1().KatalystVerticalPodAutoscalers("default").
    74  				Get(context.TODO(), tc.oldVPA.Name, metav1.GetOptions{})
    75  			assert.NoError(t, err)
    76  			assert.Equal(t, tc.newVPA, vpa)
    77  		})
    78  	}
    79  }
    80  
    81  func TestPatchVPAStatus(t *testing.T) {
    82  	t.Parallel()
    83  
    84  	oldvpa1 := &apis.KatalystVerticalPodAutoscaler{
    85  		ObjectMeta: metav1.ObjectMeta{
    86  			Name:      "vpa1",
    87  			Namespace: "default",
    88  		},
    89  	}
    90  	newvpa1 := &apis.KatalystVerticalPodAutoscaler{
    91  		ObjectMeta: metav1.ObjectMeta{
    92  			Name:      "vpa1",
    93  			Namespace: "default",
    94  		},
    95  		Status: apis.KatalystVerticalPodAutoscalerStatus{
    96  			Conditions: []apis.VerticalPodAutoscalerCondition{
    97  				{
    98  					Type: apis.RecommendationUpdated,
    99  				},
   100  			},
   101  		},
   102  	}
   103  
   104  	for _, tc := range []struct {
   105  		name   string
   106  		oldVPA *apis.KatalystVerticalPodAutoscaler
   107  		newVPA *apis.KatalystVerticalPodAutoscaler
   108  	}{
   109  		{
   110  			name:   "add condition",
   111  			oldVPA: oldvpa1,
   112  			newVPA: newvpa1,
   113  		},
   114  		{
   115  			name:   "delete condition",
   116  			oldVPA: newvpa1,
   117  			newVPA: oldvpa1,
   118  		},
   119  	} {
   120  		tc := tc
   121  		t.Run(tc.name, func(t *testing.T) {
   122  			t.Parallel()
   123  
   124  			internalClient := externalfake.NewSimpleClientset(tc.oldVPA)
   125  			updater := NewRealVPAUpdater(internalClient)
   126  			_, err := updater.PatchVPAStatus(context.TODO(), tc.oldVPA, tc.newVPA)
   127  			assert.NoError(t, err)
   128  			vpa, err := internalClient.AutoscalingV1alpha1().KatalystVerticalPodAutoscalers("default").
   129  				Get(context.TODO(), tc.oldVPA.Name, metav1.GetOptions{})
   130  			assert.NoError(t, err)
   131  			assert.Equal(t, tc.newVPA, vpa)
   132  		})
   133  	}
   134  }