github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/serviceregistration/checks/result_test.go (about) 1 package checks 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/hashicorp/nomad/nomad/structs" 8 "github.com/shoenig/test/must" 9 ) 10 11 func TestChecks_GetCheckQuery(t *testing.T) { 12 cases := []struct { 13 name string 14 cType string 15 protocol string 16 onUpdate string 17 expMode structs.CheckMode 18 expProtocol string 19 }{ 20 { 21 name: "http check and http set", 22 cType: "http", 23 protocol: "http", 24 onUpdate: "checks", 25 expMode: structs.Healthiness, 26 expProtocol: "http", 27 }, 28 { 29 name: "http check and https set", 30 cType: "http", 31 protocol: "https", 32 onUpdate: "checks", 33 expMode: structs.Healthiness, 34 expProtocol: "https", 35 }, 36 { 37 name: "http check and protocol unset", 38 cType: "http", 39 protocol: "", 40 onUpdate: "checks", 41 expMode: structs.Healthiness, 42 expProtocol: "http", // inherit default 43 }, 44 { 45 name: "tcp check and protocol unset", 46 cType: "tcp", 47 protocol: "", 48 onUpdate: "checks", 49 expMode: structs.Healthiness, 50 expProtocol: "", 51 }, 52 { 53 name: "http check and http set", 54 cType: "http", 55 protocol: "http", 56 onUpdate: "checks", 57 expMode: structs.Healthiness, 58 expProtocol: "http", 59 }, 60 { 61 name: "on-update ignore", 62 cType: "http", 63 protocol: "http", 64 onUpdate: structs.OnUpdateIgnore, 65 expMode: structs.Readiness, 66 expProtocol: "http", 67 }, 68 } 69 70 for _, tc := range cases { 71 t.Run(tc.name, func(t *testing.T) { 72 serviceCheck := &structs.ServiceCheck{ 73 Type: tc.cType, 74 Path: "/", 75 Protocol: tc.protocol, 76 PortLabel: "web", 77 AddressMode: "host", 78 Interval: 10 * time.Second, 79 Timeout: 2 * time.Second, 80 Method: "GET", 81 OnUpdate: tc.onUpdate, 82 } 83 query := GetCheckQuery(serviceCheck) 84 must.Eq(t, tc.expMode, query.Mode) 85 must.Eq(t, tc.expProtocol, query.Protocol) 86 }) 87 } 88 } 89 90 func TestChecks_Stub(t *testing.T) { 91 now := time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC).Unix() 92 result := Stub( 93 "abc123", // check id 94 structs.Healthiness, // kind 95 now, // timestamp 96 "group", "task", "service", "check", 97 ) 98 must.Eq(t, &structs.CheckQueryResult{ 99 ID: "abc123", 100 Mode: structs.Healthiness, 101 Status: structs.CheckPending, 102 Output: "nomad: waiting to run", 103 Timestamp: now, 104 Group: "group", 105 Task: "task", 106 Service: "service", 107 Check: "check", 108 }, result) 109 } 110 111 func TestChecks_AllocationResults_diff(t *testing.T) { 112 cases := []struct { 113 name string 114 results []structs.CheckID 115 ids []structs.CheckID 116 exp []structs.CheckID 117 }{{ 118 name: "empty results and empty ids", 119 results: nil, 120 ids: nil, 121 exp: nil, 122 }, { 123 name: "empty results and nonempty ids", 124 results: nil, 125 ids: []structs.CheckID{"aaa", "bbb"}, 126 exp: []structs.CheckID{"aaa", "bbb"}, 127 }, { 128 name: "nonempty results and empty ids", 129 results: []structs.CheckID{"aaa", "bbb"}, 130 ids: nil, 131 exp: nil, 132 }, { 133 name: "mix", 134 results: []structs.CheckID{"aaa", "ccc", "ddd", "fff"}, 135 ids: []structs.CheckID{"bbb", "ccc", "fff", "eee"}, 136 exp: []structs.CheckID{"bbb", "eee"}, 137 }} 138 139 for _, tc := range cases { 140 t.Run(tc.name, func(t *testing.T) { 141 ar := make(AllocationResults) 142 for _, id := range tc.results { 143 ar[id] = nil 144 } 145 result := ar.diff(tc.ids) 146 must.Eq(t, tc.exp, result) 147 }) 148 } 149 } 150 151 func TestChecks_ClientResults_Insert(t *testing.T) { 152 cr := make(ClientResults) 153 cr.Insert("alloc1", &structs.CheckQueryResult{ID: "qr1", Check: "c1"}) 154 cr.Insert("alloc2", &structs.CheckQueryResult{ID: "qr2", Check: "c2"}) 155 cr.Insert("alloc3", &structs.CheckQueryResult{ID: "qr3", Check: "c3"}) 156 cr.Insert("alloc2", &structs.CheckQueryResult{ID: "qr2", Check: "c4"}) // overwrite 157 cr.Insert("alloc3", &structs.CheckQueryResult{ID: "qr4", Check: "c5"}) 158 exp := ClientResults{ 159 "alloc1": { 160 "qr1": &structs.CheckQueryResult{ID: "qr1", Check: "c1"}, 161 }, 162 "alloc2": { 163 "qr2": &structs.CheckQueryResult{ID: "qr2", Check: "c4"}, 164 }, 165 "alloc3": { 166 "qr3": &structs.CheckQueryResult{ID: "qr3", Check: "c3"}, 167 "qr4": &structs.CheckQueryResult{ID: "qr4", Check: "c5"}, 168 }, 169 } 170 must.Eq(t, exp, cr) 171 }