knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/profiling/server_test.go (about) 1 /* 2 Copyright 2019 The Knative 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 17 package profiling 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 24 "go.uber.org/zap" 25 corev1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 o11yconfigmap "knative.dev/pkg/observability/configmap" 28 "knative.dev/pkg/system" 29 30 _ "knative.dev/pkg/system/testing" 31 ) 32 33 func TestUpdateFromConfigMap(t *testing.T) { 34 observabilityConfigTests := []struct { 35 name string 36 wantEnabled bool 37 wantStatusCode int 38 config *corev1.ConfigMap 39 }{{ 40 name: "observability with profiling disabled", 41 wantEnabled: false, 42 wantStatusCode: http.StatusNotFound, 43 config: &corev1.ConfigMap{ 44 ObjectMeta: metav1.ObjectMeta{ 45 Namespace: system.Namespace(), 46 Name: o11yconfigmap.Name(), 47 }, 48 Data: map[string]string{ 49 "profiling.enable": "false", 50 }, 51 }, 52 }, { 53 name: "observability config with profiling enabled", 54 wantEnabled: true, 55 wantStatusCode: http.StatusOK, 56 config: &corev1.ConfigMap{ 57 ObjectMeta: metav1.ObjectMeta{ 58 Namespace: system.Namespace(), 59 Name: o11yconfigmap.Name(), 60 }, 61 Data: map[string]string{ 62 "profiling.enable": "true", 63 }, 64 }, 65 }, { 66 name: "observability config with unparseable value", 67 wantEnabled: false, 68 wantStatusCode: http.StatusNotFound, 69 config: &corev1.ConfigMap{ 70 ObjectMeta: metav1.ObjectMeta{ 71 Namespace: system.Namespace(), 72 Name: o11yconfigmap.Name(), 73 }, 74 Data: map[string]string{ 75 "profiling.enable": "get me some profiles", 76 }, 77 }, 78 }} 79 80 for _, tt := range observabilityConfigTests { 81 t.Run(tt.name, func(t *testing.T) { 82 handler := NewHandler(zap.NewNop().Sugar(), false) 83 84 handler.UpdateFromConfigMap(tt.config) 85 86 req, err := http.NewRequest(http.MethodGet, "/debug/pprof/", nil) 87 if err != nil { 88 t.Fatal("Error creating request:", err) 89 } 90 91 rr := httptest.NewRecorder() 92 93 handler.ServeHTTP(rr, req) 94 95 if rr.Code != tt.wantStatusCode { 96 t.Errorf("StatusCode: %v, want: %v", rr.Code, tt.wantStatusCode) 97 } 98 99 if handler.enabled.Load() != tt.wantEnabled { 100 t.Fatalf("Enabled got %v, want %v", handler.enabled.Load(), tt.wantEnabled) 101 } 102 }) 103 } 104 }