Skip to content

Commit 8e23c65

Browse files
committed
add testmain setup func to the integration framework
1 parent 3f59f21 commit 8e23c65

File tree

8 files changed

+130
-14
lines changed

8 files changed

+130
-14
lines changed

build/root/WORKSPACE

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ http_archive(
1212
urls = ["https://github.com/kubernetes/repo-infra/archive/9dedd5f4093884c133ad5ea73695b28338b954ab.tar.gz"],
1313
)
1414

15+
ETCD_VERSION = "3.0.17"
16+
17+
new_http_archive(
18+
name = "com_coreos_etcd",
19+
build_file = "third_party/etcd.BUILD",
20+
sha256 = "274c46a7f8d26f7ae99d6880610f54933cbcf7f3beafa19236c52eb5df8c7a0b",
21+
strip_prefix = "etcd-v%s-linux-amd64" % ETCD_VERSION,
22+
urls = ["https://github.com/coreos/etcd/releases/download/v%s/etcd-v%s-linux-amd64.tar.gz" % (ETCD_VERSION, ETCD_VERSION)],
23+
)
24+
1525
# This contains a patch to not prepend ./ to tarfiles produced by pkg_tar.
1626
# When merged upstream, we'll no longer need to use ixdy's fork:
1727
# https://bazel-review.googlesource.com/#/c/10390/

test/integration/etcd/etcd_storage_path_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func startRealMasterOrDie(t *testing.T, certDir string) (*allClient, clientv3.KV
588588
kubeAPIServerOptions := options.NewServerRunOptions()
589589
kubeAPIServerOptions.SecureServing.BindAddress = net.ParseIP("127.0.0.1")
590590
kubeAPIServerOptions.SecureServing.ServerCert.CertDirectory = certDir
591-
kubeAPIServerOptions.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURLFromEnv()}
591+
kubeAPIServerOptions.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURL()}
592592
kubeAPIServerOptions.Etcd.DefaultStorageMediaType = runtime.ContentTypeJSON // TODO use protobuf?
593593
kubeAPIServerOptions.ServiceClusterIPRange = *defaultServiceClusterIPRange
594594
kubeAPIServerOptions.Authorization.Mode = "RBAC"

test/integration/examples/apiserver_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestAggregatedAPIServer(t *testing.T) {
102102
kubeAPIServerOptions.SecureServing.BindPort = kubePort
103103
kubeAPIServerOptions.SecureServing.ServerCert.CertDirectory = certDir
104104
kubeAPIServerOptions.InsecureServing.BindPort = 0
105-
kubeAPIServerOptions.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURLFromEnv()}
105+
kubeAPIServerOptions.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURL()}
106106
kubeAPIServerOptions.ServiceClusterIPRange = *defaultServiceClusterIPRange
107107
kubeAPIServerOptions.Authentication.RequestHeader.UsernameHeaders = []string{"X-Remote-User"}
108108
kubeAPIServerOptions.Authentication.RequestHeader.GroupHeaders = []string{"X-Remote-Group"}
@@ -190,7 +190,7 @@ func TestAggregatedAPIServer(t *testing.T) {
190190
"--requestheader-allowed-names=kube-aggregator",
191191
"--authentication-kubeconfig", kubeconfigFile.Name(),
192192
"--authorization-kubeconfig", kubeconfigFile.Name(),
193-
"--etcd-servers", framework.GetEtcdURLFromEnv(),
193+
"--etcd-servers", framework.GetEtcdURL(),
194194
"--cert-dir", wardleCertDir,
195195
})
196196
if err := wardleCmd.Execute(); err != nil {
@@ -266,7 +266,7 @@ func TestAggregatedAPIServer(t *testing.T) {
266266
"--core-kubeconfig", kubeconfigFile.Name(),
267267
"--authentication-kubeconfig", kubeconfigFile.Name(),
268268
"--authorization-kubeconfig", kubeconfigFile.Name(),
269-
"--etcd-servers", framework.GetEtcdURLFromEnv(),
269+
"--etcd-servers", framework.GetEtcdURL(),
270270
"--cert-dir", aggregatorCertDir,
271271
})
272272
if err := aggregatorCmd.Execute(); err != nil {

test/integration/federation/framework/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const apiNoun = "federation apiserver"
3636
// GetRunOptions returns the default run options that can be used to run a test federation apiserver.
3737
func GetRunOptions() *options.ServerRunOptions {
3838
r := options.NewServerRunOptions()
39-
r.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURLFromEnv()}
39+
r.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURL()}
4040
// Use a unique prefix to ensure isolation from other tests using the same etcd instance
4141
r.Etcd.StorageConfig.Prefix = uuid.New()
4242
// Disable secure serving

test/integration/framework/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ load(
1010
go_library(
1111
name = "go_default_library",
1212
srcs = [
13+
"etcd.go",
1314
"master_utils.go",
1415
"perf_utils.go",
1516
"serializer.go",
1617
],
18+
data = [
19+
"@com_coreos_etcd//:etcd",
20+
],
1721
tags = ["automanaged"],
1822
deps = [
1923
"//pkg/api:go_default_library",

test/integration/framework/etcd.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

test/integration/framework/master_utils.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ import (
7070
"k8s.io/kubernetes/pkg/kubectl"
7171
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
7272
"k8s.io/kubernetes/pkg/master"
73-
"k8s.io/kubernetes/pkg/util/env"
7473
"k8s.io/kubernetes/pkg/version"
7574
"k8s.io/kubernetes/plugin/pkg/admission/admit"
7675
)
@@ -298,20 +297,13 @@ func parseCIDROrDie(cidr string) *net.IPNet {
298297
return parsed
299298
}
300299

301-
// return the EtcdURL
302-
func GetEtcdURLFromEnv() string {
303-
url := env.GetEnvAsStringOrFallback("KUBE_INTEGRATION_ETCD_URL", "http://127.0.0.1:2379")
304-
glog.V(4).Infof("Using KUBE_INTEGRATION_ETCD_URL=%q", url)
305-
return url
306-
}
307-
308300
// Returns a basic master config.
309301
func NewMasterConfig() *master.Config {
310302
// This causes the integration tests to exercise the etcd
311303
// prefix code, so please don't change without ensuring
312304
// sufficient coverage in other ways.
313305
etcdOptions := options.NewEtcdOptions(storagebackend.NewDefaultConfig(uuid.New(), api.Scheme, nil))
314-
etcdOptions.StorageConfig.ServerList = []string{GetEtcdURLFromEnv()}
306+
etcdOptions.StorageConfig.ServerList = []string{GetEtcdURL()}
315307

316308
info, _ := runtime.SerializerInfoForMediaType(api.Codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
317309
ns := NewSingleContentTypeSerializer(api.Scheme, info)

third_party/etcd.BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(["etcd"])

0 commit comments

Comments
 (0)