Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1065 from mwielgus/nodes-tests
Browse files Browse the repository at this point in the history
Cluster-autoscaler: simulator node related code tests
  • Loading branch information
mwielgus committed May 25, 2016
2 parents fc0a220 + 821eceb commit 1021eb6
Show file tree
Hide file tree
Showing 4 changed files with 598 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cluster-autoscaler/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions cluster-autoscaler/simulator/nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package simulator

import (
"bytes"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"

kube_api "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/client/restclient"
client "k8s.io/kubernetes/pkg/client/unversioned"

"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/client/unversioned/fake"
"k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/runtime"
)

type MyReq struct {
Request *http.Request
}

func (m *MyReq) isFor(method string, path string) bool {
req := m.Request
return method == req.Method && (req.URL.Path == path || req.URL.Path == strings.Join([]string{"/api/v1", path}, "") || req.URL.Path == strings.Join([]string{"/apis/extensions/v1beta1", path}, ""))
}

func TestRequiredPodsForNode(t *testing.T) {
pod1 := kube_api.Pod{
ObjectMeta: kube_api.ObjectMeta{
Namespace: "default",
Name: "pod1",
SelfLink: "pod1",
},
}
// Manifest pod.
pod2 := kube_api.Pod{
ObjectMeta: kube_api.ObjectMeta{
Name: "pod2",
Namespace: "kube-system",
SelfLink: "pod2",
Annotations: map[string]string{
types.ConfigMirrorAnnotationKey: "something",
},
},
}

header := http.Header{}
header.Set("Content-Type", runtime.ContentTypeJSON)
codec := testapi.Default.Codec()
fakeClient := &fake.RESTClient{
Codec: codec,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
m := &MyReq{req}
switch {
case m.isFor("GET", "/pods"):
return &http.Response{StatusCode: 200, Header: header,
Body: objBody(codec, &kube_api.PodList{Items: []kube_api.Pod{pod1, pod2}})}, nil
default:
t.Fatalf("unexpected request: %v %#v\n%#v", req.Method, req.URL, req)
return nil, nil
}
}),
}

clientconfig := &restclient.Config{
ContentConfig: restclient.ContentConfig{
ContentType: runtime.ContentTypeJSON,
GroupVersion: testapi.Default.GroupVersion(),
},
}

client := client.NewOrDie(clientconfig)
client.Client = fakeClient.Client
client.ExtensionsClient.Client = fakeClient.Client

pods, err := GetRequiredPodsForNode("node1", client)
assert.NoError(t, err)
assert.Equal(t, 1, len(pods))
assert.Equal(t, "pod2", pods[0].Name)
}

func objBody(codec runtime.Codec, obj runtime.Object) io.ReadCloser {
return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj))))
}
Loading

0 comments on commit 1021eb6

Please sign in to comment.