| 143 | } |
| 144 | |
| 145 | func TestCheckerWithTLSNoVerify(t *testing.T) { |
| 146 | // Listen on localhost, random port |
| 147 | certPair, err := tls.LoadX509KeyPair("testdata/leaf.pem", "testdata/leaf.key") |
| 148 | if err != nil { |
| 149 | t.Error("Failed to load certificate.", err) |
| 150 | } |
| 151 | config := tls.Config{ |
| 152 | Certificates: []tls.Certificate{certPair}, |
| 153 | } |
| 154 | srv, err := tls.Listen("tcp", "localhost:0", &config) |
| 155 | if err != nil { |
| 156 | t.Errorf("There was an error while starting TLS: %v", err) |
| 157 | } |
| 158 | defer srv.Close() |
| 159 | |
| 160 | // Accept a future connection |
| 161 | go func() { |
| 162 | for { |
| 163 | conn, err := srv.Accept() |
| 164 | if err != nil { |
| 165 | return |
| 166 | } |
| 167 | // Keep connection open for enough time to complete test |
| 168 | conn.SetDeadline(time.Now().Add(100 * time.Millisecond)) |
| 169 | tmp := make([]byte, 1) |
| 170 | conn.Read(tmp) |
| 171 | conn.Close() |
| 172 | } |
| 173 | }() |
| 174 | |
| 175 | // Should know the host:port by now |
| 176 | endpt := srv.Addr().String() |
| 177 | testName := "TestWithTLSNoVerify" |
| 178 | hc := Checker{Name: testName, URL: endpt, TLSEnabled: true, TLSSkipVerify: true, Attempts: 2} |
| 179 | |
| 180 | // Try an up server |
| 181 | result, err := hc.Check() |
| 182 | if err != nil { |
| 183 | t.Errorf("Didn't expect an error: %v", err) |
| 184 | } |
| 185 | |
| 186 | if got, want := result.Title, testName; got != want { |
| 187 | t.Errorf("Expected result.Title='%s', got '%s'", want, got) |
| 188 | } |
| 189 | if got, want := result.Endpoint, endpt; got != want { |
| 190 | t.Errorf("Expected result.Endpoint='%s', got '%s'", want, got) |
| 191 | } |
| 192 | if got, want := result.Down, false; got != want { |
| 193 | t.Errorf("Expected result.Down=%v, got %v", want, got) |
| 194 | } |
| 195 | if got, want := result.Degraded, false; got != want { |
| 196 | t.Errorf("Expected result.Degraded=%v, got %v", want, got) |
| 197 | } |
| 198 | if got, want := result.Healthy, true; got != want { |
| 199 | t.Errorf("Expected result.Healthy=%v, got %v", want, got) |
| 200 | } |
| 201 | if got, want := len(result.Times), hc.Attempts; got != want { |
| 202 | t.Errorf("Expected %d attempts, got %d", want, got) |