mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
tests for path resolver, add KUBE_ROOT to both top level func calls
This commit is contained in:
parent
e75f3fb563
commit
169583bf4e
@ -37,16 +37,14 @@ const (
|
||||
// Should equal to final directory name of kubeMetricImportPath
|
||||
kubeMetricsDefaultImportName = "metrics"
|
||||
|
||||
kubeURLRoot = "k8s.io/kubernetes"
|
||||
kubeURLRoot = "k8s.io/kubernetes/"
|
||||
)
|
||||
|
||||
var (
|
||||
// env configs
|
||||
GOROOT string = os.Getenv("GOROOT")
|
||||
GOOS string = os.Getenv("GOOS")
|
||||
GOROOT string = os.Getenv("GOROOT")
|
||||
GOOS string = os.Getenv("GOOS")
|
||||
KUBE_ROOT string = os.Getenv("KUBE_ROOT")
|
||||
|
||||
kubeRootDeSuffixed string = kubeRootDesuffix(KUBE_ROOT)
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -191,16 +189,13 @@ func globalVariableDeclarations(tree *ast.File) map[string]ast.Expr {
|
||||
return consts
|
||||
}
|
||||
|
||||
func kubeRootDesuffix(kubeRoot string) string {
|
||||
return strings.Replace(kubeRoot, kubeURLRoot, "", 1) // k8s/k8s refs need this stripped
|
||||
}
|
||||
|
||||
func localImportPath(importExpr string) (string, error) {
|
||||
// parse directory path
|
||||
pathPrefix := "unknown"
|
||||
if strings.Contains(importExpr, kubeURLRoot) {
|
||||
// search k/k local checkout
|
||||
pathPrefix = kubeRootDeSuffixed
|
||||
pathPrefix = KUBE_ROOT
|
||||
importExpr = strings.Replace(importExpr, kubeURLRoot, "", 1)
|
||||
} else if strings.Contains(importExpr, "k8s.io/") {
|
||||
// search k/k/staging local checkout
|
||||
pathPrefix = strings.Join([]string{KUBE_ROOT, "staging", "src"}, string(os.PathSeparator))
|
||||
@ -214,7 +209,9 @@ func localImportPath(importExpr string) (string, error) {
|
||||
// stdlib -> prefix with GOROOT
|
||||
pathPrefix = strings.Join([]string{GOROOT, "src"}, string(os.PathSeparator))
|
||||
} // ToDo: support non go mod
|
||||
importDirectory := strings.Join([]string{pathPrefix, strings.Trim(importExpr, "\"")}, string(os.PathSeparator))
|
||||
|
||||
crossPlatformImportExpr := strings.Replace(importExpr, "/", string(os.PathSeparator), 0)
|
||||
importDirectory := strings.Join([]string{pathPrefix, strings.Trim(crossPlatformImportExpr, "\"")}, string(os.PathSeparator))
|
||||
|
||||
return importDirectory, nil
|
||||
}
|
||||
@ -236,7 +233,7 @@ func importedGlobalVariableDeclaration(localVariables map[string]ast.Expr, impor
|
||||
fmt.Fprint(os.Stderr, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
files, err := ioutil.ReadDir(importDirectory)
|
||||
if err != nil {
|
||||
//fmt.Fprintf(os.Stderr, "failed to read import path directory %s with error %w, skipping\n", importDirectory, err)
|
||||
@ -260,7 +257,7 @@ func importedGlobalVariableDeclaration(localVariables map[string]ast.Expr, impor
|
||||
}
|
||||
|
||||
fileset := token.NewFileSet()
|
||||
tree, err := parser.ParseFile(fileset, strings.Join([]string{importDirectory,file.Name()}, string(os.PathSeparator)), nil, parser.AllErrors)
|
||||
tree, err := parser.ParseFile(fileset, strings.Join([]string{importDirectory, file.Name()}, string(os.PathSeparator)), nil, parser.AllErrors)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse path %s with error %w", im.Path.Value, err)
|
||||
}
|
||||
|
@ -18,7 +18,9 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/component-base/metrics"
|
||||
@ -118,9 +120,9 @@ var _ = NewCounter(
|
||||
|
||||
func TestStableMetric(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
testName string
|
||||
src string
|
||||
metric metric
|
||||
testName string
|
||||
src string
|
||||
metric metric
|
||||
kubeRoot string
|
||||
}{
|
||||
{
|
||||
@ -464,7 +466,7 @@ var _ = metrics.NewCounter(
|
||||
Subsystem: "kubelet",
|
||||
Type: counterMetricType,
|
||||
},
|
||||
kubeRoot: "/home/pchristopher/go/src/k8s.io/kubernetes",
|
||||
kubeRoot: "/home/pchristopher/go/src/k8s.io/kubernetes",
|
||||
src: `
|
||||
package test
|
||||
import compbasemetrics "k8s.io/component-base/metrics"
|
||||
@ -483,10 +485,8 @@ var _ = compbasemetrics.NewCounter(
|
||||
if test.kubeRoot != "" {
|
||||
priorKRoot := KUBE_ROOT
|
||||
KUBE_ROOT = test.kubeRoot
|
||||
kubeRootDeSuffixed = kubeRootDesuffix(KUBE_ROOT)
|
||||
defer func(){
|
||||
defer func() {
|
||||
KUBE_ROOT = priorKRoot
|
||||
kubeRootDeSuffixed = kubeRootDesuffix(KUBE_ROOT)
|
||||
}()
|
||||
}
|
||||
|
||||
@ -738,3 +738,56 @@ var _ = metrics.NewHistogram(
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_localImportPath(t *testing.T) {
|
||||
KUBE_ROOT = "/home/pchristopher/go/src/k8s.io/kubernetes"
|
||||
GOROOT := os.Getenv("GOROOT")
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
importExpr string
|
||||
expectedPath string
|
||||
errorExp bool
|
||||
}{
|
||||
{
|
||||
name: "k8s local package",
|
||||
importExpr: "k8s.io/kubernetes/pkg/kubelet/metrics",
|
||||
expectedPath: strings.Join([]string{KUBE_ROOT, "pkg", "kubelet", "metrics"}, string(os.PathSeparator)),
|
||||
errorExp: false,
|
||||
},
|
||||
{
|
||||
name: "k8s staging package",
|
||||
importExpr: "k8s.io/kubelet/metrics",
|
||||
expectedPath: strings.Join([]string{KUBE_ROOT, "staging", "src", "k8s.io", "kubelet", "metrics"}, string(os.PathSeparator)),
|
||||
errorExp: false,
|
||||
},
|
||||
{
|
||||
name: "public package",
|
||||
importExpr: "github.com/thisisnot/thesoundofthetrain",
|
||||
errorExp: true,
|
||||
},
|
||||
{
|
||||
name: "stl package",
|
||||
importExpr: "os",
|
||||
expectedPath: strings.Join([]string{GOROOT, "src", "os"}, string(os.PathSeparator)),
|
||||
errorExp: false,
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
path, err := localImportPath(test.importExpr)
|
||||
if test.errorExp {
|
||||
if err == nil {
|
||||
t.Error("did not receive error as expected")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("received unexpected error %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if path != test.expectedPath {
|
||||
t.Errorf("did not received expected path. \nwant: %s \ngot: %s", test.expectedPath, path)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ kube::validate::stablemetrics() {
|
||||
kube::update::stablemetrics() {
|
||||
stability_check_setup
|
||||
temp_file=$(mktemp)
|
||||
doCheckStability=$(find_files_to_check | grep -E ".*.go" | grep -v ".*_test.go" | sort | xargs -L 200 go run "test/instrumentation/main.go" "test/instrumentation/decode_metric.go" "test/instrumentation/find_stable_metric.go" "test/instrumentation/error.go" "test/instrumentation/metric.go" -- 1>"${temp_file}")
|
||||
doCheckStability=$(find_files_to_check | grep -E ".*.go" | grep -v ".*_test.go" | sort | KUBE_ROOT=${KUBE_ROOT} xargs -L 200 go run "test/instrumentation/main.go" "test/instrumentation/decode_metric.go" "test/instrumentation/find_stable_metric.go" "test/instrumentation/error.go" "test/instrumentation/metric.go" -- 1>"${temp_file}")
|
||||
|
||||
if ! $doCheckStability; then
|
||||
echo "${red}!!! updating golden list of metrics has failed! ${reset}" >&2
|
||||
|
Loading…
Reference in New Issue
Block a user