github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/e2e/events_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  	"time"
     9  
    10  	. "github.com/containers/podman/v2/test/utils"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("Podman events", func() {
    17  	var (
    18  		tempdir    string
    19  		podmanTest *PodmanTestIntegration
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		var err error
    24  		tempdir, err = CreateTempDirInTempDir()
    25  		if err != nil {
    26  			os.Exit(1)
    27  		}
    28  		podmanTest = PodmanTestCreate(tempdir)
    29  		podmanTest.Setup()
    30  		podmanTest.SeedImages()
    31  	})
    32  
    33  	AfterEach(func() {
    34  		podmanTest.Cleanup()
    35  		f := CurrentGinkgoTestDescription()
    36  		processTestResult(f)
    37  	})
    38  
    39  	// For most, all, of these tests we do not "live" test following a log because it may make a fragile test
    40  	// system more complex.  Instead we run the "events" and then verify that the events are processed correctly.
    41  	// Perhaps a future version of this test would put events in a go func and send output back over a channel
    42  	// while events occur.
    43  
    44  	// These tests are only known to work on Fedora ATM.  Other distributions
    45  	// will be skipped.
    46  	It("podman events", func() {
    47  		SkipIfNotFedora()
    48  		_, ec, _ := podmanTest.RunLsContainer("")
    49  		Expect(ec).To(Equal(0))
    50  		result := podmanTest.Podman([]string{"events", "--stream=false"})
    51  		result.WaitWithDefaultTimeout()
    52  		Expect(result.ExitCode()).To(BeZero())
    53  	})
    54  
    55  	It("podman events with an event filter", func() {
    56  		SkipIfNotFedora()
    57  		_, ec, _ := podmanTest.RunLsContainer("")
    58  		Expect(ec).To(Equal(0))
    59  		result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "event=start"})
    60  		result.WaitWithDefaultTimeout()
    61  		Expect(result.ExitCode()).To(Equal(0))
    62  		Expect(len(result.OutputToStringArray()) >= 1)
    63  	})
    64  
    65  	It("podman events with an event filter and container=cid", func() {
    66  		Skip("Does not work on v2")
    67  		SkipIfNotFedora()
    68  		_, ec, cid := podmanTest.RunLsContainer("")
    69  		Expect(ec).To(Equal(0))
    70  		_, ec2, cid2 := podmanTest.RunLsContainer("")
    71  		Expect(ec2).To(Equal(0))
    72  		time.Sleep(5 * time.Second)
    73  		result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "event=start", "--filter", fmt.Sprintf("container=%s", cid)})
    74  		result.WaitWithDefaultTimeout()
    75  		Expect(result.ExitCode()).To(Equal(0))
    76  		Expect(len(result.OutputToStringArray())).To(Equal(1))
    77  		Expect(!strings.Contains(result.OutputToString(), cid2))
    78  	})
    79  
    80  	It("podman events with a type and filter container=id", func() {
    81  		SkipIfNotFedora()
    82  		_, ec, cid := podmanTest.RunLsContainer("")
    83  		Expect(ec).To(Equal(0))
    84  		result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "type=pod", "--filter", fmt.Sprintf("container=%s", cid)})
    85  		result.WaitWithDefaultTimeout()
    86  		Expect(result.ExitCode()).To(Equal(0))
    87  		Expect(len(result.OutputToStringArray())).To(Equal(0))
    88  	})
    89  
    90  	It("podman events with a type", func() {
    91  		SkipIfNotFedora()
    92  		setup := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:foobarpod", ALPINE, "top"})
    93  		setup.WaitWithDefaultTimeout()
    94  		stop := podmanTest.Podman([]string{"pod", "stop", "foobarpod"})
    95  		stop.WaitWithDefaultTimeout()
    96  		Expect(stop.ExitCode()).To(Equal(0))
    97  		Expect(setup.ExitCode()).To(Equal(0))
    98  		result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "type=pod", "--filter", "pod=foobarpod"})
    99  		result.WaitWithDefaultTimeout()
   100  		Expect(result.ExitCode()).To(Equal(0))
   101  		fmt.Println(result.OutputToStringArray())
   102  		Expect(len(result.OutputToStringArray()) >= 2)
   103  	})
   104  
   105  	It("podman events --since", func() {
   106  		SkipIfNotFedora()
   107  		_, ec, _ := podmanTest.RunLsContainer("")
   108  		Expect(ec).To(Equal(0))
   109  		result := podmanTest.Podman([]string{"events", "--stream=false", "--since", "1m"})
   110  		result.WaitWithDefaultTimeout()
   111  		Expect(result.ExitCode()).To(BeZero())
   112  	})
   113  
   114  	It("podman events --until", func() {
   115  		SkipIfNotFedora()
   116  		_, ec, _ := podmanTest.RunLsContainer("")
   117  		Expect(ec).To(Equal(0))
   118  		test := podmanTest.Podman([]string{"events", "--help"})
   119  		test.WaitWithDefaultTimeout()
   120  		fmt.Println(test.OutputToStringArray())
   121  		result := podmanTest.Podman([]string{"events", "--stream=false", "--since", "1h"})
   122  		result.WaitWithDefaultTimeout()
   123  		Expect(result.ExitCode()).To(BeZero())
   124  	})
   125  
   126  	It("podman events format", func() {
   127  		SkipIfNotFedora()
   128  		_, ec, _ := podmanTest.RunLsContainer("")
   129  		Expect(ec).To(Equal(0))
   130  
   131  		test := podmanTest.Podman([]string{"events", "--stream=false", "--format", "json"})
   132  		test.WaitWithDefaultTimeout()
   133  		Expect(test).To(Exit(0))
   134  
   135  		jsonArr := test.OutputToStringArray()
   136  		Expect(test.OutputToStringArray()).ShouldNot(BeEmpty())
   137  
   138  		eventsMap := make(map[string]string)
   139  		err := json.Unmarshal([]byte(jsonArr[0]), &eventsMap)
   140  		Expect(err).ToNot(HaveOccurred())
   141  
   142  		Expect(eventsMap).To(HaveKey("Status"))
   143  
   144  		test = podmanTest.Podman([]string{"events", "--stream=false", "--format", "{{json.}}"})
   145  		test.WaitWithDefaultTimeout()
   146  		Expect(test).To(Exit(0))
   147  
   148  		jsonArr = test.OutputToStringArray()
   149  		Expect(test.OutputToStringArray()).ShouldNot(BeEmpty())
   150  
   151  		eventsMap = make(map[string]string)
   152  		err = json.Unmarshal([]byte(jsonArr[0]), &eventsMap)
   153  		Expect(err).ToNot(HaveOccurred())
   154  
   155  		Expect(eventsMap).To(HaveKey("Status"))
   156  	})
   157  })