vitess.io/vitess@v0.16.2/go/vt/vtadmin/cluster/config_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 TestMergeConfig(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	tests := []struct {
    29  		name     string
    30  		base     Config
    31  		override Config
    32  		expected Config
    33  	}{
    34  		{
    35  			name: "no flags",
    36  			base: Config{
    37  				ID:   "c1",
    38  				Name: "cluster1",
    39  			},
    40  			override: Config{
    41  				DiscoveryImpl: "consul",
    42  			},
    43  			expected: Config{
    44  				ID:                   "c1",
    45  				Name:                 "cluster1",
    46  				DiscoveryImpl:        "consul",
    47  				DiscoveryFlagsByImpl: FlagsByImpl{},
    48  				VtSQLFlags:           map[string]string{},
    49  				VtctldFlags:          map[string]string{},
    50  			},
    51  		},
    52  		{
    53  			name: "merging discovery flags",
    54  			base: Config{
    55  				ID:   "c1",
    56  				Name: "cluster1",
    57  				DiscoveryFlagsByImpl: map[string]map[string]string{
    58  					"consul": {
    59  						"key1": "val1",
    60  					},
    61  					"zk": {
    62  						"foo": "bar",
    63  					},
    64  				},
    65  				VtSQLFlags: map[string]string{},
    66  			},
    67  			override: Config{
    68  				DiscoveryFlagsByImpl: map[string]map[string]string{
    69  					"zk": {
    70  						"foo": "baz",
    71  					},
    72  				},
    73  			},
    74  			expected: Config{
    75  				ID:   "c1",
    76  				Name: "cluster1",
    77  				DiscoveryFlagsByImpl: map[string]map[string]string{
    78  					"consul": {
    79  						"key1": "val1",
    80  					},
    81  					"zk": {
    82  						"foo": "baz",
    83  					},
    84  				},
    85  				VtSQLFlags:  map[string]string{},
    86  				VtctldFlags: map[string]string{},
    87  			},
    88  		},
    89  		{
    90  			name: "merging vtsql/vtctld flags",
    91  			base: Config{
    92  				ID:   "c1",
    93  				Name: "cluster1",
    94  				VtSQLFlags: map[string]string{
    95  					"one": "one",
    96  					"two": "2",
    97  				},
    98  				VtctldFlags: map[string]string{
    99  					"a": "A",
   100  					"b": "B",
   101  				},
   102  			},
   103  			override: Config{
   104  				ID:   "c1",
   105  				Name: "cluster1",
   106  				VtSQLFlags: map[string]string{
   107  					"two":   "two",
   108  					"three": "three",
   109  				},
   110  				VtctldFlags: map[string]string{
   111  					"a": "alpha",
   112  					"c": "C",
   113  				},
   114  			},
   115  			expected: Config{
   116  				ID:                   "c1",
   117  				Name:                 "cluster1",
   118  				DiscoveryFlagsByImpl: FlagsByImpl{},
   119  				VtSQLFlags: map[string]string{
   120  					"one":   "one",
   121  					"two":   "two",
   122  					"three": "three",
   123  				},
   124  				VtctldFlags: map[string]string{
   125  					"a": "alpha",
   126  					"b": "B",
   127  					"c": "C",
   128  				},
   129  			},
   130  		},
   131  	}
   132  
   133  	for _, tt := range tests {
   134  		tt := tt
   135  
   136  		t.Run(tt.name, func(t *testing.T) {
   137  			t.Parallel()
   138  
   139  			actual := tt.base.Merge(tt.override)
   140  			assert.Equal(t, tt.expected, actual)
   141  		})
   142  	}
   143  }