github.com/verrazzano/verrazzano@v1.7.1/tools/oam-converter/pkg/resources/workloads/resources_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  
     4  package workloads
     5  
     6  import (
     7  	"github.com/stretchr/testify/assert"
     8  	vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1"
     9  	"github.com/verrazzano/verrazzano/tools/oam-converter/pkg/types"
    10  	vsapi "istio.io/client-go/pkg/apis/networking/v1beta1"
    11  	k8net "k8s.io/api/networking/v1"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    14  	k8scheme "k8s.io/client-go/kubernetes/scheme"
    15  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    16  	"testing"
    17  )
    18  
    19  func TestCreateIngressChildResourcesFromHelidon(t *testing.T) {
    20  	// Create sample ConversionComponents, Gateway, and allHostsForTrait
    21  	conversionComponent := &types.ConversionComponents{
    22  		AppName:       "myapp",
    23  		ComponentName: "mycomponent",
    24  		AppNamespace:  "mynamespace",
    25  		IngressTrait: &vzapi.IngressTrait{
    26  			ObjectMeta: metav1.ObjectMeta{
    27  				Name:      "my-trait",
    28  				Namespace: "mynamespace",
    29  			},
    30  			Spec: vzapi.IngressTraitSpec{
    31  				Rules: []vzapi.IngressRule{
    32  					{
    33  						Paths: []vzapi.IngressPath{
    34  							{Path: "/api/v1", PathType: "prefix"},
    35  						},
    36  					},
    37  				},
    38  			},
    39  		},
    40  		Helidonworkload: &unstructured.Unstructured{
    41  			Object: map[string]interface{}{
    42  				"apiVersion": "oam.verrazzano.io/v1alpha1",
    43  				"kind":       "VerrazzanoHelidonWorkload",
    44  				"metadata": map[string]interface{}{
    45  					"name": "hello-helidon-workload",
    46  					"labels": map[string]interface{}{
    47  						"app":     "hello-helidon",
    48  						"version": "v1",
    49  					},
    50  				},
    51  				"spec": map[string]interface{}{
    52  					"deploymentTemplate": map[string]interface{}{
    53  						"metadata": map[string]interface{}{
    54  							"name": "hello-helidon-deployment",
    55  						},
    56  						"podSpec": map[string]interface{}{
    57  							"containers": []interface{}{
    58  								map[string]interface{}{
    59  									"name":  "hello-helidon-container",
    60  									"image": "ghcr.io/verrazzano/example-helidon-greet-app-v1:1.0.0-1-20230126194830-31cd41f",
    61  									"ports": []interface{}{
    62  										map[string]interface{}{
    63  											"containerPort": int64(8080),
    64  											"name":          "http",
    65  										},
    66  									},
    67  								},
    68  							},
    69  						},
    70  					},
    71  				},
    72  			},
    73  		},
    74  	}
    75  
    76  	gateway := &vsapi.Gateway{}
    77  	allHostsForTrait := []string{"example.com"}
    78  	ingress := k8net.Ingress{
    79  		ObjectMeta: metav1.ObjectMeta{
    80  			Name:      "verrazzano-ingress",
    81  			Namespace: "verrazzano-system",
    82  			Annotations: map[string]string{
    83  				"external-dns.alpha.kubernetes.io/target": "test.nip.io",
    84  			},
    85  		},
    86  	}
    87  	cli := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(&ingress).Build()
    88  	// Call the function with the sample inputs
    89  	virtualServices, _, _, err := CreateIngressChildResourcesFromWorkload(cli, conversionComponent, gateway, allHostsForTrait)
    90  
    91  	// Verify the output
    92  	assert.NoError(t, err, "Expected no error")
    93  	assert.NotNil(t, virtualServices, "Expected non-nil virtualServices")
    94  	// Check the length of the slices
    95  	assert.NotEmpty(t, virtualServices, "Expected non-empty virtualServices")
    96  
    97  }