vitess.io/vitess@v0.16.2/go/vt/vtadmin/cluster/flags_test.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cluster
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestMergeFlagsByImpl(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	var NilMap map[string]map[string]string
    29  
    30  	tests := []struct {
    31  		name     string
    32  		base     map[string]map[string]string
    33  		in       map[string]map[string]string
    34  		expected map[string]map[string]string
    35  	}{
    36  		{
    37  			name:     "nil",
    38  			base:     nil,
    39  			in:       nil,
    40  			expected: map[string]map[string]string{},
    41  		},
    42  		{
    43  			name:     "wrapped nil",
    44  			base:     NilMap,
    45  			in:       NilMap,
    46  			expected: map[string]map[string]string{},
    47  		},
    48  		{
    49  			name: "all overrides",
    50  			base: nil,
    51  			in: map[string]map[string]string{
    52  				"consul": {
    53  					"flag1": "value1",
    54  				},
    55  			},
    56  			expected: map[string]map[string]string{
    57  				"consul": {
    58  					"flag1": "value1",
    59  				},
    60  			},
    61  		},
    62  		{
    63  			name: "all defaults",
    64  			base: map[string]map[string]string{
    65  				"consul": {
    66  					"flag1": "value1",
    67  				},
    68  			},
    69  			in: nil,
    70  			expected: map[string]map[string]string{
    71  				"consul": {
    72  					"flag1": "value1",
    73  				},
    74  			},
    75  		},
    76  		{
    77  			name: "mixed",
    78  			base: map[string]map[string]string{
    79  				"consul": {
    80  					"flag1": "value1",
    81  					"flag2": "value2",
    82  				},
    83  				"other": {},
    84  			},
    85  			in: map[string]map[string]string{
    86  				"consul": {
    87  					"flag1": "othervalue",
    88  				},
    89  			},
    90  			expected: map[string]map[string]string{
    91  				"consul": {
    92  					"flag1": "othervalue",
    93  					"flag2": "value2",
    94  				},
    95  				"other": {},
    96  			},
    97  		},
    98  	}
    99  
   100  	for _, tt := range tests {
   101  		tt := tt
   102  
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			t.Parallel()
   105  
   106  			flags := FlagsByImpl(tt.base)
   107  			flags.Merge(tt.in)
   108  			assert.Equal(t, FlagsByImpl(tt.expected), flags)
   109  		})
   110  	}
   111  }