@@ -9,6 +9,45 @@ import (
99 "github.com/square/p2/pkg/kp"
1010)
1111
12+ func TestPickHealthResult (t * testing.T ) {
13+ catalogResults := []Result {Result {Status : Passing }}
14+ kvCheck := kp.WatchResult {Status : string (Critical )}
15+ errCh := make (chan error )
16+ resultCh := make (chan Result )
17+
18+ go pickHealthResult ([]Result {}, kp.WatchResult {}, fmt .Errorf ("no kvCheck" ), resultCh , errCh )
19+ select {
20+ case _ = <- resultCh :
21+ t .Fatal ("pickhealthresult was passed no results but did not return an error" )
22+ case err := <- errCh :
23+ Assert (t ).AreNotEqual (err , nil , "err should not have been nil" )
24+ }
25+
26+ go pickHealthResult ([]Result {}, kvCheck , nil , resultCh , errCh )
27+ select {
28+ case res := <- resultCh :
29+ Assert (t ).AreEqual (res , Result {Status : Critical }, "pick health result did not return the correct kv result" )
30+ case err := <- errCh :
31+ t .Fatal (fmt .Sprintf ("pickhealthresult returned an error: %s, but was passed a valid kvCheck" , err ))
32+ }
33+
34+ go pickHealthResult (catalogResults , kp.WatchResult {}, fmt .Errorf ("no kvCheck" ), resultCh , errCh )
35+ select {
36+ case res := <- resultCh :
37+ Assert (t ).AreEqual (res , Result {Status : Passing }, "pick health result did not return the correct kv result" )
38+ case err := <- errCh :
39+ t .Fatal (fmt .Sprintf ("pickhealthresult returned an error: %s, but was passed a valid catalogue check" , err ))
40+ }
41+
42+ go pickHealthResult (catalogResults , kvCheck , nil , resultCh , errCh )
43+ select {
44+ case res := <- resultCh :
45+ Assert (t ).AreEqual (res , Result {Status : Passing }, "pick health result did not return the correct kv result" )
46+ case err := <- errCh :
47+ t .Fatal (fmt .Sprintf ("pickhealthresult returned an error: %s, but was passed a valid checks from both sources" , err ))
48+ }
49+ }
50+
1251func TestFindWorst (t * testing.T ) {
1352 a := Result {
1453 ID : "testcrit" ,
@@ -23,14 +62,17 @@ func TestFindWorst(t *testing.T) {
2362 Status : Passing ,
2463 }
2564
26- id , _ := FindWorst ([]Result {a , b })
65+ id , _ , _ := FindWorst ([]Result {a , b })
2766 Assert (t ).AreEqual (id , a .ID , "FindWorst between critical and warning should have returned testcrit" )
2867
29- id , _ = FindWorst ([]Result {b , c })
68+ id , _ , _ = FindWorst ([]Result {b , c })
3069 Assert (t ).AreEqual (id , b .ID , "FindWorst between warning and passing should have returned testwarn" )
3170
32- id , _ = FindWorst ([]Result {c , c })
71+ id , _ , _ = FindWorst ([]Result {c , c })
3372 Assert (t ).AreEqual (id , c .ID , "FindWorst between two passing results should have returned testpass" )
73+
74+ id , _ , err := FindWorst ([]Result {})
75+ Assert (t ).AreNotEqual (err , nil , "FindWorst did not return error for empty result slice" )
3476}
3577
3678func TestPickServiceResult (t * testing.T ) {
@@ -39,26 +81,24 @@ func TestPickServiceResult(t *testing.T) {
3981 t3 := mockServiceEntry ("3" , Passing )
4082
4183 // Catalog is healthy but KV is not present
42- testMap := getResult ([]* api.ServiceEntry {t1 , t2 , t3 })
84+ testMap , err := getResult ([]* api.ServiceEntry {t1 , t2 , t3 })
85+ Assert (t ).AreEqual (err , nil , "getResult failed" )
4386 catalog := []* api.ServiceEntry {t1 , t2 , t3 }
44- res , err := selectResult (catalog , nil )
45- Assert (t ).AreEqual (err , nil , "catalog is healthy, kv not present and selectResult returned err" )
87+ res := selectResult (catalog , nil )
4688 for key , value := range testMap {
4789 Assert (t ).AreEqual (res [key ], value , "catalog is healthy, kv not present, selectResult did not match what was expected" )
4890 }
4991
5092 // Catalog is healthy but KV is not
5193 watchRes := mockWatchResult (catalog , Critical )
52- res , err = selectResult (catalog , watchRes )
53- Assert (t ).AreEqual (err , nil , "catalog is healthy but kv is not and selectResult returned err" )
94+ res = selectResult (catalog , watchRes )
5495 for key , value := range testMap {
5596 Assert (t ).AreEqual (res [key ], value , "catalog is healthy, kv is not, selectResult did not match what was expected" )
5697 }
5798
5899 // KV is healthy but catalog is not present
59100 kv := mockWatchResult (catalog , Passing )
60- res , err = selectResult (nil , kv )
61- Assert (t ).AreEqual (err , nil , "kv is healthy, catalog not present and selectResult returned err" )
101+ res = selectResult (nil , kv )
62102 for key , value := range testMap {
63103 Assert (t ).AreEqual (consulWatchToResult (kv [key ]), value , "kv is healthy, catalog not present, selectResult did not match what was expected" )
64104 }
@@ -69,8 +109,7 @@ func TestPickServiceResult(t *testing.T) {
69109 catalog = []* api.ServiceEntry {t1 , t2 , t3 }
70110
71111 // KV is healthy but catalog is not
72- res , err = selectResult (catalog , kv )
73- Assert (t ).AreEqual (err , nil , "kv is healthy, catalog is not and selectResult returned err" )
112+ res = selectResult (catalog , kv )
74113 for key , value := range testMap {
75114 Assert (t ).AreEqual (consulWatchToResult (kv [key ]), value , "kv is healthy, catalog is not and selectResult did not match what was expected" )
76115 }
@@ -125,17 +164,21 @@ func mockWatchResult(entries []*api.ServiceEntry, st HealthState) map[string]kp.
125164 return ret
126165}
127166
128- func getResult (entries []* api.ServiceEntry ) map [string ]Result {
167+ func getResult (entries []* api.ServiceEntry ) (map [string ]Result , error ) {
168+ var HEErr * HealthEmpty
129169 res := make (map [string ]Result )
130170 for _ , entry := range entries {
131171 val := make ([]Result , 0 , len (entry .Checks ))
132172 for _ , check := range entry .Checks {
133173 val = append (val , consulCheckToResult (* check ))
134174 }
135- res [entry .Node .Node ] = findWorstResult (val )
175+ res [entry .Node .Node ], HEErr = findWorstResult (val )
176+ if HEErr != nil {
177+ return res , HEErr
178+ }
136179 }
137180
138- return res
181+ return res , nil
139182}
140183
141184func mockServiceEntry (s string , st HealthState ) * api.ServiceEntry {
0 commit comments