summaryrefslogtreecommitdiff
path: root/pkg/bindings/test/connection_test.go
blob: 561cf32b5ba008dae0d7797c952fd972216158d1 (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
package test_bindings

import (
	"context"
	"time"

	"github.com/containers/podman/v3/pkg/bindings/containers"
	"github.com/containers/podman/v3/pkg/bindings/system"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Podman connection", func() {
	var (
		bt *bindingTest
		s  *gexec.Session
	)

	BeforeEach(func() {
		bt = newBindingTest()
		bt.RestoreImagesFromCache()
		s = bt.startAPIService()
		time.Sleep(1 * time.Second)
		err := bt.NewConnection()
		Expect(err).To(BeNil())
	})

	AfterEach(func() {
		s.Kill()
		bt.cleanup()
	})

	It("request on cancelled context results in error", func() {
		ctx, cancel := context.WithCancel(bt.conn)
		cancel()
		_, err := system.Version(ctx, nil)
		Expect(err).To(MatchError(ctx.Err()))
	})

	It("cancel request in flight reports cancelled context", func() {
		var name = "top"
		_, err := bt.RunTopContainer(&name, nil)
		Expect(err).To(BeNil())

		errChan := make(chan error)
		ctx, cancel := context.WithCancel(bt.conn)

		go func() {
			defer close(errChan)
			_, err := containers.Wait(ctx, name, nil)
			errChan <- err
		}()

		// Wait for the goroutine to fire the request
		time.Sleep(1 * time.Second)

		cancel()

		select {
		case err, ok := <-errChan:
			Expect(ok).To(BeTrue())
			Expect(err).To(MatchError(ctx.Err()))
		case <-time.NewTimer(1 * time.Second).C:
			Fail("cancelled request did not return in less than 1 second")
		}
	})
})