podman_connection_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build !windows
  4. // +build !windows
  5. package podmanreceiver
  6. import (
  7. "context"
  8. "net"
  9. "net/http"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. "go.uber.org/zap"
  15. )
  16. func TestNewPodmanConnectionUnsupported(t *testing.T) {
  17. logger := zap.NewNop()
  18. c, err := newPodmanConnection(logger, "xyz://hello", "", "")
  19. assert.EqualError(t, err, `unable to create connection. "xyz" is not a supported schema`)
  20. assert.Nil(t, c)
  21. }
  22. func TestNewPodmanConnectionUnix(t *testing.T) {
  23. tmpDir := t.TempDir()
  24. socketPath := filepath.Join(tmpDir, "test.sock")
  25. l, err := net.Listen("unix", socketPath)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer l.Close()
  30. logger := zap.NewNop()
  31. c, err := newPodmanConnection(logger, "unix:///"+socketPath, "", "")
  32. assert.NoError(t, err)
  33. assert.NotNil(t, c)
  34. tr, ok := c.Transport.(*http.Transport)
  35. assert.True(t, ok)
  36. assert.True(t, tr.DisableCompression)
  37. conn, err := tr.DialContext(context.Background(), "", "")
  38. assert.NoError(t, err)
  39. assert.Equal(t, socketPath, conn.RemoteAddr().String())
  40. }
  41. func TestNewPodmanConnectionSSH(t *testing.T) {
  42. // We only test that the connection tries to connect over SSH.
  43. // Actual SSH connection to podman should be tested in an integration test if desired.
  44. logger := zap.NewNop()
  45. c, err := newPodmanConnection(logger, "ssh://otel-test-podman-server", "", "")
  46. assert.Error(t, err)
  47. assert.True(t, strings.HasPrefix(err.Error(), "connection to bastion host (ssh://otel-test-podman-server) failed:"))
  48. assert.Nil(t, c)
  49. }