k3s/pkg/controller/replication_controller_test.go

375 lines
10 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +00:00
/*
Copyright 2014 Google Inc. 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 controller
2014-06-06 23:40:48 +00:00
import (
"fmt"
"net/http"
2014-06-06 23:40:48 +00:00
"net/http/httptest"
"path"
"sync"
2014-06-06 23:40:48 +00:00
"testing"
"time"
2014-06-06 23:40:48 +00:00
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
2014-06-06 23:40:48 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
2014-06-06 23:40:48 +00:00
"github.com/coreos/go-etcd/etcd"
)
func makeNamespaceURL(namespace, suffix string) string {
if !(testapi.Version() == "v1beta1" || testapi.Version() == "v1beta2") {
return makeURL("/ns/" + namespace + suffix)
}
return makeURL(suffix + "?namespace=" + namespace)
}
2014-07-10 11:47:10 +00:00
func makeURL(suffix string) string {
return path.Join("/api", testapi.Version(), suffix)
2014-06-06 23:40:48 +00:00
}
2014-06-09 05:38:45 +00:00
type FakePodControl struct {
2014-06-12 20:17:34 +00:00
controllerSpec []api.ReplicationController
2014-10-23 20:55:48 +00:00
deletePodName []string
lock sync.Mutex
2014-06-06 23:40:48 +00:00
}
2014-10-23 20:55:48 +00:00
func (f *FakePodControl) createReplica(namespace string, spec api.ReplicationController) {
f.lock.Lock()
defer f.lock.Unlock()
2014-06-06 23:40:48 +00:00
f.controllerSpec = append(f.controllerSpec, spec)
}
2014-10-23 20:55:48 +00:00
func (f *FakePodControl) deletePod(namespace string, podName string) error {
f.lock.Lock()
defer f.lock.Unlock()
2014-10-23 20:55:48 +00:00
f.deletePodName = append(f.deletePodName, podName)
2014-06-06 23:40:48 +00:00
return nil
}
func newReplicationController(replicas int) api.ReplicationController {
2014-06-12 20:17:34 +00:00
return api.ReplicationController{
Spec: api.ReplicationControllerSpec{
2014-06-06 23:40:48 +00:00
Replicas: replicas,
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "foo",
"type": "production",
2014-06-06 23:40:48 +00:00
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Image: "foo/bar",
},
},
NodeSelector: map[string]string{
"baz": "blah",
},
2014-06-06 23:40:48 +00:00
},
},
},
}
}
2014-09-08 04:14:18 +00:00
func newPodList(count int) *api.PodList {
2014-06-12 20:17:34 +00:00
pods := []api.Pod{}
2014-06-06 23:40:48 +00:00
for i := 0; i < count; i++ {
2014-06-12 20:17:34 +00:00
pods = append(pods, api.Pod{
ObjectMeta: api.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: fmt.Sprintf("pod%d", i),
2014-06-06 23:40:48 +00:00
},
})
}
2014-09-08 04:14:18 +00:00
return &api.PodList{
2014-06-09 05:38:45 +00:00
Items: pods,
2014-06-06 23:40:48 +00:00
}
}
2014-06-09 05:38:45 +00:00
func validateSyncReplication(t *testing.T, fakePodControl *FakePodControl, expectedCreates, expectedDeletes int) {
if len(fakePodControl.controllerSpec) != expectedCreates {
t.Errorf("Unexpected number of creates. Expected %d, saw %d\n", expectedCreates, len(fakePodControl.controllerSpec))
2014-06-06 23:40:48 +00:00
}
2014-10-23 20:55:48 +00:00
if len(fakePodControl.deletePodName) != expectedDeletes {
t.Errorf("Unexpected number of deletes. Expected %d, saw %d\n", expectedDeletes, len(fakePodControl.deletePodName))
2014-06-06 23:40:48 +00:00
}
}
func TestSyncReplicationControllerDoesNothing(t *testing.T) {
body, _ := latest.Codec.Encode(newPodList(2))
2014-06-06 23:40:48 +00:00
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
2014-10-31 01:15:44 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
2014-06-06 23:40:48 +00:00
2014-06-09 05:38:45 +00:00
fakePodControl := FakePodControl{}
2014-06-06 23:40:48 +00:00
manager := NewReplicationManager(client)
2014-06-09 05:38:45 +00:00
manager.podControl = &fakePodControl
2014-06-06 23:40:48 +00:00
controllerSpec := newReplicationController(2)
2014-06-06 23:40:48 +00:00
manager.syncReplicationController(controllerSpec)
2014-06-09 05:38:45 +00:00
validateSyncReplication(t, &fakePodControl, 0, 0)
2014-06-06 23:40:48 +00:00
}
func TestSyncReplicationControllerDeletes(t *testing.T) {
body, _ := latest.Codec.Encode(newPodList(2))
2014-06-06 23:40:48 +00:00
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
2014-10-31 01:15:44 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
2014-06-06 23:40:48 +00:00
2014-06-09 05:38:45 +00:00
fakePodControl := FakePodControl{}
2014-06-06 23:40:48 +00:00
manager := NewReplicationManager(client)
2014-06-09 05:38:45 +00:00
manager.podControl = &fakePodControl
2014-06-06 23:40:48 +00:00
controllerSpec := newReplicationController(1)
2014-06-06 23:40:48 +00:00
manager.syncReplicationController(controllerSpec)
2014-06-09 05:38:45 +00:00
validateSyncReplication(t, &fakePodControl, 0, 1)
2014-06-06 23:40:48 +00:00
}
func TestSyncReplicationControllerCreates(t *testing.T) {
body := runtime.EncodeOrDie(testapi.Codec(), newPodList(0))
2014-06-06 23:40:48 +00:00
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
2014-10-31 01:15:44 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
2014-06-06 23:40:48 +00:00
2014-06-09 05:38:45 +00:00
fakePodControl := FakePodControl{}
2014-06-06 23:40:48 +00:00
manager := NewReplicationManager(client)
2014-06-09 05:38:45 +00:00
manager.podControl = &fakePodControl
2014-06-06 23:40:48 +00:00
controllerSpec := newReplicationController(2)
2014-06-06 23:40:48 +00:00
manager.syncReplicationController(controllerSpec)
2014-06-09 05:38:45 +00:00
validateSyncReplication(t, &fakePodControl, 2, 0)
2014-06-06 23:40:48 +00:00
}
func TestCreateReplica(t *testing.T) {
2014-10-23 20:55:48 +00:00
ns := api.NamespaceDefault
body := runtime.EncodeOrDie(testapi.Codec(), &api.Pod{})
2014-06-06 23:40:48 +00:00
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
2014-10-31 01:15:44 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
2014-06-06 23:40:48 +00:00
2014-06-09 05:38:45 +00:00
podControl := RealPodControl{
2014-06-06 23:40:48 +00:00
kubeClient: client,
}
2014-06-12 20:17:34 +00:00
controllerSpec := api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "test",
2014-07-16 21:30:28 +00:00
},
Spec: api.ReplicationControllerSpec{
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "foo",
"type": "production",
"replicationController": "test",
2014-06-06 23:40:48 +00:00
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Image: "foo/bar",
},
},
NodeSelector: map[string]string{
"foo": "bar",
},
2014-06-06 23:40:48 +00:00
},
},
},
}
2014-10-23 20:55:48 +00:00
podControl.createReplica(ns, controllerSpec)
2014-06-06 23:40:48 +00:00
manifest := api.ContainerManifest{}
if err := api.Scheme.Convert(&controllerSpec.Spec.Template.Spec, &manifest); err != nil {
2014-11-20 12:42:48 +00:00
t.Fatalf("unexpected error: %v", err)
}
expectedPod := api.Pod{
ObjectMeta: api.ObjectMeta{
Labels: controllerSpec.Spec.Template.Labels,
GenerateName: fmt.Sprintf("%s-", controllerSpec.Name),
},
2014-11-13 15:52:13 +00:00
Spec: controllerSpec.Spec.Template.Spec,
}
fakeHandler.ValidateRequest(t, makeNamespaceURL("default", "/pods"), "POST", nil)
actualPod, err := client.Codec.Decode([]byte(fakeHandler.RequestBody))
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
if !api.Semantic.DeepDerivative(&expectedPod, actualPod) {
t.Logf("Body: %s", fakeHandler.RequestBody)
t.Errorf("Unexpected mismatch. Expected\n %#v,\n Got:\n %#v", &expectedPod, actualPod)
}
2014-06-06 23:40:48 +00:00
}
func TestSynchonize(t *testing.T) {
controllerSpec1 := api.ReplicationController{
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
Spec: api.ReplicationControllerSpec{
Replicas: 4,
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "foo",
"type": "production",
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Image: "foo/bar",
},
},
},
},
},
}
controllerSpec2 := api.ReplicationController{
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
Spec: api.ReplicationControllerSpec{
Replicas: 3,
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "bar",
"type": "production",
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Image: "bar/baz",
},
},
},
},
},
}
fakeEtcd := tools.NewFakeEtcdClient(t)
fakeEtcd.Data["/registry/controllers"] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Nodes: []*etcd.Node{
{
Value: runtime.EncodeOrDie(testapi.Codec(), &controllerSpec1),
},
{
Value: runtime.EncodeOrDie(testapi.Codec(), &controllerSpec2),
},
},
},
},
}
fakePodHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: "{\"apiVersion\": \"" + testapi.Version() + "\", \"kind\": \"PodList\"}",
T: t,
}
fakeControllerHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: runtime.EncodeOrDie(latest.Codec, &api.ReplicationControllerList{
Items: []api.ReplicationController{
controllerSpec1,
controllerSpec2,
},
}),
T: t,
}
mux := http.NewServeMux()
mux.Handle("/api/"+testapi.Version()+"/pods/", &fakePodHandler)
mux.Handle("/api/"+testapi.Version()+"/replicationControllers/", &fakeControllerHandler)
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
t.Errorf("Unexpected request for %v", req.RequestURI)
})
2014-08-05 23:20:50 +00:00
testServer := httptest.NewServer(mux)
2014-10-31 01:15:44 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
manager := NewReplicationManager(client)
fakePodControl := FakePodControl{}
manager.podControl = &fakePodControl
manager.synchronize()
validateSyncReplication(t, &fakePodControl, 7, 0)
}
2014-08-05 23:20:50 +00:00
type FakeWatcher struct {
w *watch.FakeWatcher
*client.Fake
2014-08-05 23:20:50 +00:00
}
2014-08-05 23:20:50 +00:00
func TestWatchControllers(t *testing.T) {
2014-10-23 20:55:48 +00:00
fakeWatch := watch.NewFake()
client := &client.Fake{Watch: fakeWatch}
manager := NewReplicationManager(client)
var testControllerSpec api.ReplicationController
received := make(chan struct{})
manager.syncHandler = func(controllerSpec api.ReplicationController) error {
if !api.Semantic.DeepDerivative(controllerSpec, testControllerSpec) {
t.Errorf("Expected %#v, but got %#v", testControllerSpec, controllerSpec)
}
2014-06-30 21:48:57 +00:00
close(received)
return nil
}
resourceVersion := ""
go manager.watchControllers(&resourceVersion)
2014-06-30 21:48:57 +00:00
// Test normal case
2014-10-22 17:02:02 +00:00
testControllerSpec.Name = "foo"
2014-10-23 20:55:48 +00:00
fakeWatch.Add(&testControllerSpec)
2014-06-30 21:48:57 +00:00
select {
case <-received:
case <-time.After(10 * time.Millisecond):
t.Errorf("Expected 1 call but got 0")
}
}