Skip to content

Commit 18f422d

Browse files
committed
Add envtest
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
1 parent 3743157 commit 18f422d

36 files changed

+4608
-40
lines changed

envtest/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package envtest provides a test environment for testing code against a
2+
// kcp control plane.
3+
package envtest
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2021 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 addr_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestAddr(t *testing.T) {
27+
t.Parallel()
28+
RegisterFailHandler(Fail)
29+
RunSpecs(t, "Addr Suite")
30+
}

envtest/internal/addr/manager.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Copyright 2021 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 addr
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"io/fs"
23+
"net"
24+
"os"
25+
"path/filepath"
26+
"strings"
27+
"time"
28+
29+
"github.com/kcp-dev/multicluster-provider/envtest/internal/flock"
30+
)
31+
32+
// TODO(directxman12): interface / release functionality for external port managers
33+
34+
const (
35+
portReserveTime = 2 * time.Minute
36+
portConflictRetry = 100
37+
portFilePrefix = "port-"
38+
)
39+
40+
var (
41+
cacheDir string
42+
)
43+
44+
func init() {
45+
baseDir, err := os.UserCacheDir()
46+
if err == nil {
47+
cacheDir = filepath.Join(baseDir, "kubebuilder-envtest")
48+
err = os.MkdirAll(cacheDir, 0o750)
49+
}
50+
if err != nil {
51+
// Either we didn't get a cache directory, or we can't use it
52+
baseDir = os.TempDir()
53+
cacheDir = filepath.Join(baseDir, "kubebuilder-envtest")
54+
err = os.MkdirAll(cacheDir, 0o750)
55+
}
56+
if err != nil {
57+
panic(err)
58+
}
59+
}
60+
61+
type portCache struct{}
62+
63+
func (c *portCache) add(port int) (bool, error) {
64+
// Remove outdated ports.
65+
if err := fs.WalkDir(os.DirFS(cacheDir), ".", func(path string, d fs.DirEntry, err error) error {
66+
if err != nil {
67+
return err
68+
}
69+
if d.IsDir() || !d.Type().IsRegular() || !strings.HasPrefix(path, portFilePrefix) {
70+
return nil
71+
}
72+
info, err := d.Info()
73+
if err != nil {
74+
// No-op if file no longer exists; may have been deleted by another
75+
// process/thread trying to allocate ports.
76+
if errors.Is(err, fs.ErrNotExist) {
77+
return nil
78+
}
79+
return err
80+
}
81+
if time.Since(info.ModTime()) > portReserveTime {
82+
if err := os.Remove(filepath.Join(cacheDir, path)); err != nil {
83+
// No-op if file no longer exists; may have been deleted by another
84+
// process/thread trying to allocate ports.
85+
if os.IsNotExist(err) {
86+
return nil
87+
}
88+
return err
89+
}
90+
}
91+
return nil
92+
}); err != nil {
93+
return false, err
94+
}
95+
// Try allocating new port, by acquiring a file.
96+
path := fmt.Sprintf("%s/%s%d", cacheDir, portFilePrefix, port)
97+
if err := flock.Acquire(path); errors.Is(err, flock.ErrAlreadyLocked) {
98+
return false, nil
99+
} else if err != nil {
100+
return false, err
101+
}
102+
return true, nil
103+
}
104+
105+
var cache = &portCache{}
106+
107+
func suggest(listenHost string) (*net.TCPListener, int, string, error) {
108+
if listenHost == "" {
109+
listenHost = "localhost"
110+
}
111+
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(listenHost, "0"))
112+
if err != nil {
113+
return nil, -1, "", err
114+
}
115+
l, err := net.ListenTCP("tcp", addr)
116+
if err != nil {
117+
return nil, -1, "", err
118+
}
119+
return l, l.Addr().(*net.TCPAddr).Port,
120+
addr.IP.String(),
121+
nil
122+
}
123+
124+
// Suggest suggests an address a process can listen on. It returns
125+
// a tuple consisting of a free port and the hostname resolved to its IP.
126+
// It makes sure that new port allocated does not conflict with old ports
127+
// allocated within 1 minute.
128+
func Suggest(listenHost string) (int, string, error) {
129+
for i := 0; i < portConflictRetry; i++ {
130+
listener, port, resolvedHost, err := suggest(listenHost)
131+
if err != nil {
132+
return -1, "", err
133+
}
134+
defer listener.Close()
135+
if ok, err := cache.add(port); ok {
136+
return port, resolvedHost, nil
137+
} else if err != nil {
138+
return -1, "", err
139+
}
140+
}
141+
return -1, "", fmt.Errorf("no free ports found after %d retries", portConflictRetry)
142+
}

envtest/internal/addr/manager_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright 2021 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 addr_test
18+
19+
import (
20+
"net"
21+
"strconv"
22+
23+
"github.com/kcp-dev/multicluster-provider/envtest/internal/addr"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
var _ = Describe("SuggestAddress", func() {
30+
It("returns a free port and an address to bind to", func() {
31+
port, host, err := addr.Suggest("")
32+
33+
Expect(err).NotTo(HaveOccurred())
34+
Expect(host).To(Or(Equal("127.0.0.1"), Equal("::1")))
35+
Expect(port).NotTo(Equal(0))
36+
37+
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
38+
Expect(err).NotTo(HaveOccurred())
39+
l, err := net.ListenTCP("tcp", addr)
40+
defer func() {
41+
Expect(l.Close()).To(Succeed())
42+
}()
43+
Expect(err).NotTo(HaveOccurred())
44+
})
45+
46+
It("supports an explicit listenHost", func() {
47+
port, host, err := addr.Suggest("localhost")
48+
49+
Expect(err).NotTo(HaveOccurred())
50+
Expect(host).To(Or(Equal("127.0.0.1"), Equal("::1")))
51+
Expect(port).NotTo(Equal(0))
52+
53+
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
54+
Expect(err).NotTo(HaveOccurred())
55+
l, err := net.ListenTCP("tcp", addr)
56+
defer func() {
57+
Expect(l.Close()).To(Succeed())
58+
}()
59+
Expect(err).NotTo(HaveOccurred())
60+
})
61+
62+
It("supports a 0.0.0.0 listenHost", func() {
63+
port, host, err := addr.Suggest("0.0.0.0")
64+
65+
Expect(err).NotTo(HaveOccurred())
66+
Expect(host).To(Equal("0.0.0.0"))
67+
Expect(port).NotTo(Equal(0))
68+
69+
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
70+
Expect(err).NotTo(HaveOccurred())
71+
l, err := net.ListenTCP("tcp", addr)
72+
defer func() {
73+
Expect(l.Close()).To(Succeed())
74+
}()
75+
Expect(err).NotTo(HaveOccurred())
76+
})
77+
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2021 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 certs_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestInternal(t *testing.T) {
27+
t.Parallel()
28+
RegisterFailHandler(Fail)
29+
RunSpecs(t, "TinyCA (Internal Certs) Suite")
30+
}

0 commit comments

Comments
 (0)