github.com/kubewharf/katalyst-core@v0.5.3/pkg/client/control/vparec_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 TestPatchVPARec(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	oldvparec1 := &apis.VerticalPodAutoscalerRecommendation{
    34  		ObjectMeta: metav1.ObjectMeta{
    35  			Name:      "vpa1",
    36  			Namespace: "default",
    37  		},
    38  	}
    39  	newvparec1 := &apis.VerticalPodAutoscalerRecommendation{
    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  		oldVPARec *apis.VerticalPodAutoscalerRecommendation
    52  		newVPARec *apis.VerticalPodAutoscalerRecommendation
    53  	}{
    54  		{
    55  			name:      "add annotation",
    56  			oldVPARec: oldvparec1,
    57  			newVPARec: newvparec1,
    58  		},
    59  		{
    60  			name:      "delete annotation",
    61  			oldVPARec: newvparec1,
    62  			newVPARec: oldvparec1,
    63  		},
    64  	} {
    65  		tc := tc
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			t.Parallel()
    68  
    69  			internalClient := externalfake.NewSimpleClientset(tc.oldVPARec)
    70  			updater := NewRealVPARecommendationUpdater(internalClient)
    71  			err := updater.PatchVPARecommendation(context.TODO(), tc.oldVPARec, tc.newVPARec)
    72  			assert.NoError(t, err)
    73  			vpa, err := internalClient.AutoscalingV1alpha1().VerticalPodAutoscalerRecommendations("default").
    74  				Get(context.TODO(), tc.oldVPARec.Name, metav1.GetOptions{})
    75  			assert.NoError(t, err)
    76  			assert.Equal(t, tc.newVPARec, vpa)
    77  		})
    78  	}
    79  }
    80  
    81  func TestPatchVPARecStatus(t *testing.T) {
    82  	t.Parallel()
    83  
    84  	oldvparec1 := &apis.VerticalPodAutoscalerRecommendation{
    85  		ObjectMeta: metav1.ObjectMeta{
    86  			Name:      "vpa1",
    87  			Namespace: "default",
    88  		},
    89  	}
    90  	newvparec1 := &apis.VerticalPodAutoscalerRecommendation{
    91  		ObjectMeta: metav1.ObjectMeta{
    92  			Name:      "vpa1",
    93  			Namespace: "default",
    94  		},
    95  		Status: apis.VerticalPodAutoscalerRecommendationStatus{
    96  			Conditions: []apis.VerticalPodAutoscalerRecommendationCondition{
    97  				{
    98  					Type: apis.RecommendationUpdatedToVPA,
    99  				},
   100  			},
   101  		},
   102  	}
   103  
   104  	for _, tc := range []struct {
   105  		name      string
   106  		oldVPARec *apis.VerticalPodAutoscalerRecommendation
   107  		newVPARec *apis.VerticalPodAutoscalerRecommendation
   108  	}{
   109  		{
   110  			name:      "add annotation",
   111  			oldVPARec: oldvparec1,
   112  			newVPARec: newvparec1,
   113  		},
   114  		{
   115  			name:      "delete annotation",
   116  			oldVPARec: newvparec1,
   117  			newVPARec: oldvparec1,
   118  		},
   119  	} {
   120  		tc := tc
   121  		t.Run(tc.name, func(t *testing.T) {
   122  			t.Parallel()
   123  
   124  			internalClient := externalfake.NewSimpleClientset(tc.oldVPARec)
   125  			updater := NewRealVPARecommendationUpdater(internalClient)
   126  			err := updater.PatchVPARecommendationStatus(context.TODO(), tc.oldVPARec, tc.newVPARec)
   127  			assert.NoError(t, err)
   128  			vpa, err := internalClient.AutoscalingV1alpha1().VerticalPodAutoscalerRecommendations("default").
   129  				Get(context.TODO(), tc.oldVPARec.Name, metav1.GetOptions{})
   130  			assert.NoError(t, err)
   131  			assert.Equal(t, tc.newVPARec, vpa)
   132  		})
   133  	}
   134  }