mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-08-28 01:47:20 +00:00
Fix unit test.
This commit is contained in:
@@ -17,10 +17,11 @@ limitations under the License.
|
|||||||
package condition
|
package condition
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"k8s.io/node-problem-detector/pkg/problemclient"
|
"k8s.io/node-problem-detector/pkg/problemclient"
|
||||||
"k8s.io/node-problem-detector/pkg/types"
|
"k8s.io/node-problem-detector/pkg/types"
|
||||||
problemutil "k8s.io/node-problem-detector/pkg/util"
|
problemutil "k8s.io/node-problem-detector/pkg/util"
|
||||||
@@ -36,9 +37,9 @@ func newTestManager() (*conditionManager, *problemclient.FakeProblemClient, *uti
|
|||||||
return manager.(*conditionManager), fakeClient, fakeClock
|
return manager.(*conditionManager), fakeClient, fakeClock
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestCondition() types.Condition {
|
func newTestCondition(condition string) types.Condition {
|
||||||
return types.Condition{
|
return types.Condition{
|
||||||
Type: "TestCondition",
|
Type: condition,
|
||||||
Status: true,
|
Status: true,
|
||||||
Transition: time.Now(),
|
Transition: time.Now(),
|
||||||
Reason: "TestReason",
|
Reason: "TestReason",
|
||||||
@@ -47,35 +48,46 @@ func newTestCondition() types.Condition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckUpdates(t *testing.T) {
|
func TestCheckUpdates(t *testing.T) {
|
||||||
condition := newTestCondition()
|
|
||||||
m, _, _ := newTestManager()
|
m, _, _ := newTestManager()
|
||||||
m.UpdateCondition(condition)
|
var c types.Condition
|
||||||
if !m.checkUpdates() {
|
for desc, test := range map[string]struct {
|
||||||
t.Error("expected checkUpdates to be true, got false")
|
condition string
|
||||||
}
|
update bool
|
||||||
if !reflect.DeepEqual(condition, m.conditions[condition.Type]) {
|
}{
|
||||||
t.Errorf("expected %+v, got %+v", condition, m.conditions[condition.Type])
|
"Init condition needs update": {
|
||||||
}
|
condition: "TestCondition",
|
||||||
if m.checkUpdates() {
|
update: true,
|
||||||
t.Error("expected checkUpdates to be false, got true")
|
},
|
||||||
|
"Same condition doesn't need update": {
|
||||||
|
// not set condition, the test will reuse the condition in last case.
|
||||||
|
update: false,
|
||||||
|
},
|
||||||
|
"Same condition with different timestamp need update": {
|
||||||
|
condition: "TestCondition",
|
||||||
|
update: true,
|
||||||
|
},
|
||||||
|
"New condition needs update": {
|
||||||
|
condition: "TestConditionNew",
|
||||||
|
update: true,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
if test.condition != "" {
|
||||||
|
c = newTestCondition(test.condition)
|
||||||
|
}
|
||||||
|
m.UpdateCondition(c)
|
||||||
|
assert.Equal(t, test.update, m.checkUpdates(), desc)
|
||||||
|
assert.Equal(t, c, m.conditions[c.Type], desc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSync(t *testing.T) {
|
func TestSync(t *testing.T) {
|
||||||
m, fakeClient, fakeClock := newTestManager()
|
m, fakeClient, fakeClock := newTestManager()
|
||||||
condition := newTestCondition()
|
condition := newTestCondition("TestCondition")
|
||||||
m.conditions = map[string]types.Condition{condition.Type: condition}
|
m.conditions = map[string]types.Condition{condition.Type: condition}
|
||||||
m.sync()
|
m.sync()
|
||||||
expected := []api.NodeCondition{problemutil.ConvertToAPICondition(condition)}
|
expected := []api.NodeCondition{problemutil.ConvertToAPICondition(condition)}
|
||||||
err := fakeClient.AssertConditions(expected)
|
assert.Nil(t, fakeClient.AssertConditions(expected), "Condition should be updated via client")
|
||||||
if err != nil {
|
assert.False(t, m.checkResync(), "Should not resync before timeout exceeds")
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if m.checkResync() {
|
|
||||||
t.Error("expected checkResync to be false, got true")
|
|
||||||
}
|
|
||||||
fakeClock.Step(resyncPeriod)
|
fakeClock.Step(resyncPeriod)
|
||||||
if !m.checkResync() {
|
assert.True(t, m.checkResync(), "Should resync after timeout exceeds")
|
||||||
t.Error("expected checkResync to be true, got false")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
)
|
)
|
||||||
@@ -61,19 +60,14 @@ func (f *FakeProblemClient) AssertConditions(expected []api.NodeCondition) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetConditions is a fake mimic of SetConditions, it only update the internal condition cache.
|
// SetConditions is a fake mimic of SetConditions, it only update the internal condition cache.
|
||||||
func (f *FakeProblemClient) SetConditions(conditions []api.NodeCondition, timeout time.Duration) error {
|
func (f *FakeProblemClient) SetConditions(conditions []api.NodeCondition) error {
|
||||||
f.Lock()
|
f.Lock()
|
||||||
defer f.Unlock()
|
defer f.Unlock()
|
||||||
if err, ok := f.errors["SetConditions"]; ok {
|
if err, ok := f.errors["SetConditions"]; ok {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, condition := range conditions {
|
for _, condition := range conditions {
|
||||||
t := condition.Type
|
f.conditions[condition.Type] = condition
|
||||||
if condition.Status == api.ConditionFalse {
|
|
||||||
delete(f.conditions, t)
|
|
||||||
} else {
|
|
||||||
f.conditions[t] = condition
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user