vitess.io/vitess@v0.16.2/go/vt/vtadmin/cluster/file_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  	"os"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestFileConfigFromViper(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	tests := []struct {
    31  		name   string
    32  		data   string
    33  		config FileConfig
    34  		err    error
    35  	}{
    36  		{
    37  			name: "clusters.yaml",
    38  			data: `defaults:
    39      discovery: consul
    40      discovery-consul-vtctld-datacenter-tmpl: "dev-{{ .Cluster.Name }}"
    41      discovery-consul-vtctld-service-name: vtctld-svc
    42      discovery-consul-vtctld-addr-tmpl: "{{ .Hostname }}.example.com:15000"
    43      discovery-consul-vtgate-datacenter-tmpl: "dev-{{ .Cluster.Name }}"
    44      discovery-consul-vtgate-service-name: vtgate-svc
    45      discovery-consul-vtgate-pool-tag: type
    46      discovery-consul-vtgate-cell-tag: zone
    47      discovery-consul-vtgate-addr-tmpl: "{{ .Hostname }}.example.com:15999"
    48  
    49  clusters:
    50      c1:
    51          name: testcluster1
    52          discovery-consul-vtgate-datacenter-tmpl: "dev-{{ .Cluster.Name }}-test"
    53      c2:
    54          name: devcluster`,
    55  			config: FileConfig{
    56  				Defaults: Config{
    57  					DiscoveryImpl: "consul",
    58  					DiscoveryFlagsByImpl: map[string]map[string]string{
    59  						"consul": {
    60  							"vtctld-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
    61  							"vtctld-service-name":    "vtctld-svc",
    62  							"vtctld-addr-tmpl":       "{{ .Hostname }}.example.com:15000",
    63  							"vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
    64  							"vtgate-service-name":    "vtgate-svc",
    65  							"vtgate-pool-tag":        "type",
    66  							"vtgate-cell-tag":        "zone",
    67  							"vtgate-addr-tmpl":       "{{ .Hostname }}.example.com:15999",
    68  						},
    69  					},
    70  				},
    71  				Clusters: map[string]Config{
    72  					"c1": {
    73  						ID:   "c1",
    74  						Name: "testcluster1",
    75  						DiscoveryFlagsByImpl: map[string]map[string]string{
    76  							"consul": {
    77  								"vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}-test",
    78  							},
    79  						},
    80  						VtSQLFlags:  map[string]string{},
    81  						VtctldFlags: map[string]string{},
    82  					},
    83  					"c2": {
    84  						ID:                   "c2",
    85  						Name:                 "devcluster",
    86  						DiscoveryFlagsByImpl: map[string]map[string]string{},
    87  						VtSQLFlags:           map[string]string{},
    88  						VtctldFlags:          map[string]string{},
    89  					},
    90  				},
    91  			},
    92  			err: nil,
    93  		},
    94  		{
    95  			name: "clusters.json",
    96  			data: `{
    97  	"defaults": {
    98  		"discovery": "consul",
    99  		"discovery-consul-vtctld-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
   100  		"discovery-consul-vtctld-service-name": "vtctld-svc",
   101  		"discovery-consul-vtctld-addr-tmpl": "{{ .Hostname }}.example.com:15000",
   102  		"discovery-consul-vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
   103  		"discovery-consul-vtgate-service-name": "vtgate-svc",
   104  		"discovery-consul-vtgate-pool-tag": "type",
   105  		"discovery-consul-vtgate-cell-tag": "zone",
   106  		"discovery-consul-vtgate-addr-tmpl": "{{ .Hostname }}.example.com:15999"
   107  	},
   108  	"clusters": {
   109  		"c1": {
   110  			"name": "testcluster1",
   111  			"discovery-consul-vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}-test"
   112  		},
   113  		"c2": {
   114  			"name": "devcluster"
   115  		}
   116  	}
   117  }`,
   118  			config: FileConfig{
   119  				Defaults: Config{
   120  					DiscoveryImpl: "consul",
   121  					DiscoveryFlagsByImpl: map[string]map[string]string{
   122  						"consul": {
   123  							"vtctld-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
   124  							"vtctld-service-name":    "vtctld-svc",
   125  							"vtctld-addr-tmpl":       "{{ .Hostname }}.example.com:15000",
   126  							"vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
   127  							"vtgate-service-name":    "vtgate-svc",
   128  							"vtgate-pool-tag":        "type",
   129  							"vtgate-cell-tag":        "zone",
   130  							"vtgate-addr-tmpl":       "{{ .Hostname }}.example.com:15999",
   131  						},
   132  					},
   133  				},
   134  				Clusters: map[string]Config{
   135  					"c1": {
   136  						ID:   "c1",
   137  						Name: "testcluster1",
   138  						DiscoveryFlagsByImpl: map[string]map[string]string{
   139  							"consul": {
   140  								"vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}-test",
   141  							},
   142  						},
   143  						VtSQLFlags:  map[string]string{},
   144  						VtctldFlags: map[string]string{},
   145  					},
   146  					"c2": {
   147  						ID:                   "c2",
   148  						Name:                 "devcluster",
   149  						DiscoveryFlagsByImpl: map[string]map[string]string{},
   150  						VtSQLFlags:           map[string]string{},
   151  						VtctldFlags:          map[string]string{},
   152  					},
   153  				},
   154  			},
   155  			err: nil,
   156  		},
   157  		{
   158  			name: "clusters.toml",
   159  			data: `[defaults]
   160  discovery="consul"
   161  discovery-consul-vtctld-datacenter-tmpl="dev-{{ .Cluster.Name }}"
   162  discovery-consul-vtctld-service-name="vtctld-svc"
   163  discovery-consul-vtctld-addr-tmpl="{{ .Hostname }}.example.com:15000"
   164  discovery-consul-vtgate-datacenter-tmpl="dev-{{ .Cluster.Name }}"
   165  discovery-consul-vtgate-service-name="vtgate-svc"
   166  discovery-consul-vtgate-pool-tag="type"
   167  discovery-consul-vtgate-cell-tag="zone"
   168  discovery-consul-vtgate-addr-tmpl="{{ .Hostname }}.example.com:15999"
   169  
   170  [clusters]
   171  [clusters.c1]
   172  name="testcluster1"
   173  discovery-consul-vtgate-datacenter-tmpl="dev-{{ .Cluster.Name }}-test"
   174  [clusters.c2]
   175  name="devcluster"`,
   176  			config: FileConfig{
   177  				Defaults: Config{
   178  					DiscoveryImpl: "consul",
   179  					DiscoveryFlagsByImpl: map[string]map[string]string{
   180  						"consul": {
   181  							"vtctld-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
   182  							"vtctld-service-name":    "vtctld-svc",
   183  							"vtctld-addr-tmpl":       "{{ .Hostname }}.example.com:15000",
   184  							"vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}",
   185  							"vtgate-service-name":    "vtgate-svc",
   186  							"vtgate-pool-tag":        "type",
   187  							"vtgate-cell-tag":        "zone",
   188  							"vtgate-addr-tmpl":       "{{ .Hostname }}.example.com:15999",
   189  						},
   190  					},
   191  				},
   192  				Clusters: map[string]Config{
   193  					"c1": {
   194  						ID:   "c1",
   195  						Name: "testcluster1",
   196  						DiscoveryFlagsByImpl: map[string]map[string]string{
   197  							"consul": {
   198  								"vtgate-datacenter-tmpl": "dev-{{ .Cluster.Name }}-test",
   199  							},
   200  						},
   201  						VtSQLFlags:  map[string]string{},
   202  						VtctldFlags: map[string]string{},
   203  					},
   204  					"c2": {
   205  						ID:                   "c2",
   206  						Name:                 "devcluster",
   207  						DiscoveryFlagsByImpl: map[string]map[string]string{},
   208  						VtSQLFlags:           map[string]string{},
   209  						VtctldFlags:          map[string]string{},
   210  					},
   211  				},
   212  			},
   213  			err: nil,
   214  		},
   215  	}
   216  
   217  	for _, tt := range tests {
   218  		tt := tt
   219  		t.Run(tt.name, func(t *testing.T) {
   220  			t.Parallel()
   221  
   222  			f, err := os.CreateTemp(t.TempDir(), "*"+tt.name)
   223  			require.NoError(t, err)
   224  
   225  			t.Cleanup(func() { os.Remove(f.Name()) })
   226  
   227  			err = os.WriteFile(f.Name(), []byte(tt.data), 0777)
   228  			require.NoError(t, err)
   229  
   230  			fc := FileConfig{}
   231  			err = fc.Set(f.Name())
   232  			require.NoError(t, err)
   233  
   234  			assert.Equal(t, tt.config, fc)
   235  		})
   236  	}
   237  }
   238  
   239  func TestCombine(t *testing.T) {
   240  	t.Parallel()
   241  
   242  	tests := []struct {
   243  		name     string
   244  		fc       FileConfig
   245  		defaults Config
   246  		configs  map[string]Config
   247  		expected []Config
   248  	}{
   249  		{
   250  			name: "default overrides file",
   251  			fc: FileConfig{
   252  				Defaults: Config{
   253  					DiscoveryImpl: "consul",
   254  					DiscoveryFlagsByImpl: map[string]map[string]string{
   255  						"consul": {
   256  							"vtgate-datacenter-tmpl": "dev-{{ .Cluster }}",
   257  						},
   258  					},
   259  				},
   260  			},
   261  			defaults: Config{
   262  				DiscoveryImpl:        "zk",
   263  				DiscoveryFlagsByImpl: map[string]map[string]string{},
   264  			},
   265  			configs: map[string]Config{
   266  				"1": {
   267  					ID:   "1",
   268  					Name: "one",
   269  				},
   270  				"2": {
   271  					ID:   "2",
   272  					Name: "two",
   273  					DiscoveryFlagsByImpl: map[string]map[string]string{
   274  						"consul": {
   275  							"vtgate-datacenter-tmpl": "dev-{{ .Cluster }}-test",
   276  						},
   277  					},
   278  				},
   279  			},
   280  			expected: []Config{
   281  				{
   282  					ID:            "1",
   283  					Name:          "one",
   284  					DiscoveryImpl: "zk",
   285  					DiscoveryFlagsByImpl: map[string]map[string]string{
   286  						"consul": {
   287  							"vtgate-datacenter-tmpl": "dev-{{ .Cluster }}",
   288  						},
   289  					},
   290  					VtSQLFlags:  map[string]string{},
   291  					VtctldFlags: map[string]string{},
   292  				},
   293  				{
   294  					ID:            "2",
   295  					Name:          "two",
   296  					DiscoveryImpl: "zk",
   297  					DiscoveryFlagsByImpl: map[string]map[string]string{
   298  						"consul": {
   299  							"vtgate-datacenter-tmpl": "dev-{{ .Cluster }}-test",
   300  						},
   301  					},
   302  					VtSQLFlags:  map[string]string{},
   303  					VtctldFlags: map[string]string{},
   304  				},
   305  			},
   306  		},
   307  		{
   308  			name: "mixed",
   309  			fc: FileConfig{
   310  				Defaults: Config{
   311  					DiscoveryImpl: "consul",
   312  				},
   313  				Clusters: map[string]Config{
   314  					"c1": {
   315  						ID:   "c1",
   316  						Name: "cluster1",
   317  					},
   318  					"c2": {
   319  						ID:   "c2",
   320  						Name: "cluster2",
   321  					},
   322  				},
   323  			},
   324  			defaults: Config{
   325  				DiscoveryFlagsByImpl: map[string]map[string]string{
   326  					"zk": {
   327  						"flag": "val",
   328  					},
   329  				},
   330  			},
   331  			configs: map[string]Config{
   332  				"c1": {
   333  					ID:   "c1",
   334  					Name: "cluster1",
   335  				},
   336  				"c3": {
   337  					ID:   "c3",
   338  					Name: "cluster3",
   339  				},
   340  			},
   341  			expected: []Config{
   342  				{
   343  					ID:            "c1",
   344  					Name:          "cluster1",
   345  					DiscoveryImpl: "consul",
   346  					DiscoveryFlagsByImpl: map[string]map[string]string{
   347  						"zk": {
   348  							"flag": "val",
   349  						},
   350  					},
   351  					VtSQLFlags:  map[string]string{},
   352  					VtctldFlags: map[string]string{},
   353  				},
   354  				{
   355  					ID:            "c2",
   356  					Name:          "cluster2",
   357  					DiscoveryImpl: "consul",
   358  					DiscoveryFlagsByImpl: map[string]map[string]string{
   359  						"zk": {
   360  							"flag": "val",
   361  						},
   362  					},
   363  					VtSQLFlags:  map[string]string{},
   364  					VtctldFlags: map[string]string{},
   365  				},
   366  				{
   367  					ID:            "c3",
   368  					Name:          "cluster3",
   369  					DiscoveryImpl: "consul",
   370  					DiscoveryFlagsByImpl: map[string]map[string]string{
   371  						"zk": {
   372  							"flag": "val",
   373  						},
   374  					},
   375  					VtSQLFlags:  map[string]string{},
   376  					VtctldFlags: map[string]string{},
   377  				},
   378  			},
   379  		},
   380  	}
   381  
   382  	for _, tt := range tests {
   383  		tt := tt
   384  
   385  		t.Run(tt.name, func(t *testing.T) {
   386  			t.Parallel()
   387  
   388  			actual := tt.fc.Combine(tt.defaults, tt.configs)
   389  			assert.ElementsMatch(t, tt.expected, actual)
   390  		})
   391  	}
   392  }