github.com/m3db/m3@v1.5.0/src/cluster/placement/selector/common_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package selector
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/cluster/placement"
    27  	"github.com/m3db/m3/src/cluster/shard"
    28  
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestGetValidCandidates(t *testing.T) {
    33  	i1 := placement.NewInstance().SetID("i1").SetZone("z1")
    34  	i1.Shards().Add(shard.NewShard(0).SetState(shard.Available))
    35  
    36  	i2 := placement.NewInstance().SetID("i2").SetZone("z1")
    37  	i2.Shards().Add(shard.NewShard(0).SetState(shard.Initializing).SetSourceID("i2"))
    38  
    39  	i3 := placement.NewInstance().SetID("i3").SetZone("z1")
    40  	i3.Shards().Add(shard.NewShard(0).SetState(shard.Leaving))
    41  
    42  	p := placement.NewPlacement().
    43  		SetInstances([]placement.Instance{i1, i2, i3}).
    44  		SetIsSharded(true).
    45  		SetReplicaFactor(2).
    46  		SetShards([]uint32{0})
    47  
    48  	emptyI3 := placement.NewInstance().SetID("i3").SetZone("z3")
    49  	i4 := placement.NewInstance().SetID("i4").SetZone("z1")
    50  	candidates := []placement.Instance{i3, emptyI3, i1, i4}
    51  	res, err := getValidCandidates(p, candidates, placement.NewOptions().SetValidZone("z1"))
    52  	require.NoError(t, err)
    53  	require.Equal(t, []placement.Instance{i3, i3, i4}, res)
    54  }
    55  
    56  func TestGetValidCandidatesAllowAllZones(t *testing.T) {
    57  	i1 := placement.NewInstance().SetID("i1").SetZone("z1")
    58  	i2 := placement.NewInstance().SetID("i2").SetZone("z1")
    59  	i3 := placement.NewInstance().SetID("i3").SetZone("z2")
    60  
    61  	p := placement.NewPlacement().
    62  		SetInstances([]placement.Instance{i3, i1, i2}).
    63  		SetIsSharded(false).
    64  		SetReplicaFactor(1)
    65  
    66  	i4 := placement.NewInstance().SetID("i4").SetZone("z2")
    67  	i5 := placement.NewInstance().SetID("i5").SetZone("z3")
    68  	candidates := []placement.Instance{i4, i5}
    69  	res, err := getValidCandidates(p, candidates, placement.NewOptions().
    70  		SetAllowAllZones(true))
    71  	require.NoError(t, err)
    72  	require.Equal(t, []placement.Instance{i4, i5}, res)
    73  }