@@ -17,10 +17,11 @@ limitations under the License.
17
17
package condition
18
18
19
19
import (
20
- "reflect"
21
20
"testing"
22
21
"time"
23
22
23
+ "github.com/stretchr/testify/assert"
24
+
24
25
"k8s.io/node-problem-detector/pkg/problemclient"
25
26
"k8s.io/node-problem-detector/pkg/types"
26
27
problemutil "k8s.io/node-problem-detector/pkg/util"
@@ -36,9 +37,9 @@ func newTestManager() (*conditionManager, *problemclient.FakeProblemClient, *uti
36
37
return manager .(* conditionManager ), fakeClient , fakeClock
37
38
}
38
39
39
- func newTestCondition () types.Condition {
40
+ func newTestCondition (condition string ) types.Condition {
40
41
return types.Condition {
41
- Type : "TestCondition" ,
42
+ Type : condition ,
42
43
Status : true ,
43
44
Transition : time .Now (),
44
45
Reason : "TestReason" ,
@@ -47,35 +48,46 @@ func newTestCondition() types.Condition {
47
48
}
48
49
49
50
func TestCheckUpdates (t * testing.T ) {
50
- condition := newTestCondition ()
51
51
m , _ , _ := newTestManager ()
52
- m .UpdateCondition (condition )
53
- if ! m .checkUpdates () {
54
- t .Error ("expected checkUpdates to be true, got false" )
55
- }
56
- if ! reflect .DeepEqual (condition , m .conditions [condition .Type ]) {
57
- t .Errorf ("expected %+v, got %+v" , condition , m .conditions [condition .Type ])
58
- }
59
- if m .checkUpdates () {
60
- t .Error ("expected checkUpdates to be false, got true" )
52
+ var c types.Condition
53
+ for desc , test := range map [string ]struct {
54
+ condition string
55
+ update bool
56
+ }{
57
+ "Init condition needs update" : {
58
+ condition : "TestCondition" ,
59
+ update : true ,
60
+ },
61
+ "Same condition doesn't need update" : {
62
+ // not set condition, the test will reuse the condition in last case.
63
+ update : false ,
64
+ },
65
+ "Same condition with different timestamp need update" : {
66
+ condition : "TestCondition" ,
67
+ update : true ,
68
+ },
69
+ "New condition needs update" : {
70
+ condition : "TestConditionNew" ,
71
+ update : true ,
72
+ },
73
+ } {
74
+ if test .condition != "" {
75
+ c = newTestCondition (test .condition )
76
+ }
77
+ m .UpdateCondition (c )
78
+ assert .Equal (t , test .update , m .checkUpdates (), desc )
79
+ assert .Equal (t , c , m .conditions [c .Type ], desc )
61
80
}
62
81
}
63
82
64
83
func TestSync (t * testing.T ) {
65
84
m , fakeClient , fakeClock := newTestManager ()
66
- condition := newTestCondition ()
85
+ condition := newTestCondition ("TestCondition" )
67
86
m .conditions = map [string ]types.Condition {condition .Type : condition }
68
87
m .sync ()
69
88
expected := []api.NodeCondition {problemutil .ConvertToAPICondition (condition )}
70
- err := fakeClient .AssertConditions (expected )
71
- if err != nil {
72
- t .Error (err )
73
- }
74
- if m .checkResync () {
75
- t .Error ("expected checkResync to be false, got true" )
76
- }
89
+ assert .Nil (t , fakeClient .AssertConditions (expected ), "Condition should be updated via client" )
90
+ assert .False (t , m .checkResync (), "Should not resync before timeout exceeds" )
77
91
fakeClock .Step (resyncPeriod )
78
- if ! m .checkResync () {
79
- t .Error ("expected checkResync to be true, got false" )
80
- }
92
+ assert .True (t , m .checkResync (), "Should resync after timeout exceeds" )
81
93
}
0 commit comments