github.com/matrixorigin/matrixone@v0.7.0/pkg/hakeeper/operator/steps_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Portions of this file are additionally subject to the following
    15  // copyright.
    16  //
    17  // Copyright (C) 2021 Matrix Origin.
    18  //
    19  // Modified some tests.
    20  
    21  package operator
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  
    27  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestIsFinish(t *testing.T) {
    32  	logState := pb.LogState{
    33  		Shards: map[uint64]pb.LogShardInfo{1: {
    34  			ShardID:  1,
    35  			Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"},
    36  			Epoch:    1,
    37  		}},
    38  		Stores: nil,
    39  	}
    40  
    41  	dnState := pb.DNState{}
    42  	cnState := pb.CNState{}
    43  
    44  	assert.False(t, AddLogService{Replica: Replica{UUID: "d", ShardID: 1, ReplicaID: 4}}.IsFinish(logState, dnState, cnState))
    45  	assert.True(t, AddLogService{Replica: Replica{UUID: "c", ShardID: 1, ReplicaID: 3}}.IsFinish(logState, dnState, cnState))
    46  	assert.False(t, RemoveLogService{Replica: Replica{UUID: "c", ShardID: 1, ReplicaID: 3}}.IsFinish(logState, dnState, cnState))
    47  	assert.True(t, RemoveLogService{Replica: Replica{UUID: "d", ShardID: 1, ReplicaID: 4}}.IsFinish(logState, dnState, cnState))
    48  }
    49  
    50  func TestAddLogService(t *testing.T) {
    51  	cases := []struct {
    52  		desc     string
    53  		command  AddLogService
    54  		state    pb.LogState
    55  		expected bool
    56  	}{
    57  		{
    58  			desc: "add log service completed",
    59  			command: AddLogService{
    60  				Replica: Replica{
    61  					UUID:      "a",
    62  					ShardID:   1,
    63  					ReplicaID: 1},
    64  			},
    65  			state: pb.LogState{
    66  				Shards: map[uint64]pb.LogShardInfo{
    67  					1: {ShardID: 1, Replicas: map[uint64]string{1: "a"}},
    68  				},
    69  			},
    70  			expected: true,
    71  		},
    72  		{
    73  			desc: "add log service not completed",
    74  			command: AddLogService{
    75  				Replica: Replica{
    76  					UUID:      "a",
    77  					ShardID:   1,
    78  					ReplicaID: 1},
    79  			},
    80  			state: pb.LogState{
    81  				Shards: map[uint64]pb.LogShardInfo{
    82  					1: {ShardID: 1, Replicas: map[uint64]string{}},
    83  				},
    84  			},
    85  			expected: false,
    86  		},
    87  	}
    88  
    89  	for i, c := range cases {
    90  		fmt.Printf("case %v: %s\n", i, c.desc)
    91  		assert.Equal(t, c.expected, c.command.IsFinish(c.state, pb.DNState{}, pb.CNState{}))
    92  	}
    93  }
    94  
    95  func TestRemoveLogService(t *testing.T) {
    96  	cases := []struct {
    97  		desc     string
    98  		command  RemoveLogService
    99  		state    pb.LogState
   100  		expected bool
   101  	}{
   102  		{
   103  			desc: "remove log service not completed",
   104  			command: RemoveLogService{
   105  				Replica: Replica{
   106  					UUID:      "a",
   107  					ShardID:   1,
   108  					ReplicaID: 1},
   109  			},
   110  			state: pb.LogState{
   111  				Shards: map[uint64]pb.LogShardInfo{
   112  					1: {ShardID: 1, Replicas: map[uint64]string{1: "a"}},
   113  				},
   114  			},
   115  			expected: false,
   116  		},
   117  		{
   118  			desc: "remove log service completed",
   119  			command: RemoveLogService{
   120  				Replica: Replica{
   121  					UUID:      "a",
   122  					ShardID:   1,
   123  					ReplicaID: 1,
   124  				},
   125  			},
   126  			state: pb.LogState{
   127  				Shards: map[uint64]pb.LogShardInfo{
   128  					1: {ShardID: 1, Replicas: map[uint64]string{}},
   129  				},
   130  			},
   131  			expected: true,
   132  		},
   133  	}
   134  
   135  	for i, c := range cases {
   136  		fmt.Printf("case %v: %s\n", i, c.desc)
   137  		assert.Equal(t, c.expected, c.command.IsFinish(c.state, pb.DNState{}, pb.CNState{}))
   138  	}
   139  }
   140  
   141  func TestStartLogService(t *testing.T) {
   142  	cases := []struct {
   143  		desc     string
   144  		command  StartLogService
   145  		state    pb.LogState
   146  		expected bool
   147  	}{
   148  		{
   149  			desc: "start log service not completed",
   150  			command: StartLogService{
   151  				Replica: Replica{
   152  					UUID:      "a",
   153  					ShardID:   1,
   154  					ReplicaID: 1,
   155  				},
   156  			},
   157  			state: pb.LogState{
   158  				Stores: map[string]pb.LogStoreInfo{"a": {
   159  					Replicas: []pb.LogReplicaInfo{{}},
   160  				}},
   161  			},
   162  			expected: false,
   163  		},
   164  		{
   165  			desc: "start log service completed",
   166  			command: StartLogService{
   167  				Replica: Replica{
   168  					UUID:      "a",
   169  					ShardID:   1,
   170  					ReplicaID: 1,
   171  				},
   172  			},
   173  			state: pb.LogState{
   174  				Stores: map[string]pb.LogStoreInfo{"a": {
   175  					Replicas: []pb.LogReplicaInfo{{
   176  						LogShardInfo: pb.LogShardInfo{
   177  							ShardID:  1,
   178  							Replicas: map[uint64]string{1: "a"},
   179  						},
   180  						ReplicaID: 1,
   181  					}},
   182  				}},
   183  			},
   184  			expected: true,
   185  		},
   186  	}
   187  
   188  	for i, c := range cases {
   189  		fmt.Printf("case %v: %s\n", i, c.desc)
   190  		assert.Equal(t, c.expected, c.command.IsFinish(c.state, pb.DNState{}, pb.CNState{}))
   191  	}
   192  }
   193  
   194  func TestStopLogService(t *testing.T) {
   195  	cases := []struct {
   196  		desc     string
   197  		command  StopLogService
   198  		state    pb.LogState
   199  		expected bool
   200  	}{
   201  		{
   202  			desc: "stop log service completed",
   203  			command: StopLogService{
   204  				Replica: Replica{
   205  					UUID:    "a",
   206  					ShardID: 1,
   207  				},
   208  			},
   209  			state: pb.LogState{
   210  				Stores: map[string]pb.LogStoreInfo{"a": {
   211  					Replicas: []pb.LogReplicaInfo{{}},
   212  				}},
   213  			},
   214  			expected: true,
   215  		},
   216  		{
   217  			desc: "stop log service not completed",
   218  			command: StopLogService{
   219  				Replica: Replica{
   220  					UUID:    "a",
   221  					ShardID: 1,
   222  				},
   223  			},
   224  			state: pb.LogState{
   225  				Stores: map[string]pb.LogStoreInfo{"a": {
   226  					Replicas: []pb.LogReplicaInfo{{
   227  						LogShardInfo: pb.LogShardInfo{
   228  							ShardID:  1,
   229  							Replicas: map[uint64]string{1: "a"},
   230  						},
   231  						ReplicaID: 1,
   232  					}},
   233  				}},
   234  			},
   235  			expected: false,
   236  		},
   237  	}
   238  
   239  	for i, c := range cases {
   240  		fmt.Printf("case %v: %s\n", i, c.desc)
   241  		assert.Equal(t, c.expected, c.command.IsFinish(c.state, pb.DNState{}, pb.CNState{}))
   242  	}
   243  }
   244  
   245  func TestAddDnReplica(t *testing.T) {
   246  	cases := []struct {
   247  		desc     string
   248  		command  AddDnReplica
   249  		state    pb.DNState
   250  		expected bool
   251  	}{
   252  		{
   253  			desc: "add dn replica completed",
   254  			command: AddDnReplica{
   255  				StoreID:   "a",
   256  				ShardID:   1,
   257  				ReplicaID: 1,
   258  			},
   259  			state: pb.DNState{
   260  				Stores: map[string]pb.DNStoreInfo{"a": {
   261  					Tick: 0,
   262  					Shards: []pb.DNShardInfo{{
   263  						ShardID:   1,
   264  						ReplicaID: 1,
   265  					}},
   266  				}},
   267  			},
   268  			expected: true,
   269  		},
   270  		{
   271  			desc: "add dn replica not completed",
   272  			command: AddDnReplica{
   273  				StoreID:   "a",
   274  				ShardID:   1,
   275  				ReplicaID: 1,
   276  			},
   277  			state: pb.DNState{
   278  				Stores: map[string]pb.DNStoreInfo{"a": {
   279  					Tick:   0,
   280  					Shards: []pb.DNShardInfo{},
   281  				}},
   282  			},
   283  			expected: false,
   284  		},
   285  	}
   286  
   287  	for i, c := range cases {
   288  		fmt.Printf("case %v: %s\n", i, c.desc)
   289  		assert.Equal(t, c.expected, c.command.IsFinish(pb.LogState{}, c.state, pb.CNState{}))
   290  	}
   291  }
   292  
   293  func TestRemoveDnReplica(t *testing.T) {
   294  	cases := []struct {
   295  		desc     string
   296  		command  RemoveDnReplica
   297  		state    pb.DNState
   298  		expected bool
   299  	}{
   300  		{
   301  			desc: "remove dn replica not completed",
   302  			command: RemoveDnReplica{
   303  				StoreID:   "a",
   304  				ShardID:   1,
   305  				ReplicaID: 1,
   306  			},
   307  			state: pb.DNState{
   308  				Stores: map[string]pb.DNStoreInfo{"a": {
   309  					Tick: 0,
   310  					Shards: []pb.DNShardInfo{{
   311  						ShardID:   1,
   312  						ReplicaID: 1,
   313  					}},
   314  				}},
   315  			},
   316  			expected: false,
   317  		},
   318  		{
   319  			desc: "remove dn replica completed",
   320  			command: RemoveDnReplica{
   321  				StoreID:   "a",
   322  				ShardID:   1,
   323  				ReplicaID: 1,
   324  			},
   325  			state: pb.DNState{
   326  				Stores: map[string]pb.DNStoreInfo{"a": {
   327  					Tick:   0,
   328  					Shards: []pb.DNShardInfo{},
   329  				}},
   330  			},
   331  			expected: true,
   332  		},
   333  	}
   334  
   335  	for i, c := range cases {
   336  		fmt.Printf("case %v: %s\n", i, c.desc)
   337  		assert.Equal(t, c.expected, c.command.IsFinish(pb.LogState{}, c.state, pb.CNState{}))
   338  	}
   339  }