mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 02:34:03 +00:00
Converted TestPodHasServiceEnvVars to native ginkgo syntax #3941
This commit is contained in:
parent
eb5d340851
commit
da2594710a
@ -17,19 +17,26 @@ limitations under the License.
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
"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/golang/glog"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestPodHasServiceEnvVars checks that kubelets and scheduler send events about pods scheduling and running.
|
var _ = Describe("TestPodHasServiceEnvVars", func() {
|
||||||
func TestPodHasServiceEnvVars(c *client.Client) bool {
|
var (
|
||||||
|
c *client.Client
|
||||||
|
)
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
c = loadClientOrDie()
|
||||||
|
})
|
||||||
|
|
||||||
|
It("should contain environment variables for services", func() {
|
||||||
// Make a pod that will be a service.
|
// Make a pod that will be a service.
|
||||||
// This pod serves its hostname via HTTP.
|
// This pod serves its hostname via HTTP.
|
||||||
serverPod := parsePodOrDie(`{
|
serverPod := parsePodOrDie(`{
|
||||||
@ -56,10 +63,12 @@ func TestPodHasServiceEnvVars(c *client.Client) bool {
|
|||||||
}`)
|
}`)
|
||||||
_, err := c.Pods(api.NamespaceDefault).Create(serverPod)
|
_, err := c.Pods(api.NamespaceDefault).Create(serverPod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Failed to create serverPod: %v", err)
|
Fail(fmt.Sprintf("Failed to create serverPod: %v", err))
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
defer c.Pods(api.NamespaceDefault).Delete(serverPod.Name)
|
defer func() {
|
||||||
|
defer GinkgoRecover()
|
||||||
|
c.Pods(api.NamespaceDefault).Delete(serverPod.Name)
|
||||||
|
}()
|
||||||
waitForPodRunning(c, serverPod.Name)
|
waitForPodRunning(c, serverPod.Name)
|
||||||
|
|
||||||
// This service exposes pod p's port 8080 as a service on port 8765
|
// This service exposes pod p's port 8080 as a service on port 8765
|
||||||
@ -74,19 +83,19 @@ func TestPodHasServiceEnvVars(c *client.Client) bool {
|
|||||||
}
|
}
|
||||||
}`)
|
}`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Failed to delete service: %v", err)
|
Fail(fmt.Sprintf("Failed to delete service: %v", err))
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
time.Sleep(2)
|
time.Sleep(2)
|
||||||
_, err = c.Services(api.NamespaceDefault).Create(svc)
|
_, err = c.Services(api.NamespaceDefault).Create(svc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Failed to create service: %v", err)
|
Fail(fmt.Sprintf("Failed to create service: %v", err))
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
defer c.Services(api.NamespaceDefault).Delete(svc.Name)
|
defer func() {
|
||||||
|
defer GinkgoRecover()
|
||||||
|
c.Services(api.NamespaceDefault).Delete(svc.Name)
|
||||||
|
}()
|
||||||
|
|
||||||
// TODO: we don't have a way to wait for a service to be "running".
|
// TODO: we don't have a way to wait for a service to be "running". // If this proves flaky, then we will need to retry the clientPod or insert a sleep.
|
||||||
// If this proves flaky, then we will need to retry the clientPod or insert a sleep.
|
|
||||||
|
|
||||||
// Make a client pod that verifies that it has the service environment variables.
|
// Make a client pod that verifies that it has the service environment variables.
|
||||||
clientPod := parsePodOrDie(`{
|
clientPod := parsePodOrDie(`{
|
||||||
@ -109,25 +118,26 @@ func TestPodHasServiceEnvVars(c *client.Client) bool {
|
|||||||
}`)
|
}`)
|
||||||
_, err = c.Pods(api.NamespaceDefault).Create(clientPod)
|
_, err = c.Pods(api.NamespaceDefault).Create(clientPod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Failed to create pod: %v", err)
|
Fail(fmt.Sprintf("Failed to create pod: %v", err))
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
defer c.Pods(api.NamespaceDefault).Delete(clientPod.Name)
|
defer func() {
|
||||||
|
defer GinkgoRecover()
|
||||||
|
c.Pods(api.NamespaceDefault).Delete(clientPod.Name)
|
||||||
|
}()
|
||||||
|
|
||||||
// Wait for client pod to complete.
|
// Wait for client pod to complete.
|
||||||
success := waitForPodSuccess(c, clientPod.Name, clientPod.Spec.Containers[0].Name)
|
success := waitForPodSuccess(c, clientPod.Name, clientPod.Spec.Containers[0].Name)
|
||||||
if !success {
|
if !success {
|
||||||
glog.Errorf("Failed to run client pod to detect service env vars.")
|
Fail(fmt.Sprintf("Failed to run client pod to detect service env vars."))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab its logs. Get host first.
|
// Grab its logs. Get host first.
|
||||||
clientPodStatus, err := c.Pods(api.NamespaceDefault).Get(clientPod.Name)
|
clientPodStatus, err := c.Pods(api.NamespaceDefault).Get(clientPod.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Failed to get clientPod to know host: %v", err)
|
Fail(fmt.Sprintf("Failed to get clientPod to know host: %v", err))
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
glog.Infof("Trying to get logs from host %s pod %s container %s: %v",
|
By(fmt.Sprintf("Trying to get logs from host %s pod %s container %s: %v",
|
||||||
clientPodStatus.Status.Host, clientPodStatus.Name, clientPodStatus.Spec.Containers[0].Name, err)
|
clientPodStatus.Status.Host, clientPodStatus.Name, clientPodStatus.Spec.Containers[0].Name, err))
|
||||||
logs, err := c.Get().
|
logs, err := c.Get().
|
||||||
Prefix("proxy").
|
Prefix("proxy").
|
||||||
Resource("minions").
|
Resource("minions").
|
||||||
@ -136,11 +146,10 @@ func TestPodHasServiceEnvVars(c *client.Client) bool {
|
|||||||
Do().
|
Do().
|
||||||
Raw()
|
Raw()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Failed to get logs from host %s pod %s container %s: %v",
|
Fail(fmt.Sprintf("Failed to get logs from host %s pod %s container %s: %v",
|
||||||
clientPodStatus.Status.Host, clientPodStatus.Name, clientPodStatus.Spec.Containers[0].Name, err)
|
clientPodStatus.Status.Host, clientPodStatus.Name, clientPodStatus.Spec.Containers[0].Name, err))
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
glog.Info("clientPod logs:", string(logs))
|
fmt.Sprintf("clientPod logs:%v\n", string(logs))
|
||||||
|
|
||||||
toFind := []string{
|
toFind := []string{
|
||||||
"FOOSERVICE_SERVICE_HOST=",
|
"FOOSERVICE_SERVICE_HOST=",
|
||||||
@ -153,20 +162,9 @@ func TestPodHasServiceEnvVars(c *client.Client) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, m := range toFind {
|
for _, m := range toFind {
|
||||||
if !strings.Contains(string(logs), m) {
|
Expect(string(logs)).To(ContainSubstring(m), "%q in client env vars", m)
|
||||||
glog.Errorf("Unable to find env var %q in client env vars.", m)
|
|
||||||
success = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We could try a wget the service from the client pod. But services.sh e2e test covers that pretty well.
|
// We could try a wget the service from the client pod. But services.sh e2e test covers that pretty well.
|
||||||
return success
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ = Describe("TestPodHasServiceEnvVars", 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(TestPodHasServiceEnvVars(loadClientOrDie())).To(BeTrue())
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user