summaryrefslogtreecommitdiff
path: root/test/e2e/system_service_test.go
blob: 922a8c4230282f595055483bceae9ec89c0b2cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package integration

import (
	"io/ioutil"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"time"

	. "github.com/containers/podman/v3/test/utils"
	"github.com/containers/podman/v3/utils"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"
)

var _ = Describe("podman system service", func() {
	var podmanTest *PodmanTestIntegration

	BeforeEach(func() {
		tempdir, err := CreateTempDirInTempDir()
		Expect(err).ShouldNot(HaveOccurred())

		podmanTest = PodmanTestCreate(tempdir)
		podmanTest.Setup()
	})

	AfterEach(func() {
		podmanTest.Cleanup()
		processTestResult(CurrentGinkgoTestDescription())
	})

	Describe("verify timeout", func() {
		It("of 2 seconds", func() {
			SkipIfRemote("service subcommand not supported remotely")

			address := url.URL{
				Scheme: "tcp",
				Host:   net.JoinHostPort("localhost", randomPort()),
			}
			session := podmanTest.Podman([]string{
				"system", "service", "--time=2", address.String(),
			})
			defer session.Kill()

			WaitForService(address)
			Eventually(session, 5).Should(Exit(0))
		})
	})

	Describe("verify pprof endpoints", func() {
		// Depends on pkg/api/server/server.go:255
		const magicComment = "pprof service listening on"

		It("are available", func() {
			SkipIfRemote("service subcommand not supported remotely")

			address := url.URL{
				Scheme: "tcp",
				Host:   net.JoinHostPort("localhost", randomPort()),
			}

			pprofPort := randomPort()
			session := podmanTest.Podman([]string{
				"system", "service", "--log-level=debug", "--time=0",
				"--pprof-address=localhost:" + pprofPort, address.String(),
			})
			defer session.Kill()

			WaitForService(address)

			// Combined with test below we have positive/negative test for pprof
			Expect(session.Err.Contents()).Should(ContainSubstring(magicComment))

			heap := url.URL{
				Scheme:   "http",
				Host:     net.JoinHostPort("localhost", pprofPort),
				Path:     "/debug/pprof/heap",
				RawQuery: "seconds=2",
			}
			resp, err := http.Get(heap.String())
			Expect(err).ShouldNot(HaveOccurred())
			defer resp.Body.Close()
			Expect(resp).To(HaveHTTPStatus(http.StatusOK))

			body, err := ioutil.ReadAll(resp.Body)
			Expect(err).ShouldNot(HaveOccurred())
			Expect(body).ShouldNot(BeEmpty())

			session.Interrupt().Wait(2 * time.Second)
			Eventually(session, 5).Should(Exit(1))
		})

		It("are not available", func() {
			SkipIfRemote("service subcommand not supported remotely")

			address := url.URL{
				Scheme: "tcp",
				Host:   net.JoinHostPort("localhost", randomPort()),
			}

			session := podmanTest.Podman([]string{
				"system", "service", "--log-level=debug", "--time=0", address.String(),
			})
			defer session.Kill()

			WaitForService(address)

			// Combined with test above we have positive/negative test for pprof
			Expect(session.Err.Contents()).ShouldNot(ContainSubstring(magicComment))

			session.Interrupt().Wait(2 * time.Second)
			Eventually(session, 5).Should(Exit(1))
		})
	})
})

// WaitForService blocks, waiting for some service listening on given host:port
func WaitForService(address url.URL) {
	// Wait for podman to be ready
	var conn net.Conn
	var err error
	for i := 1; i <= 5; i++ {
		conn, err = net.Dial("tcp", address.Host)
		if err != nil {
			// Podman not available yet...
			time.Sleep(time.Duration(i) * time.Second)
		}
	}
	Expect(err).ShouldNot(HaveOccurred())
	conn.Close()
}

// randomPort leans on the go net library to find an available port...
func randomPort() string {
	port, err := utils.GetRandomPort()
	Expect(err).ShouldNot(HaveOccurred())
	return strconv.Itoa(port)
}