github.com/zntrio/harp/v2@v2.0.9/pkg/template/cmdutil/values_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 cmdutil
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	fuzz "github.com/google/gofuzz"
    25  )
    26  
    27  func TestMergeValues(t *testing.T) {
    28  	nestedMap := map[string]interface{}{
    29  		"foo": "bar",
    30  		"baz": map[string]string{
    31  			"cool": "stuff",
    32  		},
    33  	}
    34  	anotherNestedMap := map[string]interface{}{
    35  		"foo": "bar",
    36  		"baz": map[string]string{
    37  			"cool":    "things",
    38  			"awesome": "stuff",
    39  		},
    40  	}
    41  	flatMap := map[string]interface{}{
    42  		"foo": "bar",
    43  		"baz": "stuff",
    44  	}
    45  	anotherFlatMap := map[string]interface{}{
    46  		"testing": "fun",
    47  	}
    48  
    49  	testMap := mergeMaps(flatMap, nestedMap)
    50  	equal := reflect.DeepEqual(testMap, nestedMap)
    51  	if !equal {
    52  		t.Errorf("Expected a nested map to overwrite a flat value. Expected: %v, got %v", nestedMap, testMap)
    53  	}
    54  
    55  	testMap = mergeMaps(nestedMap, flatMap)
    56  	equal = reflect.DeepEqual(testMap, flatMap)
    57  	if !equal {
    58  		t.Errorf("Expected a flat value to overwrite a map. Expected: %v, got %v", flatMap, testMap)
    59  	}
    60  
    61  	testMap = mergeMaps(nestedMap, anotherNestedMap)
    62  	equal = reflect.DeepEqual(testMap, anotherNestedMap)
    63  	if !equal {
    64  		t.Errorf("Expected a nested map to overwrite another nested map. Expected: %v, got %v", anotherNestedMap, testMap)
    65  	}
    66  
    67  	testMap = mergeMaps(anotherFlatMap, anotherNestedMap)
    68  	expectedMap := map[string]interface{}{
    69  		"testing": "fun",
    70  		"foo":     "bar",
    71  		"baz": map[string]string{
    72  			"cool":    "things",
    73  			"awesome": "stuff",
    74  		},
    75  	}
    76  	equal = reflect.DeepEqual(testMap, expectedMap)
    77  	if !equal {
    78  		t.Errorf("Expected a map with different keys to merge properly with another map. Expected: %v, got %v", expectedMap, testMap)
    79  	}
    80  }
    81  
    82  func Test_MergeValues_Fuzz(t *testing.T) {
    83  	// Making sure the function never panics
    84  	for i := 0; i < 50; i++ {
    85  		f := fuzz.New()
    86  
    87  		// Prepare arguments
    88  		var (
    89  			opts ValueOptions
    90  		)
    91  
    92  		f.Fuzz(&opts.FileValues)
    93  		f.Fuzz(&opts.StringValues)
    94  		f.Fuzz(&opts.ValueFiles)
    95  		f.Fuzz(&opts.Values)
    96  
    97  		// Execute
    98  		opts.MergeValues()
    99  	}
   100  }