Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit 47422ef

Browse files
togashidmkillianmuldoon
authored andcommitted
Fix unit tests from refactoring
Move scheduling unit tests to new telemetry scheduler file. Fix composite literal for unkeyed fields
1 parent 2683ea1 commit 47422ef

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

cmd/tas-scheduler-extender/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020
flag.Parse()
2121
cacheReader := cache.RemoteClient{}
2222
cacheReader.RegisterEndpoint(cacheEndpoint)
23-
telemetry := telemetryscheduler.NewMetricsExtender(&cacheReader)
24-
sch := scheduler.Server{ExtenderScheduler: telemetry}
23+
tscheduler := telemetryscheduler.NewMetricsExtender(&cacheReader)
24+
sch := scheduler.Server{ExtenderScheduler: tscheduler}
2525
sch.StartServer(port, certFile, keyFile, caFile, unsafe)
2626
}

pkg/scheduler/scheduler.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package scheduler
44
import (
55
"crypto/tls"
66
"crypto/x509"
7+
"io/ioutil"
78
"log"
89
"net/http"
910
"os"
@@ -40,18 +41,34 @@ func requestContentType(next http.HandlerFunc) http.HandlerFunc {
4041
requestContentType := r.Header.Get("Content-Type")
4142
if requestContentType != "application/json" {
4243
w.WriteHeader(http.StatusNotFound)
43-
log.Print("request size too large")
44+
log.Print("request content type not application/json")
4445
return
4546
}
4647
next.ServeHTTP(w, r)
4748
}
4849
}
4950

51+
/*
52+
handlerWithMiddleware runs each function in sequence starting from the outermost function. These middleware functions
53+
are pass/fail checks on the scheduling request. If a check fails the response with an appropriate error is immediately
54+
written and returned.
55+
i.e. with this version of the code:
56+
return requestContentType(
57+
contentLength(
58+
postOnly(handle),
59+
),
60+
)
61+
if the content type is not correct - i.e. NOT application/json - the response will be written. contentLength or postOnly
62+
will not run.
63+
*/
64+
5065
//handlerWithMiddleware is handler wrapped with middleware to serve the prechecks at endpoint
5166
func handlerWithMiddleware(handle http.HandlerFunc) http.HandlerFunc {
5267
return requestContentType(
53-
contentLength(
54-
postOnly(handle)))
68+
contentLength(
69+
postOnly(handle),
70+
),
71+
)
5572
}
5673

5774
//error handler deals with requests sent to an invalid endpoint and returns a 404.

pkg/scheduler/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ type ExtenderScheduler interface {
1111
Filter(w http.ResponseWriter, r *http.Request)
1212
}
1313

14-
//Server declares ExtenderScheduler
14+
//Server type wraps the implementation of the scheduler.
1515
type Server struct {
1616
ExtenderScheduler
1717
}
1818

19-
//TODO: These types are in the k8s.io/kubernes/scheduler/api package
19+
//TODO: These types are in the k8s.io/kubernetes/scheduler/api package
2020
// Some import issue is making them tough to access, so they are reimplemented here pending a solution.
2121

2222
// HostPriority represents the priority of scheduling to a particular host, higher priority is better.

pkg/scheduler/scheduler_test.go renamed to pkg/telemetryscheduler/scheduler_test.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//Logic for the scheduler extender - including the server it starts and prioritize + filter methods - is implemented in this package.
2-
package scheduler
2+
package telemetryscheduler
33

44
import (
55
"bytes"
66
"encoding/json"
77
"errors"
88
"github.com/intel/telemetry-aware-scheduling/pkg/cache"
99
"github.com/intel/telemetry-aware-scheduling/pkg/metrics"
10+
"github.com/intel/telemetry-aware-scheduling/pkg/scheduler"
1011
telpolv1 "github.com/intel/telemetry-aware-scheduling/pkg/telemetrypolicy/api/v1alpha1"
1112
telpolclient "github.com/intel/telemetry-aware-scheduling/pkg/telemetrypolicy/client/v1alpha1"
1213
"io/ioutil"
@@ -20,19 +21,19 @@ import (
2021
"testing"
2122
)
2223

23-
var prioritizerArgs1 = ExtenderArgs{
24+
var prioritizerArgs1 = scheduler.ExtenderArgs{
2425
Pod: v1.Pod{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "big pod", Labels: map[string]string{"telemetry-policy": "test-policy"}, Namespace: "default"}},
2526
Nodes: &v1.NodeList{Items: []v1.Node{{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "node A"}, Spec: v1.NodeSpec{}, Status: v1.NodeStatus{}}}},
2627
NodeNames: &[]string{"node A", "node B"},
2728
}
2829

29-
var twoNodeArgument = ExtenderArgs{
30+
var twoNodeArgument = scheduler.ExtenderArgs{
3031
Pod: v1.Pod{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "big pod", Labels: map[string]string{"telemetry-policy": "test-policy"}, Namespace: "default"}},
3132
Nodes: &v1.NodeList{Items: []v1.Node{{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "node A"}, Spec: v1.NodeSpec{}, Status: v1.NodeStatus{}}, {TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "node B"}, Spec: v1.NodeSpec{}, Status: v1.NodeStatus{}}}},
3233
NodeNames: &[]string{"node A", "node B"},
3334
}
3435

35-
var noPolicyPod = ExtenderArgs{
36+
var noPolicyPod = scheduler.ExtenderArgs{
3637
Pod: v1.Pod{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "big pod", Labels: map[string]string{"useless-label": "test-policy"}, Namespace: "default"}},
3738
Nodes: &v1.NodeList{Items: []v1.Node{{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "node A"}, Spec: v1.NodeSpec{}, Status: v1.NodeStatus{}}}},
3839
NodeNames: &[]string{"node A", "node B"},
@@ -93,8 +94,8 @@ func TestMetricsExtender_prescheduleChecks(t *testing.T) {
9394
fields fields
9495
args args
9596
metric metrics.NodeMetricsInfo
96-
prioritizeArgs ExtenderArgs
97-
wanted HostPriorityList
97+
prioritizeArgs scheduler.ExtenderArgs
98+
wanted scheduler.HostPriorityList
9899
wantErr bool
99100
}{
100101
{name: "unlabelled pod",
@@ -103,7 +104,7 @@ func TestMetricsExtender_prescheduleChecks(t *testing.T) {
103104
args: args{httptest.NewRequest("POST", "http://localhost/scheduler/prioritize", nil)},
104105
metric: map[string]metrics.NodeMetric{"node A": {Value: *resource.NewQuantity(100, resource.DecimalSI)}, "node B": {Value: *resource.NewQuantity(90, resource.DecimalSI)}},
105106
prioritizeArgs: noPolicyPod,
106-
wanted: []HostPriority{{"node A", 10}, {"node B", 9}},
107+
wanted: []scheduler.HostPriority{{Host: "node A", Score: 10}, {Host: "node B", Score: 9}},
107108
wantErr: true,
108109
},
109110
}
@@ -128,7 +129,7 @@ func TestMetricsExtender_prescheduleChecks(t *testing.T) {
128129
tt.args.r.Body = ioutil.NopCloser(bytes.NewReader(argsAsJSON))
129130
w := httptest.NewRecorder()
130131
m.Prioritize(w, tt.args.r)
131-
result := HostPriorityList{}
132+
result := scheduler.HostPriorityList{}
132133
b := w.Body.Bytes()
133134
err = json.Unmarshal(b, &result)
134135
log.Print(result)
@@ -154,8 +155,8 @@ func TestMetricsExtender_Prioritize(t *testing.T) {
154155
fields fields
155156
args args
156157
metric metrics.NodeMetricsInfo
157-
prioritizeArgs ExtenderArgs
158-
wanted HostPriorityList
158+
prioritizeArgs scheduler.ExtenderArgs
159+
wanted scheduler.HostPriorityList
159160
wantErr bool
160161
}{
161162
{"get and return node test",
@@ -164,7 +165,7 @@ func TestMetricsExtender_Prioritize(t *testing.T) {
164165
args{httptest.NewRequest("POST", "http://localhost/scheduler/prioritize", nil)},
165166
map[string]metrics.NodeMetric{"node A": {Value: *resource.NewQuantity(100, resource.DecimalSI)}, "node B": {Value: *resource.NewQuantity(90, resource.DecimalSI)}},
166167
twoNodeArgument,
167-
[]HostPriority{{"node A", 10}, {"node B", 9}},
168+
[]scheduler.HostPriority{{Host: "node A", Score: 10}, {Host: "node B", Score: 9}},
168169
false,
169170
},
170171
{"policy not found",
@@ -173,7 +174,7 @@ func TestMetricsExtender_Prioritize(t *testing.T) {
173174
args{httptest.NewRequest("POST", "http://localhost/scheduler/prioritize", nil)},
174175
map[string]metrics.NodeMetric{"node A": {Value: *resource.NewQuantity(90, resource.DecimalSI)}, "node B": {Value: *resource.NewQuantity(100, resource.DecimalSI)}},
175176
twoNodeArgument,
176-
[]HostPriority{},
177+
[]scheduler.HostPriority{},
177178
true,
178179
},
179180
{"cache returns error if empty",
@@ -182,15 +183,15 @@ func TestMetricsExtender_Prioritize(t *testing.T) {
182183
args{httptest.NewRequest("POST", "http://localhost/scheduler/prioritize", nil)},
183184
map[string]metrics.NodeMetric{"node A": {Value: *resource.NewQuantity(100, resource.DecimalSI)}},
184185
prioritizerArgs1,
185-
[]HostPriority{{"node B", 10}},
186+
[]scheduler.HostPriority{{Host: "node B", Score: 10}},
186187
true,
187188
},
188-
{"malformed arguments return error", fields{*dummyClient, cache.MockEmptySelfUpdatingCache(),
189-
testPolicy1},
189+
{"malformed arguments return error",fields{*dummyClient, cache.MockEmptySelfUpdatingCache(),
190+
testPolicy1},
190191
args{httptest.NewRequest("POST", "http://localhost/scheduler/prioritize", nil)},
191192
map[string]metrics.NodeMetric{"node A": {Value: *resource.NewQuantity(100, resource.DecimalSI)}},
192-
ExtenderArgs{},
193-
[]HostPriority{{"node B", 10}},
193+
scheduler.ExtenderArgs{},
194+
[]scheduler.HostPriority{{Host: "node B", Score: 10}},
194195
true,
195196
},
196197
}
@@ -216,7 +217,7 @@ func TestMetricsExtender_Prioritize(t *testing.T) {
216217
tt.args.r.Body = ioutil.NopCloser(bytes.NewReader(argsAsJSON))
217218
w := httptest.NewRecorder()
218219
m.Prioritize(w, tt.args.r)
219-
result := HostPriorityList{}
220+
result := scheduler.HostPriorityList{}
220221
b := w.Body.Bytes()
221222
err = json.Unmarshal(b, &result)
222223
if err != nil && tt.wantErr {
@@ -265,7 +266,7 @@ func TestMetricsExtender_Filter(t *testing.T) {
265266
name string
266267
fields fields
267268
args args
268-
wanted ExtenderFilterResult
269+
wanted scheduler.ExtenderFilterResult
269270
wantErr bool
270271
}{
271272
{name: "get and return node test",
@@ -275,14 +276,14 @@ func TestMetricsExtender_Filter(t *testing.T) {
275276
testPolicy1},
276277
args: args{
277278
httptest.NewRequest("POST", "http://localhost/scheduler/prioritize", nil), metrics.TestNodeMetricCustomInfo([]string{"node A", "node B"}, []int64{10, 30})},
278-
wanted: ExtenderFilterResult{&v1.NodeList{}, &[]string{"node A"}, map[string]string{}, ""},
279+
wanted: scheduler.ExtenderFilterResult{Nodes: &v1.NodeList{}, NodeNames: &[]string{"node A"}, FailedNodes: map[string]string{}},
279280
},
280281
{name: "filter out one node",
281282
fields: fields{*dummyClient, cache.MockSelfUpdatingCache(),
282283
metrics.NewDummyMetricsClient(metrics.InstanceOfMockMetricClientMap),
283284
testPolicy1},
284285
args: args{httptest.NewRequest("POST", "http://localhost/scheduler/prioritize", nil), metrics.TestNodeMetricCustomInfo([]string{"node A", "node B"}, []int64{50, 30})},
285-
wanted: ExtenderFilterResult{&v1.NodeList{}, &[]string{"node A"}, map[string]string{"node A": ""}, ""},
286+
wanted: scheduler.ExtenderFilterResult{Nodes: &v1.NodeList{}, NodeNames: &[]string{"node A"}, FailedNodes: map[string]string{"node A": ""}},
286287
},
287288
}
288289
for _, tt := range tests {
@@ -308,7 +309,7 @@ func TestMetricsExtender_Filter(t *testing.T) {
308309
tt.args.r.Header.Add("Content-Type", "application/json")
309310
w := httptest.NewRecorder()
310311
m.Filter(w, tt.args.r)
311-
result := ExtenderFilterResult{}
312+
result := scheduler.ExtenderFilterResult{}
312313
b := w.Body.Bytes()
313314
err = json.Unmarshal(b, &result)
314315
if err != nil {

pkg/telemetryscheduler/telemetryscheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (m MetricsExtender) Prioritize(w http.ResponseWriter, r *http.Request) {
4747
return
4848
}
4949
if _, ok := extenderArgs.Pod.Labels[tasPolicy]; !ok {
50-
err = fmt.Errorf("no policy associated with pod")
50+
log.Printf("no policy associated with pod")
5151
w.WriteHeader(http.StatusBadRequest)
5252
}
5353
prioritizedNodes := m.prioritizeNodes(extenderArgs)

0 commit comments

Comments
 (0)