|
| 1 | +/* |
| 2 | +© Copyright IBM Corporation 2018 |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// chkacelively checks that ACE is still runing, by checking if the admin REST endpoint port is available. |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "net/http" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "k8s.io/apimachinery/pkg/api/errors" |
| 27 | +) |
| 28 | + |
| 29 | +func Test_httpChecklocal(t *testing.T) { |
| 30 | + |
| 31 | + t.Run("http get succeeds", func(t *testing.T) { |
| 32 | + |
| 33 | + oldhttpGet := httpGet |
| 34 | + defer func() { httpGet = oldhttpGet }() |
| 35 | + httpGet = func(string) (resp *http.Response, err error) { |
| 36 | + response := &http.Response{ |
| 37 | + StatusCode: 200, |
| 38 | + } |
| 39 | + return response, nil |
| 40 | + } |
| 41 | + |
| 42 | + err := httpChecklocal("LMAP Port", "http://localhost:3002/admin/ready") |
| 43 | + assert.Nil(t, err) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("http get fails with err on get", func(t *testing.T) { |
| 47 | + |
| 48 | + oldhttpGet := httpGet |
| 49 | + defer func() { httpGet = oldhttpGet }() |
| 50 | + httpGet = func(string) (resp *http.Response, err error) { |
| 51 | + response := &http.Response{} |
| 52 | + return response, errors.NewBadRequest("mock err") |
| 53 | + } |
| 54 | + |
| 55 | + err := httpChecklocal("LMAP Port", "http://localhost:3002/admin/ready") |
| 56 | + assert.Error(t, err, "mock err") |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("http get fails with non 200", func(t *testing.T) { |
| 60 | + |
| 61 | + oldhttpGet := httpGet |
| 62 | + defer func() { httpGet = oldhttpGet }() |
| 63 | + httpGet = func(string) (resp *http.Response, err error) { |
| 64 | + response := &http.Response{ |
| 65 | + StatusCode: 404, |
| 66 | + } |
| 67 | + return response, nil |
| 68 | + } |
| 69 | + |
| 70 | + err := httpChecklocal("Test", "http://localhost:3002/admin/ready") |
| 71 | + assert.Error(t, err, "Test ready check failed - HTTP Status is not 200 range") |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func Test_checkDesignerHealth(t *testing.T) { |
| 76 | + t.Run("connector service enabled and health check is successful", func(t *testing.T) { |
| 77 | + os.Setenv("CONNECTOR_SERVICE", "true") |
| 78 | + defer os.Unsetenv("CONNECTOR_SERVICE") |
| 79 | + oldhttpGet := httpGet |
| 80 | + defer func() { httpGet = oldhttpGet }() |
| 81 | + httpGet = func(string) (resp *http.Response, err error) { |
| 82 | + response := &http.Response{ |
| 83 | + StatusCode: 200, |
| 84 | + } |
| 85 | + return response, nil |
| 86 | + } |
| 87 | + |
| 88 | + oldSocketCheck := socketCheck |
| 89 | + defer func() { socketCheck = oldSocketCheck }() |
| 90 | + socketCheck = func(string, string) (err error) { |
| 91 | + return nil |
| 92 | + } |
| 93 | + err := checkDesignerHealth() |
| 94 | + assert.Nil(t, err) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("connector service enabled and health check fails", func(t *testing.T) { |
| 98 | + os.Setenv("CONNECTOR_SERVICE", "true") |
| 99 | + defer os.Unsetenv("CONNECTOR_SERVICE") |
| 100 | + oldhttpGet := httpGet |
| 101 | + defer func() { httpGet = oldhttpGet }() |
| 102 | + httpGet = func(string) (resp *http.Response, err error) { |
| 103 | + response := &http.Response{} |
| 104 | + return response, errors.NewBadRequest("mock err") |
| 105 | + } |
| 106 | + |
| 107 | + oldSocketCheck := socketCheck |
| 108 | + defer func() { socketCheck = oldSocketCheck }() |
| 109 | + socketCheck = func(string, string) (err error) { |
| 110 | + return nil |
| 111 | + } |
| 112 | + err := checkDesignerHealth() |
| 113 | + assert.Error(t, err, "mock err") |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("health check fails for socket http server", func(t *testing.T) { |
| 117 | + oldhttpGet := httpGet |
| 118 | + defer func() { httpGet = oldhttpGet }() |
| 119 | + httpGet = func(string) (resp *http.Response, err error) { |
| 120 | + response := &http.Response{ |
| 121 | + StatusCode: 200, |
| 122 | + } |
| 123 | + return response, nil |
| 124 | + } |
| 125 | + |
| 126 | + oldSocketCheck := socketCheck |
| 127 | + defer func() { socketCheck = oldSocketCheck }() |
| 128 | + socketCheck = func(string, string) (err error) { |
| 129 | + return errors.NewBadRequest("mock err") |
| 130 | + } |
| 131 | + err := checkDesignerHealth() |
| 132 | + assert.Error(t, err, "mock err") |
| 133 | + }) |
| 134 | +} |
0 commit comments