Skip to content

Commit 77f7fe7

Browse files
committed
Fix some integration test issues when using rancher-desktop
Signed-off-by: Thomas Hallgren <[email protected]>
1 parent 534e891 commit 77f7fe7

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

integration_test/itest/cluster.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ func (s *cluster) Initialize(ctx context.Context) context.Context {
195195
s.agentVersion = s.managerVersion
196196
}
197197

198+
// We cannot use t.TempDir() here, because it will not mount correctly in
199+
// rancher-desktop and docker-desktop (unless they are configured to allow
200+
// mounts directly from /tmp). So we use a tempdir in BUILD_OUTPUT instead
201+
// because it's believed to be both mountable and writable.
202+
tempDir := dos.Getenv(ctx, "TELEPRESENCE_TEMP_DIR")
203+
if tempDir == "" {
204+
tempDir = filepath.Join(BuildOutput(ctx), "tmp")
205+
}
206+
_ = dos.RemoveAll(ctx, tempDir)
207+
require.NoError(t, dos.MkdirAll(ctx, tempDir, 0o777))
208+
ctx = withTempDirBase(ctx, &tempDirBase{tempDir: tempDir})
209+
t.Cleanup(func() {
210+
_ = os.RemoveAll(tempDir)
211+
})
212+
198213
registry := dos.Getenv(ctx, "TELEPRESENCE_REGISTRY")
199214
if registry == "" {
200215
registry = "ghcr.io/telepresenceio"
@@ -290,11 +305,8 @@ func (s *cluster) Initialize(ctx context.Context) context.Context {
290305

291306
s.ensureQuit(ctx)
292307
s.ensureNoManager(ctx)
293-
_ = Run(ctx, "kubectl", "delete", "ns", "-l", AssignPurposeLabel)
294308
_ = Run(ctx, "kubectl", "delete", "-f", filepath.Join("testdata", "k8s", "client_rbac.yaml"))
295-
_ = Run(ctx, "kubectl", "delete", "ns", "-l", AssignPurposeLabel)
296-
_ = Run(ctx, "kubectl", "delete", "pv", "-l", AssignPurposeLabel)
297-
_ = Run(ctx, "kubectl", "delete", "storageclass", "-l", AssignPurposeLabel)
309+
_ = Run(ctx, "kubectl", "delete", "all", "-l", AssignPurposeLabel)
298310
return ctx
299311
}
300312

@@ -354,10 +366,7 @@ func (s *cluster) tearDown(ctx context.Context) {
354366
if s.kubeConfig != "" {
355367
ctx = WithWorkingDir(ctx, GetOSSRoot(ctx))
356368
_ = Run(ctx, "kubectl", "delete", "-f", filepath.Join("testdata", "k8s", "client_rbac.yaml"))
357-
_ = Run(ctx, "kubectl", "delete", "--wait=false", "ns", "-l", AssignPurposeLabel)
358-
_ = Run(ctx, "kubectl", "delete", "--wait=false", "pv", "-l", AssignPurposeLabel)
359-
_ = Run(ctx, "kubectl", "delete", "--wait=false", "storageclass", "-l", AssignPurposeLabel)
360-
_ = Run(ctx, "kubectl", "delete", "--wait=false", "mutatingwebhookconfigurations", "-l", AssignPurposeLabel)
369+
_ = Run(ctx, "kubectl", "delete", "--wait=false", "all", "-l", AssignPurposeLabel)
361370
}
362371
}
363372

@@ -439,7 +448,7 @@ func (s *cluster) withBasicConfig(c context.Context, t *testing.T) context.Conte
439448
require.NoError(t, err)
440449
configYamlStr := string(configYaml)
441450

442-
configDir := t.TempDir()
451+
configDir := TempDir(c)
443452
c = filelocation.WithAppUserConfigDir(c, configDir)
444453
c, err = SetConfig(c, configDir, configYamlStr)
445454
require.NoError(t, err)
@@ -1071,7 +1080,7 @@ func WithConfig(c context.Context, modifierFunc func(config client.Config)) cont
10711080
configYaml, err := configCopy.(client.Config).MarshalYAML()
10721081
require.NoError(t, err)
10731082
configYamlStr := string(configYaml)
1074-
configDir, err := os.MkdirTemp(t.TempDir(), "config")
1083+
configDir, err := os.MkdirTemp(TempDir(c), "config")
10751084
require.NoError(t, err)
10761085
c, err = SetConfig(c, configDir, configYamlStr)
10771086
require.NoError(t, err)

integration_test/itest/namespace.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ func (s *nsPair) setup(ctx context.Context) bool {
103103
return false
104104
}
105105
err := Kubectl(ctx, s.Namespace, "apply", "-f", filepath.Join(GetOSSRoot(ctx), "testdata", "k8s", "client_sa.yaml"))
106-
assert.NoError(t, err, "failed to create connect ServiceAccount")
106+
if assert.NoError(t, err, "failed to create connect ServiceAccount") {
107+
db, err := ReadTemplate(ctx, filepath.Join("testdata", "k8s", "client_rancher.goyaml"), map[string]string{
108+
"ManagerNamespace": s.Namespace,
109+
})
110+
if assert.NoError(t, err) {
111+
assert.NoError(t, Kubectl(dos.WithStdin(ctx, bytes.NewReader(db)), s.Namespace, "apply", "-f", "-"))
112+
}
113+
}
107114
return !t.Failed()
108115
}
109116

integration_test/itest/tempdir.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package itest
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"sync/atomic"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
type tempDirBase struct {
13+
tempDir string
14+
tempDirSeq uint64
15+
}
16+
17+
type tempDirBaseKey struct{}
18+
19+
func withTempDirBase(ctx context.Context, td *tempDirBase) context.Context {
20+
return context.WithValue(ctx, tempDirBaseKey{}, td)
21+
}
22+
23+
// TempDir returns a temporary directory for the test to use.
24+
// The directory is automatically removed when the test and
25+
// all its subtests complete.
26+
// Each subsequent call to t.TempDir returns a unique directory;
27+
// if the directory creation fails, TempDir terminates the test by calling Fatal.
28+
func TempDir(ctx context.Context) string {
29+
t := getT(ctx)
30+
if td, ok := ctx.Value(tempDirBaseKey{}).(*tempDirBase); ok {
31+
seq := atomic.AddUint64(&td.tempDirSeq, 1)
32+
dir := fmt.Sprintf("%s%c%03d", td.tempDir, os.PathSeparator, seq)
33+
require.NoError(t, os.Mkdir(dir, 0o777))
34+
return dir
35+
}
36+
return t.TempDir()
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
kind: ClusterRole
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
metadata:
5+
name: rancher-inspect
6+
labels:
7+
purpose: tp-cli-testing
8+
rules:
9+
- apiGroups: [""]
10+
resources: ["nodes"]
11+
verbs: ["get", "list"]
12+
13+
---
14+
apiVersion: rbac.authorization.k8s.io/v1
15+
kind: ClusterRoleBinding
16+
metadata:
17+
name: rancher-inspect
18+
labels:
19+
purpose: tp-cli-testing
20+
subjects:
21+
- kind: ServiceAccount
22+
name: telepresence-test-developer
23+
namespace: {{ .ManagerNamespace }}
24+
roleRef:
25+
apiGroup: rbac.authorization.k8s.io
26+
name: rancher-inspect
27+
kind: ClusterRole

0 commit comments

Comments
 (0)