vitess.io/vitess@v0.16.2/go/vt/vttablet/endtoend/framework/debugvars.go (about)

     1  /*
     2  Copyright 2019 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 framework
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  	"net/url"
    25  	"strings"
    26  )
    27  
    28  // FetchJSON fetches JSON content from the specified URL path and returns it
    29  // as a map. The function returns an empty map on error.
    30  func FetchJSON(urlPath string) map[string]any {
    31  	out := map[string]any{}
    32  	response, err := http.Get(fmt.Sprintf("%s%s", ServerAddress, urlPath))
    33  	if err != nil {
    34  		return out
    35  	}
    36  	defer response.Body.Close()
    37  	_ = json.NewDecoder(response.Body).Decode(&out)
    38  	return out
    39  }
    40  
    41  // PostJSON performs a post and fetches JSON content from the specified URL path and returns it
    42  // as a map. The function returns an empty map on error.
    43  func PostJSON(urlPath string, values map[string]string) map[string]any {
    44  	urlValues := url.Values{}
    45  	for k, v := range values {
    46  		urlValues.Add(k, v)
    47  	}
    48  	out := map[string]any{}
    49  	response, err := http.PostForm(fmt.Sprintf("%s%s", ServerAddress, urlPath), urlValues)
    50  	if err != nil {
    51  		return out
    52  	}
    53  	defer response.Body.Close()
    54  	_ = json.NewDecoder(response.Body).Decode(&out)
    55  	return out
    56  }
    57  
    58  // DebugVars parses /debug/vars and returns a map. The function returns
    59  // an empty map on error.
    60  func DebugVars() map[string]any {
    61  	return FetchJSON("/debug/vars")
    62  }
    63  
    64  // FetchInt fetches the specified slash-separated tag and returns the
    65  // value as an int. It returns 0 on error, or if not found.
    66  func FetchInt(vars map[string]any, tags string) int {
    67  	val, _ := FetchVal(vars, tags).(float64)
    68  	return int(val)
    69  }
    70  
    71  // IsPresent returns whether the specified slash-separated tag
    72  // is present in the vars provided
    73  func IsPresent(vars map[string]any, tags string) bool {
    74  	val := FetchVal(vars, tags)
    75  	return val != nil
    76  }
    77  
    78  // FetchVal fetches the specified slash-separated tag and returns the
    79  // value as an interface. It returns nil on error, or if not found.
    80  func FetchVal(vars map[string]any, tags string) any {
    81  	splitTags := strings.Split(tags, "/")
    82  	if len(tags) == 0 {
    83  		return nil
    84  	}
    85  	current := vars
    86  	for _, tag := range splitTags[:len(splitTags)-1] {
    87  		icur, ok := current[tag]
    88  		if !ok {
    89  			return nil
    90  		}
    91  		current, ok = icur.(map[string]any)
    92  		if !ok {
    93  			return nil
    94  		}
    95  	}
    96  	return current[splitTags[len(splitTags)-1]]
    97  }
    98  
    99  // FetchURL fetches the content from the specified URL path and returns it
   100  // as a string. The function returns an empty string on error.
   101  func FetchURL(urlPath string) string {
   102  	response, err := http.Get(fmt.Sprintf("%s%s", ServerAddress, urlPath))
   103  	if err != nil {
   104  		return ""
   105  	}
   106  	defer response.Body.Close()
   107  	b, err := io.ReadAll(response.Body)
   108  	if err != nil {
   109  		return ""
   110  	}
   111  	return string(b)
   112  }