mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
add happy path tests for two types of imports
This commit is contained in:
parent
d3aabe2397
commit
e75f3fb563
@ -190,7 +190,7 @@ func (c *metricDecoder) decodeOpts(expr ast.Expr) (metric, error) {
|
|||||||
|
|
||||||
variableExpr, found := c.variables[strings.Join([]string{s.Name,v.Sel.Name}, ".")]
|
variableExpr, found := c.variables[strings.Join([]string{s.Name,v.Sel.Name}, ".")]
|
||||||
if !found {
|
if !found {
|
||||||
return m, newDecodeErrorf(expr, errBadVariableAttribute)
|
return m, newDecodeErrorf(expr, errBadImportedVariableAttribute)
|
||||||
}
|
}
|
||||||
bl, ok := variableExpr.(*ast.BasicLit)
|
bl, ok := variableExpr.(*ast.BasicLit)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -23,18 +23,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
errNotDirectCall = "Opts for STABLE metric was not directly passed to new metric function"
|
errNotDirectCall = "Opts for STABLE metric was not directly passed to new metric function"
|
||||||
errPositionalArguments = "Positional arguments are not supported"
|
errPositionalArguments = "Positional arguments are not supported"
|
||||||
errStabilityLevel = "StabilityLevel should be passed STABLE, ALPHA or removed"
|
errStabilityLevel = "StabilityLevel should be passed STABLE, ALPHA or removed"
|
||||||
errStableSummary = "Stable summary metric is not supported"
|
errStableSummary = "Stable summary metric is not supported"
|
||||||
errInvalidNewMetricCall = "Invalid new metric call, please ensure code compiles"
|
errInvalidNewMetricCall = "Invalid new metric call, please ensure code compiles"
|
||||||
errNonStringAttribute = "Non string attribute is not supported"
|
errNonStringAttribute = "Non string attribute is not supported"
|
||||||
errBadVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in same file"
|
errBadVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in same file"
|
||||||
errFieldNotSupported = "Field %s is not supported"
|
errBadImportedVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in correclty impoprted same file"
|
||||||
errBuckets = "Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"
|
errFieldNotSupported = "Field %s is not supported"
|
||||||
errLabels = "Labels were not set to list of strings"
|
errBuckets = "Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"
|
||||||
errImport = `Importing using "." is not supported`
|
errLabels = "Labels were not set to list of strings"
|
||||||
errExprNotIdent = "expr selector does not refer to type ast.Ident, is type %s"
|
errImport = `Importing using "." is not supported`
|
||||||
|
errExprNotIdent = "expr selector does not refer to type ast.Ident, is type %s"
|
||||||
)
|
)
|
||||||
|
|
||||||
type decodeError struct {
|
type decodeError struct {
|
||||||
|
@ -46,7 +46,7 @@ var (
|
|||||||
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 = strings.Replace(KUBE_ROOT, kubeURLRoot, "", 1) // k8s/k8s refs need this stripped
|
kubeRootDeSuffixed string = kubeRootDesuffix(KUBE_ROOT)
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -191,6 +191,9 @@ 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
|
||||||
|
@ -118,9 +118,10 @@ 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
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
testName: "Counter",
|
testName: "Counter",
|
||||||
@ -434,9 +435,61 @@ var _ = metrics.NewHistogram(
|
|||||||
Buckets: metrics.DefBuckets,
|
Buckets: metrics.DefBuckets,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
`},
|
||||||
|
{
|
||||||
|
testName: "Imported stdlib constant",
|
||||||
|
metric: metric{
|
||||||
|
Name: "importedCounter",
|
||||||
|
StabilityLevel: "STABLE",
|
||||||
|
Subsystem: "GET",
|
||||||
|
Type: counterMetricType,
|
||||||
|
},
|
||||||
|
src: `
|
||||||
|
package test
|
||||||
|
import "k8s.io/component-base/metrics"
|
||||||
|
import "net/http"
|
||||||
|
var _ = metrics.NewCounter(
|
||||||
|
&metrics.CounterOpts{
|
||||||
|
Name: "importedCounter",
|
||||||
|
StabilityLevel: metrics.STABLE,
|
||||||
|
Subsystem: http.MethodGet,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
`},
|
||||||
|
{
|
||||||
|
testName: "Imported k8s.io constant",
|
||||||
|
metric: metric{
|
||||||
|
Name: "importedCounter",
|
||||||
|
StabilityLevel: "STABLE",
|
||||||
|
Subsystem: "kubelet",
|
||||||
|
Type: counterMetricType,
|
||||||
|
},
|
||||||
|
kubeRoot: "/home/pchristopher/go/src/k8s.io/kubernetes",
|
||||||
|
src: `
|
||||||
|
package test
|
||||||
|
import compbasemetrics "k8s.io/component-base/metrics"
|
||||||
|
import "k8s.io/kubernetes/pkg/kubelet/metrics"
|
||||||
|
var _ = compbasemetrics.NewCounter(
|
||||||
|
&compbasemetrics.CounterOpts{
|
||||||
|
Name: "importedCounter",
|
||||||
|
StabilityLevel: compbasemetrics.STABLE,
|
||||||
|
Subsystem: metrics.KubeletSubsystem,
|
||||||
|
},
|
||||||
|
)
|
||||||
`},
|
`},
|
||||||
} {
|
} {
|
||||||
t.Run(test.testName, func(t *testing.T) {
|
t.Run(test.testName, func(t *testing.T) {
|
||||||
|
// these sub-tests cannot be run in parallel with the below
|
||||||
|
if test.kubeRoot != "" {
|
||||||
|
priorKRoot := KUBE_ROOT
|
||||||
|
KUBE_ROOT = test.kubeRoot
|
||||||
|
kubeRootDeSuffixed = kubeRootDesuffix(KUBE_ROOT)
|
||||||
|
defer func(){
|
||||||
|
KUBE_ROOT = priorKRoot
|
||||||
|
kubeRootDeSuffixed = kubeRootDesuffix(KUBE_ROOT)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
metrics, errors := searchFileForStableMetrics(fakeFilename, test.src)
|
metrics, errors := searchFileForStableMetrics(fakeFilename, test.src)
|
||||||
if len(errors) != 0 {
|
if len(errors) != 0 {
|
||||||
t.Errorf("Unexpected errors: %s", errors)
|
t.Errorf("Unexpected errors: %s", errors)
|
||||||
|
Loading…
Reference in New Issue
Block a user