github.phpd.cn/cilium/cilium@v1.6.12/pkg/aws/eni/node_test.go (about) 1 // Copyright 2019 Authors of Cilium 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package eni 18 19 import ( 20 "gopkg.in/check.v1" 21 ) 22 23 type testNeededDef struct { 24 available int 25 used int 26 preallocate int 27 minallocate int 28 result int 29 } 30 31 type testExcessDef struct { 32 available int 33 used int 34 preallocate int 35 minallocate int 36 maxabovewatermark int 37 result int 38 } 39 40 var neededDef = []testNeededDef{ 41 {0, 0, 0, 16, 16}, 42 {0, 0, 8, 16, 16}, 43 {0, 0, 16, 8, 16}, 44 {0, 0, 16, 0, 16}, 45 {8, 0, 0, 16, 8}, 46 {8, 4, 8, 0, 4}, 47 {8, 4, 8, 8, 4}, 48 } 49 50 var excessDef = []testExcessDef{ 51 {0, 0, 0, 16, 0, 0}, 52 {15, 0, 8, 16, 8, 0}, 53 {17, 0, 8, 16, 0, 9}, // 17 used, 8 pre-allocate, 16 min-allocate => 1 excess 54 {20, 0, 8, 16, 4, 0}, // 20 used, 8 pre-allocate, 16 min-allocate, 4 max-above-watermark => 0 excess 55 {21, 0, 8, 0, 4, 9}, // 21 used, 8 pre-allocate, 4 max-above-watermark => 9 excess 56 {20, 0, 8, 20, 8, 0}, 57 {16, 1, 8, 16, 8, 0}, 58 {20, 4, 8, 17, 8, 0}, 59 {20, 4, 0, 0, 0, 8}, 60 {20, 4, 0, 0, 8, 0}, 61 } 62 63 func (e *ENISuite) TestCalculateNeededIPs(c *check.C) { 64 for _, d := range neededDef { 65 result := calculateNeededIPs(d.available, d.used, d.preallocate, d.minallocate) 66 c.Assert(result, check.Equals, d.result) 67 } 68 } 69 70 func (e *ENISuite) TestCalculateExcessIPs(c *check.C) { 71 for _, d := range excessDef { 72 result := calculateExcessIPs(d.available, d.used, d.preallocate, d.minallocate, d.maxabovewatermark) 73 c.Assert(result, check.Equals, d.result) 74 } 75 }