vitess.io/vitess@v0.16.2/go/vt/mysqlctl/xtrabackupengine_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  	"io"
    22  	"math/rand"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/vt/logutil"
    26  )
    27  
    28  func TestFindReplicationPosition(t *testing.T) {
    29  	input := `MySQL binlog position: filename 'vt-0476396352-bin.000005', position '310088991', GTID of the last change '145e508e-ae54-11e9-8ce6-46824dd1815e:1-3,
    30  	1e51f8be-ae54-11e9-a7c6-4280a041109b:1-3,
    31  	47b59de1-b368-11e9-b48b-624401d35560:1-152981,
    32  	557def0a-b368-11e9-84ed-f6fffd91cc57:1-3,
    33  	599ef589-ae55-11e9-9688-ca1f44501925:1-14857169,
    34  	b9ce485d-b36b-11e9-9b17-2a6e0a6011f4:1-371262'
    35  	MySQL slave binlog position: master host '10.128.0.43', purge list '145e508e-ae54-11e9-8ce6-46824dd1815e:1-3, 1e51f8be-ae54-11e9-a7c6-4280a041109b:1-3, 47b59de1-b368-11e9-b48b-624401d35560:1-152981, 557def0a-b368-11e9-84ed-f6fffd91cc57:1-3, 599ef589-ae55-11e9-9688-ca1f44501925:1-14857169, b9ce485d-b36b-11e9-9b17-2a6e0a6011f4:1-371262', channel name: ''
    36  	
    37  	190809 00:15:44 [00] Streaming <STDOUT>
    38  	190809 00:15:44 [00]        ...done
    39  	190809 00:15:44 [00] Streaming <STDOUT>
    40  	190809 00:15:44 [00]        ...done
    41  	xtrabackup: Transaction log of lsn (405344842034) to (406364859653) was copied.
    42  	190809 00:16:14 completed OK!`
    43  	want := "145e508e-ae54-11e9-8ce6-46824dd1815e:1-3,1e51f8be-ae54-11e9-a7c6-4280a041109b:1-3,47b59de1-b368-11e9-b48b-624401d35560:1-152981,557def0a-b368-11e9-84ed-f6fffd91cc57:1-3,599ef589-ae55-11e9-9688-ca1f44501925:1-14857169,b9ce485d-b36b-11e9-9b17-2a6e0a6011f4:1-371262"
    44  
    45  	pos, err := findReplicationPosition(input, "MySQL56", logutil.NewConsoleLogger())
    46  	if err != nil {
    47  		t.Fatalf("findReplicationPosition error: %v", err)
    48  	}
    49  	if got := pos.String(); got != want {
    50  		t.Errorf("findReplicationPosition() = %v; want %v", got, want)
    51  	}
    52  }
    53  
    54  func TestFindReplicationPositionNoMatch(t *testing.T) {
    55  	// Make sure failure to find a match triggers an error.
    56  	input := `nothing`
    57  
    58  	_, err := findReplicationPosition(input, "MySQL56", logutil.NewConsoleLogger())
    59  	if err == nil {
    60  		t.Fatalf("expected error from findReplicationPosition but got nil")
    61  	}
    62  }
    63  
    64  func TestFindReplicationPositionEmptyMatch(t *testing.T) {
    65  	// Make sure failure to find a match triggers an error.
    66  	input := `GTID of the last change '
    67  	
    68  	'`
    69  
    70  	_, err := findReplicationPosition(input, "MySQL56", logutil.NewConsoleLogger())
    71  	if err == nil {
    72  		t.Fatalf("expected error from findReplicationPosition but got nil")
    73  	}
    74  }
    75  
    76  func TestStripeRoundTrip(t *testing.T) {
    77  	// Generate some deterministic input data.
    78  	dataSize := int64(1000000)
    79  	input := make([]byte, dataSize)
    80  	rng := rand.New(rand.NewSource(1))
    81  	rng.Read(input)
    82  
    83  	test := func(blockSize int64, stripes int) {
    84  		// Write it out striped across some buffers.
    85  		buffers := make([]bytes.Buffer, stripes)
    86  		readers := []io.Reader{}
    87  		writers := []io.Writer{}
    88  		for i := range buffers {
    89  			readers = append(readers, &buffers[i])
    90  			writers = append(writers, &buffers[i])
    91  		}
    92  		copyToStripes(writers, bytes.NewReader(input), blockSize)
    93  
    94  		// Read it back and merge.
    95  		outBuf := &bytes.Buffer{}
    96  		written, err := io.Copy(outBuf, stripeReader(readers, blockSize))
    97  		if err != nil {
    98  			t.Errorf("dataSize=%d, blockSize=%d, stripes=%d; copy error: %v", dataSize, blockSize, stripes, err)
    99  		}
   100  		if written != dataSize {
   101  			t.Errorf("dataSize=%d, blockSize=%d, stripes=%d; copy error: wrote %d total bytes instead of dataSize", dataSize, blockSize, stripes, written)
   102  		}
   103  		output := outBuf.Bytes()
   104  		if !bytes.Equal(input, output) {
   105  			t.Errorf("output bytes are not the same as input")
   106  		}
   107  	}
   108  
   109  	// Test block size that evenly divides data size.
   110  	test(1000, 10)
   111  	// Test block size that doesn't evenly divide data size.
   112  	test(3000, 10)
   113  	// Test stripe count that doesn't evenly divide data size.
   114  	test(1000, 30)
   115  	// Test block size and stripe count that don't evenly divide data size.
   116  	test(6000, 7)
   117  }