mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Ported TestNetwork to native ginkgo syntax #4179
This commit is contained in:
parent
6d171a458e
commit
a1dcec8a1c
@ -17,147 +17,156 @@ limitations under the License.
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"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/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/golang/glog"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNetwork(c *client.Client) bool {
|
var _ = Describe("Networking", func() {
|
||||||
if testContext.provider == "vagrant" {
|
var c *client.Client
|
||||||
glog.Infof("Skipping test which is broken for vagrant (See https://github.com/GoogleCloudPlatform/kubernetes/issues/3580)")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test basic external connectivity.
|
BeforeEach(func() {
|
||||||
resp, err := http.Get("http://google.com/")
|
c = loadClientOrDie()
|
||||||
if err != nil {
|
|
||||||
glog.Errorf("unable to talk to the external internet: %v", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
glog.Errorf("unexpected error code. expected 200, got: %v (%v)", resp.StatusCode, resp)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
ns := api.NamespaceDefault
|
|
||||||
// TODO(satnam6502): Replace call of randomSuffix with call to NewUUID when service
|
|
||||||
// names have the same form as pod and replication controller names.
|
|
||||||
name := "nettest-" + randomSuffix()
|
|
||||||
svc, err := c.Services(ns).Create(&api.Service{
|
|
||||||
ObjectMeta: api.ObjectMeta{
|
|
||||||
Name: name,
|
|
||||||
Labels: map[string]string{
|
|
||||||
"name": name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Spec: api.ServiceSpec{
|
|
||||||
Port: 8080,
|
|
||||||
ContainerPort: util.NewIntOrStringFromInt(8080),
|
|
||||||
Selector: map[string]string{
|
|
||||||
"name": name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
glog.Infof("Creating service with name %s", svc.Name)
|
|
||||||
if err != nil {
|
It("should function for pods", func() {
|
||||||
glog.Errorf("unable to create test service %s: %v", svc.Name, err)
|
if testContext.provider == "vagrant" {
|
||||||
return false
|
By("Skipping test which is broken for vagrant (See https://github.com/GoogleCloudPlatform/kubernetes/issues/3580)")
|
||||||
}
|
return
|
||||||
// Clean up service
|
|
||||||
defer func() {
|
|
||||||
if err = c.Services(ns).Delete(svc.Name); err != nil {
|
|
||||||
glog.Errorf("unable to delete svc %v: %v", svc.Name, err)
|
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
rc, err := c.ReplicationControllers(ns).Create(&api.ReplicationController{
|
// Test basic external connectivity.
|
||||||
ObjectMeta: api.ObjectMeta{
|
resp, err := http.Get("http://google.com/")
|
||||||
Name: name,
|
if err != nil {
|
||||||
Labels: map[string]string{
|
Fail(fmt.Sprintf("unable to talk to the external internet: %v", err))
|
||||||
"name": name,
|
}
|
||||||
},
|
if resp.StatusCode != http.StatusOK {
|
||||||
},
|
Fail(fmt.Sprintf("unexpected error code. expected 200, got: %v (%v)", resp.StatusCode, resp))
|
||||||
Spec: api.ReplicationControllerSpec{
|
}
|
||||||
Replicas: 8,
|
|
||||||
Selector: map[string]string{
|
ns := api.NamespaceDefault
|
||||||
"name": name,
|
// TODO(satnam6502): Replace call of randomSuffix with call to NewUUID when service
|
||||||
},
|
// names have the same form as pod and replication controller names.
|
||||||
Template: &api.PodTemplateSpec{
|
name := "nettest-" + randomSuffix()
|
||||||
ObjectMeta: api.ObjectMeta{
|
|
||||||
Labels: map[string]string{"name": name},
|
svc, err := c.Services(ns).Create(&api.Service{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Labels: map[string]string{
|
||||||
|
"name": name,
|
||||||
},
|
},
|
||||||
Spec: api.PodSpec{
|
},
|
||||||
Containers: []api.Container{
|
Spec: api.ServiceSpec{
|
||||||
{
|
Port: 8080,
|
||||||
Name: "webserver",
|
ContainerPort: util.NewIntOrStringFromInt(8080),
|
||||||
Image: "kubernetes/nettest:latest",
|
Selector: map[string]string{
|
||||||
Command: []string{"-service=" + name},
|
"name": name,
|
||||||
Ports: []api.Port{{ContainerPort: 8080}},
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
By(fmt.Sprintf("Creating service with name %s", svc.Name))
|
||||||
|
if err != nil {
|
||||||
|
Fail(fmt.Sprintf("unable to create test service %s: %v", svc.Name, err))
|
||||||
|
}
|
||||||
|
// Clean up service
|
||||||
|
defer func() {
|
||||||
|
defer GinkgoRecover()
|
||||||
|
By("Cleaning up the service")
|
||||||
|
if err = c.Services(ns).Delete(svc.Name); err != nil {
|
||||||
|
Fail(fmt.Sprintf("unable to delete svc %v: %v", svc.Name, err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
By("Creating a replication controller")
|
||||||
|
rc, err := c.ReplicationControllers(ns).Create(&api.ReplicationController{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Labels: map[string]string{
|
||||||
|
"name": name,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: api.ReplicationControllerSpec{
|
||||||
|
Replicas: 8,
|
||||||
|
Selector: map[string]string{
|
||||||
|
"name": name,
|
||||||
|
},
|
||||||
|
Template: &api.PodTemplateSpec{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Labels: map[string]string{"name": name},
|
||||||
|
},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{
|
||||||
|
Name: "webserver",
|
||||||
|
Image: "kubernetes/nettest:latest",
|
||||||
|
Command: []string{"-service=" + name},
|
||||||
|
Ports: []api.Port{{ContainerPort: 8080}},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
glog.Errorf("unable to create test rc: %v", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// Clean up rc
|
|
||||||
defer func() {
|
|
||||||
rc.Spec.Replicas = 0
|
|
||||||
rc, err = c.ReplicationControllers(ns).Update(rc)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("unable to modify replica count for rc %v: %v", rc.Name, err)
|
Fail(fmt.Sprintf("unable to create test rc: %v", err))
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if err = c.ReplicationControllers(ns).Delete(rc.Name); err != nil {
|
// Clean up rc
|
||||||
glog.Errorf("unable to delete rc %v: %v", rc.Name, err)
|
defer func() {
|
||||||
}
|
defer GinkgoRecover()
|
||||||
}()
|
By("Cleaning up the replication controller")
|
||||||
const maxAttempts = 60
|
rc.Spec.Replicas = 0
|
||||||
for i := 0; i < maxAttempts; i++ {
|
rc, err = c.ReplicationControllers(ns).Update(rc)
|
||||||
time.Sleep(2 * time.Second)
|
if err != nil {
|
||||||
body, err := c.Get().Prefix("proxy").Resource("services").Name(svc.Name).Suffix("status").Do().Raw()
|
Fail(fmt.Sprintf("unable to modify replica count for rc %v: %v", rc.Name, err))
|
||||||
if err != nil {
|
}
|
||||||
glog.Infof("Attempt %v/%v: service/pod still starting. (error: '%v')", i, maxAttempts, err)
|
if err = c.ReplicationControllers(ns).Delete(rc.Name); err != nil {
|
||||||
continue
|
Fail(fmt.Sprintf("unable to delete rc %v: %v", rc.Name, err))
|
||||||
}
|
}
|
||||||
switch string(body) {
|
}()
|
||||||
case "pass":
|
|
||||||
glog.Infof("Passed on attempt %v. Cleaning up.", i)
|
By("Waiting for connectivity to be verified")
|
||||||
return true
|
const maxAttempts = 60
|
||||||
case "running":
|
passed := false
|
||||||
glog.Infof("Attempt %v/%v: test still running", i, maxAttempts)
|
var body []byte
|
||||||
case "fail":
|
for i := 0; i < maxAttempts && !passed; i++ {
|
||||||
if body, err := c.Get().Prefix("proxy").Resource("services").Name(svc.Name).Suffix("read").Do().Raw(); err != nil {
|
time.Sleep(2 * time.Second)
|
||||||
glog.Infof("Failed on attempt %v. Cleaning up. Error reading details: %v", i, err)
|
body, err = c.Get().Prefix("proxy").Resource("services").Name(svc.Name).Suffix("status").Do().Raw()
|
||||||
} else {
|
if err != nil {
|
||||||
glog.Infof("Failed on attempt %v. Cleaning up. Details:\n%v", i, string(body))
|
fmt.Printf("Attempt %v/%v: service/pod still starting. (error: '%v')\n", i, maxAttempts, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch string(body) {
|
||||||
|
case "pass":
|
||||||
|
fmt.Printf("Passed on attempt %v. Cleaning up.\n", i)
|
||||||
|
passed = true
|
||||||
|
break
|
||||||
|
case "running":
|
||||||
|
fmt.Printf("Attempt %v/%v: test still running\n", i, maxAttempts)
|
||||||
|
break
|
||||||
|
case "fail":
|
||||||
|
if body, err = c.Get().Prefix("proxy").Resource("services").Name(svc.Name).Suffix("read").Do().Raw(); err != nil {
|
||||||
|
Fail(fmt.Sprintf("Failed on attempt %v. Cleaning up. Error reading details: %v", i, err))
|
||||||
|
} else {
|
||||||
|
Fail(fmt.Sprintf("Failed on attempt %v. Cleaning up. Details:\n%v", i, string(body)))
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if body, err := c.Get().Prefix("proxy").Resource("services").Name(svc.Name).Suffix("read").Do().Raw(); err != nil {
|
if !passed {
|
||||||
glog.Infof("Timed out. Cleaning up. Error reading details: %v", err)
|
if body, err = c.Get().Prefix("proxy").Resource("services").Name(svc.Name).Suffix("read").Do().Raw(); err != nil {
|
||||||
} else {
|
Fail(fmt.Sprintf("Timed out. Cleaning up. Error reading details: %v", err))
|
||||||
glog.Infof("Timed out. Cleaning up. Details:\n%v", string(body))
|
} else {
|
||||||
}
|
Fail(fmt.Sprintf("Timed out. Cleaning up. Details:\n%v", string(body)))
|
||||||
|
}
|
||||||
return false
|
}
|
||||||
}
|
Expect(string(body)).To(Equal("pass"))
|
||||||
|
|
||||||
var _ = Describe("TestNetwork", func() {
|
|
||||||
It("should pass", func() {
|
|
||||||
// TODO: Instead of OrDie, client should Fail the test if there's a problem.
|
|
||||||
// In general tests should Fail() instead of glog.Fatalf().
|
|
||||||
Expect(TestNetwork(loadClientOrDie())).To(BeTrue())
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user