mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-10 05:30:26 +00:00
Remove host ports from the update demo and update tests (take 2).
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -36,13 +37,50 @@ const (
|
||||
updateDemoSelector = "name=update-demo"
|
||||
updateDemoContainer = "update-demo"
|
||||
validateTimeout = 60 * time.Second
|
||||
kubectlProxyPort = 8011
|
||||
)
|
||||
|
||||
var _ = Describe("kubectl", func() {
|
||||
|
||||
updateDemoRoot := filepath.Join(testContext.repoRoot, "examples/update-demo")
|
||||
nautilusPath := filepath.Join(updateDemoRoot, "nautilus-rc.yaml")
|
||||
kittenPath := filepath.Join(updateDemoRoot, "kitten-rc.yaml")
|
||||
// Constants.
|
||||
var (
|
||||
updateDemoRoot = filepath.Join(testContext.repoRoot, "examples/update-demo")
|
||||
nautilusPath = filepath.Join(updateDemoRoot, "nautilus-rc.yaml")
|
||||
kittenPath = filepath.Join(updateDemoRoot, "kitten-rc.yaml")
|
||||
)
|
||||
|
||||
var cmd *exec.Cmd
|
||||
|
||||
BeforeEach(func() {
|
||||
cmd = kubectlCmd("proxy", fmt.Sprintf("--port=%d", kubectlProxyPort))
|
||||
if err := cmd.Start(); err != nil {
|
||||
Failf("Unable to start kubectl proxy: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
if cmd.Process == nil {
|
||||
Logf("No process associated with the kubectl proxy")
|
||||
return
|
||||
}
|
||||
// Kill the proxy
|
||||
errChan := make(chan error, 1)
|
||||
go func() {
|
||||
if err := cmd.Process.Signal(os.Interrupt); err != nil {
|
||||
errChan <- err
|
||||
}
|
||||
_, err := cmd.Process.Wait()
|
||||
errChan <- err
|
||||
}()
|
||||
select {
|
||||
case <-time.After(10 * time.Second):
|
||||
Fail("Timed out waiting for kubectl proxy process to exit")
|
||||
case err := <-errChan:
|
||||
if err != nil {
|
||||
Failf("Unable to kill kubectl proxy: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
It("should create and stop a replication controller", func() {
|
||||
defer cleanup(nautilusPath)
|
||||
@@ -107,8 +145,6 @@ func validateController(image string, replicas int) {
|
||||
|
||||
getImageTemplate := fmt.Sprintf(`--template={{(index .currentState.info "%s").image}}`, updateDemoContainer)
|
||||
|
||||
getHostIPTemplate := "--template={{.currentState.hostIP}}"
|
||||
|
||||
By(fmt.Sprintf("waiting for all containers in %s pods to come up.", updateDemoSelector))
|
||||
for start := time.Now(); time.Since(start) < validateTimeout; time.Sleep(5 * time.Second) {
|
||||
getPodsOutput := runKubectl("get", "pods", "-o", "template", getPodsTemplate, "-l", updateDemoSelector)
|
||||
@@ -118,32 +154,31 @@ func validateController(image string, replicas int) {
|
||||
continue
|
||||
}
|
||||
var runningPods []string
|
||||
for _, podId := range pods {
|
||||
running := runKubectl("get", "pods", podId, "-o", "template", getContainerStateTemplate)
|
||||
for _, podID := range pods {
|
||||
running := runKubectl("get", "pods", podID, "-o", "template", getContainerStateTemplate)
|
||||
if running == "false" {
|
||||
By(fmt.Sprintf("%s is created but not running", podId))
|
||||
Logf("%s is created but not running", podID)
|
||||
continue
|
||||
}
|
||||
|
||||
currentImage := runKubectl("get", "pods", podId, "-o", "template", getImageTemplate)
|
||||
currentImage := runKubectl("get", "pods", podID, "-o", "template", getImageTemplate)
|
||||
if currentImage != image {
|
||||
By(fmt.Sprintf("%s is created but running wrong image; expected: %s, actual: %s", podId, image, currentImage))
|
||||
Logf("%s is created but running wrong image; expected: %s, actual: %s", podID, image, currentImage)
|
||||
continue
|
||||
}
|
||||
|
||||
hostIP := runKubectl("get", "pods", podId, "-o", "template", getHostIPTemplate)
|
||||
data, err := getData(hostIP)
|
||||
data, err := getData(podID)
|
||||
if err != nil {
|
||||
By(fmt.Sprintf("%s is running right image but fetching data failed: %v", podId, err))
|
||||
Logf("%s is running right image but fetching data failed: %v", podID, err)
|
||||
continue
|
||||
}
|
||||
if strings.Contains(data.image, image) {
|
||||
By(fmt.Sprintf("%s is running right image but fetched data has the wrong info: %s", podId, data))
|
||||
Logf("%s is running right image but fetched data has the wrong info: %s", podID, data)
|
||||
continue
|
||||
}
|
||||
|
||||
Logf("%s is verified up and running", podId)
|
||||
runningPods = append(runningPods, podId)
|
||||
Logf("%s is verified up and running", podID)
|
||||
runningPods = append(runningPods, podID)
|
||||
}
|
||||
if len(runningPods) == replicas {
|
||||
return
|
||||
@@ -156,9 +191,8 @@ type updateDemoData struct {
|
||||
image string `json:"image"`
|
||||
}
|
||||
|
||||
func getData(hostIP string) (*updateDemoData, error) {
|
||||
addr := fmt.Sprintf("http://%s:8080/data.json", hostIP)
|
||||
resp, err := http.Get(fmt.Sprintf(addr))
|
||||
func getData(podID string) (*updateDemoData, error) {
|
||||
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/api/v1beta1/proxy/pods/%s/data.json", kubectlProxyPort, podID))
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -173,7 +207,7 @@ func getData(hostIP string) (*updateDemoData, error) {
|
||||
return &data, err
|
||||
}
|
||||
|
||||
func runKubectl(args ...string) string {
|
||||
func kubectlCmd(args ...string) *exec.Cmd {
|
||||
defaultArgs := []string{"--auth-path=" + testContext.authConfig}
|
||||
if testContext.certDir != "" {
|
||||
defaultArgs = append(defaultArgs,
|
||||
@@ -186,9 +220,14 @@ func runKubectl(args ...string) string {
|
||||
if testContext.provider == "gke" {
|
||||
kubectlArgs = append(kubectlArgs, "--server="+testContext.host)
|
||||
}
|
||||
Logf("Running 'kubectl %v'", strings.Join(kubectlArgs, " "))
|
||||
cmd := exec.Command("kubectl", kubectlArgs...)
|
||||
Logf("Running '%s %s'", cmd.Path, strings.Join(cmd.Args, " "))
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runKubectl(args ...string) string {
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd := kubectlCmd(args...)
|
||||
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
|
Reference in New Issue
Block a user