Skip to content

Commit 72fc2c6

Browse files
authored
Merge pull request #1540 from DirectXMan12/refactor/shuffle-internal-testing
✨ Clean up pkg/internal/testing
2 parents 12f94f1 + 294af4f commit 72fc2c6

36 files changed

+729
-1456
lines changed

hack/check-everything.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ tmp_bin=/tmp/cr-tests-bin
3737
)
3838
source <(${tmp_bin}/setup-envtest use --use-env -p env ${ENVTEST_K8S_VERSION})
3939

40-
# link the assets into integration
41-
for tool in kube-apiserver etcd kubectl; do
42-
ln -f -s "${KUBEBUILDER_ASSETS:?unable find envtest assets}/${tool}" "${hack_dir}/../pkg/internal/testing/integration/assets/bin/${tool}"
43-
done
44-
4540
${hack_dir}/verify.sh
4641
${hack_dir}/test-all.sh
4742

pkg/builder/builder_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"k8s.io/client-go/rest"
3030
"sigs.k8s.io/controller-runtime/pkg/envtest"
3131
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
32-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/addr"
32+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
3333
logf "sigs.k8s.io/controller-runtime/pkg/log"
3434
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3535
"sigs.k8s.io/controller-runtime/pkg/metrics"

pkg/envtest/server.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"k8s.io/client-go/rest"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
3030
"sigs.k8s.io/controller-runtime/pkg/client/config"
31-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration"
31+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/controlplane"
3232

3333
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3434
)
@@ -82,20 +82,20 @@ func (te *Environment) getBinAssetPath(binary string) string {
8282
return filepath.Join(defaultKubebuilderPath, binary)
8383
}
8484

85-
// ControlPlane is the re-exported ControlPlane type from the internal integration package
86-
type ControlPlane = integration.ControlPlane
85+
// ControlPlane is the re-exported ControlPlane type from the internal testing package
86+
type ControlPlane = controlplane.ControlPlane
8787

88-
// APIServer is the re-exported APIServer type from the internal integration package
89-
type APIServer = integration.APIServer
88+
// APIServer is the re-exported APIServer type from the internal testing package
89+
type APIServer = controlplane.APIServer
9090

91-
// Etcd is the re-exported Etcd type from the internal integration package
92-
type Etcd = integration.Etcd
91+
// Etcd is the re-exported Etcd type from the internal testing package
92+
type Etcd = controlplane.Etcd
9393

9494
// Environment creates a Kubernetes test environment that will start / stop the Kubernetes control plane and
9595
// install extension APIs
9696
type Environment struct {
9797
// ControlPlane is the ControlPlane including the apiserver and etcd
98-
ControlPlane integration.ControlPlane
98+
ControlPlane controlplane.ControlPlane
9999

100100
// Scheme is used to determine if conversion webhooks should be enabled
101101
// for a particular CRD / object.
@@ -219,10 +219,10 @@ func (te *Environment) Start() (*rest.Config, error) {
219219
}
220220
} else {
221221
if te.ControlPlane.APIServer == nil {
222-
te.ControlPlane.APIServer = &integration.APIServer{Args: te.getAPIServerFlags()}
222+
te.ControlPlane.APIServer = &controlplane.APIServer{Args: te.getAPIServerFlags()}
223223
}
224224
if te.ControlPlane.Etcd == nil {
225-
te.ControlPlane.Etcd = &integration.Etcd{}
225+
te.ControlPlane.Etcd = &controlplane.Etcd{}
226226
}
227227

228228
if os.Getenv(envAttachOutput) == "true" {
@@ -357,4 +357,4 @@ func (te *Environment) useExistingCluster() bool {
357357

358358
// DefaultKubeAPIServerFlags exposes the default args for the APIServer so that
359359
// you can use those to append your own additional arguments.
360-
var DefaultKubeAPIServerFlags = integration.APIServerDefaultArgs
360+
var DefaultKubeAPIServerFlags = controlplane.APIServerDefaultArgs

pkg/envtest/webhook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
"k8s.io/apimachinery/pkg/util/wait"
3434
"k8s.io/client-go/rest"
3535
"sigs.k8s.io/controller-runtime/pkg/client"
36-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration"
37-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/addr"
36+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
37+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/certs"
3838
"sigs.k8s.io/yaml"
3939
)
4040

@@ -274,7 +274,7 @@ func (p *webhookPoller) poll() (done bool, err error) {
274274

275275
// setupCA creates CA for testing and writes them to disk
276276
func (o *WebhookInstallOptions) setupCA() error {
277-
hookCA, err := integration.NewTinyCA()
277+
hookCA, err := certs.NewTinyCA()
278278
if err != nil {
279279
return fmt.Errorf("unable to set up webhook CA: %v", err)
280280
}

pkg/internal/testing/integration/addr/manager.go renamed to pkg/internal/testing/addr/manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"time"
88
)
99

10+
// TODO(directxman12): interface / release functionality for external port managers
11+
1012
const (
1113
portReserveTime = 1 * time.Minute
1214
portConflictRetry = 100

pkg/internal/testing/integration/addr/manager_test.go renamed to pkg/internal/testing/addr/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package addr_test
22

33
import (
4-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/addr"
5-
64
"net"
75
"strconv"
86

97
. "github.com/onsi/ginkgo"
108
. "github.com/onsi/gomega"
9+
10+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
1111
)
1212

1313
var _ = Describe("SuggestAddress", func() {

pkg/internal/testing/integration/internal/tinyca.go renamed to pkg/internal/testing/certs/tinyca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal
1+
package certs
22

33
// NB(directxman12): nothing has verified that this has good settings. In fact,
44
// the setting generated here are probably terrible, but they're fine for integration

pkg/internal/testing/integration/apiserver.go renamed to pkg/internal/testing/controlplane/apiserver.go

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
package integration
1+
package controlplane
22

33
import (
44
"fmt"
55
"io"
66
"io/ioutil"
7+
"net"
78
"net/url"
89
"os"
910
"path/filepath"
11+
"strconv"
1012
"time"
1113

12-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/addr"
13-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/internal"
14+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
15+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/certs"
16+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/process"
1417
)
1518

1619
// APIServer knows how to run a kubernetes apiserver.
@@ -68,38 +71,34 @@ type APIServer struct {
6871
Out io.Writer
6972
Err io.Writer
7073

71-
processState *internal.ProcessState
74+
processState *process.State
7275
}
7376

7477
// Start starts the apiserver, waits for it to come up, and returns an error,
7578
// if occurred.
7679
func (s *APIServer) Start() error {
7780
if s.processState == nil {
78-
if err := s.setProcessState(); err != nil {
81+
if err := s.setState(); err != nil {
7982
return err
8083
}
8184
}
8285
return s.processState.Start(s.Out, s.Err)
8386
}
8487

85-
func (s *APIServer) setProcessState() error {
88+
func (s *APIServer) setState() error {
8689
if s.EtcdURL == nil {
8790
return fmt.Errorf("expected EtcdURL to be configured")
8891
}
8992

9093
var err error
9194

92-
s.processState = &internal.ProcessState{}
93-
94-
s.processState.DefaultedProcessInput, err = internal.DoDefaulting(
95-
"kube-apiserver",
96-
s.URL,
97-
s.CertDir,
98-
s.Path,
99-
s.StartTimeout,
100-
s.StopTimeout,
101-
)
102-
if err != nil {
95+
s.processState = &process.State{
96+
Dir: s.CertDir,
97+
Path: s.Path,
98+
StartTimeout: s.StartTimeout,
99+
StopTimeout: s.StopTimeout,
100+
}
101+
if err := s.processState.Init("kube-apiserver"); err != nil {
103102
return err
104103
}
105104

@@ -111,9 +110,20 @@ func (s *APIServer) setProcessState() error {
111110
}
112111
}
113112

114-
s.processState.HealthCheckEndpoint = "/healthz"
113+
if s.URL == nil {
114+
port, host, err := addr.Suggest("")
115+
if err != nil {
116+
return err
117+
}
118+
s.URL = &url.URL{
119+
Scheme: "http",
120+
Host: net.JoinHostPort(host, strconv.Itoa(port)),
121+
}
122+
}
123+
124+
s.processState.HealthCheck.URL = *s.URL
125+
s.processState.HealthCheck.Path = "/healthz"
115126

116-
s.URL = &s.processState.URL
117127
s.CertDir = s.processState.Dir
118128
s.Path = s.processState.Path
119129
s.StartTimeout = s.processState.StartTimeout
@@ -123,9 +133,12 @@ func (s *APIServer) setProcessState() error {
123133
return err
124134
}
125135

126-
s.processState.Args, err = internal.RenderTemplates(
127-
internal.DoAPIServerArgDefaulting(s.Args), s,
128-
)
136+
args := s.Args
137+
if len(args) == 0 {
138+
args = APIServerDefaultArgs
139+
}
140+
141+
s.processState.Args, err = process.RenderTemplates(args, s)
129142
return err
130143
}
131144

@@ -135,7 +148,7 @@ func (s *APIServer) populateAPIServerCerts() error {
135148
return statErr
136149
}
137150

138-
ca, err := internal.NewTinyCA()
151+
ca, err := certs.NewTinyCA()
139152
if err != nil {
140153
return err
141154
}
@@ -171,7 +184,16 @@ func (s *APIServer) Stop() error {
171184

172185
// APIServerDefaultArgs exposes the default args for the APIServer so that you
173186
// can use those to append your own additional arguments.
174-
//
175-
// The internal default arguments are explicitly copied here, we don't want to
176-
// allow users to change the internal ones.
177-
var APIServerDefaultArgs = append([]string{}, internal.APIServerDefaultArgs...)
187+
var APIServerDefaultArgs = []string{
188+
"--advertise-address=127.0.0.1",
189+
"--etcd-servers={{ if .EtcdURL }}{{ .EtcdURL.String }}{{ end }}",
190+
"--cert-dir={{ .CertDir }}",
191+
"--insecure-port={{ if .URL }}{{ .URL.Port }}{{ end }}",
192+
"--insecure-bind-address={{ if .URL }}{{ .URL.Hostname }}{{ end }}",
193+
"--secure-port={{ if .SecurePort }}{{ .SecurePort }}{{ end }}",
194+
// we're keeping this disabled because if enabled, default SA is missing which would force all tests to create one
195+
// in normal apiserver operation this SA is created by controller, but that is not run in integration environment
196+
"--disable-admission-plugins=ServiceAccount",
197+
"--service-cluster-ip-range=10.0.0.0/24",
198+
"--allow-privileged=true",
199+
}

pkg/internal/testing/integration/integration_suite_test.go renamed to pkg/internal/testing/controlplane/controlplane_suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package integration_test
1+
package controlplane_test
22

33
import (
44
"testing"
@@ -12,6 +12,6 @@ import (
1212
func TestIntegration(t *testing.T) {
1313
t.Parallel()
1414
RegisterFailHandler(Fail)
15-
suiteName := "Integration Framework Unit Tests"
15+
suiteName := "Control Plane Standup Unit Tests"
1616
RunSpecsWithDefaultAndCustomReporters(t, suiteName, []Reporter{printer.NewlineReporter{}, printer.NewProwReporter(suiteName)})
1717
}

pkg/internal/testing/integration/etcd.go renamed to pkg/internal/testing/controlplane/etcd.go

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package integration
1+
package controlplane
22

33
import (
44
"io"
5-
"time"
6-
5+
"net"
76
"net/url"
7+
"strconv"
8+
"time"
89

9-
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/internal"
10+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
11+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/process"
1012
)
1113

1214
// Etcd knows how to run an etcd server.
@@ -55,48 +57,61 @@ type Etcd struct {
5557
Out io.Writer
5658
Err io.Writer
5759

58-
processState *internal.ProcessState
60+
// processState contains the actual details about this running process
61+
processState *process.State
5962
}
6063

6164
// Start starts the etcd, waits for it to come up, and returns an error, if one
6265
// occoured.
6366
func (e *Etcd) Start() error {
6467
if e.processState == nil {
65-
if err := e.setProcessState(); err != nil {
68+
if err := e.setState(); err != nil {
6669
return err
6770
}
6871
}
6972
return e.processState.Start(e.Out, e.Err)
7073
}
7174

72-
func (e *Etcd) setProcessState() error {
75+
func (e *Etcd) setState() error {
7376
var err error
7477

75-
e.processState = &internal.ProcessState{}
76-
77-
e.processState.DefaultedProcessInput, err = internal.DoDefaulting(
78-
"etcd",
79-
e.URL,
80-
e.DataDir,
81-
e.Path,
82-
e.StartTimeout,
83-
e.StopTimeout,
84-
)
85-
if err != nil {
78+
e.processState = &process.State{
79+
Dir: e.DataDir,
80+
Path: e.Path,
81+
StartTimeout: e.StartTimeout,
82+
StopTimeout: e.StopTimeout,
83+
}
84+
85+
if err := e.processState.Init("etcd"); err != nil {
8686
return err
8787
}
8888

89-
e.processState.StartMessage = internal.GetEtcdStartMessage(e.processState.URL)
89+
if e.URL == nil {
90+
port, host, err := addr.Suggest("")
91+
if err != nil {
92+
return err
93+
}
94+
e.URL = &url.URL{
95+
Scheme: "http",
96+
Host: net.JoinHostPort(host, strconv.Itoa(port)),
97+
}
98+
}
99+
100+
// can use /health as of etcd 3.3.0
101+
e.processState.HealthCheck.URL = *e.URL
102+
e.processState.HealthCheck.Path = "/health"
90103

91-
e.URL = &e.processState.URL
92104
e.DataDir = e.processState.Dir
93105
e.Path = e.processState.Path
94106
e.StartTimeout = e.processState.StartTimeout
95107
e.StopTimeout = e.processState.StopTimeout
96108

97-
e.processState.Args, err = internal.RenderTemplates(
98-
internal.DoEtcdArgDefaulting(e.Args), e,
99-
)
109+
args := e.Args
110+
if len(args) == 0 {
111+
args = EtcdDefaultArgs
112+
}
113+
114+
e.processState.Args, err = process.RenderTemplates(args, e)
100115
return err
101116
}
102117

@@ -108,7 +123,9 @@ func (e *Etcd) Stop() error {
108123

109124
// EtcdDefaultArgs exposes the default args for Etcd so that you
110125
// can use those to append your own additional arguments.
111-
//
112-
// The internal default arguments are explicitly copied here, we don't want to
113-
// allow users to change the internal ones.
114-
var EtcdDefaultArgs = append([]string{}, internal.EtcdDefaultArgs...)
126+
var EtcdDefaultArgs = []string{
127+
"--listen-peer-urls=http://localhost:0",
128+
"--advertise-client-urls={{ if .URL }}{{ .URL.String }}{{ end }}",
129+
"--listen-client-urls={{ if .URL }}{{ .URL.String }}{{ end }}",
130+
"--data-dir={{ .DataDir }}",
131+
}

0 commit comments

Comments
 (0)