|
| 1 | +// Copyright Splunk, Inc. |
| 2 | +// Copyright The OpenTelemetry Authors |
| 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 | +package configprovider |
| 17 | + |
| 18 | +import ( |
| 19 | + "io/ioutil" |
| 20 | + "net/http" |
| 21 | + "os" |
| 22 | + "strconv" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + "go.opentelemetry.io/collector/testutil" |
| 28 | + "go.uber.org/zap" |
| 29 | + "gopkg.in/yaml.v2" |
| 30 | +) |
| 31 | + |
| 32 | +func TestConfigServer_RequireEnvVar(t *testing.T) { |
| 33 | + initial := map[string]interface{}{ |
| 34 | + "minimal": "config", |
| 35 | + } |
| 36 | + |
| 37 | + cs := newConfigServer(zap.NewNop(), initial, initial) |
| 38 | + require.NotNil(t, cs) |
| 39 | + |
| 40 | + require.NoError(t, cs.start()) |
| 41 | + t.Cleanup(func() { |
| 42 | + require.NoError(t, cs.shutdown()) |
| 43 | + }) |
| 44 | + |
| 45 | + client := &http.Client{} |
| 46 | + path := "/debug/configz/initial" |
| 47 | + _, err := client.Get("http://" + defaultConfigServerEndpoint + path) |
| 48 | + assert.Error(t, err) |
| 49 | +} |
| 50 | + |
| 51 | +func TestConfigServer_EnvVar(t *testing.T) { |
| 52 | + alternativePort := strconv.FormatUint(uint64(testutil.GetAvailablePort(t)), 10) |
| 53 | + require.NoError(t, os.Setenv(configServerEnabledEnvVar, "true")) |
| 54 | + t.Cleanup(func() { |
| 55 | + assert.NoError(t, os.Unsetenv(configServerEnabledEnvVar)) |
| 56 | + }) |
| 57 | + |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + portEnvVar string |
| 61 | + endpoint string |
| 62 | + setPortEnvVar bool |
| 63 | + serverDown bool |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "default", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "disable_server", |
| 70 | + setPortEnvVar: true, // Explicitly setting it to empty to disable the server. |
| 71 | + serverDown: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "change_port", |
| 75 | + portEnvVar: alternativePort, |
| 76 | + endpoint: "http://localhost:" + alternativePort, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + initial := map[string]interface{}{ |
| 83 | + "key": "value", |
| 84 | + } |
| 85 | + |
| 86 | + if tt.portEnvVar != "" || tt.setPortEnvVar { |
| 87 | + require.NoError(t, os.Setenv(configServerPortEnvVar, tt.portEnvVar)) |
| 88 | + defer func() { |
| 89 | + assert.NoError(t, os.Unsetenv(configServerPortEnvVar)) |
| 90 | + }() |
| 91 | + } |
| 92 | + |
| 93 | + cs := newConfigServer(zap.NewNop(), initial, initial) |
| 94 | + require.NoError(t, cs.start()) |
| 95 | + defer func() { |
| 96 | + assert.NoError(t, cs.shutdown()) |
| 97 | + }() |
| 98 | + |
| 99 | + endpoint := tt.endpoint |
| 100 | + if endpoint == "" { |
| 101 | + endpoint = "http://" + defaultConfigServerEndpoint |
| 102 | + } |
| 103 | + |
| 104 | + path := "/debug/configz/initial" |
| 105 | + if tt.serverDown { |
| 106 | + client := &http.Client{} |
| 107 | + _, err := client.Get(endpoint + path) |
| 108 | + assert.Error(t, err) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + client := &http.Client{} |
| 113 | + resp, err := client.Get(endpoint + path) |
| 114 | + require.NoError(t, err) |
| 115 | + assert.Equal(t, http.StatusOK, resp.StatusCode, "unsuccessful zpage %q GET", path) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestConfigServer_Serve(t *testing.T) { |
| 121 | + require.NoError(t, os.Setenv(configServerEnabledEnvVar, "true")) |
| 122 | + t.Cleanup(func() { |
| 123 | + assert.NoError(t, os.Unsetenv(configServerEnabledEnvVar)) |
| 124 | + }) |
| 125 | + |
| 126 | + initial := map[string]interface{}{ |
| 127 | + "field": "not_redacted", |
| 128 | + "api_key": "not_redacted_on_initial", |
| 129 | + "int": 42, |
| 130 | + "map": map[interface{}]interface{}{ |
| 131 | + "k0": true, |
| 132 | + "k1": -1, |
| 133 | + "password": "$ENV_VAR", |
| 134 | + }, |
| 135 | + } |
| 136 | + effective := map[string]interface{}{ |
| 137 | + "field": "not_redacted", |
| 138 | + "api_key": "<redacted>", |
| 139 | + "int": 42, |
| 140 | + "map": map[interface{}]interface{}{ |
| 141 | + "k0": true, |
| 142 | + "k1": -1, |
| 143 | + "password": "<redacted>", |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + cs := newConfigServer(zap.NewNop(), initial, initial) |
| 148 | + require.NotNil(t, cs) |
| 149 | + |
| 150 | + require.NoError(t, cs.start()) |
| 151 | + t.Cleanup(func() { |
| 152 | + require.NoError(t, cs.shutdown()) |
| 153 | + }) |
| 154 | + |
| 155 | + // Test for the pages to be actually valid YAML files. |
| 156 | + assertValidYAMLPages(t, initial, "/debug/configz/initial") |
| 157 | + assertValidYAMLPages(t, effective, "/debug/configz/effective") |
| 158 | +} |
| 159 | + |
| 160 | +func assertValidYAMLPages(t *testing.T, expected map[string]interface{}, path string) { |
| 161 | + url := "http://" + defaultConfigServerEndpoint + path |
| 162 | + |
| 163 | + client := &http.Client{} |
| 164 | + |
| 165 | + // Anything other the GET should return 405. |
| 166 | + resp, err := client.Head(url) |
| 167 | + assert.NoError(t, err) |
| 168 | + assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) |
| 169 | + assert.NoError(t, resp.Body.Close()) |
| 170 | + |
| 171 | + resp, err = client.Get(url) |
| 172 | + if !assert.NoError(t, err, "error retrieving zpage at %q", path) { |
| 173 | + return |
| 174 | + } |
| 175 | + assert.Equal(t, http.StatusOK, resp.StatusCode, "unsuccessful zpage %q GET", path) |
| 176 | + t.Cleanup(func() { |
| 177 | + assert.NoError(t, resp.Body.Close()) |
| 178 | + }) |
| 179 | + |
| 180 | + respBytes, err := ioutil.ReadAll(resp.Body) |
| 181 | + require.NoError(t, err) |
| 182 | + |
| 183 | + var unmarshalled map[string]interface{} |
| 184 | + require.NoError(t, yaml.Unmarshal(respBytes, &unmarshalled)) |
| 185 | + |
| 186 | + assert.Equal(t, expected, unmarshalled) |
| 187 | +} |
0 commit comments