knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/scale_test.go (about)

     1  /*
     2  Copyright 2018 The Knative 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 duck_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	appsv1 "k8s.io/api/apps/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  
    27  	"knative.dev/pkg/apis/duck"
    28  	"knative.dev/pkg/ptr"
    29  )
    30  
    31  type Scalable struct {
    32  	metav1.TypeMeta   `json:",inline"`
    33  	metav1.ObjectMeta `json:"metadata,omitempty"`
    34  
    35  	Spec   ScalableSpec   `json:"spec,omitempty"`
    36  	Status ScalableStatus `json:"status,omitempty"`
    37  }
    38  
    39  type ScalableSpec struct {
    40  	Replicas *int32                `json:"replicas,omitempty"`
    41  	Selector *metav1.LabelSelector `json:"selector,omitempty"`
    42  }
    43  
    44  type ScalableStatus struct {
    45  	Replicas int32 `json:"replicas,omitempty"`
    46  }
    47  
    48  var (
    49  	_ duck.Populatable   = (*Scalable)(nil)
    50  	_ duck.Implementable = (*Scalable)(nil)
    51  )
    52  
    53  func (*Scalable) GetObjectKind() schema.ObjectKind {
    54  	return nil // not used
    55  }
    56  
    57  func (*Scalable) DeepCopyObject() runtime.Object {
    58  	return nil // not used
    59  }
    60  
    61  func (*Scalable) GetListType() runtime.Object {
    62  	return nil // not used
    63  }
    64  
    65  // GetFullType implements duck.Implementable
    66  func (*Scalable) GetFullType() duck.Populatable {
    67  	return &Scalable{}
    68  }
    69  
    70  // Populate implements duck.Populatable
    71  func (t *Scalable) Populate() {
    72  	t.Spec = ScalableSpec{
    73  		Replicas: ptr.Int32(1),
    74  		Selector: &metav1.LabelSelector{
    75  			MatchLabels: map[string]string{
    76  				"foo": "bar",
    77  			},
    78  			MatchExpressions: []metav1.LabelSelectorRequirement{{
    79  				Key:      "baz",
    80  				Operator: metav1.LabelSelectorOpNotIn,
    81  				Values:   []string{"blah", "blog"},
    82  			}},
    83  		},
    84  	}
    85  	t.Status = ScalableStatus{
    86  		Replicas: 1,
    87  	}
    88  }
    89  
    90  func TestImplementsScalable(t *testing.T) {
    91  	instances := []interface{}{
    92  		&Scalable{},
    93  		&appsv1.ReplicaSet{},
    94  		&appsv1.Deployment{},
    95  		&appsv1.StatefulSet{},
    96  	}
    97  	for _, instance := range instances {
    98  		if err := duck.VerifyType(instance, &Scalable{}); err != nil {
    99  			t.Error(err)
   100  		}
   101  	}
   102  }