main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "flag"
  18. "fmt"
  19. "io/ioutil"
  20. "log"
  21. "net/http"
  22. "os"
  23. "strings"
  24. "k8s.io/kubernetes/test/images/net/common"
  25. "k8s.io/kubernetes/test/images/net/nat"
  26. )
  27. type runnerMap map[string]common.Runner
  28. var (
  29. // flags for the command line. See usage args below for
  30. // descriptions.
  31. flags struct {
  32. Serve string
  33. Runner string
  34. Options string
  35. }
  36. // runners is a map from runner name to runner instance.
  37. runners = makeRunnerMap()
  38. )
  39. type logOutput struct {
  40. b bytes.Buffer
  41. }
  42. func main() {
  43. initFlags()
  44. log.SetFlags(log.Flags() | log.Lshortfile)
  45. if flags.Serve == "" {
  46. output, err := executeRunner(flags.Runner, flags.Options)
  47. if err == nil {
  48. fmt.Print("output:\n\n" + output.b.String())
  49. os.Exit(0)
  50. } else {
  51. log.Printf("Error: %v", err)
  52. fmt.Print("output:\n\n" + output.b.String())
  53. os.Exit(1)
  54. }
  55. } else {
  56. http.HandleFunc("/run/", handleRunRequest)
  57. log.Printf("Running server on %v", flags.Serve)
  58. log.Fatal(http.ListenAndServe(flags.Serve, nil))
  59. }
  60. }
  61. func initFlags() {
  62. legalRunners := ""
  63. for k := range runners {
  64. legalRunners += " " + k
  65. }
  66. flag.StringVar(
  67. &flags.Serve, "serve", "",
  68. "Address and port to bind to (e.g. 127.0.0.1:8080). Setting this will "+
  69. "run the network tester in server mode runner are triggered through "+
  70. "HTTP requests.")
  71. flag.StringVar(
  72. &flags.Runner, "runner", "",
  73. "Runner to execute (available:"+legalRunners+")")
  74. flag.StringVar(
  75. &flags.Options, "options", "",
  76. "JSON options to the Runner")
  77. flag.Parse()
  78. if flags.Runner == "" && flags.Serve == "" {
  79. log.Fatalf("Must set either -runner or -serve, see --help")
  80. }
  81. }
  82. func makeRunnerMap() runnerMap {
  83. // runner name is <pkg>-<file>-<specific>.
  84. return runnerMap{
  85. "nat-closewait-client": nat.NewCloseWaitClient(),
  86. "nat-closewait-server": nat.NewCloseWaitServer(),
  87. }
  88. }
  89. func executeRunner(name string, rawOptions string) (logOutput, error) {
  90. runner, ok := runners[name]
  91. if ok {
  92. options := runner.NewOptions()
  93. if err := json.Unmarshal([]byte(rawOptions), options); err != nil {
  94. return logOutput{}, fmt.Errorf("Invalid options JSON: %v", err)
  95. }
  96. log.Printf("Options: %+v", options)
  97. output := logOutput{}
  98. logger := log.New(&output.b, "# ", log.Lshortfile)
  99. return output, runner.Run(logger, options)
  100. }
  101. return logOutput{}, fmt.Errorf("Invalid runner: '%v', see --help", runner)
  102. }
  103. // handleRunRequest handles a request JSON to the network tester.
  104. func handleRunRequest(w http.ResponseWriter, r *http.Request) {
  105. log.Printf("handleRunRequest %v", *r)
  106. urlParts := strings.Split(r.URL.Path, "/")
  107. if len(urlParts) != 3 {
  108. http.Error(w, fmt.Sprintf("invalid request to run: %v", urlParts), 400)
  109. return
  110. }
  111. runner := urlParts[2]
  112. if r.Body == nil {
  113. http.Error(w, "Missing request body", 400)
  114. return
  115. }
  116. body, err := ioutil.ReadAll(r.Body)
  117. if err != nil {
  118. http.Error(w, fmt.Sprintf("error reading body: %v", err), 400)
  119. return
  120. }
  121. var output logOutput
  122. if output, err = executeRunner(runner, string(body)); err != nil {
  123. contents := fmt.Sprintf("Error from runner: %v\noutput:\n\n%s",
  124. err, output.b.String())
  125. http.Error(w, contents, 500)
  126. return
  127. }
  128. fmt.Fprintf(w, "ok\noutput:\n\n"+output.b.String())
  129. }