Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
qu1queee committed Sep 14, 2020
1 parent 27a2a0a commit e5bca8b
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
37 changes: 37 additions & 0 deletions integration/build_to_buildruns_test.go
Original file line number Diff line number Diff line change
@@ -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"))
})
})
})
65 changes: 65 additions & 0 deletions integration/environment/environment.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions integration/environment/namespaces.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions integration/integration_suite_test.go
Original file line number Diff line number Diff line change
@@ -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())
})

0 comments on commit e5bca8b

Please sign in to comment.