vitess.io/vitess@v0.16.2/go/vt/wrangler/testlib/version_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 testlib
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	"vitess.io/vitess/go/vt/discovery"
    28  	"vitess.io/vitess/go/vt/topo/topoproto"
    29  
    30  	"vitess.io/vitess/go/vt/logutil"
    31  	"vitess.io/vitess/go/vt/topo/memorytopo"
    32  	"vitess.io/vitess/go/vt/vttablet/tmclient"
    33  	"vitess.io/vitess/go/vt/wrangler"
    34  
    35  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    36  )
    37  
    38  func expvarHandler(gitRev *string) func(http.ResponseWriter, *http.Request) {
    39  	return func(w http.ResponseWriter, r *http.Request) {
    40  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
    41  
    42  		var vars struct {
    43  			BuildHost      string
    44  			BuildUser      string
    45  			BuildTimestamp int64
    46  			BuildGitRev    string
    47  		}
    48  		vars.BuildHost = "fake host"
    49  		vars.BuildUser = "fake user"
    50  		vars.BuildTimestamp = 123
    51  		vars.BuildGitRev = *gitRev
    52  		result, err := json.Marshal(&vars)
    53  		if err != nil {
    54  			http.Error(w, fmt.Sprintf("cannot marshal json: %s", err), http.StatusInternalServerError)
    55  			return
    56  		}
    57  		fmt.Fprintf(w, string(result)+"\n")
    58  	}
    59  }
    60  
    61  func TestVersion(t *testing.T) {
    62  	delay := discovery.GetTabletPickerRetryDelay()
    63  	defer func() {
    64  		discovery.SetTabletPickerRetryDelay(delay)
    65  	}()
    66  	discovery.SetTabletPickerRetryDelay(5 * time.Millisecond)
    67  
    68  	// We need to run this test with the /debug/vars version of the
    69  	// plugin.
    70  	wrangler.ResetDebugVarsGetVersion()
    71  
    72  	// Initialize our environment
    73  	ts := memorytopo.NewServer("cell1", "cell2")
    74  	wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
    75  	vp := NewVtctlPipe(t, ts)
    76  	defer vp.Close()
    77  
    78  	// couple tablets is enough
    79  	sourcePrimary := NewFakeTablet(t, wr, "cell1", 10, topodatapb.TabletType_PRIMARY, nil,
    80  		TabletKeyspaceShard(t, "source", "0"),
    81  		StartHTTPServer())
    82  	sourceReplica := NewFakeTablet(t, wr, "cell1", 11, topodatapb.TabletType_REPLICA, nil,
    83  		TabletKeyspaceShard(t, "source", "0"),
    84  		StartHTTPServer())
    85  
    86  	// sourcePrimary loop
    87  	sourcePrimaryGitRev := "fake git rev"
    88  	sourcePrimary.StartActionLoop(t, wr)
    89  	sourcePrimary.HTTPServer.Handler.(*http.ServeMux).HandleFunc("/debug/vars", expvarHandler(&sourcePrimaryGitRev))
    90  	defer sourcePrimary.StopActionLoop(t)
    91  
    92  	// sourceReplica loop
    93  	sourceReplicaGitRev := "fake git rev"
    94  	sourceReplica.FakeMysqlDaemon.SetReplicationSourceInputs = append(sourceReplica.FakeMysqlDaemon.SetReplicationSourceInputs, topoproto.MysqlAddr(sourcePrimary.Tablet))
    95  	sourceReplica.FakeMysqlDaemon.ExpectedExecuteSuperQueryList = []string{
    96  		// These 4 statements come from tablet startup
    97  		"STOP SLAVE",
    98  		"RESET SLAVE ALL",
    99  		"FAKE SET MASTER",
   100  		"START SLAVE",
   101  	}
   102  	sourceReplica.StartActionLoop(t, wr)
   103  	sourceReplica.HTTPServer.Handler.(*http.ServeMux).HandleFunc("/debug/vars", expvarHandler(&sourceReplicaGitRev))
   104  	defer sourceReplica.StopActionLoop(t)
   105  
   106  	// test when versions are the same
   107  	sourceReplicaGitRev = "fake git rev"
   108  	if err := vp.Run([]string{"ValidateVersionKeyspace", sourcePrimary.Tablet.Keyspace}); err != nil {
   109  		t.Fatalf("ValidateVersionKeyspace(same) failed: %v", err)
   110  	}
   111  
   112  	// test when versions are different
   113  	sourceReplicaGitRev = "different fake git rev"
   114  	err := vp.Run([]string{"ValidateVersionKeyspace", sourcePrimary.Tablet.Keyspace})
   115  	fmt.Printf("ERROR %v", err)
   116  	if err == nil || !strings.Contains(err.Error(), "is different than replica") {
   117  		t.Fatalf("ValidateVersionKeyspace(different) returned an unexpected error: %v", err)
   118  	}
   119  }