Check only predicate functions in test

This commit is contained in:
Wojciech Tyczynski 2016-07-08 09:31:05 +02:00
parent b92eadfd1a
commit b1d53895a4
3 changed files with 71 additions and 18 deletions

View File

@ -24,11 +24,27 @@ import (
"io/ioutil" "io/ioutil"
"strings" "strings"
"unicode" "unicode"
go2idlparser "k8s.io/kubernetes/cmd/libs/go2idl/parser"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
) )
// GetPublicFunctions lists all public functions (not methods) from a golang source file. // GetPublicFunctions lists all public functions (not methods) from a golang source file.
func GetPublicFunctions(filePath string) ([]string, error) { func GetPublicFunctions(pkg, filePath string) ([]*types.Type, error) {
var functionNames []string builder := go2idlparser.New()
data, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
if err := builder.AddFile(pkg, filePath, data); err != nil {
return nil, err
}
universe, err := builder.FindTypes()
if err != nil {
return nil, err
}
var functions []*types.Type
// Create the AST by parsing src. // Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset fset := token.NewFileSet() // positions are relative to fset
@ -45,13 +61,13 @@ func GetPublicFunctions(filePath string) ([]string, error) {
s = x.Name.Name s = x.Name.Name
// It's a function (not method), and is public, record it. // It's a function (not method), and is public, record it.
if x.Recv == nil && isPublic(s) { if x.Recv == nil && isPublic(s) {
functionNames = append(functionNames, s) functions = append(functions, universe[pkg].Function(x.Name.Name))
} }
} }
return true return true
}) })
return functionNames, nil return functions, nil
} }
// isPublic checks if a given string is a public function name. // isPublic checks if a given string is a public function name.

View File

@ -19,10 +19,13 @@ package predicates
import ( import (
"fmt" "fmt"
"os/exec" "os/exec"
"path/filepath"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"k8s.io/kubernetes/cmd/libs/go2idl/parser"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource" "k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/util/codeinspector" "k8s.io/kubernetes/pkg/util/codeinspector"
@ -1584,8 +1587,26 @@ func TestEBSVolumeCountConflicts(t *testing.T) {
} }
} }
func getPredicateSignature() (*types.Signature, error) {
filePath := "./../types.go"
pkgName := filepath.Dir(filePath)
builder := parser.New()
if err := builder.AddDir(pkgName); err != nil {
return nil, err
}
universe, err := builder.FindTypes()
if err != nil {
return nil, err
}
result, ok := universe[pkgName].Types["FitPredicate"]
if !ok {
return nil, fmt.Errorf("FitPredicate type not defined")
}
return result.Signature, nil
}
func TestPredicatesRegistered(t *testing.T) { func TestPredicatesRegistered(t *testing.T) {
var functionNames []string var functions []*types.Type
// Files and directories which predicates may be referenced // Files and directories which predicates may be referenced
targetFiles := []string{ targetFiles := []string{
@ -1603,27 +1624,42 @@ func TestPredicatesRegistered(t *testing.T) {
// Get all public predicates in files. // Get all public predicates in files.
for _, filePath := range files { for _, filePath := range files {
functions, err := codeinspector.GetPublicFunctions(filePath) fileFunctions, err := codeinspector.GetPublicFunctions("k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates", filePath)
if err == nil { if err == nil {
functionNames = append(functionNames, functions...) functions = append(functions, fileFunctions...)
} else { } else {
t.Errorf("unexpected error when parsing %s", filePath) t.Errorf("unexpected error when parsing %s", filePath)
} }
} }
predSignature, err := getPredicateSignature()
if err != nil {
t.Fatalf("Couldn't get predicates signature")
}
// Check if all public predicates are referenced in target files. // Check if all public predicates are referenced in target files.
for _, functionName := range functionNames { for _, function := range functions {
args := []string{"-rl", functionName} // Ignore functions that doesn't match FitPredicate signature.
signature := function.Underlying.Signature
if len(predSignature.Parameters) != len(signature.Parameters) {
continue
}
if len(predSignature.Results) != len(signature.Results) {
continue
}
// TODO: Check exact types of parameters and results.
args := []string{"-rl", function.Name.Name}
args = append(args, targetFiles...) args = append(args, targetFiles...)
err := exec.Command("grep", args...).Run() err := exec.Command("grep", args...).Run()
if err != nil { if err != nil {
switch err.Error() { switch err.Error() {
case "exit status 2": case "exit status 2":
t.Errorf("unexpected error when checking %s", functionName) t.Errorf("unexpected error when checking %s", function.Name)
case "exit status 1": case "exit status 1":
t.Errorf("predicate %s is implemented as public but seems not registered or used in any other place", t.Errorf("predicate %s is implemented as public but seems not registered or used in any other place",
functionName) function.Name)
} }
} }
} }

View File

@ -23,6 +23,7 @@ import (
"strconv" "strconv"
"testing" "testing"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource" "k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/apis/extensions" "k8s.io/kubernetes/pkg/apis/extensions"
@ -887,7 +888,7 @@ func makeImageNode(node string, status api.NodeStatus) api.Node {
} }
func TestPrioritiesRegistered(t *testing.T) { func TestPrioritiesRegistered(t *testing.T) {
var functionNames []string var functions []*types.Type
// Files and directories which priorities may be referenced // Files and directories which priorities may be referenced
targetFiles := []string{ targetFiles := []string{
@ -904,27 +905,27 @@ func TestPrioritiesRegistered(t *testing.T) {
// Get all public priorities in files. // Get all public priorities in files.
for _, filePath := range files { for _, filePath := range files {
functions, err := codeinspector.GetPublicFunctions(filePath) fileFunctions, err := codeinspector.GetPublicFunctions("k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities", filePath)
if err == nil { if err == nil {
functionNames = append(functionNames, functions...) functions = append(functions, fileFunctions...)
} else { } else {
t.Errorf("unexpected error when parsing %s", filePath) t.Errorf("unexpected error when parsing %s", filePath)
} }
} }
// Check if all public priorities are referenced in target files. // Check if all public priorities are referenced in target files.
for _, functionName := range functionNames { for _, function := range functions {
args := []string{"-rl", functionName} args := []string{"-rl", function.Name.Name}
args = append(args, targetFiles...) args = append(args, targetFiles...)
err := exec.Command("grep", args...).Run() err := exec.Command("grep", args...).Run()
if err != nil { if err != nil {
switch err.Error() { switch err.Error() {
case "exit status 2": case "exit status 2":
t.Errorf("unexpected error when checking %s", functionName) t.Errorf("unexpected error when checking %s", function.Name)
case "exit status 1": case "exit status 1":
t.Errorf("priority %s is implemented as public but seems not registered or used in any other place", t.Errorf("priority %s is implemented as public but seems not registered or used in any other place",
functionName) function.Name)
} }
} }
} }