vitess.io/vitess@v0.16.2/go/vt/servenv/status_test.go (about)

     1  /*
     2  Copyright 2020 The Vitess 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 servenv
    18  
    19  import (
    20  	"html/template"
    21  	"io"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"regexp"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func init() {
    32  	AddStatusFuncs(
    33  		template.FuncMap{
    34  			"github_com_vitessio_vitess_to_upper": strings.ToUpper,
    35  		})
    36  
    37  	AddStatusPart("test_part", `{{github_com_vitessio_vitess_to_upper . }}`, func() any {
    38  		return "this should be uppercase"
    39  	})
    40  	AddStatusSection("test_section", func() string {
    41  		return "this is a section"
    42  	})
    43  }
    44  
    45  func TestStatus(t *testing.T) {
    46  	server := httptest.NewServer(nil)
    47  	defer server.Close()
    48  
    49  	resp, err := http.Get(server.URL + StatusURLPath())
    50  	require.NoError(t, err)
    51  
    52  	defer resp.Body.Close()
    53  
    54  	body, err := io.ReadAll(resp.Body)
    55  	require.NoError(t, err)
    56  
    57  	cases := []string{
    58  		`h1.*test_part.*/h1`,
    59  		`THIS SHOULD BE UPPERCASE`,
    60  		`h1.*test_section.*/h1`,
    61  	}
    62  	for _, cas := range cases {
    63  		if !regexp.MustCompile(cas).Match(body) {
    64  			t.Errorf("failed matching: %q", cas)
    65  		}
    66  	}
    67  	t.Logf("body: \n%s", body)
    68  }
    69  
    70  func TestNamedStatus(t *testing.T) {
    71  	server := httptest.NewServer(nil)
    72  	defer server.Close()
    73  
    74  	name := "test"
    75  	sp := newStatusPage(name)
    76  	sp.addStatusFuncs(
    77  		template.FuncMap{
    78  			"github_com_vitessio_vitess_to_upper": strings.ToUpper,
    79  		})
    80  
    81  	sp.addStatusPart("test_part", `{{github_com_vitessio_vitess_to_upper . }}`, func() any {
    82  		return "this should be uppercase"
    83  	})
    84  	sp.addStatusSection("test_section", func() string {
    85  		return "this is a section"
    86  	})
    87  
    88  	resp, err := http.Get(server.URL + "/" + name + StatusURLPath())
    89  	require.NoError(t, err)
    90  
    91  	defer resp.Body.Close()
    92  
    93  	body, err := io.ReadAll(resp.Body)
    94  	require.NoError(t, err)
    95  
    96  	cases := []string{
    97  		`h1.*test_part.*/h1`,
    98  		`THIS SHOULD BE UPPERCASE`,
    99  		`h1.*test_section.*/h1`,
   100  	}
   101  	for _, cas := range cases {
   102  		if !regexp.MustCompile(cas).Match(body) {
   103  			t.Errorf("failed matching: %q", cas)
   104  		}
   105  	}
   106  	t.Logf("body: \n%s", body)
   107  }