github.com/kubevela/workflow@v0.6.0/pkg/providers/metrics/prom_check_test.go (about) 1 /* 2 Copyright 2022 The KubeVela 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 metrics 17 18 import ( 19 "context" 20 "fmt" 21 "net/http" 22 "testing" 23 "time" 24 25 "github.com/crossplane/crossplane-runtime/pkg/test" 26 27 monitorContext "github.com/kubevela/pkg/monitor/context" 28 context2 "github.com/kubevela/workflow/pkg/context" 29 "github.com/kubevela/workflow/pkg/cue/model/value" 30 "github.com/stretchr/testify/assert" 31 "sigs.k8s.io/controller-runtime/pkg/client" 32 ) 33 34 func TestMetricCheck(t *testing.T) { 35 srv := runMockPrometheusServer() // no lint 36 37 v, err := value.NewValue(` 38 metricEndpoint: "http://127.0.0.1:18089" 39 query: "sum(nginx_ingress_controller_requests{host=\"canary-demo.com\",status=\"200\"})" 40 duration: "4s" 41 failDuration: "2s" 42 condition: ">=3" 43 stepID: "123456"`, nil, "") 44 assert.NoError(t, err) 45 prd := &provider{} 46 ctx := monitorContext.NewTraceContext(context.Background(), "") 47 cli := &test.MockClient{ 48 MockCreate: func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { 49 return nil 50 }, 51 MockPatch: func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { 52 return nil 53 }, 54 MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object) error { 55 return nil 56 }, 57 } 58 wfCtx, err := context2.NewContext(context.Background(), cli, "default", "v1", nil) 59 assert.NoError(t, err) 60 err = prd.PromCheck(ctx, wfCtx, v, nil) 61 assert.NoError(t, err) 62 res, err := v.GetBool("result") 63 assert.NoError(t, err) 64 assert.Equal(t, res, false) 65 message, err := v.GetString("message") 66 assert.NoError(t, err) 67 assert.Equal(t, message, "The healthy condition should be >=3, and the query result is 10, indicating success.") 68 if err := srv.Close(); err != nil { 69 fmt.Printf("Server shutdown error: %v\n", err) 70 } 71 } 72 73 func runMockPrometheusServer() *http.Server { 74 srv := http.Server{Addr: ":18089", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 75 w.Header().Set("Content-Type", "application/json") 76 w.Write([]byte(`{ 77 "status": "success", 78 "data": { 79 "resultType": "vector", 80 "result": [ 81 { 82 "metric": {}, 83 "value": [ 84 1678701380.73, 85 "10" 86 ] 87 } 88 ] 89 } 90 }`)) 91 })} 92 go srv.ListenAndServe() // no lint 93 time.Sleep(3 * time.Second) 94 return &srv 95 }