k8s.io/kubernetes@v1.29.3/test/e2e/e2e_test.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package e2e
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"math/rand"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/onsi/ginkgo/v2"
    29  	"gopkg.in/yaml.v2"
    30  
    31  	// Never, ever remove the line with "/ginkgo". Without it,
    32  	// the ginkgo test runner will not detect that this
    33  	// directory contains a Ginkgo test suite.
    34  	// See https://github.com/kubernetes/kubernetes/issues/74827
    35  	// "github.com/onsi/ginkgo/v2"
    36  
    37  	"k8s.io/component-base/version"
    38  	"k8s.io/klog/v2"
    39  	conformancetestdata "k8s.io/kubernetes/test/conformance/testdata"
    40  	"k8s.io/kubernetes/test/e2e/framework"
    41  	"k8s.io/kubernetes/test/e2e/framework/config"
    42  	"k8s.io/kubernetes/test/e2e/framework/testfiles"
    43  	e2etestingmanifests "k8s.io/kubernetes/test/e2e/testing-manifests"
    44  	testfixtures "k8s.io/kubernetes/test/fixtures"
    45  
    46  	// define and freeze constants
    47  	_ "k8s.io/kubernetes/test/e2e/feature"
    48  	_ "k8s.io/kubernetes/test/e2e/nodefeature"
    49  
    50  	// test sources
    51  	_ "k8s.io/kubernetes/test/e2e/apimachinery"
    52  	_ "k8s.io/kubernetes/test/e2e/apps"
    53  	_ "k8s.io/kubernetes/test/e2e/architecture"
    54  	_ "k8s.io/kubernetes/test/e2e/auth"
    55  	_ "k8s.io/kubernetes/test/e2e/autoscaling"
    56  	_ "k8s.io/kubernetes/test/e2e/cloud"
    57  	_ "k8s.io/kubernetes/test/e2e/common"
    58  	_ "k8s.io/kubernetes/test/e2e/dra"
    59  	_ "k8s.io/kubernetes/test/e2e/instrumentation"
    60  	_ "k8s.io/kubernetes/test/e2e/kubectl"
    61  	_ "k8s.io/kubernetes/test/e2e/lifecycle"
    62  	_ "k8s.io/kubernetes/test/e2e/lifecycle/bootstrap"
    63  	_ "k8s.io/kubernetes/test/e2e/network"
    64  	_ "k8s.io/kubernetes/test/e2e/node"
    65  	_ "k8s.io/kubernetes/test/e2e/scheduling"
    66  	_ "k8s.io/kubernetes/test/e2e/storage"
    67  	_ "k8s.io/kubernetes/test/e2e/storage/csi_mock"
    68  	_ "k8s.io/kubernetes/test/e2e/storage/external"
    69  	_ "k8s.io/kubernetes/test/e2e/windows"
    70  
    71  	// reconfigure framework
    72  	_ "k8s.io/kubernetes/test/e2e/framework/debug/init"
    73  	_ "k8s.io/kubernetes/test/e2e/framework/metrics/init"
    74  	_ "k8s.io/kubernetes/test/e2e/framework/node/init"
    75  	_ "k8s.io/kubernetes/test/utils/format"
    76  )
    77  
    78  // handleFlags sets up all flags and parses the command line.
    79  func handleFlags() {
    80  	config.CopyFlags(config.Flags, flag.CommandLine)
    81  	framework.RegisterCommonFlags(flag.CommandLine)
    82  	framework.RegisterClusterFlags(flag.CommandLine)
    83  	flag.Parse()
    84  }
    85  
    86  func TestMain(m *testing.M) {
    87  	var versionFlag bool
    88  	flag.CommandLine.BoolVar(&versionFlag, "version", false, "Displays version information.")
    89  	listConformanceTests := flag.CommandLine.Bool("list-conformance-tests", false, "If true, will show list of conformance tests.")
    90  
    91  	// Register test flags, then parse flags.
    92  	handleFlags()
    93  
    94  	if versionFlag {
    95  		fmt.Printf("%s\n", version.Get())
    96  		os.Exit(0)
    97  	}
    98  
    99  	if flag.CommandLine.NArg() > 0 {
   100  		fmt.Fprintf(os.Stderr, "unknown additional command line arguments: %s", flag.CommandLine.Args())
   101  		os.Exit(1)
   102  	}
   103  
   104  	// Enable embedded FS file lookup as fallback
   105  	testfiles.AddFileSource(e2etestingmanifests.GetE2ETestingManifestsFS())
   106  	testfiles.AddFileSource(testfixtures.GetTestFixturesFS())
   107  	testfiles.AddFileSource(conformancetestdata.GetConformanceTestdataFS())
   108  
   109  	if *listConformanceTests {
   110  		var tests []struct {
   111  			Testname    string `yaml:"testname"`
   112  			Codename    string `yaml:"codename"`
   113  			Description string `yaml:"description"`
   114  			Release     string `yaml:"release"`
   115  			File        string `yaml:"file"`
   116  		}
   117  
   118  		data, err := testfiles.Read("test/conformance/testdata/conformance.yaml")
   119  		if err != nil {
   120  			fmt.Fprintln(os.Stderr, err)
   121  			os.Exit(1)
   122  		}
   123  		if err := yaml.Unmarshal(data, &tests); err != nil {
   124  			fmt.Fprintln(os.Stderr, err)
   125  			os.Exit(1)
   126  		}
   127  		if err := yaml.NewEncoder(os.Stdout).Encode(tests); err != nil {
   128  			fmt.Fprintln(os.Stderr, err)
   129  			os.Exit(1)
   130  		}
   131  		os.Exit(0)
   132  	}
   133  
   134  	framework.AfterReadingAllFlags(&framework.TestContext)
   135  
   136  	// TODO: Deprecating repo-root over time... instead just use gobindata_util.go , see #23987.
   137  	// Right now it is still needed, for example by
   138  	// test/e2e/framework/ingress/ingress_utils.go
   139  	// for providing the optional secret.yaml file and by
   140  	// test/e2e/framework/util.go for cluster/log-dump.
   141  	if framework.TestContext.RepoRoot != "" {
   142  		testfiles.AddFileSource(testfiles.RootFileSource{Root: framework.TestContext.RepoRoot})
   143  	}
   144  
   145  	rand.Seed(time.Now().UnixNano())
   146  	os.Exit(m.Run())
   147  }
   148  
   149  func TestE2E(t *testing.T) {
   150  	RunE2ETests(t)
   151  }
   152  
   153  var _ = ginkgo.ReportAfterEach(func(report ginkgo.SpecReport) {
   154  	progressReporter.ProcessSpecReport(report)
   155  })
   156  
   157  var _ = ginkgo.ReportBeforeSuite(func(report ginkgo.Report) {
   158  	progressReporter.SetTestsTotal(report.PreRunStats.SpecsThatWillRun)
   159  })
   160  
   161  var _ = ginkgo.ReportAfterSuite("Kubernetes e2e suite report", func(report ginkgo.Report) {
   162  	var err error
   163  	// The DetailsRepoerter will output details about every test (name, files, lines, etc) which helps
   164  	// when documenting our tests.
   165  	if len(framework.TestContext.SpecSummaryOutput) <= 0 {
   166  		return
   167  	}
   168  	absPath, err := filepath.Abs(framework.TestContext.SpecSummaryOutput)
   169  	if err != nil {
   170  		klog.Errorf("%#v\n", err)
   171  		panic(err)
   172  	}
   173  	f, err := os.Create(absPath)
   174  	if err != nil {
   175  		klog.Errorf("%#v\n", err)
   176  		panic(err)
   177  	}
   178  
   179  	defer f.Close()
   180  
   181  	for _, specReport := range report.SpecReports {
   182  		b, err := specReport.MarshalJSON()
   183  		if err != nil {
   184  			klog.Errorf("Error in detail reporter: %v", err)
   185  			return
   186  		}
   187  		_, err = f.Write(b)
   188  		if err != nil {
   189  			klog.Errorf("Error saving test details in detail reporter: %v", err)
   190  			return
   191  		}
   192  		// Printing newline between records for easier viewing in various tools.
   193  		_, err = fmt.Fprintln(f, "")
   194  		if err != nil {
   195  			klog.Errorf("Error saving test details in detail reporter: %v", err)
   196  			return
   197  		}
   198  	}
   199  })