Skip to content

Commit

Permalink
Include tests in golint checks, fix warnings (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineco authored and aledbf committed Mar 7, 2018
1 parent 3130665 commit 86a3a63
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ e2e-image: sub-container-amd64
.PHONY: e2e-test
e2e-test:
@go test -o e2e-tests -c ./test/e2e
@KUBECONFIG=${HOME}/.kube/config INGRESSNGINXCONFIG=${HOME}/.kube/config ./e2e-tests -test.parallel 1
@KUBECONFIG=${HOME}/.kube/config ./e2e-tests -test.parallel 1

.PHONY: cover
cover:
Expand Down
4 changes: 2 additions & 2 deletions hack/verify-golint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
cd "${KUBE_ROOT}"

GOLINT=${GOLINT:-"golint"}
PACKAGES=($(go list ./... | grep -v /vendor/ | grep -v /test\/e2e/))
PACKAGES=($(go list ./... | grep -v /vendor/))
bad_files=()
for package in "${PACKAGES[@]}"; do
out=$("${GOLINT}" -min_confidence=0.9 "${package}")
out=$("${GOLINT}" -min_confidence=0.9 "${package}" | grep -v 'should not use dot imports' || :)
if [[ -n "${out}" ]]; then
bad_files+=("${out}")
fi
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/framework/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package framework

import "sync"

// CleanupActionHandle is a handle used to perform a cleanup action.
type CleanupActionHandle *int

var cleanupActionsLock sync.Mutex
var cleanupActions = map[CleanupActionHandle]func(){}

// AddCleanupAction installs a function that will be called in the event of the
// whole test being terminated. This allows arbitrary pieces of the overall
// whole test being terminated. This allows arbitrary pieces of the overall
// test to hook into SynchronizedAfterSuite().
func AddCleanupAction(fn func()) CleanupActionHandle {
p := CleanupActionHandle(new(int))
Expand All @@ -41,7 +42,7 @@ func RemoveCleanupAction(p CleanupActionHandle) {
delete(cleanupActions, p)
}

// RunCleanupActions runs all functions installed by AddCleanupAction. It does
// RunCleanupActions runs all functions installed by AddCleanupAction. It does
// not remove them (see RemoveCleanupAction) but it does run unlocked, so they
// may remove themselves.
func RunCleanupActions() {
Expand Down
13 changes: 2 additions & 11 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,7 @@ import (
. "github.com/onsi/gomega"
)

const (
podName = "test-ingress-controller"
controllerPodName = "nginx-ingress-controller"
)

const (
MaxRetry = 200
NoRetry = 1
)

// RequestScheme define a scheme used in a test request.
type RequestScheme string

// These are valid test request schemes.
Expand All @@ -63,7 +54,7 @@ type Framework struct {
Namespace *v1.Namespace

// To make sure that this framework cleans up after itself, no matter what,
// we install a Cleanup action before each test and clear it after. If we
// we install a Cleanup action before each test and clear it after. If we
// should abort, the AfterSuite hook should run all Cleanup actions.
cleanupHandle CleanupActionHandle

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/framework/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

// EnsureSecret creates a Secret object or returns it if it already exists.
func (f *Framework) EnsureSecret(secret *api.Secret) (*api.Secret, error) {
s, err := f.KubeClientSet.CoreV1().Secrets(secret.Namespace).Create(secret)
if err != nil {
Expand All @@ -38,6 +39,7 @@ func (f *Framework) EnsureSecret(secret *api.Secret) (*api.Secret, error) {
return s, nil
}

// EnsureIngress creates an Ingress object or returns it if it already exists.
func (f *Framework) EnsureIngress(ingress *extensions.Ingress) (*extensions.Ingress, error) {
s, err := f.KubeClientSet.ExtensionsV1beta1().Ingresses(ingress.Namespace).Update(ingress)
if err != nil {
Expand All @@ -49,6 +51,7 @@ func (f *Framework) EnsureIngress(ingress *extensions.Ingress) (*extensions.Ingr
return s, nil
}

// EnsureService creates a Service object or returns it if it already exists.
func (f *Framework) EnsureService(service *core.Service) (*core.Service, error) {
s, err := f.KubeClientSet.CoreV1().Services(service.Namespace).Update(service)
if err != nil {
Expand All @@ -60,6 +63,7 @@ func (f *Framework) EnsureService(service *core.Service) (*core.Service, error)
return s, nil
}

// EnsureDeployment creates a Deployment object or returns it if it already exists.
func (f *Framework) EnsureDeployment(deployment *extensions.Deployment) (*extensions.Deployment, error) {
d, err := f.KubeClientSet.Extensions().Deployments(deployment.Namespace).Update(deployment)
if err != nil {
Expand All @@ -71,6 +75,7 @@ func (f *Framework) EnsureDeployment(deployment *extensions.Deployment) (*extens
return d, nil
}

// WaitForPodsReady waits for a given amount of time until a group of Pods is running.
func (f *Framework) WaitForPodsReady(timeout time.Duration, expectedReplicas int, opts metav1.ListOptions) error {
return wait.Poll(time.Second, timeout, func() (bool, error) {
pl, err := f.KubeClientSet.CoreV1().Pods(f.Namespace.Name).List(opts)
Expand Down
1 change: 1 addition & 0 deletions test/e2e/framework/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/api/core/v1"
)

// Logs returns the log entries of a given Pod.
func (f *Framework) Logs(pod *v1.Pod) (string, error) {
var (
execOut bytes.Buffer
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/framework/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func CreateIngressTLSSecret(client kubernetes.Interface, hosts []string, secretN
// of rsaBits, valid for validFor time.
func generateRSACerts(host string, isCA bool, keyOut, certOut io.Writer) error {
if len(host) == 0 {
return fmt.Errorf("Require a non-empty host for client hello")
return fmt.Errorf("require a non-empty host for client hello")
}
priv, err := rsa.GenerateKey(rand.Reader, rsaBits)
if err != nil {
return fmt.Errorf("Failed to generate key: %v", err)
return fmt.Errorf("failed to generate key: %v", err)
}
notBefore := time.Now()
notAfter := notBefore.Add(validFor)
Expand Down Expand Up @@ -119,13 +119,13 @@ func generateRSACerts(host string, isCA bool, keyOut, certOut io.Writer) error {

derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
return fmt.Errorf("Failed to create certificate: %s", err)
return fmt.Errorf("failed to create certificate: %s", err)
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return fmt.Errorf("Failed creating cert: %v", err)
return fmt.Errorf("failed creating cert: %v", err)
}
if err := pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil {
return fmt.Errorf("Failed creating keay: %v", err)
return fmt.Errorf("failed creating keay: %v", err)
}
return nil
}
9 changes: 4 additions & 5 deletions test/e2e/framework/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

const (
RecommendedConfigPathEnvVar = "INGRESSNGINXCONFIG"
)

// TestContextType describes the client context to use in communications with the Kubernetes API.
type TestContextType struct {
KubeHost string
KubeConfig string
KubeContext string
}

// TestContext is the global client context for tests.
var TestContext TestContextType

// Register flags common to all e2e test suites.
// RegisterCommonFlags registers flags common to all e2e test suites.
func RegisterCommonFlags() {
// Turn on verbose by default to get spec names
config.DefaultReporterConfig.Verbose = true
Expand All @@ -53,6 +51,7 @@ func RegisterCommonFlags() {
flag.StringVar(&TestContext.KubeContext, "kubernetes-context", "", "config context to use for kubernetes. If unset, will use value from 'current-context'")
}

// RegisterParseFlags registers and parses flags for the test binary.
func RegisterParseFlags() {
RegisterCommonFlags()
flag.Parse()
Expand Down
10 changes: 7 additions & 3 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,30 @@ func log(level string, format string, args ...interface{}) {
fmt.Fprintf(GinkgoWriter, nowStamp()+": "+level+": "+format+"\n", args...)
}

// Logf logs to the INFO logs.
func Logf(format string, args ...interface{}) {
log("INFO", format, args...)
}

// Failf logs to the INFO logs and fails the test.
func Failf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
log("INFO", msg)
Fail(nowStamp()+": "+msg, 1)
}

// Skipf logs to the INFO logs and skips the test.
func Skipf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
log("INFO", msg)
Skip(nowStamp() + ": " + msg)
}

// RestclientConfig deserializes the contents of a kubeconfig file into a Config object.
func RestclientConfig(config, context string) (*api.Config, error) {
Logf(">>> config: %s\n", config)
if config == "" {
return nil, fmt.Errorf("Config file must be specified to load client config")
return nil, fmt.Errorf("config file must be specified to load client config")
}
c, err := clientcmd.LoadFromFile(config)
if err != nil {
Expand All @@ -83,8 +87,7 @@ func RestclientConfig(config, context string) (*api.Config, error) {
return c, nil
}

type ClientConfigGetter func() (*rest.Config, error)

// LoadConfig deserializes the contents of a kubeconfig file into a REST configuration.
func LoadConfig(config, context string) (*rest.Config, error) {
c, err := RestclientConfig(config, context)
if err != nil {
Expand Down Expand Up @@ -127,6 +130,7 @@ func DeleteKubeNamespace(c kubernetes.Interface, namespace string) error {
return c.CoreV1().Namespaces().Delete(namespace, metav1.NewDeleteOptions(0))
}

// ExpectNoError tests whether an error occured.
func ExpectNoError(err error, explain ...interface{}) {
if err != nil {
Logf("Unexpected error occurred: %v", err)
Expand Down

0 comments on commit 86a3a63

Please sign in to comment.