Skip to content

Commit f265f79

Browse files
authored
Merge pull request #31 from Random-Liu/enable-travis-test
NPD: Enable travis test
2 parents ea83111 + 8f6ba9e commit f265f79

File tree

12 files changed

+570
-38
lines changed

12 files changed

+570
-38
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: go
2+
go:
3+
- 1.6
4+
install:
5+
- mkdir -p $HOME/gopath/src/k8s.io
6+
- mv $TRAVIS_BUILD_DIR $HOME/gopath/src/k8s.io/node-problem-detector
7+
- cd $HOME/gopath/src/k8s.io/node-problem-detector
8+
script:
9+
- go test -v -race ./pkg/...
10+
- go build -race

Godeps/Godeps.json

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/condition/manager_test.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ limitations under the License.
1717
package condition
1818

1919
import (
20-
"reflect"
2120
"testing"
2221
"time"
2322

23+
"github.com/stretchr/testify/assert"
24+
2425
"k8s.io/node-problem-detector/pkg/problemclient"
2526
"k8s.io/node-problem-detector/pkg/types"
2627
problemutil "k8s.io/node-problem-detector/pkg/util"
@@ -36,9 +37,9 @@ func newTestManager() (*conditionManager, *problemclient.FakeProblemClient, *uti
3637
return manager.(*conditionManager), fakeClient, fakeClock
3738
}
3839

39-
func newTestCondition() types.Condition {
40+
func newTestCondition(condition string) types.Condition {
4041
return types.Condition{
41-
Type: "TestCondition",
42+
Type: condition,
4243
Status: true,
4344
Transition: time.Now(),
4445
Reason: "TestReason",
@@ -47,35 +48,46 @@ func newTestCondition() types.Condition {
4748
}
4849

4950
func TestCheckUpdates(t *testing.T) {
50-
condition := newTestCondition()
5151
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)
6180
}
6281
}
6382

6483
func TestSync(t *testing.T) {
6584
m, fakeClient, fakeClock := newTestManager()
66-
condition := newTestCondition()
85+
condition := newTestCondition("TestCondition")
6786
m.conditions = map[string]types.Condition{condition.Type: condition}
6887
m.sync()
6988
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")
7791
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")
8193
}

pkg/problemclient/fake_problem_client.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"reflect"
2222
"sync"
23-
"time"
2423

2524
"k8s.io/kubernetes/pkg/api"
2625
)
@@ -61,19 +60,14 @@ func (f *FakeProblemClient) AssertConditions(expected []api.NodeCondition) error
6160
}
6261

6362
// SetConditions is a fake mimic of SetConditions, it only update the internal condition cache.
64-
func (f *FakeProblemClient) SetConditions(conditions []api.NodeCondition, timeout time.Duration) error {
63+
func (f *FakeProblemClient) SetConditions(conditions []api.NodeCondition) error {
6564
f.Lock()
6665
defer f.Unlock()
6766
if err, ok := f.errors["SetConditions"]; ok {
6867
return err
6968
}
7069
for _, condition := range conditions {
71-
t := condition.Type
72-
if condition.Status == api.ConditionFalse {
73-
delete(f.conditions, t)
74-
} else {
75-
f.conditions[t] = condition
76-
}
70+
f.conditions[condition.Type] = condition
7771
}
7872
return nil
7973
}

0 commit comments

Comments
 (0)