...

Source file src/github.com/chaos-mesh/chaos-mesh/test/e2e/e2e_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/test/e2e

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package e2e
    15  
    16  import (
    17  	"flag"
    18  	"fmt"
    19  	"math/rand"
    20  	"os"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/onsi/ginkgo"
    25  	ginkgoconfig "github.com/onsi/ginkgo/config"
    26  	"github.com/onsi/gomega"
    27  	runtimeutils "k8s.io/apimachinery/pkg/util/runtime"
    28  	"k8s.io/client-go/tools/clientcmd"
    29  	"k8s.io/component-base/logs"
    30  	"k8s.io/klog"
    31  	"k8s.io/kubernetes/test/e2e/framework"
    32  	"k8s.io/kubernetes/test/e2e/framework/config"
    33  	e2elog "k8s.io/kubernetes/test/e2e/framework/log"
    34  	"k8s.io/kubernetes/test/e2e/framework/testfiles"
    35  	"k8s.io/kubernetes/test/e2e/framework/viperconfig"
    36  
    37  	e2econfig "github.com/chaos-mesh/chaos-mesh/test/e2e/config"
    38  
    39  	// test sources
    40  	_ "github.com/chaos-mesh/chaos-mesh/test/e2e/chaos"
    41  )
    42  
    43  var viperConfig = flag.String("viper-config", "", "The name of a viper config file (https://github.com/spf13/viper#what-is-viper). All e2e command line parameters can also be configured in such a file. May contain a path and may or may not contain the file suffix. The default is to look for an optional file with `e2e` as base name. If a file is specified explicitly, it must be present.")
    44  
    45  // handleFlags sets up all flags and parses the command line.
    46  func handleFlags() {
    47  	config.CopyFlags(config.Flags, flag.CommandLine)
    48  	framework.RegisterCommonFlags(flag.CommandLine)
    49  	hackRegisterClusterFlags(flag.CommandLine)
    50  	e2econfig.RegisterOperatorFlags(flag.CommandLine)
    51  	flag.Parse()
    52  }
    53  
    54  func TestMain(m *testing.M) {
    55  	// Register test flags, then parse flags.
    56  	handleFlags()
    57  
    58  	// Now that we know which Viper config (if any) was chosen,
    59  	// parse it and update those options which weren't already set via command line flags
    60  	// (which have higher priority).
    61  	if err := viperconfig.ViperizeFlags(*viperConfig, "e2e", flag.CommandLine); err != nil {
    62  		fmt.Fprintln(os.Stderr, err)
    63  		os.Exit(1)
    64  	}
    65  
    66  	framework.AfterReadingAllFlags(&framework.TestContext)
    67  
    68  	if framework.TestContext.RepoRoot != "" {
    69  		testfiles.AddFileSource(testfiles.RootFileSource{Root: framework.TestContext.RepoRoot})
    70  	}
    71  
    72  	rand.Seed(time.Now().UnixNano())
    73  	os.Exit(m.Run())
    74  }
    75  
    76  func TestE2E(t *testing.T) {
    77  	RunE2ETests(t)
    78  }
    79  
    80  func RunE2ETests(t *testing.T) {
    81  	runtimeutils.ReallyCrash = true
    82  	logs.InitLogs()
    83  	defer logs.FlushLogs()
    84  
    85  	gomega.RegisterFailHandler(e2elog.Fail)
    86  
    87  	// Run tests through the Ginkgo runner with output to console + JUnit for Jenkins
    88  	var r []ginkgo.Reporter
    89  	klog.Infof("Starting e2e run %q on Ginkgo node %d", framework.RunID, ginkgoconfig.GinkgoConfig.ParallelNode)
    90  
    91  	ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "chaosmesh e2e suit", r)
    92  }
    93  
    94  // we hack framework.RegisterClusterFlags to avoid redefine flag error
    95  // caused by controller-runtime client
    96  func hackRegisterClusterFlags(flags *flag.FlagSet) {
    97  	framework.TestContext.KubeConfig = os.Getenv("KUBECONFIG")
    98  	if len(framework.TestContext.KubeConfig) < 1 {
    99  		klog.Fatalf("KUBECONFIG ENV NOT SET")
   100  	}
   101  	flags.BoolVar(&framework.TestContext.VerifyServiceAccount, "e2e-verify-service-account", true, "If true tests will verify the service account before running.")
   102  	flags.StringVar(&framework.TestContext.KubeContext, clientcmd.FlagContext, "", "kubeconfig context to use/override. If unset, will use value from 'current-context'")
   103  	flags.StringVar(&framework.TestContext.KubeAPIContentType, "kube-api-content-type", "application/vnd.kubernetes.protobuf", "ContentType used to communicate with apiserver")
   104  
   105  	flags.StringVar(&framework.TestContext.KubeVolumeDir, "volume-dir", "/var/lib/kubelet", "Path to the directory containing the kubelet volumes.")
   106  	flags.StringVar(&framework.TestContext.CertDir, "cert-dir", "", "Path to the directory containing the certs. Default is empty, which doesn't use certs.")
   107  	flags.StringVar(&framework.TestContext.RepoRoot, "repo-root", "../../", "Root directory of kubernetes repository, for finding test files.")
   108  	flags.StringVar(&framework.TestContext.Provider, "provider", "", "The name of the Kubernetes provider (gce, gke, local, skeleton (the fallback if not set), etc.)")
   109  	flags.StringVar(&framework.TestContext.Tooling, "tooling", "", "The tooling in use (kops, gke, etc.)")
   110  	flags.StringVar(&framework.TestContext.OutputDir, "e2e-output-dir", "/tmp", "Output directory for interesting/useful test data, like performance data, benchmarks, and other metrics.")
   111  	flags.StringVar(&framework.TestContext.Prefix, "prefix", "e2e", "A prefix to be added to cloud resources created during testing.")
   112  	flags.StringVar(&framework.TestContext.MasterOSDistro, "master-os-distro", "debian", "The OS distribution of cluster master (debian, ubuntu, gci, coreos, or custom).")
   113  	flags.StringVar(&framework.TestContext.NodeOSDistro, "node-os-distro", "debian", "The OS distribution of cluster VM instances (debian, ubuntu, gci, coreos, or custom).")
   114  	flags.StringVar(&framework.TestContext.ClusterMonitoringMode, "cluster-monitoring-mode", "standalone", "The monitoring solution that is used in the cluster.")
   115  	flags.StringVar(&framework.TestContext.ClusterDNSDomain, "dns-domain", "cluster.local", "The DNS Domain of the cluster.")
   116  
   117  	flags.IntVar(&framework.TestContext.MinStartupPods, "minStartupPods", 0, "The number of pods which we need to see in 'Running' state with a 'Ready' condition of true, before we try running tests. This is useful in any cluster which needs some base pod-based services running before it can be used.")
   118  	flags.DurationVar(&framework.TestContext.SystemPodsStartupTimeout, "system-pods-startup-timeout", 10*time.Minute, "Timeout for waiting for all system pods to be running before starting tests.")
   119  	flags.DurationVar(&framework.TestContext.NodeSchedulableTimeout, "node-schedulable-timeout", 30*time.Minute, "Timeout for waiting for all nodes to be schedulable.")
   120  	flags.DurationVar(&framework.TestContext.SystemDaemonsetStartupTimeout, "system-daemonsets-startup-timeout", 5*time.Minute, "Timeout for waiting for all system daemonsets to be ready.")
   121  	flags.StringVar(&framework.TestContext.EtcdUpgradeStorage, "etcd-upgrade-storage", "", "The storage version to upgrade to (either 'etcdv2' or 'etcdv3') if doing an etcd upgrade test.")
   122  	flags.StringVar(&framework.TestContext.EtcdUpgradeVersion, "etcd-upgrade-version", "", "The etcd binary version to upgrade to (e.g., '3.0.14', '2.3.7') if doing an etcd upgrade test.")
   123  	flags.StringVar(&framework.TestContext.GCEUpgradeScript, "gce-upgrade-script", "", "Script to use to upgrade a GCE cluster.")
   124  	flags.BoolVar(&framework.TestContext.CleanStart, "clean-start", false, "If true, purge all namespaces except default and system before running tests. This serves to Cleanup test namespaces from failed/interrupted e2e runs in a long-lived cluster.")
   125  
   126  	nodeKiller := &framework.TestContext.NodeKiller
   127  	flags.BoolVar(&nodeKiller.Enabled, "node-killer", false, "Whether NodeKiller should kill any nodes.")
   128  	flags.Float64Var(&nodeKiller.FailureRatio, "node-killer-failure-ratio", 0.01, "Percentage of nodes to be killed")
   129  	flags.DurationVar(&nodeKiller.Interval, "node-killer-interval", 1*time.Minute, "Time between node failures.")
   130  	flags.Float64Var(&nodeKiller.JitterFactor, "node-killer-jitter-factor", 60, "Factor used to jitter node failures.")
   131  	flags.DurationVar(&nodeKiller.SimulatedDowntime, "node-killer-simulated-downtime", 10*time.Minute, "A delay between node death and recreation")
   132  }
   133