add happy path tests for two types of imports

This commit is contained in:
Pat Christopher 2021-07-13 20:08:13 -07:00
parent d3aabe2397
commit e75f3fb563
No known key found for this signature in database
GPG Key ID: F8BCFE8CB1484819
4 changed files with 75 additions and 18 deletions

View File

@ -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}, ".")]
if !found {
return m, newDecodeErrorf(expr, errBadVariableAttribute)
return m, newDecodeErrorf(expr, errBadImportedVariableAttribute)
}
bl, ok := variableExpr.(*ast.BasicLit)
if !ok {

View File

@ -23,18 +23,19 @@ import (
)
const (
errNotDirectCall = "Opts for STABLE metric was not directly passed to new metric function"
errPositionalArguments = "Positional arguments are not supported"
errStabilityLevel = "StabilityLevel should be passed STABLE, ALPHA or removed"
errStableSummary = "Stable summary metric is not supported"
errInvalidNewMetricCall = "Invalid new metric call, please ensure code compiles"
errNonStringAttribute = "Non string attribute is not supported"
errBadVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in same file"
errFieldNotSupported = "Field %s is not supported"
errBuckets = "Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"
errLabels = "Labels were not set to list of strings"
errImport = `Importing using "." is not supported`
errExprNotIdent = "expr selector does not refer to type ast.Ident, is type %s"
errNotDirectCall = "Opts for STABLE metric was not directly passed to new metric function"
errPositionalArguments = "Positional arguments are not supported"
errStabilityLevel = "StabilityLevel should be passed STABLE, ALPHA or removed"
errStableSummary = "Stable summary metric is not supported"
errInvalidNewMetricCall = "Invalid new metric call, please ensure code compiles"
errNonStringAttribute = "Non string attribute is not supported"
errBadVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in same file"
errBadImportedVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in correclty impoprted same file"
errFieldNotSupported = "Field %s is not supported"
errBuckets = "Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"
errLabels = "Labels were not set to list of strings"
errImport = `Importing using "." is not supported`
errExprNotIdent = "expr selector does not refer to type ast.Ident, is type %s"
)
type decodeError struct {

View File

@ -46,7 +46,7 @@ var (
GOOS string = os.Getenv("GOOS")
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() {
@ -191,6 +191,9 @@ 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
@ -233,7 +236,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)

View File

@ -118,9 +118,10 @@ 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
}{
{
testName: "Counter",
@ -434,9 +435,61 @@ var _ = metrics.NewHistogram(
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) {
// 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)
if len(errors) != 0 {
t.Errorf("Unexpected errors: %s", errors)