github.com/verrazzano/verrazzano@v1.7.0/pkg/k8sutil/yaml_util_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 k8sutil
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"gopkg.in/yaml.v3"
    12  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    13  )
    14  
    15  func TestMarshal(t *testing.T) {
    16  	obj1 := makeUnstructured("obj1")
    17  	obj2 := makeUnstructured("obj2")
    18  	objs := []unstructured.Unstructured{
    19  		{Object: obj1},
    20  		{Object: obj2},
    21  	}
    22  	obj1Bytes, err := yaml.Marshal(obj1)
    23  	assert.NoError(t, err)
    24  	obj2Bytes, err := yaml.Marshal(obj2)
    25  	assert.NoError(t, err)
    26  	marshalled, err := Marshal(objs)
    27  	assert.NoError(t, err)
    28  	marshalledStr := string(marshalled)
    29  	fmt.Println(marshalledStr)
    30  	assert.Contains(t, marshalledStr, fmt.Sprintf("%s\n%s\n%s", string(obj1Bytes), sep, string(obj2Bytes)))
    31  }
    32  
    33  func makeUnstructured(objname string) map[string]interface{} {
    34  	return map[string]interface{}{
    35  		"apiVersion": "myapi/v1",
    36  		"kind":       "SomeObject",
    37  		"metadata": map[string]interface{}{
    38  			"name":      objname,
    39  			"namespace": "ns1",
    40  		},
    41  	}
    42  }