github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/convert/yaml_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package convert
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    26  )
    27  
    28  func Test_PBtoYAML(t *testing.T) {
    29  	spec := &bundlev1.Patch{
    30  		ApiVersion: "harp.elastic.co/v1",
    31  		Kind:       "BundlePatch",
    32  		Meta: &bundlev1.PatchMeta{
    33  			Name: "test-patch",
    34  		},
    35  		Spec: &bundlev1.PatchSpec{
    36  			Rules: []*bundlev1.PatchRule{
    37  				{
    38  					Package:  &bundlev1.PatchPackage{},
    39  					Selector: &bundlev1.PatchSelector{},
    40  				},
    41  			},
    42  		},
    43  	}
    44  
    45  	expectedOutput := []byte("apiVersion: harp.elastic.co/v1\nkind: BundlePatch\nmeta:\n  name: test-patch\nspec:\n  rules:\n  - package: {}\n    selector: {}\n")
    46  
    47  	out, err := PBtoYAML(spec)
    48  	if err != nil {
    49  		t.Error(err)
    50  	}
    51  
    52  	if report := cmp.Diff(string(out), string(expectedOutput)); report != "" {
    53  		t.Errorf("unexpected conversion output:\n%v", report)
    54  	}
    55  }
    56  
    57  func Test_convertMapStringInterface(t *testing.T) {
    58  	type args struct {
    59  		val interface{}
    60  	}
    61  	tests := []struct {
    62  		name    string
    63  		args    args
    64  		want    interface{}
    65  		wantErr bool
    66  	}{
    67  		{
    68  			name:    "nil",
    69  			wantErr: false,
    70  		},
    71  		{
    72  			name: "map[interface{}]interface{}",
    73  			args: args{
    74  				val: map[interface{}]interface{}{
    75  					"abc":  1234,
    76  					"true": 12.56,
    77  				},
    78  			},
    79  			wantErr: false,
    80  			want: map[string]interface{}{
    81  				"abc":  1234,
    82  				"true": 12.56,
    83  			},
    84  		},
    85  		{
    86  			name: "[]interface{}",
    87  			args: args{
    88  				val: []interface{}{
    89  					"abc", "true",
    90  				},
    91  			},
    92  			wantErr: false,
    93  			want:    []interface{}{"abc", "true"},
    94  		},
    95  	}
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			got, err := convertMapStringInterface(tt.args.val)
    99  			if (err != nil) != tt.wantErr {
   100  				t.Errorf("convertMapStringInterface() error = %v, wantErr %v", err, tt.wantErr)
   101  				return
   102  			}
   103  			if !reflect.DeepEqual(got, tt.want) {
   104  				t.Errorf("convertMapStringInterface() = %v, want %v", got, tt.want)
   105  			}
   106  		})
   107  	}
   108  }