sigs.k8s.io/cluster-api@v1.7.1/internal/runtime/test/v1alpha1/conversion_test.go (about)

     1  /*
     2  Copyright 2022 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
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    27  	runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
    28  	"sigs.k8s.io/cluster-api/internal/runtime/test/v1alpha2"
    29  )
    30  
    31  func TestConversion(t *testing.T) {
    32  	g := NewWithT(t)
    33  
    34  	var c = runtimecatalog.New()
    35  	_ = AddToCatalog(c)
    36  	_ = v1alpha2.AddToCatalog(c)
    37  
    38  	t.Run("down-convert FakeRequest v1alpha2 to v1alpha1", func(*testing.T) {
    39  		request := &v1alpha2.FakeRequest{Cluster: clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{
    40  			Name: "test",
    41  		}}}
    42  		requestLocal := &FakeRequest{}
    43  
    44  		g.Expect(c.Convert(request, requestLocal, context.Background())).To(Succeed())
    45  		g.Expect(requestLocal.Cluster.GetName()).To(Equal(request.Cluster.Name))
    46  	})
    47  
    48  	t.Run("up-convert FakeResponse v1alpha1 to v1alpha2", func(*testing.T) {
    49  		responseLocal := &FakeResponse{
    50  			First:  1,
    51  			Second: "foo",
    52  		}
    53  		response := &v1alpha2.FakeResponse{}
    54  		g.Expect(c.Convert(responseLocal, response, context.Background())).To(Succeed())
    55  
    56  		g.Expect(response.First).To(Equal(responseLocal.First))
    57  		g.Expect(response.Second).To(Equal(responseLocal.Second))
    58  	})
    59  }