sigs.k8s.io/kueue@v0.6.2/apis/visibility/v1alpha1/conversion_test.go (about)

     1  /*
     2  Copyright 2023 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 v1alpha1_test
    18  
    19  import (
    20  	"net/url"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/util/diff"
    26  
    27  	. "sigs.k8s.io/kueue/apis/visibility/v1alpha1"
    28  	"sigs.k8s.io/kueue/pkg/visibility/api"
    29  )
    30  
    31  func TestPendingWorkloadsOptions(t *testing.T) {
    32  	codec := runtime.NewParameterCodec(api.Scheme)
    33  
    34  	cases := map[string]struct {
    35  		inputQueryParams           url.Values
    36  		wantQueryParams            url.Values
    37  		wantPendingWorkloadOptions PendingWorkloadOptions
    38  	}{
    39  		"correct parameters": {
    40  			inputQueryParams: url.Values{
    41  				"limit":  {"1"},
    42  				"offset": {"2"},
    43  			},
    44  			wantQueryParams: url.Values{
    45  				"limit":  {"1"},
    46  				"offset": {"2"},
    47  			},
    48  			wantPendingWorkloadOptions: PendingWorkloadOptions{
    49  				Limit:  1,
    50  				Offset: 2,
    51  			},
    52  		},
    53  		"default values": {
    54  			inputQueryParams: url.Values{
    55  				"limit":  {"0"},
    56  				"offset": {"0"},
    57  			},
    58  			wantQueryParams: url.Values{
    59  				"limit":  {"1000"},
    60  				"offset": {"0"},
    61  			},
    62  			wantPendingWorkloadOptions: PendingWorkloadOptions{
    63  				Limit:  1000,
    64  				Offset: 0,
    65  			},
    66  		},
    67  	}
    68  
    69  	for name, tc := range cases {
    70  		t.Run(name, func(t *testing.T) {
    71  			// versioned -> query params
    72  			actualParameters, err := codec.EncodeParameters(&tc.wantPendingWorkloadOptions, SchemeGroupVersion)
    73  			if err != nil {
    74  				t.Fatal(err)
    75  			}
    76  			if d := cmp.Diff(actualParameters, tc.wantQueryParams); d != "" {
    77  				t.Fatalf("Unxpected serialization:\n%s", diff.ObjectGoPrintSideBySide(tc.wantQueryParams, actualParameters))
    78  			}
    79  
    80  			// query params -> versioned
    81  			convertedPendingWorkloadOptions := PendingWorkloadOptions{}
    82  			err = codec.DecodeParameters(tc.inputQueryParams, SchemeGroupVersion, &convertedPendingWorkloadOptions)
    83  			if err != nil {
    84  				t.Fatal(err)
    85  			}
    86  			if d := cmp.Diff(convertedPendingWorkloadOptions, tc.wantPendingWorkloadOptions); d != "" {
    87  				t.Fatalf("Unexpected deserialization:\n%s", diff.ObjectGoPrintSideBySide(tc.wantPendingWorkloadOptions, convertedPendingWorkloadOptions))
    88  			}
    89  		})
    90  	}
    91  }