vitess.io/vitess@v0.16.2/go/vt/mysqlctl/mycnf_test.go (about)

     1  /*
     2  Copyright 2019 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 mysqlctl
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/vt/dbconfigs"
    26  	"vitess.io/vitess/go/vt/servenv"
    27  )
    28  
    29  var MycnfPath = "/tmp/my.cnf"
    30  
    31  func TestMycnf(t *testing.T) {
    32  	uid := uint32(11111)
    33  	cnf := NewMycnf(uid, 6802)
    34  	myTemplateSource := new(bytes.Buffer)
    35  	myTemplateSource.WriteString("[mysqld]\n")
    36  	// Assigning ServerID to be different from tablet UID to make sure that there are no
    37  	// assumptions in the code that those IDs are the same.
    38  	cnf.ServerID = 22222
    39  	f, _ := os.ReadFile("../../../config/mycnf/default.cnf")
    40  	myTemplateSource.Write(f)
    41  	data, err := cnf.makeMycnf(myTemplateSource.String())
    42  	if err != nil {
    43  		t.Errorf("err: %v", err)
    44  	} else {
    45  		t.Logf("data: %v", data)
    46  	}
    47  	err = os.WriteFile(MycnfPath, []byte(data), 0666)
    48  	if err != nil {
    49  		t.Errorf("failed creating my.cnf %v", err)
    50  	}
    51  	_, err = os.ReadFile(MycnfPath)
    52  	if err != nil {
    53  		t.Errorf("failed reading, err %v", err)
    54  		return
    55  	}
    56  	mycnf := NewMycnf(uid, 0)
    57  	mycnf.Path = MycnfPath
    58  	mycnf, err = ReadMycnf(mycnf)
    59  	if err != nil {
    60  		t.Errorf("failed reading, err %v", err)
    61  	} else {
    62  		t.Logf("socket file %v", mycnf.SocketFile)
    63  	}
    64  	// Tablet UID should be 11111, which determines tablet/data dir.
    65  	if got, want := mycnf.DataDir, "/vt_0000011111/"; !strings.Contains(got, want) {
    66  		t.Errorf("mycnf.DataDir = %v, want *%v*", got, want)
    67  	}
    68  	// MySQL server-id should be 22222, different from Tablet UID.
    69  	if got, want := mycnf.ServerID, uint32(22222); got != want {
    70  		t.Errorf("mycnf.ServerID = %v, want %v", got, want)
    71  	}
    72  }
    73  
    74  // Run this test if any changes are made to hook handling / make_mycnf hook
    75  // other tests fail if we keep the hook around
    76  // 1. ln -snf $VTROOT/test/vthook-make_mycnf $VTROOT/vthook/make_mycnf
    77  // 2. Remove "No" prefix from func name
    78  // 3. go test
    79  // 4. \rm $VTROOT/vthook/make_mycnf
    80  // 5. Add No Prefix back
    81  
    82  // nolint
    83  func NoTestMycnfHook(t *testing.T) {
    84  	uid := uint32(11111)
    85  	cnf := NewMycnf(uid, 6802)
    86  	// Assigning ServerID to be different from tablet UID to make sure that there are no
    87  	// assumptions in the code that those IDs are the same.
    88  	cnf.ServerID = 22222
    89  
    90  	// expect these in the output my.cnf
    91  	os.Setenv("KEYSPACE", "test-messagedb")
    92  	os.Setenv("SHARD", "0")
    93  	os.Setenv("TABLET_TYPE", "PRIMARY")
    94  	os.Setenv("TABLET_ID", "11111")
    95  	os.Setenv("TABLET_DIR", TabletDir(uid))
    96  	os.Setenv("MYSQL_PORT", "15306")
    97  	// this is not being passed, so it should be nil
    98  	os.Setenv("MY_VAR", "myvalue")
    99  
   100  	dbconfigs.GlobalDBConfigs.InitWithSocket(cnf.SocketFile)
   101  	mysqld := NewMysqld(&dbconfigs.GlobalDBConfigs)
   102  	servenv.OnClose(mysqld.Close)
   103  
   104  	err := mysqld.InitConfig(cnf)
   105  	if err != nil {
   106  		t.Errorf("err: %v", err)
   107  	}
   108  	_, err = os.ReadFile(cnf.Path)
   109  	if err != nil {
   110  		t.Errorf("failed reading, err %v", err)
   111  		return
   112  	}
   113  	mycnf := NewMycnf(uid, 0)
   114  	mycnf.Path = cnf.Path
   115  	mycnf, err = ReadMycnf(mycnf)
   116  	if err != nil {
   117  		t.Errorf("failed reading, err %v", err)
   118  	} else {
   119  		t.Logf("socket file %v", mycnf.SocketFile)
   120  	}
   121  	// Tablet UID should be 11111, which determines tablet/data dir.
   122  	if got, want := mycnf.DataDir, "/vt_0000011111/"; !strings.Contains(got, want) {
   123  		t.Errorf("mycnf.DataDir = %v, want *%v*", got, want)
   124  	}
   125  	// MySQL server-id should be 22222, different from Tablet UID.
   126  	if got, want := mycnf.ServerID, uint32(22222); got != want {
   127  		t.Errorf("mycnf.ServerID = %v, want %v", got, want)
   128  	}
   129  	// check that the env variables we set were passed correctly to the hook
   130  	if got, want := mycnf.lookup("KEYSPACE"), "test-messagedb"; got != want {
   131  		t.Errorf("Error passing env %v, got %v, want %v", "KEYSPACE", got, want)
   132  	}
   133  	if got, want := mycnf.lookup("SHARD"), "0"; got != want {
   134  		t.Errorf("Error passing env %v, got %v, want %v", "SHARD", got, want)
   135  	}
   136  	if got, want := mycnf.lookup("TABLET_TYPE"), "PRIMARY"; got != want {
   137  		t.Errorf("Error passing env %v, got %v, want %v", "TABLET_TYPE", got, want)
   138  	}
   139  	if got, want := mycnf.lookup("TABLET_ID"), "11111"; got != want {
   140  		t.Errorf("Error passing env %v, got %v, want %v", "TABLET_ID", got, want)
   141  	}
   142  	if got, want := mycnf.lookup("TABLET_DIR"), "/vt_0000011111"; !strings.Contains(got, want) {
   143  		t.Errorf("Error passing env %v, got %v, want %v", "TABLET_DIR", got, want)
   144  	}
   145  	if got, want := mycnf.lookup("MYSQL_PORT"), "15306"; got != want {
   146  		t.Errorf("Error passing env %v, got %v, want %v", "MYSQL_PORT", got, want)
   147  	}
   148  	if got := mycnf.lookup("MY_VAR"); got != "" {
   149  		t.Errorf("Unexpected env %v set to %v", "MY_VAR", got)
   150  	}
   151  }