github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/operator/builder_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 the behavior of the builder.
    20  
    21  package operator
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/matrixorigin/matrixone/pkg/pb/logservice"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestAddReplica(t *testing.T) {
    31  	logShard := logservice.LogShardInfo{ShardID: 1, Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"}, Epoch: 1}
    32  	assert.NoError(t, NewBuilder("", logShard).AddPeer("d", 4).err)
    33  	assert.Error(t, NewBuilder("", logShard).AddPeer("", 4).err)
    34  	assert.Error(t, NewBuilder("", logShard).AddPeer("c", 4).err)
    35  	assert.Error(t, NewBuilder("", logShard).AddPeer("d", 3).err)
    36  }
    37  
    38  func TestRemoveReplica(t *testing.T) {
    39  	logShard := logservice.LogShardInfo{ShardID: 1, Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"}, Epoch: 1}
    40  	assert.NoError(t, NewBuilder("", logShard).RemovePeer("c").err)
    41  	assert.Error(t, NewBuilder("", logShard).RemovePeer("d").err)
    42  }
    43  
    44  func TestAddBuild(t *testing.T) {
    45  	logShard := logservice.LogShardInfo{ShardID: 1, Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"}, Epoch: 1}
    46  
    47  	_, err := NewBuilder("", logShard).Build()
    48  	assert.Error(t, err)
    49  
    50  	_, err = NewBuilder("", logShard).AddPeer("", 4).Build()
    51  	assert.Error(t, err)
    52  
    53  	build, err := NewBuilder("", logShard).AddPeer("d", 4).Build()
    54  	assert.NoError(t, err)
    55  	assert.Equal(t, "add peer: store [d]", build.brief)
    56  	assert.Equal(t, uint64(1), build.shardID)
    57  	assert.Equal(t, uint64(1), build.epoch)
    58  	assert.Equal(t, AddLogService{
    59  		Target: "a",
    60  		Replica: Replica{
    61  			UUID:      "d",
    62  			ShardID:   1,
    63  			ReplicaID: 4,
    64  			Epoch:     1,
    65  		},
    66  	}, build.steps[0])
    67  }
    68  
    69  func TestRemoveBuild(t *testing.T) {
    70  	logShard := logservice.LogShardInfo{ShardID: 1, Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"}, Epoch: 1}
    71  
    72  	_, err := NewBuilder("", logShard).Build()
    73  	assert.Error(t, err)
    74  
    75  	_, err = NewBuilder("", logShard).RemovePeer("").Build()
    76  	assert.Error(t, err)
    77  
    78  	_, err = NewBuilder("", logShard).RemovePeer("d").Build()
    79  	assert.Error(t, err)
    80  
    81  	build, err := NewBuilder("", logShard).RemovePeer("c").Build()
    82  	assert.NoError(t, err)
    83  	assert.Equal(t, "rm peer: store [c]", build.brief)
    84  	assert.Equal(t, uint64(1), build.shardID)
    85  	assert.Equal(t, uint64(1), build.epoch)
    86  	assert.Equal(t, RemoveLogService{
    87  		Target: "a",
    88  		Replica: Replica{
    89  			UUID:      "c",
    90  			ShardID:   1,
    91  			ReplicaID: 3,
    92  			Epoch:     1,
    93  		},
    94  	}, build.steps[0])
    95  }