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

     1  /*
     2  Copyright 2021 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 mysqlctlproto
    18  
    19  import (
    20  	"path"
    21  	"testing"
    22  	"time"
    23  
    24  	"vitess.io/vitess/go/protoutil"
    25  	"vitess.io/vitess/go/test/utils"
    26  	"vitess.io/vitess/go/vt/mysqlctl/backupstorage"
    27  
    28  	mysqlctlpb "vitess.io/vitess/go/vt/proto/mysqlctl"
    29  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    30  )
    31  
    32  type backupHandle struct {
    33  	backupstorage.BackupHandle
    34  	name      string
    35  	directory string
    36  }
    37  
    38  func (bh *backupHandle) Name() string      { return bh.name }
    39  func (bh *backupHandle) Directory() string { return bh.directory }
    40  func (bh *backupHandle) testname() string  { return path.Join(bh.directory, bh.name) }
    41  
    42  func TestBackupHandleToProto(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	now := time.Date(2021, time.June, 12, 15, 4, 5, 0, time.UTC)
    46  	tests := []struct {
    47  		bh   *backupHandle
    48  		want *mysqlctlpb.BackupInfo
    49  	}{
    50  		{
    51  			bh: &backupHandle{
    52  				name:      "2021-06-12.150405.zone1-100",
    53  				directory: "foo",
    54  			},
    55  			want: &mysqlctlpb.BackupInfo{
    56  				Name:      "2021-06-12.150405.zone1-100",
    57  				Directory: "foo",
    58  				TabletAlias: &topodatapb.TabletAlias{
    59  					Cell: "zone1",
    60  					Uid:  100,
    61  				},
    62  				Time: protoutil.TimeToProto(now),
    63  			},
    64  		},
    65  		{
    66  			bh: &backupHandle{
    67  				name:      "bar",
    68  				directory: "foo",
    69  			},
    70  			want: &mysqlctlpb.BackupInfo{
    71  				Name:      "bar",
    72  				Directory: "foo",
    73  			},
    74  		},
    75  		{
    76  			bh: &backupHandle{
    77  				name:      "invalid.time.zone1-100",
    78  				directory: "foo",
    79  			},
    80  			want: &mysqlctlpb.BackupInfo{
    81  				Name:      "invalid.time.zone1-100",
    82  				Directory: "foo",
    83  				TabletAlias: &topodatapb.TabletAlias{
    84  					Cell: "zone1",
    85  					Uid:  100,
    86  				},
    87  				Time: nil,
    88  			},
    89  		},
    90  		{
    91  			bh: &backupHandle{
    92  				name:      "2021-06-12.150405.not_an_alias",
    93  				directory: "foo",
    94  			},
    95  			want: &mysqlctlpb.BackupInfo{
    96  				Name:        "2021-06-12.150405.not_an_alias",
    97  				Directory:   "foo",
    98  				TabletAlias: nil,
    99  				Time:        protoutil.TimeToProto(now),
   100  			},
   101  		},
   102  	}
   103  
   104  	for _, tt := range tests {
   105  		tt := tt
   106  
   107  		t.Run(tt.bh.testname(), func(t *testing.T) {
   108  			t.Parallel()
   109  
   110  			got := BackupHandleToProto(tt.bh)
   111  			utils.MustMatch(t, tt.want, got)
   112  		})
   113  	}
   114  }