github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/network_list_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 "errors" 21 "fmt" 22 "strings" 23 "testing" 24 25 "github.com/containerd/nerdctl/pkg/testutil" 26 ) 27 28 func TestNetworkLsFilter(t *testing.T) { 29 t.Parallel() 30 base := testutil.NewBase(t) 31 tID := testutil.Identifier(t) 32 33 var net1, net2 = tID + "net-1", tID + "net-2" 34 var label1 = tID + "=label-1" 35 netID1 := base.Cmd("network", "create", "--label="+label1, net1).Out() 36 defer base.Cmd("network", "rm", "-f", netID1).Run() 37 38 netID2 := base.Cmd("network", "create", net2).Out() 39 defer base.Cmd("network", "rm", "-f", netID2).Run() 40 41 base.Cmd("network", "ls", "--quiet", "--filter", "label="+tID).AssertOutWithFunc(func(stdout string) error { 42 var lines = strings.Split(strings.TrimSpace(stdout), "\n") 43 if len(lines) < 1 { 44 return errors.New("expected at least 1 lines") 45 } 46 netNames := map[string]struct{}{ 47 netID1[:12]: {}, 48 } 49 50 for _, name := range lines { 51 _, ok := netNames[name] 52 if !ok { 53 return fmt.Errorf("unexpected netume %s found", name) 54 } 55 } 56 return nil 57 }) 58 59 base.Cmd("network", "ls", "--quiet", "--filter", "name="+net2).AssertOutWithFunc(func(stdout string) error { 60 var lines = strings.Split(strings.TrimSpace(stdout), "\n") 61 if len(lines) < 1 { 62 return errors.New("expected at least 1 lines") 63 } 64 netNames := map[string]struct{}{ 65 netID2[:12]: {}, 66 } 67 68 for _, name := range lines { 69 _, ok := netNames[name] 70 if !ok { 71 return fmt.Errorf("unexpected netume %s found", name) 72 } 73 } 74 return nil 75 }) 76 }