github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/system_events_linux_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/containerd/nerdctl/pkg/testutil"
    26  	"gotest.tools/v3/assert"
    27  )
    28  
    29  func testEventFilter(t *testing.T, args ...string) string {
    30  	t.Parallel()
    31  	base := testutil.NewBase(t)
    32  	testContainerName := testutil.Identifier(t)
    33  	defer base.Cmd("rm", "-f", testContainerName).Run()
    34  
    35  	fullArgs := []string{"events", "--filter"}
    36  	fullArgs = append(fullArgs, args...)
    37  	fullArgs = append(fullArgs,
    38  		"--format",
    39  		"json",
    40  	)
    41  
    42  	eventsCmd := base.Cmd(fullArgs...).Start()
    43  	base.Cmd("run", "--rm", testutil.CommonImage).Start()
    44  	time.Sleep(3 * time.Second)
    45  	return eventsCmd.Stdout()
    46  }
    47  
    48  func TestEventFilters(t *testing.T) {
    49  
    50  	type testCase struct {
    51  		name       string
    52  		args       []string
    53  		nerdctlOut string
    54  		dockerOut  string
    55  		dockerSkip bool
    56  	}
    57  	testCases := []testCase{
    58  		{
    59  			name:       "CapitializedFilter",
    60  			args:       []string{"event=START"},
    61  			nerdctlOut: "\"Status\":\"start\"",
    62  			dockerOut:  "\"status\":\"start\"",
    63  			dockerSkip: true,
    64  		},
    65  		{
    66  			name:       "StartEventFilter",
    67  			args:       []string{"event=start"},
    68  			nerdctlOut: "\"Status\":\"start\"",
    69  			dockerOut:  "\"status\":\"start\"",
    70  			dockerSkip: false,
    71  		},
    72  		{
    73  			name:       "UnsupportedEventFilter",
    74  			args:       []string{"event=unknown"},
    75  			nerdctlOut: "\"Status\":\"unknown\"",
    76  			dockerSkip: true,
    77  		},
    78  		{
    79  			name:       "StatusFilter",
    80  			args:       []string{"status=start"},
    81  			nerdctlOut: "\"Status\":\"start\"",
    82  			dockerOut:  "\"status\":\"start\"",
    83  			dockerSkip: false,
    84  		},
    85  		{
    86  			name:       "UnsupportedStatusFilter",
    87  			args:       []string{"status=unknown"},
    88  			nerdctlOut: "\"Status\":\"unknown\"",
    89  			dockerSkip: true,
    90  		},
    91  	}
    92  
    93  	for _, tc := range testCases {
    94  		tc := tc
    95  		t.Run(tc.name, func(t *testing.T) {
    96  			actualOut := testEventFilter(t, tc.args...)
    97  			errorMsg := fmt.Sprintf("%s failed;\nActual Filter Result: '%s'", tc.name, actualOut)
    98  
    99  			isDocker := testutil.GetTarget() == testutil.Docker
   100  			if isDocker && tc.dockerSkip {
   101  				t.Skip("test is incompatible with Docker")
   102  			}
   103  
   104  			if isDocker {
   105  				assert.Equal(t, true, strings.Contains(actualOut, tc.dockerOut), errorMsg)
   106  			} else {
   107  				assert.Equal(t, true, strings.Contains(actualOut, tc.nerdctlOut), errorMsg)
   108  			}
   109  		})
   110  	}
   111  }