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

     1  package strategy
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/projecteru2/core/types"
    10  )
    11  
    12  func TestGlobalPlan1(t *testing.T) {
    13  	// normal case
    14  	n1 := Info{
    15  		Nodename: "n1",
    16  		Usage:    0.8,
    17  		Rate:     0.05,
    18  		Capacity: 1,
    19  	}
    20  	n2 := Info{
    21  		Nodename: "n2",
    22  		Usage:    0.5,
    23  		Rate:     0.12,
    24  		Capacity: 2,
    25  	}
    26  	n3 := Info{
    27  		Nodename: "n3",
    28  		Usage:    2.2,
    29  		Rate:     0.05,
    30  		Capacity: 1,
    31  	}
    32  	arg := []Info{n1, n2, n3}
    33  	r, err := GlobalPlan(context.Background(), arg, 3, 100, 0)
    34  	assert.NoError(t, err)
    35  	assert.Equal(t, r, map[string]int{"n1": 1, "n2": 2})
    36  
    37  	// normal case 2
    38  	n1 = Info{
    39  		Nodename: "n1",
    40  		Usage:    0.8,
    41  		Rate:     0.05,
    42  		Capacity: 4,
    43  	}
    44  	n2 = Info{
    45  		Nodename: "n2",
    46  		Usage:    0.5,
    47  		Rate:     0.35,
    48  		Capacity: 1,
    49  	}
    50  	n3 = Info{
    51  		Nodename: "n3",
    52  		Usage:    2.2,
    53  		Rate:     0.05,
    54  		Capacity: 1,
    55  	}
    56  	arg = []Info{n1, n2, n3}
    57  	r, err = GlobalPlan(context.Background(), arg, 3, 100, 0)
    58  	assert.Equal(t, r, map[string]int{"n1": 2, "n2": 1})
    59  
    60  	// insufficient total
    61  	n1 = Info{
    62  		Nodename: "n1",
    63  		Usage:    0.8,
    64  		Rate:     0.05,
    65  		Capacity: 4,
    66  	}
    67  	n2 = Info{
    68  		Nodename: "n2",
    69  		Usage:    0.5,
    70  		Rate:     0.35,
    71  		Capacity: 1,
    72  	}
    73  	n3 = Info{
    74  		Nodename: "n3",
    75  		Usage:    2.2,
    76  		Rate:     0.05,
    77  		Capacity: 1,
    78  	}
    79  	arg = []Info{n1, n2, n3}
    80  	r, err = GlobalPlan(context.Background(), arg, 100, 6, 0)
    81  	assert.ErrorIs(t, err, types.ErrInsufficientResource)
    82  
    83  	// fake total
    84  	n1 = Info{
    85  		Nodename: "n1",
    86  		Usage:    0.8,
    87  		Rate:     0.05,
    88  		Capacity: 4,
    89  	}
    90  	n2 = Info{
    91  		Nodename: "n2",
    92  		Usage:    0.5,
    93  		Rate:     0.35,
    94  		Capacity: 1,
    95  	}
    96  	n3 = Info{
    97  		Nodename: "n3",
    98  		Usage:    2.2,
    99  		Rate:     0.05,
   100  		Capacity: 1,
   101  	}
   102  	arg = []Info{n1, n2, n3}
   103  	r, err = GlobalPlan(context.Background(), arg, 10, 100, 0)
   104  	assert.ErrorIs(t, err, types.ErrInsufficientResource)
   105  
   106  	// small rate
   107  	n1 = Info{
   108  		Nodename: "n1",
   109  		Usage:    0.8,
   110  		Rate:     0,
   111  		Capacity: 1e10,
   112  	}
   113  	n2 = Info{
   114  		Nodename: "n2",
   115  		Usage:    0.5,
   116  		Rate:     0,
   117  		Capacity: 1e10,
   118  	}
   119  	n3 = Info{
   120  		Nodename: "n3",
   121  		Usage:    2.2,
   122  		Rate:     0,
   123  		Capacity: 1e10,
   124  	}
   125  	arg = []Info{n1, n2, n3}
   126  	r, err = GlobalPlan(context.Background(), arg, 10, 100, 0)
   127  	assert.NoError(t, err)
   128  	assert.Equal(t, r, map[string]int{"n2": 10})
   129  
   130  	// old test case 2
   131  	n1 = Info{
   132  		Nodename: "n1",
   133  		Usage:    1.6,
   134  		Rate:     0.05,
   135  		Capacity: 100,
   136  	}
   137  	n2 = Info{
   138  		Nodename: "n2",
   139  		Usage:    0.5,
   140  		Rate:     0.11,
   141  		Capacity: 100,
   142  	}
   143  	arg = []Info{n2, n1}
   144  	r, err = GlobalPlan(context.Background(), arg, 2, 100, 0)
   145  	assert.NoError(t, err)
   146  	assert.Equal(t, r, map[string]int{"n2": 2})
   147  
   148  	// old test case 3
   149  	n1 = Info{
   150  		Nodename: "n1",
   151  		Usage:    0.5259232954545454,
   152  		Rate:     0.0000712,
   153  		Capacity: 100,
   154  	}
   155  
   156  	r, err = GlobalPlan(context.Background(), []Info{n1}, 1, 100, 0)
   157  	assert.NoError(t, err)
   158  	assert.Equal(t, r["n1"], 1)
   159  
   160  	// old test case 4
   161  	n1 = Info{
   162  		Nodename: "n1",
   163  		Usage:    1,
   164  		Rate:     0.3,
   165  		Capacity: 100,
   166  		Count:    21,
   167  	}
   168  	r, err = GlobalPlan(context.Background(), []Info{n1}, 10, 100, 0)
   169  	assert.NoError(t, err)
   170  	assert.Equal(t, r["n1"], 10)
   171  }
   172  
   173  func TestGlobalIssue455(t *testing.T) {
   174  	infos := []Info{
   175  		{
   176  			Nodename: "spp-qa-vm-node-1",
   177  			Usage:    0.07999999999999996,
   178  			Rate:     3.725290298461914e-08,
   179  			Capacity: 10726691,
   180  			Count:    7,
   181  		},
   182  		{
   183  			Nodename: "spp-qa-vm-node-2",
   184  			Usage:    0.24,
   185  			Rate:     3.725290298461914e-08,
   186  			Capacity: 4290676,
   187  			Count:    5,
   188  		},
   189  		{
   190  			Nodename: "spp-qa-vm-node-3",
   191  			Usage:    0.45999999999999996,
   192  			Rate:     3.725290298461914e-08,
   193  			Capacity: 4290676,
   194  			Count:    6,
   195  		},
   196  	}
   197  	deployMap, err := GlobalPlan(context.Background(), infos, 1, 19308043, 1)
   198  	assert.NoError(t, err)
   199  	assert.EqualValues(t, 1, deployMap["spp-qa-vm-node-1"])
   200  }