From bfefcac016ab55887c3706078086ee90fec875f5 Mon Sep 17 00:00:00 2001 From: ENCALADA Date: Fri, 21 Aug 2020 17:49:49 +0200 Subject: [PATCH] For #322 wip --- integration/build_to_buildruns_test.go | 37 +++++++++++++++ integration/environment/environment.go | 65 ++++++++++++++++++++++++++ integration/environment/namespaces.go | 34 ++++++++++++++ integration/integration_suite_test.go | 40 ++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 integration/build_to_buildruns_test.go create mode 100644 integration/environment/environment.go create mode 100644 integration/environment/namespaces.go create mode 100644 integration/integration_suite_test.go diff --git a/integration/build_to_buildruns_test.go b/integration/build_to_buildruns_test.go new file mode 100644 index 0000000000..9f76a0593b --- /dev/null +++ b/integration/build_to_buildruns_test.go @@ -0,0 +1,37 @@ +package integration_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("todo", func() { + + Context("todo", func() { + + It("todo", func() { + Expect("foo").To(Equal("foo")) + Expect("bar").To(Equal("bar")) + }) + + It("todo2", func() { + Expect("foo2").To(Equal("foo2")) + Expect("bar2").To(Equal("bar2")) + }) + + It("todo3", func() { + Expect("foo3").To(Equal("foo3")) + Expect("bar3").To(Equal("bar3")) + }) + + It("todo4", func() { + Expect("foo4").To(Equal("foo4")) + Expect("bar4").To(Equal("bar4")) + }) + + It("todo5", func() { + Expect("foo5").To(Equal("foo5")) + Expect("bar5").To(Equal("bar5")) + }) + }) +}) diff --git a/integration/environment/environment.go b/integration/environment/environment.go new file mode 100644 index 0000000000..25582445d4 --- /dev/null +++ b/integration/environment/environment.go @@ -0,0 +1,65 @@ +package environment + +import ( + "os" + "path/filepath" + "strconv" + "sync/atomic" + + gomegaConfig "github.com/onsi/ginkgo/config" + "k8s.io/client-go/kubernetes" + _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" +) + +// Environment ... +type Environment struct { + // KubeConfig *rest.Config + Clientset *kubernetes.Clientset + Namespace string +} + +var ( + namespaceCounter int32 +) + +// NewEnvironment ... +func NewEnvironment() (*Environment, error) { + atomic.AddInt32(&namespaceCounter, 1) + namespaceID := gomegaConfig.GinkgoConfig.ParallelNode*200 + int(namespaceCounter) + ns := "test-build-" + strconv.Itoa(int(namespaceID)) + + kubeconfig, err := KubeConfig() + if err != nil { + return nil, err + } + e := &Environment{ + Clientset: kubeconfig, + Namespace: ns, + } + return e, nil +} + +// KubeConfig ... +func KubeConfig() (*kubernetes.Clientset, error) { + location := os.Getenv("KUBECONFIG") + if location == "" { + location = filepath.Join(os.Getenv("HOME"), ".kube", "config") + } + + config, err := clientcmd.BuildConfigFromFlags("", location) + if err != nil { + config, err = rest.InClusterConfig() + if err != nil { + return nil, err + } + } + + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + return nil, err + } + + return clientset, nil +} diff --git a/integration/environment/namespaces.go b/integration/environment/namespaces.go new file mode 100644 index 0000000000..a84d63b637 --- /dev/null +++ b/integration/environment/namespaces.go @@ -0,0 +1,34 @@ +package environment + +import ( + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// CreateNamespace ... +func (e *Environment) CreateNamespace() error { + client := e.Clientset.CoreV1().Namespaces() + ns := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: e.Namespace, + }, + } + _, err := client.Create(ns) + if err != nil { + return err + } + return nil +} + +// DeleteNamespaces ... +func (e *Environment) DeleteNamespaces(nsList []string) error { + client := e.Clientset.CoreV1().Namespaces() + + for _, ns := range nsList { + err := client.Delete(ns, &metav1.DeleteOptions{}) + if err != nil { + return err + } + } + return nil +} diff --git a/integration/integration_suite_test.go b/integration/integration_suite_test.go new file mode 100644 index 0000000000..d419f7fc47 --- /dev/null +++ b/integration/integration_suite_test.go @@ -0,0 +1,40 @@ +package integration_test + +import ( + "fmt" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/shipwright-io/build/integration/environment" +) + +func TestIntegration(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Integration Suite") +} + +var ( + deleteNSList []string + env *environment.Environment + err error +) + +var _ = BeforeEach(func() { + env, err = environment.NewEnvironment() + if err != nil { + fmt.Printf("fail to get an instance of Environment, error: %v", err) + } + + err := env.CreateNamespace() + if err != nil { + fmt.Printf("fail to create test namespace: %v, error: %v", env.Namespace, err) + } + + deleteNSList = append(deleteNSList, env.Namespace) + +}) + +var _ = AfterSuite(func() { + Expect(env.DeleteNamespaces(deleteNSList)).To(BeNil()) +})