forked from shipwright-io/build
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) |