github.com/verrazzano/verrazzano@v1.7.1/tools/oam-converter/pkg/resources/workloads/destinationrule_test.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  package workloads
     4  
     5  import (
     6  	"github.com/stretchr/testify/assert"
     7  	vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1"
     8  	reader "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/testdata"
     9  	corev1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    12  	"testing"
    13  )
    14  
    15  func TestCreateDestinationRuleFromWorkload(t *testing.T) {
    16  	// Create a sample trait and rule
    17  	trait := &vzapi.IngressTrait{
    18  		ObjectMeta: metav1.ObjectMeta{
    19  			Namespace: "example-namespace",
    20  		},
    21  	}
    22  	rule := vzapi.IngressRule{
    23  		Destination: vzapi.IngressDestination{
    24  			HTTPCookie: &vzapi.IngressDestinationHTTPCookie{
    25  				Name: "example-cookie",
    26  				Path: "/example-path",
    27  				TTL:  60,
    28  			},
    29  		},
    30  	}
    31  
    32  	compConf, err := reader.ReadFromYAMLTemplate("testdata/template/helidon_workload.yaml")
    33  	if err != nil {
    34  		t.Fatalf("error in reading yaml file : %v", err)
    35  	}
    36  
    37  	compSpec, found, err := unstructured.NestedMap(compConf, "spec")
    38  	if !found || err != nil {
    39  		t.Fatalf("component spec doesn't exist or not found in specified type: %v", err)
    40  	}
    41  	compWorkload, found, err := unstructured.NestedMap(compSpec, "workload")
    42  	if !found || err != nil {
    43  		t.Fatalf("component workload doesn't exist or not found in specified type: %v", err)
    44  	}
    45  	helidonWorkload := &unstructured.Unstructured{
    46  		Object: compWorkload,
    47  	}
    48  	service := &corev1.Service{}
    49  
    50  	// Call the function being tested
    51  	destinationRule, err := createDestinationRuleFromWorkload(trait, rule, "example-rule", helidonWorkload, service)
    52  
    53  	// Assert that there is no error
    54  	assert.NoError(t, err)
    55  
    56  	// Assert that the destinationrule is not nil
    57  	assert.NotNil(t, destinationRule)
    58  
    59  	// Assert that the destinationrule has the correct APIVersion and Kind
    60  	assert.Equal(t, "networking.istio.io/v1beta13", destinationRule.APIVersion)
    61  	assert.Equal(t, "DestinationRule", destinationRule.Kind)
    62  
    63  }