More reliable updates in rollingupdate
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
@@ -183,6 +184,96 @@ func TestAddDeploymentHash(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateWithRetries(t *testing.T) {
|
||||
f, tf, codec := NewAPIFactory()
|
||||
rc := &api.ReplicationController{
|
||||
ObjectMeta: api.ObjectMeta{Name: "rc",
|
||||
Labels: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
Spec: api.ReplicationControllerSpec{
|
||||
Selector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
Template: &api.PodTemplateSpec{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Labels: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
RestartPolicy: api.RestartPolicyAlways,
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Test end to end updating of the rc with retries. Essentially make sure the update handler
|
||||
// sees the right updates, failures in update/get are handled properly, and that the updated
|
||||
// rc with new resource version is returned to the caller. Without any of these rollingupdate
|
||||
// will fail cryptically.
|
||||
newRc := *rc
|
||||
newRc.ResourceVersion = "2"
|
||||
newRc.Spec.Selector["baz"] = "foobar"
|
||||
updates := []*http.Response{
|
||||
{StatusCode: 500, Body: objBody(codec, &api.ReplicationController{})},
|
||||
{StatusCode: 500, Body: objBody(codec, &api.ReplicationController{})},
|
||||
{StatusCode: 200, Body: objBody(codec, &newRc)},
|
||||
}
|
||||
gets := []*http.Response{
|
||||
{StatusCode: 500, Body: objBody(codec, &api.ReplicationController{})},
|
||||
{StatusCode: 200, Body: objBody(codec, rc)},
|
||||
}
|
||||
tf.Client = &client.FakeRESTClient{
|
||||
Codec: codec,
|
||||
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||
switch p, m := req.URL.Path, req.Method; {
|
||||
case p == "/api/v1beta3/namespaces/default/replicationcontrollers/rc" && m == "PUT":
|
||||
update := updates[0]
|
||||
updates = updates[1:]
|
||||
// We should always get an update with a valid rc even when the get fails. The rc should always
|
||||
// contain the update.
|
||||
if c, ok := readOrDie(t, req, codec).(*api.ReplicationController); !ok || !reflect.DeepEqual(rc, c) {
|
||||
t.Errorf("Unexpected update body, got %+v expected %+v", c, rc)
|
||||
} else if sel, ok := c.Spec.Selector["baz"]; !ok || sel != "foobar" {
|
||||
t.Errorf("Expected selector label update, got %+v", c.Spec.Selector)
|
||||
} else {
|
||||
delete(c.Spec.Selector, "baz")
|
||||
}
|
||||
return update, nil
|
||||
case p == "/api/v1beta3/namespaces/default/replicationcontrollers/rc" && m == "GET":
|
||||
get := gets[0]
|
||||
gets = gets[1:]
|
||||
return get, nil
|
||||
default:
|
||||
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||
return nil, nil
|
||||
}
|
||||
}),
|
||||
}
|
||||
tf.ClientConfig = &client.Config{Version: latest.Version}
|
||||
client, err := f.Client()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if rc, err := updateWithRetries(
|
||||
client.ReplicationControllers("default"), rc, func(c *api.ReplicationController) {
|
||||
c.Spec.Selector["baz"] = "foobar"
|
||||
}); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
} else if sel, ok := rc.Spec.Selector["baz"]; !ok || sel != "foobar" || rc.ResourceVersion != "2" {
|
||||
t.Errorf("Expected updated rc, got %+v", rc)
|
||||
}
|
||||
if len(updates) != 0 || len(gets) != 0 {
|
||||
t.Errorf("Remaining updates %+v gets %+v", updates, gets)
|
||||
}
|
||||
}
|
||||
|
||||
func readOrDie(t *testing.T, req *http.Request, codec runtime.Codec) runtime.Object {
|
||||
data, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user