@@ -20,6 +20,7 @@ import (
20
20
"path"
21
21
"path/filepath"
22
22
"reflect"
23
+ "sort"
23
24
"testing"
24
25
25
26
"github.com/pkg/errors"
@@ -99,12 +100,21 @@ func TestGetPreferredAllocation(t *testing.T) {
99
100
rqt := & v1beta1.PreferredAllocationRequest {
100
101
ContainerRequests : []* v1beta1.ContainerPreferredAllocationRequest {
101
102
{
102
- AvailableDeviceIDs : []string {"card0-4" , "card1-1" , "card2-3" , "card2-4" , "card2-1" , "card1-0" , "card1-4" , "card3-4" , "card1-2" , "card0-1" , "card2-0" , "card2-2" , "card1-3" , "card0-2 " , "card3-0" , "card3-3" , "card0-3" , "card0-0" , "card3-1" , "card3-2" },
103
+ AvailableDeviceIDs : []string {"card0-4" , "card0-2" , " card1-1" , "card2-3" , "card2-4" , "card2-1" , "card1-0" , "card1-4" , "card3-4" , "card1-2" , "card0-1" , "card2-0" , "card2-2" , "card1-3" , "card3-0" , "card3-3" , "card0-3" , "card0-0" , "card3-1" , "card3-2" },
103
104
AllocationSize : 4 ,
104
105
},
105
106
},
106
107
}
107
108
109
+ rqtNotEnough := & v1beta1.PreferredAllocationRequest {
110
+ ContainerRequests : []* v1beta1.ContainerPreferredAllocationRequest {
111
+ {
112
+ AvailableDeviceIDs : []string {"card0-1" , "card0-2" , "card0-3" , "card1-1" },
113
+ AllocationSize : 3 ,
114
+ },
115
+ },
116
+ }
117
+
108
118
rqtErr := & v1beta1.PreferredAllocationRequest {
109
119
ContainerRequests : []* v1beta1.ContainerPreferredAllocationRequest {
110
120
{
@@ -117,22 +127,24 @@ func TestGetPreferredAllocation(t *testing.T) {
117
127
plugin := newDevicePlugin ("" , "" , cliOptions {sharedDevNum : 5 , resourceManagement : false , preferredAllocationPolicy : "none" })
118
128
response , _ := plugin .GetPreferredAllocation (rqt )
119
129
120
- if ! reflect .DeepEqual (response .ContainerResponses [0 ].DeviceIDs , []string {"card0-4" , "card1-1" , "card2-3" , "card2-4" }) {
121
- t .Error ("Unexpected return value for none preferred allocation" )
130
+ sort .Strings (response .ContainerResponses [0 ].DeviceIDs )
131
+
132
+ if ! reflect .DeepEqual (response .ContainerResponses [0 ].DeviceIDs , []string {"card0-4" , "card1-1" , "card2-3" , "card3-4" }) {
133
+ t .Error ("Unexpected return value for none preferred allocation" , response .ContainerResponses [0 ].DeviceIDs )
122
134
}
123
135
124
136
plugin = newDevicePlugin ("" , "" , cliOptions {sharedDevNum : 5 , resourceManagement : false , preferredAllocationPolicy : "balanced" })
125
137
response , _ = plugin .GetPreferredAllocation (rqt )
126
138
127
139
if ! reflect .DeepEqual (response .ContainerResponses [0 ].DeviceIDs , []string {"card0-0" , "card1-0" , "card2-0" , "card3-0" }) {
128
- t .Error ("Unexpected return value for balanced preferred allocation" )
140
+ t .Error ("Unexpected return value for balanced preferred allocation" , response . ContainerResponses [ 0 ]. DeviceIDs )
129
141
}
130
142
131
143
plugin = newDevicePlugin ("" , "" , cliOptions {sharedDevNum : 5 , resourceManagement : false , preferredAllocationPolicy : "packed" })
132
144
response , _ = plugin .GetPreferredAllocation (rqt )
133
145
134
146
if ! reflect .DeepEqual (response .ContainerResponses [0 ].DeviceIDs , []string {"card0-0" , "card0-1" , "card0-2" , "card0-3" }) {
135
- t .Error ("Unexpected return value for packed preferred allocation" )
147
+ t .Error ("Unexpected return value for packed preferred allocation" , response . ContainerResponses [ 0 ]. DeviceIDs )
136
148
}
137
149
138
150
plugin = newDevicePlugin ("" , "" , cliOptions {sharedDevNum : 5 , resourceManagement : false , preferredAllocationPolicy : "none" })
@@ -141,6 +153,16 @@ func TestGetPreferredAllocation(t *testing.T) {
141
153
if response != nil {
142
154
t .Error ("Fail to handle the input error that req.AllocationSize is greater than len(req.AvailableDeviceIDs)." )
143
155
}
156
+
157
+ plugin = newDevicePlugin ("" , "" , cliOptions {sharedDevNum : 5 , resourceManagement : false , preferredAllocationPolicy : "none" })
158
+ response , _ = plugin .GetPreferredAllocation (rqtNotEnough )
159
+
160
+ sort .Strings (response .ContainerResponses [0 ].DeviceIDs )
161
+
162
+ if ! reflect .DeepEqual (response .ContainerResponses [0 ].DeviceIDs , []string {"card0-1" , "card0-2" , "card1-1" }) {
163
+ t .Error ("Unexpected return value for none preferred allocation with too few separate devices" ,
164
+ response .ContainerResponses [0 ].DeviceIDs )
165
+ }
144
166
}
145
167
146
168
func TestAllocate (t * testing.T ) {
0 commit comments