github.com/verrazzano/verrazzano@v1.7.1/tools/oam-converter/pkg/workloads/extractWorkload_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 "encoding/json" 8 "github.com/stretchr/testify/assert" 9 vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1" 10 consts "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/constants" 11 reader "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/testdata" 12 "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/types" 13 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 14 "testing" 15 ) 16 17 func TestExtractWorkload(t *testing.T) { 18 19 compMaps := []map[string]interface{}{} 20 21 compConf, err := reader.ReadFromYAMLTemplate("testdata/template/helidon_workload.yaml") 22 if err != nil { 23 return 24 } 25 compMaps = append(compMaps, compConf) 26 appConf, err := reader.ReadFromYAMLTemplate("testdata/template/app_conf.yaml") 27 if err != nil { 28 return 29 } 30 31 spec, found, err := unstructured.NestedMap(appConf, "spec") 32 if !found || err != nil { 33 t.Fatalf("spec doesn't exist or not in the specified type") 34 } 35 36 appComponents, found, err := unstructured.NestedSlice(spec, "components") 37 38 if !found || err != nil { 39 t.Fatalf("app components doesn't exist or not in the specified type") 40 } 41 ingressData := make(map[string]interface{}) 42 for _, component := range appComponents { 43 componentMap := component.(map[string]interface{}) 44 componentTraits, ok := componentMap[consts.YamlTraits].([]interface{}) 45 if ok && len(componentTraits) > 0 { 46 for _, trait := range componentTraits { 47 traitMap := trait.(map[string]interface{}) 48 ingressData, found, err = unstructured.NestedMap(traitMap, "trait") 49 if !found || err != nil { 50 t.Fatalf("ingress trait doesn't exist or not in the specified type") 51 52 } 53 54 } 55 } 56 } 57 jsonData, err := json.Marshal(ingressData) 58 if err != nil { 59 t.Fatalf("error in marshaling data %v", err) 60 } 61 ingressTrait := &vzapi.IngressTrait{} 62 err = json.Unmarshal(jsonData, &ingressTrait) 63 if err != nil { 64 t.Fatalf("error in unmarshalling data %v", err) 65 } 66 // Test data: conversionComponents array 67 conversionComponents := []*types.ConversionComponents{ 68 { 69 AppNamespace: "hello-helidon", 70 AppName: "hello-helidon", 71 ComponentName: "hello-helidon-component", 72 IngressTrait: ingressTrait, 73 }, 74 } 75 76 // Call the function to test 77 result, err := ExtractWorkload(compMaps, conversionComponents) 78 79 // Assertions 80 assert.NoError(t, err) 81 82 assert.NotNil(t, result[0].Helidonworkload, "Helidon Workload should not be nil") 83 84 }