mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +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
|
// Should equal to final directory name of kubeMetricImportPath
|
||||||
kubeMetricsDefaultImportName = "metrics"
|
kubeMetricsDefaultImportName = "metrics"
|
||||||
|
|
||||||
kubeURLRoot = "k8s.io/kubernetes"
|
kubeURLRoot = "k8s.io/kubernetes/"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// env configs
|
// env configs
|
||||||
GOROOT string = os.Getenv("GOROOT")
|
GOROOT string = os.Getenv("GOROOT")
|
||||||
GOOS string = os.Getenv("GOOS")
|
GOOS string = os.Getenv("GOOS")
|
||||||
KUBE_ROOT string = os.Getenv("KUBE_ROOT")
|
KUBE_ROOT string = os.Getenv("KUBE_ROOT")
|
||||||
|
|
||||||
kubeRootDeSuffixed string = kubeRootDesuffix(KUBE_ROOT)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -191,16 +189,13 @@ func globalVariableDeclarations(tree *ast.File) map[string]ast.Expr {
|
|||||||
return consts
|
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) {
|
func localImportPath(importExpr string) (string, error) {
|
||||||
// parse directory path
|
// parse directory path
|
||||||
pathPrefix := "unknown"
|
pathPrefix := "unknown"
|
||||||
if strings.Contains(importExpr, kubeURLRoot) {
|
if strings.Contains(importExpr, kubeURLRoot) {
|
||||||
// search k/k local checkout
|
// search k/k local checkout
|
||||||
pathPrefix = kubeRootDeSuffixed
|
pathPrefix = KUBE_ROOT
|
||||||
|
importExpr = strings.Replace(importExpr, kubeURLRoot, "", 1)
|
||||||
} else if strings.Contains(importExpr, "k8s.io/") {
|
} else if strings.Contains(importExpr, "k8s.io/") {
|
||||||
// search k/k/staging local checkout
|
// search k/k/staging local checkout
|
||||||
pathPrefix = strings.Join([]string{KUBE_ROOT, "staging", "src"}, string(os.PathSeparator))
|
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
|
// stdlib -> prefix with GOROOT
|
||||||
pathPrefix = strings.Join([]string{GOROOT, "src"}, string(os.PathSeparator))
|
pathPrefix = strings.Join([]string{GOROOT, "src"}, string(os.PathSeparator))
|
||||||
} // ToDo: support non go mod
|
} // 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
|
return importDirectory, nil
|
||||||
}
|
}
|
||||||
@ -236,7 +233,7 @@ func importedGlobalVariableDeclaration(localVariables map[string]ast.Expr, impor
|
|||||||
fmt.Fprint(os.Stderr, err.Error())
|
fmt.Fprint(os.Stderr, err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(importDirectory)
|
files, err := ioutil.ReadDir(importDirectory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//fmt.Fprintf(os.Stderr, "failed to read import path directory %s with error %w, skipping\n", importDirectory, err)
|
//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()
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse path %s with error %w", im.Path.Value, err)
|
return nil, fmt.Errorf("failed to parse path %s with error %w", im.Path.Value, err)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/component-base/metrics"
|
"k8s.io/component-base/metrics"
|
||||||
@ -118,9 +120,9 @@ var _ = NewCounter(
|
|||||||
|
|
||||||
func TestStableMetric(t *testing.T) {
|
func TestStableMetric(t *testing.T) {
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
testName string
|
testName string
|
||||||
src string
|
src string
|
||||||
metric metric
|
metric metric
|
||||||
kubeRoot string
|
kubeRoot string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -464,7 +466,7 @@ var _ = metrics.NewCounter(
|
|||||||
Subsystem: "kubelet",
|
Subsystem: "kubelet",
|
||||||
Type: counterMetricType,
|
Type: counterMetricType,
|
||||||
},
|
},
|
||||||
kubeRoot: "/home/pchristopher/go/src/k8s.io/kubernetes",
|
kubeRoot: "/home/pchristopher/go/src/k8s.io/kubernetes",
|
||||||
src: `
|
src: `
|
||||||
package test
|
package test
|
||||||
import compbasemetrics "k8s.io/component-base/metrics"
|
import compbasemetrics "k8s.io/component-base/metrics"
|
||||||
@ -483,10 +485,8 @@ var _ = compbasemetrics.NewCounter(
|
|||||||
if test.kubeRoot != "" {
|
if test.kubeRoot != "" {
|
||||||
priorKRoot := KUBE_ROOT
|
priorKRoot := KUBE_ROOT
|
||||||
KUBE_ROOT = test.kubeRoot
|
KUBE_ROOT = test.kubeRoot
|
||||||
kubeRootDeSuffixed = kubeRootDesuffix(KUBE_ROOT)
|
defer func() {
|
||||||
defer func(){
|
|
||||||
KUBE_ROOT = priorKRoot
|
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() {
|
kube::update::stablemetrics() {
|
||||||
stability_check_setup
|
stability_check_setup
|
||||||
temp_file=$(mktemp)
|
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
|
if ! $doCheckStability; then
|
||||||
echo "${red}!!! updating golden list of metrics has failed! ${reset}" >&2
|
echo "${red}!!! updating golden list of metrics has failed! ${reset}" >&2
|
||||||
|
Loading…
Reference in New Issue
Block a user