Skip to content

Commit

Permalink
Merge pull request #137 from fedepaol/test_reporter
Browse files Browse the repository at this point in the history
Add tests failure reporter.
  • Loading branch information
openshift-merge-robot authored Apr 20, 2020
2 parents eb3c008 + 3c95f9b commit ef3c35c
Show file tree
Hide file tree
Showing 17 changed files with 19,119 additions and 2 deletions.
53 changes: 53 additions & 0 deletions functests/test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package test_test

import (
"flag"
"log"
"os"
"testing"
"time"

Expand All @@ -17,20 +19,29 @@ import (

testutils "github.com/openshift-kni/cnf-features-deploy/functests/utils"
testclient "github.com/openshift-kni/cnf-features-deploy/functests/utils/client"
"github.com/openshift-kni/cnf-features-deploy/functests/utils/k8sreporter"
"github.com/openshift-kni/cnf-features-deploy/functests/utils/namespaces"

mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
ptpv1 "github.com/openshift/ptp-operator/pkg/apis/ptp/v1"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/runtime"
ginkgo_reporters "kubevirt.io/qe-tools/pkg/ginkgo-reporters"
)

// TODO: we should refactor tests to use client from controller-runtime package
// see - https://github.com/openshift/cluster-api-actuator-pkg/blob/master/pkg/e2e/framework/framework.go

var junitPath *string
var reportPath *string

func init() {
junitPath = flag.String("junit", "junit.xml", "the path for the junit format report")
reportPath = flag.String("report", "", "the path of the report file containing details for failed tests")
}

func TestTest(t *testing.T) {
Expand All @@ -43,6 +54,15 @@ func TestTest(t *testing.T) {
if junitPath != nil {
rr = append(rr, reporters.NewJUnitReporter(*junitPath))
}
if reportPath != nil && *reportPath != "" {
reporter, output, err := newTestsReporter(*reportPath)
if err != nil {
log.Fatalf("Failed to create log reporter %s", err)
}
defer output.Close()
rr = append(rr, reporter)
}

RunSpecsWithDefaultAndCustomReporters(t, "CNF Features e2e integration tests", rr)
}

Expand All @@ -62,3 +82,36 @@ var _ = AfterSuite(func() {
Expect(err).ToNot(HaveOccurred())
err = namespaces.WaitForDeletion(testclient.Client, testutils.NamespaceTesting, 5*time.Minute)
})

func newTestsReporter(reportPath string) (*k8sreporter.KubernetesReporter, *os.File, error) {
addToScheme := func(s *runtime.Scheme) {
ptpv1.AddToScheme(s)
mcfgv1.AddToScheme(s)
}

filterPods := func(pod *v1.Pod) bool {
if pod.Namespace == "sctptest" {
return false
}
if pod.Namespace == "openshift-ptp" {
return false
}
return true
}

f, err := os.OpenFile(reportPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, nil, err
}

crs := []k8sreporter.CRData{
k8sreporter.CRData{
Cr: &mcfgv1.MachineConfigPoolList{},
},
k8sreporter.CRData{
Cr: &ptpv1.PtpConfigList{},
},
}

return k8sreporter.New("", addToScheme, filterPods, f, crs...), f, nil
}
53 changes: 53 additions & 0 deletions functests/utils/k8sreporter/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package k8sreporter

import (
"os"

"github.com/golang/glog"
clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
"k8s.io/apimachinery/pkg/runtime"
appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

type clientSet struct {
corev1client.CoreV1Interface
clientconfigv1.ConfigV1Interface
appsv1client.AppsV1Interface
runtimeclient.Client
}

// New returns a *ClientBuilder with the given kubeconfig.
func newClient(kubeconfig string, crScheme *runtime.Scheme) *clientSet {
var config *rest.Config
var err error

if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}

if kubeconfig != "" {
glog.V(4).Infof("Loading kube client config from path %q", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
glog.V(4).Infof("Using in-cluster kube client config")
config, err = rest.InClusterConfig()
}
if err != nil {
panic(err)
}

clientSet := &clientSet{}
clientSet.CoreV1Interface = corev1client.NewForConfigOrDie(config)
clientSet.ConfigV1Interface = clientconfigv1.NewForConfigOrDie(config)
clientSet.AppsV1Interface = appsv1client.NewForConfigOrDie(config)

clientSet.Client, err = runtimeclient.New(config, client.Options{
Scheme: crScheme,
})
return clientSet
}
55 changes: 55 additions & 0 deletions functests/utils/k8sreporter/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"flag"
"log"
"os"
"time"

promv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/openshift-kni/cnf-features-deploy/functests/utils/k8sreporter"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

func main() {
kubeconfig := flag.String("kubeconfig", "", "the kubeconfig path")
report := flag.String("report", "report.log", "the file name used for the report")

flag.Parse()

addToScheme := func(s *runtime.Scheme) {
mcfgv1.AddToScheme(s)
promv1.AddToScheme(s)
}

filterPods := func(pod *v1.Pod) bool {
// never filter
return false
}

f, err := os.OpenFile(*report, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("failed to open the file: %v\n", err)
return
}
defer f.Close()

crs := []k8sreporter.CRData{
k8sreporter.CRData{
Cr: &mcfgv1.MachineConfigPoolList{},
},
k8sreporter.CRData{
Cr: &promv1.ServiceMonitorList{},
},
k8sreporter.CRData{
Cr: &promv1.ServiceMonitorList{},
Namespace: pointer.StringPtr("openshift-multus"),
},
}

reporter := k8sreporter.New(*kubeconfig, addToScheme, filterPods, f, crs...)
reporter.Dump(10 * time.Minute)
}
Loading

0 comments on commit ef3c35c

Please sign in to comment.