Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify network #2536

Merged
merged 2 commits into from
Dec 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 83 additions & 5 deletions cmd/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"flag"
"io/ioutil"
"os"
"runtime"
"path/filepath"
goruntime "runtime"
"strconv"
"time"

Expand All @@ -29,13 +30,15 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)

var (
authConfig = flag.String("auth_config", os.Getenv("HOME")+"/.kubernetes_auth", "Path to the auth info file.")
host = flag.String("host", "", "The host to connect to")
repoRoot = flag.String("repo_root", "./", "Root directory of kubernetes repository, for finding test files. Default assumes working directory is repository root")
)

func waitForPodRunning(c *client.Client, id string) {
Expand All @@ -53,7 +56,12 @@ func waitForPodRunning(c *client.Client, id string) {
}
}

func loadObjectOrDie(filePath string) interface{} {
// assetPath returns a path to the requested file; safe on all OSes.
func assetPath(pathElements ...string) string {
return filepath.Join(*repoRoot, filepath.Join(pathElements...))
}

func loadObjectOrDie(filePath string) runtime.Object {
data, err := ioutil.ReadFile(filePath)
if err != nil {
glog.Fatalf("Failed to read pod: %v", err)
Expand Down Expand Up @@ -128,7 +136,7 @@ func TestKubernetesROService(c *client.Client) bool {
func TestPodUpdate(c *client.Client) bool {
podClient := c.Pods(api.NamespaceDefault)

pod := loadPodOrDie("./api/examples/pod.json")
pod := loadPodOrDie(assetPath("api", "examples", "pod.json"))
value := strconv.Itoa(time.Now().Nanosecond())
pod.Labels["time"] = value

Expand Down Expand Up @@ -208,7 +216,7 @@ func TestKubeletSendsEvent(c *client.Client) bool {

podClient := c.Pods(api.NamespaceDefault)

pod := loadPodOrDie("./cmd/e2e/pod.json")
pod := loadPodOrDie(assetPath("cmd", "e2e", "pod.json"))
value := strconv.Itoa(time.Now().Nanosecond())
pod.Labels["time"] = value

Expand Down Expand Up @@ -274,9 +282,78 @@ func TestKubeletSendsEvent(c *client.Client) bool {
return true
}

func TestNetwork(c *client.Client) bool {
ns := api.NamespaceDefault
svc, err := c.Services(ns).Create(loadObjectOrDie(assetPath(
"contrib", "for-tests", "network-tester", "service.json",
)).(*api.Service))
if err != nil {
glog.Errorf("unable to create test service: %v", err)
return false
}
// 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(loadObjectOrDie(assetPath(
"contrib", "for-tests", "network-tester", "rc.json",
)).(*api.ReplicationController))
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 {
glog.Errorf("unable to modify replica count for rc %v: %v", rc.Name, err)
return
}
if err = c.ReplicationControllers(ns).Delete(rc.Name); err != nil {
glog.Errorf("unable to delete rc %v: %v", rc.Name, err)
}
}()

const maxAttempts = 60
for i := 0; i < maxAttempts; i++ {
time.Sleep(time.Second)
body, err := c.Get().Path("proxy").Path("services").Path(svc.Name).Path("status").Do().Raw()
if err != nil {
glog.Infof("Attempt %v/%v: service/pod still starting. (error: '%v')", i, maxAttempts, err)
continue
}
switch string(body) {
case "pass":
glog.Infof("Passed on attempt %v. Cleaning up.", i)
return true
case "running":
glog.Infof("Attempt %v/%v: test still running", i, maxAttempts)
case "fail":
if body, err := c.Get().Path("proxy").Path("services").Path(svc.Name).Path("read").Do().Raw(); err != nil {
glog.Infof("Failed on attempt %v. Cleaning up. Error reading details: %v", i, err)
} else {
glog.Infof("Failed on attempt %v. Cleaning up. Details:\n%v", i, string(body))
}
return false
}
}

if body, err := c.Get().Path("proxy").Path("services").Path(svc.Name).Path("read").Do().Raw(); err != nil {
glog.Infof("Timed out. Cleaning up. Error reading details: %v", err)
} else {
glog.Infof("Timed out. Cleaning up. Details:\n%v", string(body))
}

return false
}

func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
goruntime.GOMAXPROCS(goruntime.NumCPU())
util.ReallyCrash = true
util.InitLogs()
defer util.FlushLogs()
Expand All @@ -294,6 +371,7 @@ func main() {
TestKubeletSendsEvent,
TestImportantURLs,
TestPodUpdate,
TestNetwork,
}

passed := true
Expand Down
19 changes: 19 additions & 0 deletions contrib/for-tests/network-tester/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM scratch
MAINTAINER Daniel Smith <dbsmith@google.com>
ADD webserver webserver
EXPOSE 8080
ENTRYPOINT ["/webserver"]
13 changes: 13 additions & 0 deletions contrib/for-tests/network-tester/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all: push

webserver: webserver.go
CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w' ./webserver.go

container: webserver
sudo docker build -t kubernetes/nettest .

push: container
sudo docker push kubernetes/nettest

clean:
rm -f webserver
29 changes: 29 additions & 0 deletions contrib/for-tests/network-tester/rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"id": "nettestController",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
"replicas": 8,
"replicaSelector": {"name": "nettest"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "nettest",
"containers": [{
"name": "webserver",
"image": "kubernetes/nettest:latest",
"command": ["-service=nettest"],
"ports": [{
"containerPort": 8080
}]
}]
}
},
"labels": {
"name": "nettest"
}
}
},
"labels": {"name": "nettest"}
}
14 changes: 14 additions & 0 deletions contrib/for-tests/network-tester/service.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "nettest",
"kind": "Service",
"apiVersion": "v1beta1",
"port": 8080,
"containerPort": 8080,
"selector": {
"name": "nettest"
},
"labels": {
"name": "nettest"
}
}