github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/transform_utils_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package apps
    21  
    22  import (
    23  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	v1 "k8s.io/api/apps/v1"
    29  	corev1 "k8s.io/api/core/v1"
    30  	"k8s.io/apimachinery/pkg/api/resource"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  
    33  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    34  )
    35  
    36  func TestReflect(t *testing.T) {
    37  	var list client.ObjectList
    38  	sts := v1.StatefulSet{}
    39  	sts.SetName("hello")
    40  	list = &v1.StatefulSetList{Items: []v1.StatefulSet{sts}}
    41  	v := reflect.ValueOf(list).Elem().FieldByName("Items")
    42  	if v.Kind() != reflect.Slice {
    43  		t.Error("not slice")
    44  	}
    45  	c := v.Len()
    46  	objects := make([]client.Object, c)
    47  	for i := 0; i < c; i++ {
    48  		var st = v.Index(i).Addr().Interface()
    49  		objects[i] = st.(client.Object)
    50  	}
    51  	for _, e := range objects {
    52  		fmt.Println(e)
    53  	}
    54  
    55  	var o client.Object = &sts
    56  	ptr := reflect.ValueOf(o)
    57  	v = ptr.Elem().FieldByName("Spec")
    58  	fmt.Println(v)
    59  }
    60  
    61  func TestIsVolumeClaimTemplatesEqual(t *testing.T) {
    62  	buildVCT := func(size string) []appsv1alpha1.ClusterComponentVolumeClaimTemplate {
    63  		return []appsv1alpha1.ClusterComponentVolumeClaimTemplate{
    64  			{
    65  				Name: "data",
    66  				Spec: appsv1alpha1.PersistentVolumeClaimSpec{
    67  					Resources: corev1.ResourceRequirements{
    68  						Requests: corev1.ResourceList{
    69  							corev1.ResourceStorage: resource.MustParse(size),
    70  						},
    71  					},
    72  				},
    73  			},
    74  		}
    75  	}
    76  
    77  	assert.True(t, isVolumeClaimTemplatesEqual(buildVCT("1Gi"), buildVCT("1024Mi")))
    78  }
    79  
    80  func TestIsResourceRequirementsEqual(t *testing.T) {
    81  	buildRR := func(cpu, memory string) corev1.ResourceRequirements {
    82  		return corev1.ResourceRequirements{
    83  			Requests: corev1.ResourceList{
    84  				corev1.ResourceCPU:    resource.MustParse(cpu),
    85  				corev1.ResourceMemory: resource.MustParse(memory),
    86  			},
    87  		}
    88  	}
    89  	a := buildRR("1", "1Gi")
    90  	b := buildRR("1000m", "1024Mi")
    91  	assert.True(t, isResourceRequirementsEqual(a, b))
    92  }