github.com/splucs/witchcraft-go-server@v1.7.0/status/reporter/component_test.go (about) 1 // Copyright (c) 2018 Palantir Technologies. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package reporter 16 17 import ( 18 "context" 19 "errors" 20 "testing" 21 22 "github.com/palantir/witchcraft-go-server/conjure/witchcraft/api/health" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 const ( 27 validComponent = "TEST_COMPONENT" 28 invalidComponent = "test-invalid" 29 ) 30 31 func setup(t *testing.T) (HealthComponent, HealthReporter) { 32 healthReporter := NewHealthReporter() 33 healthComponent, err := healthReporter.InitializeHealthComponent(validComponent) 34 assert.NoError(t, err) 35 return healthComponent, healthReporter 36 } 37 38 func TestProperInitializing(t *testing.T) { 39 component, _ := setup(t) 40 assert.Equal(t, StartingState, component.Status()) 41 } 42 43 func TestHealthy(t *testing.T) { 44 component, _ := setup(t) 45 component.Healthy() 46 assert.Equal(t, HealthyState, component.Status()) 47 } 48 49 func TestWarningSetting(t *testing.T) { 50 component, healthReporter := setup(t) 51 component.Warning("warning message") 52 assert.Equal(t, WarningState, component.Status()) 53 status := healthReporter.HealthStatus(context.TODO()) 54 componentStatus, found := status.Checks[validComponent] 55 assert.True(t, found) 56 assert.Equal(t, "warning message", *componentStatus.Message) 57 } 58 59 func TestErrorSetting(t *testing.T) { 60 component, healthReporter := setup(t) 61 component.Error(errors.New("err")) 62 assert.Equal(t, ErrorState, component.Status()) 63 status := healthReporter.HealthStatus(context.TODO()) 64 componentStatus, found := status.Checks[validComponent] 65 assert.True(t, found) 66 assert.Equal(t, "err", *componentStatus.Message) 67 } 68 69 func TestSetHealthAndGetHealthResult(t *testing.T) { 70 message := "err" 71 component, _ := setup(t) 72 component.SetHealth( 73 health.HealthStateTerminal, 74 &message, 75 map[string]interface{}{"stack": "trace", "other": errors.New("err2")}, 76 ) 77 assert.Equal(t, health.HealthStateTerminal, component.Status()) 78 result := component.GetHealthCheck() 79 assert.Equal(t, "err", *result.Message) 80 assert.Equal(t, map[string]interface{}{"stack": "trace", "other": errors.New("err2")}, result.Params) 81 } 82 83 func TestNonCompliantName(t *testing.T) { 84 healthReporter := NewHealthReporter() 85 _, err := healthReporter.InitializeHealthComponent(invalidComponent) 86 assert.Error(t, err) 87 } 88 89 func TestGetHealthCheckCopy(t *testing.T) { 90 component, _ := setup(t) 91 originalMessage := "originalMessage" 92 component.SetHealth(HealthyState, &originalMessage, map[string]interface{}{"originalParamKey": "originalParamValue"}) 93 componentResult := component.GetHealthCheck() 94 95 message := "modifiedMessage" 96 componentResult.Type = "modifiedType" 97 componentResult.State = ErrorState 98 *componentResult.Message = message 99 componentResult.Params["modifiedParamKey"] = "modifiedParamValue" 100 101 assert.NotEqual(t, component.(*healthComponent).name, componentResult.Type) 102 assert.NotEqual(t, component.(*healthComponent).state, componentResult.State) 103 assert.NotEqual(t, component.(*healthComponent).message, componentResult.Message) 104 assert.NotEqual(t, component.(*healthComponent).params, componentResult.Params) 105 }