Implement checking in predicates

This commit is contained in:
Harry Zhang
2016-02-23 11:53:10 +08:00
parent b90550de25
commit 636e982ecd
4 changed files with 98 additions and 14 deletions

View File

@@ -18,11 +18,13 @@ package predicates
import (
"fmt"
"os/exec"
"reflect"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/util/codeinspector"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
@@ -1408,3 +1410,48 @@ func TestEBSVolumeCountConflicts(t *testing.T) {
}
}
}
func TestPredicatesRegistered(t *testing.T) {
var functionNames []string
// Files and directories which predicates may be referenced
targetFiles := []string{
"./../../algorithmprovider/defaults/defaults.go", // Default algorithm
"./../../factory/plugins.go", // Registered in init()
"./../../../../../pkg/", // kubernetes/pkg, often used by kubelet or controller
}
// List all golang source files under ./predicates/, excluding test files and sub-directories.
files, err := codeinspector.GetSourceCodeFiles(".")
if err != nil {
t.Errorf("unexpected error: %v when listing files in current directory", err)
}
// Get all public predicates in files.
for _, filePath := range files {
functions, err := codeinspector.GetPublicFunctions(filePath)
if err == nil {
functionNames = append(functionNames, functions...)
} else {
t.Errorf("unexpected error when parsing %s", filePath)
}
}
// Check if all public predicates are referenced in target files.
for _, functionName := range functionNames {
args := []string{"-rl", functionName}
args = append(args, targetFiles...)
err := exec.Command("grep", args...).Run()
if err != nil {
switch err.Error() {
case "exit status 2":
t.Errorf("unexpected error when checking %s", functionName)
case "exit status 1":
t.Errorf("predicate %s is implemented as public but seems not registered or used in any other place",
functionName)
}
}
}
}