|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "hash/adler32" |
| 22 | + "io" |
| 23 | + "io/ioutil" |
| 24 | + "math/rand" |
| 25 | + "os" |
| 26 | + "os/exec" |
| 27 | + "path/filepath" |
| 28 | + "sync" |
| 29 | + |
| 30 | + "k8s.io/kubernetes/pkg/util/env" |
| 31 | + |
| 32 | + "github.com/golang/glog" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + etcdSetup sync.Once |
| 37 | + etcdURL = "" |
| 38 | +) |
| 39 | + |
| 40 | +func setupETCD() { |
| 41 | + etcdSetup.Do(func() { |
| 42 | + if os.Getenv("RUNFILES_DIR") == "" { |
| 43 | + etcdURL = env.GetEnvAsStringOrFallback("KUBE_INTEGRATION_ETCD_URL", "http://127.0.0.1:2379") |
| 44 | + return |
| 45 | + } |
| 46 | + etcdPath := filepath.Join(os.Getenv("RUNFILES_DIR"), "com_coreos_etcd/etcd") |
| 47 | + // give every test the same random port each run |
| 48 | + etcdPort := 20000 + rand.New(rand.NewSource(int64(adler32.Checksum([]byte(os.Args[0]))))).Intn(5000) |
| 49 | + etcdURL = fmt.Sprintf("http://127.0.0.1:%d", etcdPort) |
| 50 | + |
| 51 | + info, err := os.Stat(etcdPath) |
| 52 | + if err != nil { |
| 53 | + glog.Fatalf("Unable to stat etcd: %v", err) |
| 54 | + } |
| 55 | + if info.IsDir() { |
| 56 | + glog.Fatalf("Did not expect %q to be a directory", etcdPath) |
| 57 | + } |
| 58 | + |
| 59 | + etcdDataDir, err := ioutil.TempDir(os.TempDir(), "integration_test_etcd_data") |
| 60 | + if err != nil { |
| 61 | + glog.Fatalf("Unable to make temp etcd data dir: %v", err) |
| 62 | + } |
| 63 | + glog.Infof("storing etcd data in: %v", etcdDataDir) |
| 64 | + |
| 65 | + etcdCmd := exec.Command( |
| 66 | + etcdPath, |
| 67 | + "--data-dir", |
| 68 | + etcdDataDir, |
| 69 | + "--listen-client-urls", |
| 70 | + GetEtcdURL(), |
| 71 | + "--advertise-client-urls", |
| 72 | + GetEtcdURL(), |
| 73 | + "--listen-peer-urls", |
| 74 | + "http://127.0.0.1:0", |
| 75 | + ) |
| 76 | + |
| 77 | + stdout, err := etcdCmd.StdoutPipe() |
| 78 | + if err != nil { |
| 79 | + glog.Fatalf("Failed to run etcd: %v", err) |
| 80 | + } |
| 81 | + stderr, err := etcdCmd.StderrPipe() |
| 82 | + if err != nil { |
| 83 | + glog.Fatalf("Failed to run etcd: %v", err) |
| 84 | + } |
| 85 | + if err := etcdCmd.Start(); err != nil { |
| 86 | + glog.Fatalf("Failed to run etcd: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + go io.Copy(os.Stdout, stdout) |
| 90 | + go io.Copy(os.Stderr, stderr) |
| 91 | + |
| 92 | + go func() { |
| 93 | + if err := etcdCmd.Wait(); err != nil { |
| 94 | + glog.Fatalf("Failed to run etcd: %v", err) |
| 95 | + } |
| 96 | + glog.Fatalf("etcd should not have succeeded") |
| 97 | + }() |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +func EtcdMain(tests func() int) { |
| 102 | + setupETCD() |
| 103 | + os.Exit(tests()) |
| 104 | +} |
| 105 | + |
| 106 | +// return the EtcdURL |
| 107 | +func GetEtcdURL() string { |
| 108 | + return etcdURL |
| 109 | +} |
0 commit comments