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

Add test image for networking related tests #35454

Merged
merged 1 commit into from
Oct 30, 2016
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
2 changes: 2 additions & 0 deletions hack/.linted_packages
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ test/images/goproxy
test/images/logs-generator
test/images/mount-tester
test/images/n-way-http
test/images/net
test/images/net/common
test/images/port-forward-tester
test/images/porter
test/images/resource-consumer/consume-cpu
Expand Down
1 change: 1 addition & 0 deletions test/images/net/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/net
21 changes: 21 additions & 0 deletions test/images/net/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
"go_test",
"cgo_library",
)

go_binary(
name = "net",
srcs = ["main.go"],
tags = ["automanaged"],
deps = [
"//test/images/net/common:go_default_library",
"//test/images/net/nat:go_default_library",
],
)
18 changes: 18 additions & 0 deletions test/images/net/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2016 The Kubernetes Authors.
#
# 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 alpine
MAINTAINER Bowei Du <bowei@google.com>
COPY net /net
RUN apk update && apk add curl
39 changes: 39 additions & 0 deletions test/images/net/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2016 The Kubernetes Authors.
#
# 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.

ARCH := amd64
PREFIX ?= gcr.io/google_containers
TAG ?= 1.0
IMAGE ?= e2e-net-$(ARCH)

SRCS := $(shell find . -name \*.go)

all: image

net: $(SRCS)
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w'

image: test net
docker build -t $(PREFIX)/$(IMAGE):$(TAG) .

push: image
gcloud docker -- push $(PREFIX)/$(IMAGE):$(TAG)

clean:
rm -f net

test:
go test ./...

.PHONY: all clean image push test
36 changes: 36 additions & 0 deletions test/images/net/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Overview

The goal of this Go project is to consolidate all low-level
network testing "daemons" into one place. In network testing we
frequently have need of simple daemons (common/Runner) that perform
some "trivial" set of actions on a socket.

# Usage

* A package for each general area that is being tested, for example
`nat/` will contain Runners that test various NAT features.
* Every runner should be registered via `main.go:makeRunnerMap()`.
* Runners receive a JSON options structure as to their configuration. `Run()`
should return the disposition of the test.

Runners can be executed into two different ways, either through the
the command-line or via an HTTP request:

## Command-line

````
$ ./net -runner <runner> -options <json>
./net \
-runner nat-closewait-client \
-options '{"RemoteAddr":"127.0.0.1:9999"}'
````

## HTTP server
````
$ ./net --serve :8889
$ curl -v -X POST localhost:8889/run/nat-closewait-server \
-d '{"LocalAddr":"127.0.0.1:9999"}'
````


[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/test/images/net/README.md?pixel)]()
17 changes: 17 additions & 0 deletions test/images/net/common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
"go_test",
"cgo_library",
)

go_library(
name = "go_default_library",
srcs = ["common.go"],
tags = ["automanaged"],
)
29 changes: 29 additions & 0 deletions test/images/net/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2014 The Kubernetes Authors.

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.
*/

package common

import "log"

// Runner is a client or server to run.
type Runner interface {
// NewOptions returns a new empty options structure to be populated
// by from the JSON -options argument.
NewOptions() interface{}
// Run the client or server, taking in options. This execute the
// test code.
Run(logger *log.Logger, options interface{}) error
}
162 changes: 162 additions & 0 deletions test/images/net/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright 2014 The Kubernetes Authors.

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.
*/

package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"

"k8s.io/kubernetes/test/images/net/common"
"k8s.io/kubernetes/test/images/net/nat"
)

type runnerMap map[string]common.Runner

type runRequestJSON struct {
runner string
options interface{}
}

var (
// flags for the command line. See usage args below for
// descriptions.
flags struct {
Serve string
Runner string
Options string
}
// runners is a map from runner name to runner instance.
runners = makeRunnerMap()
)

type logOutput struct {
b bytes.Buffer
}

func main() {
initFlags()
log.SetFlags(log.Flags() | log.Lshortfile)

if flags.Serve == "" {
output, err := executeRunner(flags.Runner, flags.Options)
if err == nil {
fmt.Print("output:\n\n" + output.b.String())
os.Exit(0)
} else {
log.Printf("Error: %v", err)
fmt.Print("output:\n\n" + output.b.String())
os.Exit(1)
}
} else {
http.HandleFunc("/run/", handleRunRequest)
log.Printf("Running server on %v", flags.Serve)
log.Fatal(http.ListenAndServe(flags.Serve, nil))
}
}

func initFlags() {
legalRunners := ""
for k := range runners {
legalRunners += " " + k
}
flag.StringVar(
&flags.Serve, "serve", "",
"Address and port to bind to (e.g. 127.0.0.1:8080). Setting this will "+
"run the network tester in server mode runner are triggered through "+
"HTTP requests.")
flag.StringVar(
&flags.Runner, "runner", "",
"Runner to execute (available:"+legalRunners+")")
flag.StringVar(
&flags.Options, "options", "",
"JSON options to the Runner")
flag.Parse()

if flags.Runner == "" && flags.Serve == "" {
log.Fatalf("Must set either -runner or -serve, see --help")
}
}

func makeRunnerMap() runnerMap {
// runner name is <pkg>-<file>-<specific>.
return runnerMap{
"nat-closewait-client": nat.NewCloseWaitClient(),
"nat-closewait-server": nat.NewCloseWaitServer(),
}
}

func executeRunner(name string, rawOptions string) (logOutput, error) {
runner, ok := runners[name]
if ok {
options := runner.NewOptions()
if err := json.Unmarshal([]byte(rawOptions), options); err != nil {
return logOutput{}, fmt.Errorf("Invalid options JSON: %v", err)
}

log.Printf("Options: %+v", options)

output := logOutput{}
logger := log.New(&output.b, "# ", log.Lshortfile)

return output, runner.Run(logger, options)
}

return logOutput{}, fmt.Errorf("Invalid runner: '%v', see --help\n", runner)
}

// handleRunRequest handles a request JSON to the network tester.
func handleRunRequest(w http.ResponseWriter, r *http.Request) {
log.Printf("handleRunRequest %v", *r)

urlParts := strings.Split(r.URL.Path, "/")
if len(urlParts) != 3 {
http.Error(w, fmt.Sprintf("invalid request to run: %v", urlParts), 400)
return
}

runner := urlParts[2]
if r.Body == nil {
http.Error(w, "Missing request body", 400)
return
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("error reading body: %v", err), 400)
return
}

var output logOutput
if output, err = executeRunner(runner, string(body)); err != nil {
contents := fmt.Sprintf("Error from runner: %v\noutput:\n\n%s",
err, output.b.String())
http.Error(w, contents, 500)
return
}

fmt.Fprintf(w, "ok\noutput:\n\n"+output.b.String())
}

func setupLogger() {
}
18 changes: 18 additions & 0 deletions test/images/net/nat/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
"go_test",
"cgo_library",
)

go_library(
name = "go_default_library",
srcs = ["closewait.go"],
tags = ["automanaged"],
deps = ["//test/images/net/common:go_default_library"],
)