Skip to content

Commit

Permalink
Use "app" selector consistently in integration tests.
Browse files Browse the repository at this point in the history
Also improves reliability of CapturePodLogs.

Signed-off-by: Thomas Hallgren <[email protected]>
  • Loading branch information
thallgren committed Feb 2, 2024
1 parent bee8231 commit 8d383b4
Show file tree
Hide file tree
Showing 31 changed files with 199 additions and 147 deletions.
2 changes: 1 addition & 1 deletion integration_test/colliding_mounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *mountsSuite) Test_CollidingMounts() {
defer itest.TelepresenceOk(ctx, "leave", "hello")
require.Contains(stdout, "Using Deployment hello")
if i == 0 {
s.CapturePodLogs(ctx, "app=hello", "traffic-agent", s.AppNamespace())
s.CapturePodLogs(ctx, "hello", "traffic-agent", s.AppNamespace())
} else {
// Mounts are sometimes slow
dtime.SleepWithContext(ctx, 3*time.Second)
Expand Down
2 changes: 1 addition & 1 deletion integration_test/env_interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *connectedSuite) Test_PrefixInterpolated() {

itest.TelepresenceOk(ctx, "intercept", "--mount", "false", svc)
defer itest.TelepresenceOk(ctx, "leave", svc)
out, err := s.KubectlOut(ctx, "get", "pod", "-o", "json", "-l", "service="+svc)
out, err := s.KubectlOut(ctx, "get", "pod", "-o", "json", "-l", "app="+svc)
rq.NoError(err)

var pods core.PodList
Expand Down
4 changes: 2 additions & 2 deletions integration_test/gather_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (s *connectedSuite) TestGatherLogs_OnlyMappedLogs() {
10*time.Second,
2*time.Second,
)
s.CapturePodLogs(ctx, fmt.Sprintf("app=%s", svc), "traffic-agent", otherOne)
s.CapturePodLogs(ctx, svc, "traffic-agent", otherOne)
itest.TelepresenceDisconnectOk(ctx)

itest.TelepresenceOk(ctx, "connect", "--namespace", otherTwo, "--manager-namespace", s.ManagerNamespace())
Expand All @@ -150,7 +150,7 @@ func (s *connectedSuite) TestGatherLogs_OnlyMappedLogs() {
10*time.Second,
2*time.Second,
)
s.CapturePodLogs(ctx, fmt.Sprintf("app=%s", svc), "traffic-agent", otherTwo)
s.CapturePodLogs(ctx, svc, "traffic-agent", otherTwo)
itest.TelepresenceOk(ctx, "leave", svc)

bothNsRx := fmt.Sprintf("(?:%s|%s)", otherOne, otherTwo)
Expand Down
2 changes: 1 addition & 1 deletion integration_test/headless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *connectedSuite) Test_SuccessfullyInterceptsHeadlessService() {
require := s.Require()
stdout := itest.TelepresenceOk(ctx, "intercept", "--mount", "false", svc, "--port", strconv.Itoa(svcPort))
require.Contains(stdout, "Using StatefulSet echo-headless")
s.CapturePodLogs(ctx, "service=echo-headless", "traffic-agent", s.AppNamespace())
s.CapturePodLogs(ctx, "echo-headless", "traffic-agent", s.AppNamespace())

defer itest.TelepresenceOk(ctx, "leave", "echo-headless")

Expand Down
70 changes: 43 additions & 27 deletions integration_test/intercept_flags_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration_test

import (
"context"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -60,48 +61,58 @@ func (s *interceptFlagSuite) Test_ContainerReplace() {
ctx := s.Context()
require := s.Require()

n1 := "container_replaced"
const (
n1 = "container_replaced"
c1 = "hello-container-1"
n2 = "container_kept"
c2 = "hello-container-2"
)

localPort1, cancel1 := itest.StartLocalHttpEchoServer(ctx, n1)
defer cancel1()

n2 := "container_kept"
localPort2, cancel2 := itest.StartLocalHttpEchoServer(ctx, n2)
defer cancel2()

tests := []struct {
name string
iceptName string
replace bool
localPort int
port uint16
name string
iceptName string
appContainer string
replace bool
localPort int
port uint16
}{
{
name: n1,
iceptName: n1,
replace: true,
localPort: localPort1,
port: 80,
name: n1,
iceptName: n1,
appContainer: c1,
replace: true,
localPort: localPort1,
port: 80,
},
{
name: n2,
iceptName: n2,
replace: false,
localPort: localPort2,
port: 81,
name: n2,
iceptName: n2,
appContainer: c2,
replace: false,
localPort: localPort2,
port: 81,
},
{
name: "container_replace_kept",
iceptName: n1,
replace: false,
localPort: localPort1,
port: 80,
name: "container_replace_kept",
iceptName: n1,
appContainer: c1,
replace: false,
localPort: localPort1,
port: 80,
},
{
name: "container_kept_replaced",
iceptName: n2,
replace: true,
localPort: localPort2,
port: 81,
name: "container_kept_replaced",
iceptName: n2,
appContainer: c2,
replace: true,
localPort: localPort2,
port: 81,
},
}

Expand All @@ -116,8 +127,13 @@ func (s *interceptFlagSuite) Test_ContainerReplace() {
}
args = append(args, "--port", fmt.Sprintf("%d:%d", tt.localPort, tt.port), "--output", "json", "--detailed-output", "--workload", s.serviceName, tt.iceptName)
jsOut := itest.TelepresenceOk(ctx, args...)
agentCaptureCtx, agentCaptureCancel := context.WithCancel(ctx)
s.CapturePodLogs(agentCaptureCtx, s.serviceName, "traffic-agent", s.AppNamespace())

defer func() {
agentCaptureCancel()
itest.TelepresenceOk(ctx, "leave", tt.iceptName)
s.CapturePodLogs(ctx, s.serviceName, tt.appContainer, s.AppNamespace())
s.Eventually(func() bool {
out, err := itest.Output(ctx, "curl", "--silent", "--max-time", "1", iputil.JoinHostPort(s.serviceName, tt.port))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion integration_test/intercept_localhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *interceptLocalhostSuite) SetupSuite() {
s.Require().NoError(err)
dlog.Infof(ctx, "ip: %s: route: %s", s.defaultRoute.LocalIP, s.defaultRoute)
s.port, s.cancelLocal = itest.StartLocalHttpEchoServerWithHost(ctx, s.ServiceName(), s.defaultRoute.LocalIP.String())
s.CapturePodLogs(ctx, "app=echo", "traffic-agent", s.AppNamespace())
s.CapturePodLogs(ctx, "echo", "traffic-agent", s.AppNamespace())
}

func (s *interceptLocalhostSuite) TearDownSuite() {
Expand Down
4 changes: 2 additions & 2 deletions integration_test/intercept_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *interceptMountSuite) SetupSuite() {
port, s.cancelLocal = itest.StartLocalHttpEchoServer(ctx, s.ServiceName())
stdout := itest.TelepresenceOk(ctx, "intercept", s.ServiceName(), "--mount", s.mountPoint, "--port", strconv.Itoa(port))
s.Contains(stdout, "Using Deployment "+s.ServiceName())
s.CapturePodLogs(ctx, "app=echo", "traffic-agent", s.AppNamespace())
s.CapturePodLogs(ctx, "echo", "traffic-agent", s.AppNamespace())
}

func (s *interceptMountSuite) TearDownSuite() {
Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *singleServiceSuite) Test_NoInterceptorResponse() {
}, 10*time.Second, time.Second)

time.Sleep(2000 * time.Millisecond) // List is really fast now, so give the mount some time to become effective
s.CapturePodLogs(ctx, "app="+s.ServiceName(), "traffic-agent", s.AppNamespace())
s.CapturePodLogs(ctx, s.ServiceName(), "traffic-agent", s.AppNamespace())

mountPoint := filepath.Join(nwd, "rel-dir")
st, err := os.Stat(mountPoint)
Expand Down
121 changes: 79 additions & 42 deletions integration_test/itest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/telepresenceio/telepresence/v2/pkg/maps"
"github.com/telepresenceio/telepresence/v2/pkg/proc"
"github.com/telepresenceio/telepresence/v2/pkg/shellquote"
"github.com/telepresenceio/telepresence/v2/pkg/slice"
"github.com/telepresenceio/telepresence/v2/pkg/version"
)

Expand Down Expand Up @@ -460,61 +461,97 @@ func (s *cluster) RootdPProf() uint16 {
}

func (s *cluster) CapturePodLogs(ctx context.Context, app, container, ns string) {
var pods string
var pods []string
for i := 0; ; i++ {
var err error
pods, err = KubectlOut(ctx, ns, "get", "pods", "--field-selector", "status.phase=Running", "-l", app, "-o", "jsonpath={.items[*].metadata.name}")
if err != nil {
dlog.Errorf(ctx, "failed to get %s pod in namespace %s: %v", app, ns, err)
return
runningPods := RunningPods(ctx, app, ns)
if len(runningPods) > 0 {
if container == "" {
pods = runningPods
} else {
for _, pod := range runningPods {
cns, err := KubectlOut(ctx, ns, "get", "pods", pod, "-o", "jsonpath={.spec.containers[*].name}")
if err == nil && slice.Contains(strings.Split(cns, " "), container) {
pods = append(pods, pod)
}
}
}
}
pods = strings.TrimSpace(pods)
if pods != "" || i == 5 {
if len(pods) > 0 || i == 5 {
break
}
dtime.SleepWithContext(ctx, 2*time.Second)
}
if pods == "" {
dlog.Errorf(ctx, "found no %s pods in namespace %s", app, ns)

if len(pods) == 0 {
if container == "" {
dlog.Errorf(ctx, "found no %s pods in namespace %s", app, ns)
} else {
dlog.Errorf(ctx, "found no %s pods in namespace %s with a %s container", app, ns, container)
}
return
}

// Let command die when the pod that it logs die
ctx = context.WithoutCancel(ctx)

present := struct{}{}

// Use another logger to avoid errors due to logs arriving after the tests complete.
ctx = dlog.WithLogger(ctx, dlog.WrapLogrus(logrus.StandardLogger()))
dlog.Infof(ctx, "Capturing logs for pods %q", pods)
for _, pod := range strings.Split(pods, " ") {
if _, ok := s.logCapturingPods.LoadOrStore(pod, present); ok {
continue
}
logFile, err := os.Create(
filepath.Join(filelocation.AppUserLogDir(ctx), fmt.Sprintf("%s-%s.log", dtime.Now().Format("20060102T150405"), pod)))
if err != nil {
s.logCapturingPods.Delete(pod)
dlog.Errorf(ctx, "unable to create pod logfile %s: %v", logFile.Name(), err)
return
}
pod := pods[0]
key := pod
if container != "" {
key += "/" + container
}
if _, ok := s.logCapturingPods.LoadOrStore(key, present); ok {
return
}

args := []string{"--namespace", ns, "logs", "-f", pod}
if container != "" {
args = append(args, "-c", container)
logFile, err := os.Create(
filepath.Join(filelocation.AppUserLogDir(ctx), fmt.Sprintf("%s-%s.log", dtime.Now().Format("20060102T150405"), pod)))
if err != nil {
s.logCapturingPods.Delete(pod)
dlog.Errorf(ctx, "unable to create pod logfile %s: %v", logFile.Name(), err)
return
}

args := []string{"--namespace", ns, "logs", "-f", pod}
if container != "" {
args = append(args, "-c", container)
}
// Let command die when the pod that it logs die
cmd := Command(context.WithoutCancel(ctx), "kubectl", args...)
cmd.Stdout = logFile
cmd.Stderr = logFile
ready := make(chan struct{})
go func() {
defer func() {
_ = logFile.Close()
s.logCapturingPods.Delete(pod)
}()
err := cmd.Start()
if err == nil {
if container == "" {
dlog.Infof(ctx, "Capturing logs for pod %s", pod)
} else {
dlog.Infof(ctx, "Capturing logs for pod %s, container %s", pod, container)
}
close(ready)
err = cmd.Wait()
}
cmd := Command(ctx, "kubectl", args...)
cmd.Stdout = logFile
cmd.Stderr = logFile
go func(pod string) {
defer func() {
_ = logFile.Close()
s.logCapturingPods.Delete(pod)
}()
if err := cmd.Run(); err != nil {
dlog.Errorf(ctx, "log capture failed: %v", err)
if err != nil {
if container == "" {
dlog.Errorf(ctx, "log capture for pod %s failed: %v", pod, err)
} else {
dlog.Errorf(ctx, "log capture for pod %s, container %s failed: %v", pod, container, err)
}
}(pod)
select {
case <-ready:
default:
close(ready)
}
}
}()
select {
case <-ctx.Done():
dlog.Infof(ctx, "log capture for pod %s interrupted prior to start", pod)
case <-ready:
}
}

Expand Down Expand Up @@ -598,7 +635,7 @@ func (s *cluster) installChart(ctx context.Context, release bool, chartFilename
if err == nil {
err = RolloutStatusWait(ctx, nss.Namespace, "deploy/traffic-manager")
if err == nil {
s.self.CapturePodLogs(ctx, "app=traffic-manager", "", nss.Namespace)
s.self.CapturePodLogs(ctx, "traffic-manager", "", nss.Namespace)
}
}
return err
Expand Down Expand Up @@ -685,7 +722,7 @@ func (s *cluster) TelepresenceHelmInstall(ctx context.Context, upgrade bool, set
if err = RolloutStatusWait(ctx, nss.Namespace, "deploy/traffic-manager"); err != nil {
return err
}
s.self.CapturePodLogs(ctx, "app=traffic-manager", "", nss.Namespace)
s.self.CapturePodLogs(ctx, "traffic-manager", "", nss.Namespace)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion integration_test/itest/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s *nsPair) RollbackTM(ctx context.Context) {
t := getT(ctx)
require.NoError(t, err)
require.NoError(t, RolloutStatusWait(ctx, s.Namespace, "deploy/traffic-manager"))
s.CapturePodLogs(ctx, "app=traffic-manager", "", s.Namespace)
s.CapturePodLogs(ctx, "traffic-manager", "", s.Namespace)
}

func (s *nsPair) AppNamespace() string {
Expand Down
2 changes: 1 addition & 1 deletion integration_test/itest/traffic_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (th *trafficManager) setup(ctx context.Context) bool {
if t.Failed() {
return false
}
th.CapturePodLogs(ctx, "app=traffic-manager", "", th.ManagerNamespace())
th.CapturePodLogs(ctx, "traffic-manager", "", th.ManagerNamespace())
return true
}

Expand Down
2 changes: 1 addition & 1 deletion integration_test/large_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (s *largeFilesSuite) createIntercepts(ctx context.Context) {
require.NotNil(info.Mount)
s.mountPoint[i] = info.Mount.LocalDir
s.NoError(itest.RolloutStatusWait(ctx, s.AppNamespace(), "deploy/"+svc))
s.CapturePodLogs(ctx, "service="+svc, "traffic-agent", s.AppNamespace())
s.CapturePodLogs(ctx, svc, "traffic-agent", s.AppNamespace())
}(i)
}
wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion integration_test/multiport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *connectedSuite) Test_UnnamedUdpAndTcpPort() {
itest.TelepresenceOk(ctx, "loglevel", "trace")
defer itest.TelepresenceOk(ctx, "loglevel", "debug")
itest.TelepresenceOk(ctx, "intercept", "--mount", "false", "--service", "echo-udp", "-p", fmt.Sprintf("%d:%s", localPort, svcPort), dep)
s.CapturePodLogs(ctx, "app="+dep, "traffic-agent", s.AppNamespace())
s.CapturePodLogs(ctx, dep, "traffic-agent", s.AppNamespace())
defer itest.TelepresenceOk(ctx, "leave", dep)

pingPong := func(conn net.Conn, msg string) {
Expand Down
Loading

0 comments on commit 8d383b4

Please sign in to comment.