github.com/m3db/m3@v1.5.0/src/ctl/service/health/service_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE
    20  
    21  package health
    22  
    23  import (
    24  	"encoding/json"
    25  	"net/http"
    26  	"net/http/httptest"
    27  	"os"
    28  	"testing"
    29  
    30  	"github.com/m3db/m3/src/x/instrument"
    31  
    32  	"github.com/gorilla/mux"
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func TestHostName(t *testing.T) {
    37  	expectedName, err := os.Hostname()
    38  	require.NoError(t, err, "Failed to get system hostname")
    39  	actualName := hostName()
    40  
    41  	require.Equal(t, expectedName, actualName)
    42  }
    43  
    44  func TestHealthCheck(t *testing.T) {
    45  	rr := httptest.NewRecorder()
    46  	// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
    47  	// pass 'nil' as the third parameter.
    48  	req, err := http.NewRequest("GET", "/health", nil)
    49  	require.NoError(t, err)
    50  
    51  	opts := instrument.NewOptions()
    52  	service := NewService(opts)
    53  	mux := mux.NewRouter().PathPrefix(service.URLPrefix()).Subrouter()
    54  	err = service.RegisterHandlers(mux)
    55  	require.NoError(t, err)
    56  
    57  	mux.ServeHTTP(rr, req)
    58  
    59  	rawResult := make([]byte, rr.Body.Len())
    60  	_, err = rr.Body.Read(rawResult)
    61  	require.NoError(t, err, "Encountered error parsing response")
    62  
    63  	var actualResult healthCheckResult
    64  	json.Unmarshal(rawResult, &actualResult)
    65  
    66  	name, _ := os.Hostname()
    67  
    68  	require.Equal(t, name, actualResult.Host)
    69  	require.Equal(t, ok, actualResult.Status)
    70  }