|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/onsi/ginkgo/v2" |
| 12 | + "github.com/onsi/gomega" |
| 13 | + |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + kclientset "k8s.io/client-go/kubernetes" |
| 16 | + "k8s.io/client-go/tools/clientcmd" |
| 17 | + "k8s.io/kubernetes/openshift-hack/e2e" |
| 18 | + conformancetestdata "k8s.io/kubernetes/test/conformance/testdata" |
| 19 | + "k8s.io/kubernetes/test/e2e/framework" |
| 20 | + "k8s.io/kubernetes/test/e2e/framework/testfiles" |
| 21 | + "k8s.io/kubernetes/test/e2e/storage/external" |
| 22 | + e2etestingmanifests "k8s.io/kubernetes/test/e2e/testing-manifests" |
| 23 | + testfixtures "k8s.io/kubernetes/test/fixtures" |
| 24 | + |
| 25 | + // this appears to inexplicably auto-register global flags. |
| 26 | + _ "k8s.io/kubernetes/test/e2e/storage/drivers" |
| 27 | + |
| 28 | + // these are loading important global flags that we need to get and set |
| 29 | + _ "k8s.io/kubernetes/test/e2e" |
| 30 | + _ "k8s.io/kubernetes/test/e2e/lifecycle" |
| 31 | +) |
| 32 | + |
| 33 | +// copied directly from github.com/openshift/origin/cmd/openshift-tests/provider.go |
| 34 | +// and github.com/openshift/origin/test/extended/util/test.go |
| 35 | +func initializeTestFramework(provider string) error { |
| 36 | + providerInfo := &ClusterConfiguration{} |
| 37 | + if err := json.Unmarshal([]byte(provider), &providerInfo); err != nil { |
| 38 | + return fmt.Errorf("provider must be a JSON object with the 'type' key at a minimum: %v", err) |
| 39 | + } |
| 40 | + if len(providerInfo.ProviderName) == 0 { |
| 41 | + return fmt.Errorf("provider must be a JSON object with the 'type' key") |
| 42 | + } |
| 43 | + config := &ClusterConfiguration{} |
| 44 | + if err := json.Unmarshal([]byte(provider), config); err != nil { |
| 45 | + return fmt.Errorf("provider must decode into the ClusterConfig object: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + // update testContext with loaded config |
| 49 | + testContext := &framework.TestContext |
| 50 | + testContext.Provider = config.ProviderName |
| 51 | + testContext.CloudConfig = framework.CloudConfig{ |
| 52 | + ProjectID: config.ProjectID, |
| 53 | + Region: config.Region, |
| 54 | + Zone: config.Zone, |
| 55 | + Zones: config.Zones, |
| 56 | + NumNodes: config.NumNodes, |
| 57 | + MultiMaster: config.MultiMaster, |
| 58 | + MultiZone: config.MultiZone, |
| 59 | + ConfigFile: config.ConfigFile, |
| 60 | + } |
| 61 | + testContext.AllowedNotReadyNodes = -1 |
| 62 | + testContext.MinStartupPods = -1 |
| 63 | + testContext.MaxNodesToGather = 0 |
| 64 | + testContext.KubeConfig = os.Getenv("KUBECONFIG") |
| 65 | + |
| 66 | + // allow the CSI tests to access test data, but only briefly |
| 67 | + // TODO: ideally CSI would not use any of these test methods |
| 68 | + // var err error |
| 69 | + // exutil.WithCleanup(func() { err = initCSITests(dryRun) }) |
| 70 | + // TODO: for now I'm only initializing CSI directly, but we probably need that |
| 71 | + // WithCleanup here as well |
| 72 | + if err := initCSITests(); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + if ad := os.Getenv("ARTIFACT_DIR"); len(strings.TrimSpace(ad)) == 0 { |
| 77 | + os.Setenv("ARTIFACT_DIR", filepath.Join(os.TempDir(), "artifacts")) |
| 78 | + } |
| 79 | + |
| 80 | + testContext.DeleteNamespace = os.Getenv("DELETE_NAMESPACE") != "false" |
| 81 | + testContext.VerifyServiceAccount = true |
| 82 | + testfiles.AddFileSource(e2etestingmanifests.GetE2ETestingManifestsFS()) |
| 83 | + testfiles.AddFileSource(testfixtures.GetTestFixturesFS()) |
| 84 | + testfiles.AddFileSource(conformancetestdata.GetConformanceTestdataFS()) |
| 85 | + testContext.KubectlPath = "kubectl" |
| 86 | + // context.KubeConfig = KubeConfigPath() |
| 87 | + testContext.KubeConfig = os.Getenv("KUBECONFIG") |
| 88 | + |
| 89 | + // "debian" is used when not set. At least GlusterFS tests need "custom". |
| 90 | + // (There is no option for "rhel" or "centos".) |
| 91 | + testContext.NodeOSDistro = "custom" |
| 92 | + testContext.MasterOSDistro = "custom" |
| 93 | + |
| 94 | + // load and set the host variable for kubectl |
| 95 | + clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&clientcmd.ClientConfigLoadingRules{ExplicitPath: testContext.KubeConfig}, &clientcmd.ConfigOverrides{}) |
| 96 | + cfg, err := clientConfig.ClientConfig() |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + testContext.Host = cfg.Host |
| 101 | + |
| 102 | + // Ensure that Kube tests run privileged (like they do upstream) |
| 103 | + testContext.CreateTestingNS = func(ctx context.Context, baseName string, c kclientset.Interface, labels map[string]string) (*corev1.Namespace, error) { |
| 104 | + return e2e.CreateTestingNS(ctx, baseName, c, labels, true) |
| 105 | + } |
| 106 | + |
| 107 | + gomega.RegisterFailHandler(ginkgo.Fail) |
| 108 | + |
| 109 | + framework.AfterReadingAllFlags(testContext) |
| 110 | + testContext.DumpLogsOnFailure = true |
| 111 | + |
| 112 | + // these constants are taken from kube e2e and used by tests |
| 113 | + testContext.IPFamily = "ipv4" |
| 114 | + if config.HasIPv6 && !config.HasIPv4 { |
| 115 | + testContext.IPFamily = "ipv6" |
| 116 | + } |
| 117 | + |
| 118 | + testContext.ReportDir = os.Getenv("TEST_JUNIT_DIR") |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +const ( |
| 124 | + manifestEnvVar = "TEST_CSI_DRIVER_FILES" |
| 125 | +) |
| 126 | + |
| 127 | +// copied directly from github.com/openshift/origin/cmd/openshift-tests/csi.go |
| 128 | +// Initialize openshift/csi suite, i.e. define CSI tests from TEST_CSI_DRIVER_FILES. |
| 129 | +func initCSITests() error { |
| 130 | + manifestList := os.Getenv(manifestEnvVar) |
| 131 | + if manifestList != "" { |
| 132 | + manifests := strings.Split(manifestList, ",") |
| 133 | + for _, manifest := range manifests { |
| 134 | + if err := external.AddDriverDefinition(manifest); err != nil { |
| 135 | + return fmt.Errorf("failed to load manifest from %q: %s", manifest, err) |
| 136 | + } |
| 137 | + // Register the base dir of the manifest file as a file source. |
| 138 | + // With this we can reference the CSI driver's storageClass |
| 139 | + // in the manifest file (FromFile field). |
| 140 | + testfiles.AddFileSource(testfiles.RootFileSource{ |
| 141 | + Root: filepath.Dir(manifest), |
| 142 | + }) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
0 commit comments