diff --git a/cmd/suite_test.go b/cmd/suite_test.go index c0dd3411e..d89668b43 100644 --- a/cmd/suite_test.go +++ b/cmd/suite_test.go @@ -14,6 +14,7 @@ import ( . "github.com/onsi/gomega" whereaboutsv1alpha1 "github.com/k8snetworkplumbingwg/whereabouts/pkg/api/whereabouts.cni.cncf.io/v1alpha1" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" @@ -24,6 +25,7 @@ import ( var cfg *rest.Config var k8sClient client.Client +var k8sClientSet *kubernetes.Clientset var testEnv *envtest.Environment var etcdHost string var tmpdir string @@ -73,6 +75,9 @@ var _ = BeforeSuite(func(done Done) { Expect(err).ToNot(HaveOccurred()) Expect(k8sClient).ToNot(BeNil()) + k8sClientSet, err = kubernetes.NewForConfig(cfg) + Expect(err).NotTo(HaveOccurred()) + close(done) }, 60) diff --git a/pkg/reconciler/ip_test.go b/pkg/reconciler/ip_test.go index 0f795b362..6e0f89e76 100644 --- a/pkg/reconciler/ip_test.go +++ b/pkg/reconciler/ip_test.go @@ -9,6 +9,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "sigs.k8s.io/controller-runtime/pkg/client" multusv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" "github.com/k8snetworkplumbingwg/whereabouts/pkg/api/whereabouts.cni.cncf.io/v1alpha1" @@ -16,6 +17,7 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8stypes "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/kubernetes" ) var _ = Describe("Whereabouts IP reconciler", func() { @@ -30,6 +32,9 @@ var _ = Describe("Whereabouts IP reconciler", func() { var ( reconcileLooper *ReconcileLooper + k8sClient client.Client + k8sClientSet *kubernetes.Clientset + kubeConfigPath string ) Context("reconciling IP pools with a single running pod", func() { diff --git a/pkg/reconciler/suite_test.go b/pkg/reconciler/suite_test.go deleted file mode 100644 index 322e35424..000000000 --- a/pkg/reconciler/suite_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package reconciler - -import ( - "fmt" - "io/fs" - "io/ioutil" - "k8s.io/client-go/kubernetes" - "os" - "path" - "path/filepath" - "strings" - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - whereaboutsv1alpha1 "github.com/k8snetworkplumbingwg/whereabouts/pkg/api/whereabouts.cni.cncf.io/v1alpha1" - "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/rest" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/envtest" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" -) - -var ( - cfg *rest.Config - k8sClient client.Client - k8sClientSet *kubernetes.Clientset - kubeConfigPath string - testEnv *envtest.Environment - tmpdir string -) - -func TestAPIs(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecsWithDefaultAndCustomReporters(t, - "Whereabouts IP reconciler Suite", - []Reporter{}) -} - -var _ = BeforeSuite(func(done Done) { - zap.WriteTo(GinkgoWriter) - logf.SetLogger(zap.New()) - - By("bootstrapping test environment") - testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("../../", "doc", "crds")}, - } - - var err error - cfg, err = testEnv.Start() - Expect(err).ToNot(HaveOccurred()) - Expect(cfg).ToNot(BeNil()) - - Expect(testEnv.ControlPlane.Etcd).ToNot(BeNil()) - etcdURL := testEnv.ControlPlane.Etcd.URL - Expect(etcdURL).ToNot(BeNil()) - - Expect(testEnv.ControlPlane.APIServer).ToNot(BeNil()) - apiURL := testEnv.ControlPlane.APIServer.SecureServing.URL("https", "/").String() - Expect(apiURL).ToNot(BeNil()) - - tmpdir, err = ioutil.TempDir("/tmp", "whereabouts") - Expect(err).ToNot(HaveOccurred()) - kubeConfigPath = fmt.Sprintf("%s/whereabouts.kubeconfig", tmpdir) - Expect(copyEnvKubeconfigFile(testEnv.ControlPlane.APIServer.CertDir, kubeConfigPath)).To(Succeed()) - - err = whereaboutsv1alpha1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) - - k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) - Expect(err).ToNot(HaveOccurred()) - Expect(k8sClient).ToNot(BeNil()) - - k8sClientSet, err = kubernetes.NewForConfig(cfg) - Expect(err).NotTo(HaveOccurred()) - - close(done) -}, 60) - -var _ = AfterSuite(func() { - defer os.RemoveAll(tmpdir) - - By("tearing down the test environment") - err := testEnv.Stop() - Expect(err).ToNot(HaveOccurred()) -}) - -func copyEnvKubeconfigFile(envCertDir string, destFilePath string) error { - files, err := ioutil.ReadDir(envCertDir) - if err != nil { - return fmt.Errorf("failed to read the certificate dir: %w", err) - } - - for _, file := range files { - if isKubeconfigFile(file) { - return copyFile(path.Join(envCertDir, file.Name()), destFilePath, file.Mode()) - } - } - return fmt.Errorf("could not find the generated kubeconfig file") -} - -func isKubeconfigFile(file fs.FileInfo) bool { - return strings.HasSuffix(file.Name(), ".kubecfg") || - strings.HasSuffix(file.Name(), ".kubeconfig") -} - -func copyFile(src string, dst string, mode fs.FileMode) error { - fileContents, err := ioutil.ReadFile(src) - if err != nil { - return fmt.Errorf("failed to read src file %s: %w", src, err) - } - - if err := ioutil.WriteFile(dst, fileContents, mode); err != nil { - return fmt.Errorf("error writing dst file %s: %w", dst, err) - } - return nil -}