mirror of https://github.com/k3s-io/k3s
Generalized namespace creation pattern for test utils
parent
6d8a25ff56
commit
e548c16f7b
|
@ -23,6 +23,8 @@ import (
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
|
//"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
|
||||||
|
//"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
|
@ -77,7 +79,7 @@ func LaunchNetTestPodPerNode(nodes *api.NodeList, name string, c *client.Client,
|
||||||
var _ = Describe("Networking", func() {
|
var _ = Describe("Networking", func() {
|
||||||
|
|
||||||
//This namespace is modified throughout the course of the test.
|
//This namespace is modified throughout the course of the test.
|
||||||
var namespaceObj *api.Namespace
|
var namespace *api.Namespace
|
||||||
var svcname = "nettest"
|
var svcname = "nettest"
|
||||||
var c *client.Client = nil
|
var c *client.Client = nil
|
||||||
|
|
||||||
|
@ -94,30 +96,19 @@ var _ = Describe("Networking", func() {
|
||||||
Failf("Unexpected error code, expected 200, got, %v (%v)", resp.StatusCode, resp)
|
Failf("Unexpected error code, expected 200, got, %v (%v)", resp.StatusCode, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
By("Building a namespace api object")
|
|
||||||
var ns = svcname + "-" + randomSuffix()
|
|
||||||
|
|
||||||
namespaceObj = &api.Namespace{
|
|
||||||
ObjectMeta: api.ObjectMeta{
|
|
||||||
Name: ns,
|
|
||||||
Namespace: "",
|
|
||||||
},
|
|
||||||
Status: api.NamespaceStatus{},
|
|
||||||
}
|
|
||||||
|
|
||||||
By("Creating a kubernetes client")
|
By("Creating a kubernetes client")
|
||||||
c, err = loadClient()
|
c, err = loadClient()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
By("Creating a namespace for this test suite")
|
By("Building a namespace api object")
|
||||||
_, err = c.Namespaces().Create(namespaceObj)
|
namespace, err = createTestingNS("nettest", c)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
AfterEach(func() {
|
||||||
By("Destroying namespace for this suite")
|
By(fmt.Sprintf("Destroying namespace for this suite %v", namespace.Name))
|
||||||
if err := c.Namespaces().Delete(namespaceObj.Name); err != nil {
|
if err := c.Namespaces().Delete(namespace.Name); err != nil {
|
||||||
Failf("Couldn't delete ns %s", err)
|
Failf("Couldn't delete ns %s", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -134,7 +125,7 @@ var _ = Describe("Networking", func() {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
By(fmt.Sprintf("testing: %s", test.path))
|
By(fmt.Sprintf("testing: %s", test.path))
|
||||||
data, err := c.RESTClient.Get().
|
data, err := c.RESTClient.Get().
|
||||||
Namespace(api.NamespaceDefault).
|
Namespace(namespace.Name).
|
||||||
AbsPath(test.path).
|
AbsPath(test.path).
|
||||||
DoRaw()
|
DoRaw()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -143,9 +134,6 @@ var _ = Describe("Networking", func() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Create a unique namespace for this test.
|
|
||||||
ns := "nettest-" + randomSuffix()
|
|
||||||
|
|
||||||
//Now we can proceed with the test.
|
//Now we can proceed with the test.
|
||||||
It("should function for intra-pod communication", func() {
|
It("should function for intra-pod communication", func() {
|
||||||
|
|
||||||
|
@ -154,8 +142,8 @@ var _ = Describe("Networking", func() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
By(fmt.Sprintf("Creating a service named [%s] in namespace %s", svcname, ns))
|
By(fmt.Sprintf("Creating a service named [%s] in namespace %s", svcname, namespace.Name))
|
||||||
svc, err := c.Services(ns).Create(&api.Service{
|
svc, err := c.Services(namespace.Name).Create(&api.Service{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
Name: svcname,
|
Name: svcname,
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
|
@ -181,7 +169,7 @@ var _ = Describe("Networking", func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
defer GinkgoRecover()
|
defer GinkgoRecover()
|
||||||
By("Cleaning up the service")
|
By("Cleaning up the service")
|
||||||
if err = c.Services(ns).Delete(svc.Name); err != nil {
|
if err = c.Services(namespace.Name).Delete(svc.Name); err != nil {
|
||||||
Failf("unable to delete svc %v: %v", svc.Name, err)
|
Failf("unable to delete svc %v: %v", svc.Name, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -193,14 +181,14 @@ var _ = Describe("Networking", func() {
|
||||||
Failf("Failed to list nodes: %v", err)
|
Failf("Failed to list nodes: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
podNames := LaunchNetTestPodPerNode(nodes, svcname, c, ns)
|
podNames := LaunchNetTestPodPerNode(nodes, svcname, c, namespace.Name)
|
||||||
|
|
||||||
// Clean up the pods
|
// Clean up the pods
|
||||||
defer func() {
|
defer func() {
|
||||||
defer GinkgoRecover()
|
defer GinkgoRecover()
|
||||||
By("Cleaning up the webserver pods")
|
By("Cleaning up the webserver pods")
|
||||||
for _, podName := range podNames {
|
for _, podName := range podNames {
|
||||||
if err = c.Pods(ns).Delete(podName); err != nil {
|
if err = c.Pods(namespace.Name).Delete(podName); err != nil {
|
||||||
Logf("Failed to delete pod %s: %v", podName, err)
|
Logf("Failed to delete pod %s: %v", podName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,7 +196,7 @@ var _ = Describe("Networking", func() {
|
||||||
|
|
||||||
By("Waiting for the webserver pods to transition to Running state")
|
By("Waiting for the webserver pods to transition to Running state")
|
||||||
for _, podName := range podNames {
|
for _, podName := range podNames {
|
||||||
err = waitForPodRunningInNamespace(c, podName, ns)
|
err = waitForPodRunningInNamespace(c, podName, namespace.Name)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,7 +212,7 @@ var _ = Describe("Networking", func() {
|
||||||
Logf("About to make a proxy status call")
|
Logf("About to make a proxy status call")
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
body, err = c.Get().
|
body, err = c.Get().
|
||||||
Namespace(ns).
|
Namespace(namespace.Name).
|
||||||
Prefix("proxy").
|
Prefix("proxy").
|
||||||
Resource("services").
|
Resource("services").
|
||||||
Name(svc.Name).
|
Name(svc.Name).
|
||||||
|
@ -246,7 +234,7 @@ var _ = Describe("Networking", func() {
|
||||||
break
|
break
|
||||||
case "fail":
|
case "fail":
|
||||||
if body, err = c.Get().
|
if body, err = c.Get().
|
||||||
Namespace(ns).Prefix("proxy").
|
Namespace(namespace.Name).Prefix("proxy").
|
||||||
Resource("services").
|
Resource("services").
|
||||||
Name(svc.Name).Suffix("read").
|
Name(svc.Name).Suffix("read").
|
||||||
DoRaw(); err != nil {
|
DoRaw(); err != nil {
|
||||||
|
@ -260,7 +248,7 @@ var _ = Describe("Networking", func() {
|
||||||
|
|
||||||
if !passed {
|
if !passed {
|
||||||
if body, err = c.Get().
|
if body, err = c.Get().
|
||||||
Namespace(ns).
|
Namespace(namespace.Name).
|
||||||
Prefix("proxy").
|
Prefix("proxy").
|
||||||
Resource("services").
|
Resource("services").
|
||||||
Name(svc.Name).
|
Name(svc.Name).
|
||||||
|
|
|
@ -81,6 +81,19 @@ func waitForPodCondition(c *client.Client, ns, podName, desc string, condition p
|
||||||
return fmt.Errorf("gave up waiting for pod %s to be %s after %.2f seconds", podName, desc, podStartTimeout.Seconds())
|
return fmt.Errorf("gave up waiting for pod %s to be %s after %.2f seconds", podName, desc, podStartTimeout.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createNS should be used by every test, note that we append a common prefix to the provided test name.
|
||||||
|
func createTestingNS(baseName string, c *client.Client) (*api.Namespace, error) {
|
||||||
|
namespaceObj := &api.Namespace{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: fmt.Sprintf("e2e-tests-%v-%v", baseName, randomSuffix()),
|
||||||
|
Namespace: "",
|
||||||
|
},
|
||||||
|
Status: api.NamespaceStatus{},
|
||||||
|
}
|
||||||
|
_, err := c.Namespaces().Create(namespaceObj)
|
||||||
|
return namespaceObj, err
|
||||||
|
}
|
||||||
|
|
||||||
func waitForPodRunningInNamespace(c *client.Client, podName string, namespace string) error {
|
func waitForPodRunningInNamespace(c *client.Client, podName string, namespace string) error {
|
||||||
return waitForPodCondition(c, namespace, podName, "running", func(pod *api.Pod) (bool, error) {
|
return waitForPodCondition(c, namespace, podName, "running", func(pod *api.Pod) (bool, error) {
|
||||||
return (pod.Status.Phase == api.PodRunning), nil
|
return (pod.Status.Phase == api.PodRunning), nil
|
||||||
|
@ -179,6 +192,8 @@ func loadClient() (*client.Client, error) {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// randomSuffix provides a random string to append to pods,services,rcs.
|
||||||
|
// NOTE: For test's namespaces, instead use createGinkgoNS(..).
|
||||||
// TODO: Allow service names to have the same form as names
|
// TODO: Allow service names to have the same form as names
|
||||||
// for pods and replication controllers so we don't
|
// for pods and replication controllers so we don't
|
||||||
// need to use such a function and can instead
|
// need to use such a function and can instead
|
||||||
|
@ -269,6 +284,7 @@ func validateController(c *client.Client, containerImage string, replicas int, c
|
||||||
Failf("Timed out after %v seconds waiting for %s pods to reach valid state", podStartTimeout.Seconds(), testname)
|
Failf("Timed out after %v seconds waiting for %s pods to reach valid state", podStartTimeout.Seconds(), testname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// kubectlCmd runs the kubectl executable.
|
||||||
// kubectlCmd runs the kubectl executable.
|
// kubectlCmd runs the kubectl executable.
|
||||||
func kubectlCmd(args ...string) *exec.Cmd {
|
func kubectlCmd(args ...string) *exec.Cmd {
|
||||||
defaultArgs := []string{}
|
defaultArgs := []string{}
|
||||||
|
|
Loading…
Reference in New Issue