github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/spd/checkpoint/checkpoint_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 checkpoint
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"reflect"
    23  	"testing"
    24  
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
    27  
    28  	"github.com/kubewharf/katalyst-api/pkg/apis/workload/v1alpha1"
    29  )
    30  
    31  // TestWriteLoadDeleteSPDs validates all combinations of write, load, and delete
    32  func TestWriteLoadDeleteSPDs(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	testSPDs := []*v1alpha1.ServiceProfileDescriptor{
    36  		{
    37  			ObjectMeta: metav1.ObjectMeta{
    38  				Namespace:         "default",
    39  				Name:              "spd-1",
    40  				CreationTimestamp: metav1.Unix(metav1.Now().Unix(), 0),
    41  			},
    42  			Spec: v1alpha1.ServiceProfileDescriptorSpec{
    43  				BusinessIndicator: []v1alpha1.ServiceBusinessIndicatorSpec{
    44  					{
    45  						Name: v1alpha1.ServiceBusinessIndicatorNameRPCLatency,
    46  					},
    47  				},
    48  			},
    49  		},
    50  		{
    51  			ObjectMeta: metav1.ObjectMeta{
    52  				Namespace:         "default",
    53  				Name:              "spd-2",
    54  				DeletionTimestamp: &metav1.Time{Time: metav1.Unix(metav1.Now().Unix(), 0).Time},
    55  			},
    56  			Status: v1alpha1.ServiceProfileDescriptorStatus{
    57  				BusinessStatus: []v1alpha1.ServiceBusinessIndicatorStatus{
    58  					{
    59  						Name: v1alpha1.ServiceBusinessIndicatorNameRPCLatency,
    60  					},
    61  				},
    62  			},
    63  		},
    64  	}
    65  
    66  	dir, err := ioutil.TempDir("", "checkpoint-TestWriteLoadDeleteSPDs")
    67  	if err != nil {
    68  		t.Errorf("failed to allocate temp directory for TestWriteLoadDeleteSPDs error=%v", err)
    69  	}
    70  	defer os.RemoveAll(dir)
    71  
    72  	cpm, err := checkpointmanager.NewCheckpointManager(dir)
    73  	if err != nil {
    74  		t.Errorf("failed to initialize checkpoint manager error=%v", err)
    75  	}
    76  
    77  	for _, p := range testSPDs {
    78  		if err := WriteSPD(cpm, p); err != nil {
    79  			t.Errorf("failed to Write SPD: %v", err)
    80  		}
    81  	}
    82  
    83  	// verify the correct written files are loaded from disk
    84  	spdList, err := LoadSPDs(cpm, false)
    85  	if err != nil {
    86  		t.Errorf("failed to Load spds: %v", err)
    87  	}
    88  
    89  	// loop through contents and check make sure
    90  	// what was loaded matched the expected results.
    91  	for _, p := range testSPDs {
    92  		spdName := p.GetName()
    93  		var spd *v1alpha1.ServiceProfileDescriptor
    94  		for _, check := range spdList {
    95  			if check.GetName() == spdName {
    96  				spd = check
    97  				break
    98  			}
    99  		}
   100  
   101  		if spd != nil {
   102  			if !reflect.DeepEqual(p, spd) {
   103  				t.Errorf("expected %#v, \ngot %#v", p, spd)
   104  			}
   105  		} else {
   106  			t.Errorf("got unexpected result for %v, should have been loaded", spdName)
   107  		}
   108  
   109  		err = DeleteSPD(cpm, p)
   110  		if err != nil {
   111  			t.Errorf("failed to delete spd %v", spdName)
   112  		}
   113  	}
   114  	// finally validate the contents of the directory is empty.
   115  	files, err := ioutil.ReadDir(dir)
   116  	if err != nil {
   117  		t.Errorf("failed to read directory %v", dir)
   118  	}
   119  	if len(files) > 0 {
   120  		t.Errorf("directory %v should be empty but found %#v", dir, files)
   121  	}
   122  }