mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-08-28 01:47:20 +00:00
Use Patch instead of UpdateStatus.
This commit is contained in:
@@ -17,19 +17,17 @@ limitations under the License.
|
||||
package problemclient
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
||||
"k8s.io/kubernetes/pkg/client/record"
|
||||
"k8s.io/kubernetes/pkg/client/testing/core"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -37,218 +35,49 @@ const (
|
||||
testNode = "test-node"
|
||||
)
|
||||
|
||||
func newFakeProblemClient(fakeClient *fake.Clientset) *nodeProblemClient {
|
||||
func newFakeProblemClient() *nodeProblemClient {
|
||||
return &nodeProblemClient{
|
||||
nodeName: testNode,
|
||||
client: fakeClient,
|
||||
nodeName: testNode,
|
||||
// There is no proper fake for *client.Client for now
|
||||
// TODO(random-liu): Add test for SetConditions when we have good fake for *client.Client
|
||||
clock: &util.FakeClock{},
|
||||
recorders: make(map[string]record.EventRecorder),
|
||||
nodeRef: getNodeRef(testNode),
|
||||
}
|
||||
}
|
||||
|
||||
func newFakeNode(conditions []api.NodeCondition) *api.Node {
|
||||
node := &api.Node{}
|
||||
node.Name = testNode
|
||||
node.Status = api.NodeStatus{Conditions: conditions}
|
||||
return node
|
||||
}
|
||||
|
||||
type action struct {
|
||||
verb string
|
||||
resource string
|
||||
subresource string
|
||||
}
|
||||
|
||||
func TestSetConditions(t *testing.T) {
|
||||
func TestGeneratePatch(t *testing.T) {
|
||||
now := time.Now()
|
||||
expectedActions := []action{
|
||||
update := []api.NodeCondition{
|
||||
{
|
||||
verb: "get",
|
||||
resource: "nodes",
|
||||
Type: "TestType1",
|
||||
Status: api.ConditionTrue,
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "TestReason1",
|
||||
Message: "TestMessage1",
|
||||
},
|
||||
{
|
||||
verb: "update",
|
||||
resource: "nodes",
|
||||
subresource: "status",
|
||||
Type: "TestType2",
|
||||
Status: api.ConditionFalse,
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "TestReason2",
|
||||
Message: "TestMessage2",
|
||||
},
|
||||
}
|
||||
for _, test := range []struct {
|
||||
init []api.NodeCondition
|
||||
update []api.NodeCondition
|
||||
expected []api.NodeCondition
|
||||
}{
|
||||
// Init condition with the same type should be override
|
||||
{
|
||||
init: []api.NodeCondition{
|
||||
{
|
||||
Type: "TestType",
|
||||
Status: api.ConditionTrue,
|
||||
},
|
||||
},
|
||||
update: []api.NodeCondition{
|
||||
{
|
||||
Type: "TestType",
|
||||
Status: api.ConditionTrue,
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "TestReason",
|
||||
Message: "TestMessage",
|
||||
},
|
||||
},
|
||||
expected: []api.NodeCondition{
|
||||
{
|
||||
// LastHeartbeatTime should be updated in SetConditions
|
||||
Type: "TestType",
|
||||
Status: api.ConditionTrue,
|
||||
LastHeartbeatTime: unversioned.NewTime(now),
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "TestReason",
|
||||
Message: "TestMessage",
|
||||
},
|
||||
},
|
||||
},
|
||||
// Init condition with different type should be kept
|
||||
{
|
||||
init: []api.NodeCondition{
|
||||
{
|
||||
Type: "InitType",
|
||||
Status: api.ConditionTrue,
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "InitReason",
|
||||
Message: "InitMessage",
|
||||
},
|
||||
},
|
||||
update: []api.NodeCondition{
|
||||
{
|
||||
Type: "TestType",
|
||||
Status: api.ConditionTrue,
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "TestReason",
|
||||
Message: "TestMessage",
|
||||
},
|
||||
},
|
||||
expected: []api.NodeCondition{
|
||||
{
|
||||
Type: "InitType",
|
||||
Status: api.ConditionTrue,
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "InitReason",
|
||||
Message: "InitMessage",
|
||||
},
|
||||
{
|
||||
// LastHeartbeatTime should be updated in SetConditions
|
||||
Type: "TestType",
|
||||
Status: api.ConditionTrue,
|
||||
LastHeartbeatTime: unversioned.NewTime(now),
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "TestReason",
|
||||
Message: "TestMessage",
|
||||
},
|
||||
},
|
||||
},
|
||||
// Condition with false status should be removed
|
||||
{
|
||||
init: []api.NodeCondition{
|
||||
{
|
||||
Type: "TestType",
|
||||
Status: api.ConditionTrue,
|
||||
LastHeartbeatTime: unversioned.NewTime(now),
|
||||
LastTransitionTime: unversioned.NewTime(now),
|
||||
Reason: "TestReason",
|
||||
Message: "TestMessage",
|
||||
},
|
||||
},
|
||||
update: []api.NodeCondition{
|
||||
{
|
||||
Type: "TestType",
|
||||
Status: api.ConditionFalse,
|
||||
},
|
||||
},
|
||||
expected: []api.NodeCondition{},
|
||||
},
|
||||
} {
|
||||
fakeClient := fake.NewSimpleClientset(newFakeNode(test.init))
|
||||
client := newFakeProblemClient(fakeClient)
|
||||
clock := client.clock.(*util.FakeClock)
|
||||
clock.SetTime(now)
|
||||
raw, err := json.Marshal(&update)
|
||||
assert.NoError(t, err)
|
||||
expectedPatch := []byte(fmt.Sprintf(`{"status":{"conditions":%s}}`, raw))
|
||||
|
||||
client.SetConditions(test.update, 10*time.Second)
|
||||
|
||||
// The actions should match the expected actions
|
||||
actions := fakeClient.Actions()
|
||||
if len(expectedActions) != len(actions) {
|
||||
t.Errorf("expected actions %+v, got %+v", expectedActions, fakeClient.Actions())
|
||||
continue
|
||||
}
|
||||
for i, a := range actions {
|
||||
if !a.Matches(expectedActions[i].verb, expectedActions[i].resource) || a.GetSubresource() != expectedActions[i].subresource {
|
||||
t.Errorf("expected action %+v, got %+v", expectedActions[i], a)
|
||||
}
|
||||
}
|
||||
// The last action should be an update
|
||||
a, ok := actions[len(actions)-1].(core.UpdateAction)
|
||||
if !ok {
|
||||
t.Errorf("expected the last action to be update, got %+v", actions[len(actions)-1])
|
||||
}
|
||||
// The updated node conditions should match the expected conditions
|
||||
node, ok := a.GetObject().(*api.Node)
|
||||
if !ok {
|
||||
t.Errorf("expected the update object to be node, got %+v", a.GetObject())
|
||||
}
|
||||
if !api.Semantic.DeepEqual(test.expected, node.Status.Conditions) {
|
||||
t.Errorf("expected conditions %+v, got %+v", test.expected, node.Status.Conditions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetConditionsError(t *testing.T) {
|
||||
timeout := time.Duration(0)
|
||||
node := newFakeNode([]api.NodeCondition{})
|
||||
for c, test := range []struct {
|
||||
errMap map[string]error
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
// Get error
|
||||
errMap: map[string]error{"get": fmt.Errorf("get error")},
|
||||
expectedErr: fmt.Errorf("get error"),
|
||||
},
|
||||
{
|
||||
// Update error
|
||||
errMap: map[string]error{"update": fmt.Errorf("update error")},
|
||||
expectedErr: fmt.Errorf("update error"),
|
||||
},
|
||||
{
|
||||
// Timeout error
|
||||
errMap: map[string]error{
|
||||
"update": &errors.StatusError{ErrStatus: unversioned.Status{Reason: unversioned.StatusReasonConflict}},
|
||||
},
|
||||
expectedErr: timeoutError{node: testNode, timeout: timeout},
|
||||
},
|
||||
{
|
||||
// No error
|
||||
errMap: map[string]error{},
|
||||
expectedErr: nil,
|
||||
},
|
||||
} {
|
||||
fakeClient := &fake.Clientset{}
|
||||
client := newFakeProblemClient(fakeClient)
|
||||
fakeClient.AddReactor("get", "nodes", func(action core.Action) (bool, runtime.Object, error) {
|
||||
return true, node, test.errMap["get"]
|
||||
})
|
||||
fakeClient.AddReactor("update", "nodes", func(action core.Action) (bool, runtime.Object, error) {
|
||||
return true, node, test.errMap["update"]
|
||||
})
|
||||
err := client.SetConditions([]api.NodeCondition{}, timeout)
|
||||
if !reflect.DeepEqual(err, test.expectedErr) {
|
||||
t.Errorf("case %d: expected error %v, got %v", c+1, test.expectedErr, err)
|
||||
}
|
||||
patch, err := generatePatch(update)
|
||||
assert.NoError(t, err)
|
||||
if string(patch) != string(expectedPatch) {
|
||||
t.Errorf("expected patch %q, got %q", expectedPatch, patch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvent(t *testing.T) {
|
||||
fakeRecorder := record.NewFakeRecorder(1)
|
||||
client := newFakeProblemClient(&fake.Clientset{})
|
||||
client := newFakeProblemClient()
|
||||
client.recorders[testSource] = fakeRecorder
|
||||
client.Eventf(api.EventTypeWarning, testSource, "test reason", "test message")
|
||||
expected := fmt.Sprintf("%s %s %s", api.EventTypeWarning, "test reason", "test message")
|
||||
|
||||
Reference in New Issue
Block a user