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