github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/strategy/fill_test.go (about)

     1  package strategy
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/projecteru2/core/types"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestFillPlan(t *testing.T) {
    15  	// 正常的全量补充
    16  	n := 10
    17  	nodes := deployedNodes()
    18  	r, err := FillPlan(context.Background(), nodes, n, 0, 0)
    19  	assert.NoError(t, err)
    20  	finalCounts := []int{}
    21  	for _, node := range nodes {
    22  		finalCounts = append(finalCounts, node.Count+r[node.Nodename])
    23  	}
    24  	sort.Ints(finalCounts)
    25  	assert.ElementsMatch(t, []int{10, 10, 10, 10}, finalCounts)
    26  
    27  	// 局部补充
    28  	n = 5
    29  	nodes = deployedNodes()
    30  	r, err = FillPlan(context.Background(), nodes, n, 0, 0)
    31  	assert.NoError(t, err)
    32  	finalCounts = []int{}
    33  	for _, node := range nodes {
    34  		finalCounts = append(finalCounts, node.Count+r[node.Nodename])
    35  	}
    36  	sort.Ints(finalCounts)
    37  	assert.ElementsMatch(t, []int{5, 5, 5, 7}, finalCounts)
    38  
    39  	// 局部补充不能
    40  	n = 15
    41  	nodes = deployedNodes()
    42  	_, err = FillPlan(context.Background(), nodes, n, 0, 0)
    43  	assert.True(t, errors.Is(err, types.ErrInsufficientResource))
    44  
    45  	// 全局补充不能
    46  	n = 1
    47  	nodes = deployedNodes()
    48  	_, err = FillPlan(context.Background(), nodes, n, 0, 0)
    49  	assert.Error(t, err)
    50  	assert.Contains(t, err.Error(), "each node has enough workloads")
    51  
    52  	// LimitNode
    53  	n = 10
    54  	nodes = deployedNodes()
    55  	_, err = FillPlan(context.Background(), nodes, n, 0, 2)
    56  	assert.NoError(t, err)
    57  
    58  	// 局部补充
    59  	n = 1
    60  	nodes = []Info{
    61  		{
    62  			Nodename: "65",
    63  			Capacity: 0,
    64  			Count:    0,
    65  		},
    66  		{
    67  			Nodename: "67",
    68  			Capacity: 10,
    69  			Count:    0,
    70  		},
    71  	}
    72  
    73  	_, err = FillPlan(context.Background(), nodes, n, 0, 3)
    74  	assert.Error(t, err)
    75  	assert.Contains(t, err.Error(), "cannot alloc a fill node plan")
    76  
    77  	nodes = genNodesByCapCount([]int{1, 2, 3, 4, 5}, []int{3, 3, 3, 3, 3})
    78  	r, err = FillPlan(context.Background(), nodes, 4, 0, 3)
    79  	assert.Nil(t, err)
    80  	assert.ElementsMatch(t, []int{3, 3, 4, 4, 4}, getFinalStatus(r, nodes))
    81  	assert.EqualValues(t, 1, r["4"])
    82  	assert.EqualValues(t, 1, r["3"])
    83  	assert.EqualValues(t, 1, r["2"])
    84  
    85  	_, err = FillPlan(context.Background(), nodes, 5, 1000, 0)
    86  	assert.Contains(t, err.Error(), "not enough nodes that can fill up to 5 instances, require 1 nodes")
    87  }