vitess.io/vitess@v0.16.2/go/vt/vtadmin/vtsql/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 vtsql
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  	"google.golang.org/grpc/backoff"
    30  
    31  	"vitess.io/vitess/go/vt/grpcclient"
    32  	"vitess.io/vitess/go/vt/vtadmin/cluster/resolver"
    33  
    34  	vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
    35  )
    36  
    37  func TestConfigParse(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	cfg := Config{}
    41  
    42  	// This asserts we do not attempt to load a credentialsFlag via its Set func
    43  	// if it's not specified in the args slice.
    44  	err := cfg.Parse([]string{})
    45  	assert.NoError(t, err)
    46  
    47  	t.Run("", func(t *testing.T) {
    48  		t.Parallel()
    49  
    50  		f, err := os.CreateTemp("", "vtsql-config-test-testcluster-*") // testcluster is going to appear in the template
    51  		require.NoError(t, err)
    52  
    53  		_, err = f.Write([]byte(`{
    54  	"Username": "vtadmin",
    55  	"Password": "hunter2"
    56  }`))
    57  		require.NoError(t, err)
    58  
    59  		path := f.Name()
    60  		defer os.Remove(path)
    61  		f.Close()
    62  
    63  		dir := filepath.Dir(path)
    64  		baseParts := strings.Split(filepath.Base(path), "-")
    65  		tmplParts := append(baseParts[:3], "{{ .Cluster.Name }}", baseParts[4])
    66  
    67  		cfg := &Config{
    68  			Cluster: &vtadminpb.Cluster{
    69  				Name: "testcluster",
    70  			},
    71  		}
    72  
    73  		credsTmplStr := filepath.Join(dir, strings.Join(tmplParts, "-"))
    74  
    75  		args := []string{
    76  			"--discovery-tags=a:1,b:2",
    77  			"--effective-user=vt_appdebug",
    78  			"--discovery-tags=c:3",
    79  			fmt.Sprintf("--credentials-path-tmpl=%s", credsTmplStr),
    80  		}
    81  
    82  		expectedCreds := &StaticAuthCredentials{
    83  			EffectiveUser: "vt_appdebug",
    84  			StaticAuthClientCreds: &grpcclient.StaticAuthClientCreds{
    85  				Username: "vtadmin",
    86  				Password: "hunter2",
    87  			},
    88  		}
    89  		expectedTags := []string{
    90  			"a:1",
    91  			"b:2",
    92  			"c:3",
    93  		}
    94  
    95  		err = cfg.Parse(args)
    96  		assert.NoError(t, err)
    97  		assert.Equal(t, expectedTags, cfg.ResolverOptions.DiscoveryTags)
    98  		assert.Equal(t, expectedCreds, cfg.Credentials)
    99  	})
   100  
   101  	t.Run("", func(t *testing.T) {
   102  		t.Parallel()
   103  
   104  		f, err := os.CreateTemp("", "vtsql-config-test-testcluster-*") // testcluster is going to appear in the template
   105  		require.NoError(t, err)
   106  
   107  		_, err = f.Write([]byte(`{
   108  	"Username": "vtadmin",
   109  	"Password": "hunter2"
   110  }`))
   111  		require.NoError(t, err)
   112  
   113  		path := f.Name()
   114  		defer os.Remove(path)
   115  		f.Close()
   116  
   117  		dir := filepath.Dir(path)
   118  		baseParts := strings.Split(filepath.Base(path), "-")
   119  		tmplParts := append(baseParts[:3], "{{ .Cluster.Name }}", baseParts[4])
   120  
   121  		credsTmplStr := filepath.Join(dir, strings.Join(tmplParts, "-"))
   122  
   123  		args := []string{
   124  			"--discovery-tags=a:1,b:2",
   125  			"--effective-user=vt_appdebug",
   126  			"--discovery-tags=c:3",
   127  			fmt.Sprintf("--credentials-path-tmpl=%s", credsTmplStr),
   128  		}
   129  
   130  		expectedCreds := &StaticAuthCredentials{
   131  			EffectiveUser: "vt_appdebug",
   132  			StaticAuthClientCreds: &grpcclient.StaticAuthClientCreds{
   133  				Username: "vtadmin",
   134  				Password: "hunter2",
   135  			},
   136  		}
   137  		expectedTags := []string{
   138  			"a:1",
   139  			"b:2",
   140  			"c:3",
   141  		}
   142  
   143  		expected := &Config{
   144  			Cluster: &vtadminpb.Cluster{
   145  				Id:   "cid",
   146  				Name: "testcluster",
   147  			},
   148  			ResolverOptions: &resolver.Options{
   149  				DiscoveryTags:        expectedTags,
   150  				DiscoveryTimeout:     100 * time.Millisecond,
   151  				MinDiscoveryInterval: time.Second * 30,
   152  				BackoffConfig:        backoff.DefaultConfig,
   153  			},
   154  			Credentials:     expectedCreds,
   155  			CredentialsPath: path,
   156  		}
   157  
   158  		cfg, err := Parse(&vtadminpb.Cluster{Id: "cid", Name: "testcluster"}, nil, args)
   159  		assert.NoError(t, err)
   160  		assert.Equal(t, expected, cfg)
   161  	})
   162  }