github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/volume_list_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/tabutil"
    26  	"github.com/containerd/nerdctl/pkg/testutil"
    27  )
    28  
    29  func TestVolumeLs(t *testing.T) {
    30  	t.Parallel()
    31  	base := testutil.NewBase(t)
    32  	tID := testutil.Identifier(t)
    33  	testutil.DockerIncompatible(t)
    34  
    35  	var vol1, vol2, vol3 = tID + "vol-1", tID + "vol-2", tID + "empty"
    36  	base.Cmd("volume", "create", vol1).AssertOK()
    37  	defer base.Cmd("volume", "rm", "-f", vol1).Run()
    38  
    39  	base.Cmd("volume", "create", vol2).AssertOK()
    40  	defer base.Cmd("volume", "rm", "-f", vol2).Run()
    41  
    42  	base.Cmd("volume", "create", vol3).AssertOK()
    43  	defer base.Cmd("volume", "rm", "-f", vol3).Run()
    44  
    45  	createFileWithSize(t, vol1, 102400)
    46  	createFileWithSize(t, vol2, 204800)
    47  
    48  	base.Cmd("volume", "ls", "--size").AssertOutWithFunc(func(stdout string) error {
    49  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
    50  		if len(lines) < 4 {
    51  			return errors.New("expected at least 4 lines")
    52  		}
    53  		volSizes := map[string]string{
    54  			vol1: "100.0 KiB",
    55  			vol2: "200.0 KiB",
    56  			vol3: "0.0 B",
    57  		}
    58  
    59  		var numMatches = 0
    60  		var tab = tabutil.NewReader("VOLUME NAME\tDIRECTORY\tSIZE")
    61  		var err = tab.ParseHeader(lines[0])
    62  		if err != nil {
    63  			return err
    64  		}
    65  		for _, line := range lines {
    66  			name, _ := tab.ReadRow(line, "VOLUME NAME")
    67  			size, _ := tab.ReadRow(line, "SIZE")
    68  			expectSize, ok := volSizes[name]
    69  			if !ok {
    70  				continue
    71  			}
    72  			if size != expectSize {
    73  				return fmt.Errorf("expected size %s for volume %s, got %s", expectSize, name, size)
    74  			}
    75  			numMatches++
    76  		}
    77  		if len(volSizes) != numMatches {
    78  			return fmt.Errorf("expected %d volumes, got: %d", len(volSizes), numMatches)
    79  		}
    80  		return nil
    81  	})
    82  
    83  }
    84  
    85  func TestVolumeLsFilter(t *testing.T) {
    86  	t.Parallel()
    87  	base := testutil.NewBase(t)
    88  	tID := testutil.Identifier(t)
    89  
    90  	var vol1, vol2, vol3, vol4 = tID + "vol-1", tID + "vol-2", tID + "vol-3", tID + "vol-4"
    91  	var label1, label2, label3, label4 = tID + "=label-1", tID + "=label-2", tID + "=label-3", tID + "-group=label-4"
    92  	base.Cmd("volume", "create", "--label="+label1, "--label="+label4, vol1).AssertOK()
    93  	defer base.Cmd("volume", "rm", "-f", vol1).Run()
    94  
    95  	base.Cmd("volume", "create", "--label="+label2, "--label="+label4, vol2).AssertOK()
    96  	defer base.Cmd("volume", "rm", "-f", vol2).Run()
    97  
    98  	base.Cmd("volume", "create", "--label="+label3, vol3).AssertOK()
    99  	defer base.Cmd("volume", "rm", "-f", vol3).Run()
   100  
   101  	base.Cmd("volume", "create", vol4).AssertOK()
   102  	defer base.Cmd("volume", "rm", "-f", vol4).Run()
   103  
   104  	base.Cmd("volume", "ls", "--quiet").AssertOutWithFunc(func(stdout string) error {
   105  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   106  		if len(lines) < 4 {
   107  			return errors.New("expected at least 4 lines")
   108  		}
   109  		volNames := map[string]struct{}{
   110  			vol1: {},
   111  			vol2: {},
   112  			vol3: {},
   113  			vol4: {},
   114  		}
   115  
   116  		var numMatches = 0
   117  		for _, name := range lines {
   118  			_, ok := volNames[name]
   119  			if !ok {
   120  				continue
   121  			}
   122  			numMatches++
   123  		}
   124  		if len(volNames) != numMatches {
   125  			return fmt.Errorf("expected %d volumes, got: %d", len(volNames), numMatches)
   126  		}
   127  		return nil
   128  	})
   129  
   130  	base.Cmd("volume", "ls", "--quiet", "--filter", "label="+tID).AssertOutWithFunc(func(stdout string) error {
   131  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   132  		if len(lines) < 3 {
   133  			return errors.New("expected at least 3 lines")
   134  		}
   135  		volNames := map[string]struct{}{
   136  			vol1: {},
   137  			vol2: {},
   138  			vol3: {},
   139  		}
   140  
   141  		for _, name := range lines {
   142  			_, ok := volNames[name]
   143  			if !ok {
   144  				return fmt.Errorf("unexpected volume %s found", name)
   145  			}
   146  		}
   147  		return nil
   148  	})
   149  
   150  	base.Cmd("volume", "ls", "--quiet", "--filter", "label="+label2).AssertOutWithFunc(func(stdout string) error {
   151  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   152  		if len(lines) < 1 {
   153  			return errors.New("expected at least 1 lines")
   154  		}
   155  		volNames := map[string]struct{}{
   156  			vol2: {},
   157  		}
   158  
   159  		for _, name := range lines {
   160  			if name == "" {
   161  				continue
   162  			}
   163  			_, ok := volNames[name]
   164  			if !ok {
   165  				return fmt.Errorf("unexpected volume %s found", name)
   166  			}
   167  		}
   168  		return nil
   169  	})
   170  
   171  	base.Cmd("volume", "ls", "--quiet", "--filter", "label="+tID+"=").AssertOutWithFunc(func(stdout string) error {
   172  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   173  		if len(lines) > 0 {
   174  			for _, name := range lines {
   175  				if name != "" {
   176  					return fmt.Errorf("unexpected volumes %d found", len(lines))
   177  				}
   178  			}
   179  		}
   180  		return nil
   181  	})
   182  
   183  	base.Cmd("volume", "ls", "--quiet", "--filter", "label="+label1, "--filter", "label="+label2).AssertOutWithFunc(func(stdout string) error {
   184  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   185  		if len(lines) > 0 {
   186  			for _, name := range lines {
   187  				if name != "" {
   188  					return fmt.Errorf("unexpected volumes %d found", len(lines))
   189  				}
   190  			}
   191  		}
   192  		return nil
   193  	})
   194  
   195  	base.Cmd("volume", "ls", "--quiet", "--filter", "label="+tID, "--filter", "label="+label4).AssertOutWithFunc(func(stdout string) error {
   196  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   197  		if len(lines) < 2 {
   198  			return errors.New("expected at least 2 lines")
   199  		}
   200  		volNames := map[string]struct{}{
   201  			vol1: {},
   202  			vol2: {},
   203  		}
   204  
   205  		for _, name := range lines {
   206  			_, ok := volNames[name]
   207  			if !ok {
   208  				return fmt.Errorf("unexpected volume %s found", name)
   209  			}
   210  		}
   211  		return nil
   212  	})
   213  
   214  	base.Cmd("volume", "ls", "--quiet", "--filter", "name="+vol1).AssertOutWithFunc(func(stdout string) error {
   215  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   216  		if len(lines) < 1 {
   217  			return errors.New("expected at least 1 lines")
   218  		}
   219  		volNames := map[string]struct{}{
   220  			vol1: {},
   221  		}
   222  
   223  		for _, name := range lines {
   224  			_, ok := volNames[name]
   225  			if !ok {
   226  				return fmt.Errorf("unexpected volume %s found", name)
   227  			}
   228  		}
   229  		return nil
   230  	})
   231  
   232  	base.Cmd("volume", "ls", "--quiet", "--filter", "name=vol-3").AssertOutWithFunc(func(stdout string) error {
   233  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   234  		if len(lines) < 1 {
   235  			return errors.New("expected at least 1 lines")
   236  		}
   237  		volNames := map[string]struct{}{
   238  			vol3: {},
   239  		}
   240  
   241  		for _, name := range lines {
   242  			_, ok := volNames[name]
   243  			if !ok {
   244  				return fmt.Errorf("unexpected volume %s found", name)
   245  			}
   246  		}
   247  		return nil
   248  	})
   249  
   250  	base.Cmd("volume", "ls", "--quiet", "--filter", "name=vol2", "--filter", "name=vol1").AssertOutWithFunc(func(stdout string) error {
   251  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   252  		if len(lines) > 0 {
   253  			for _, name := range lines {
   254  				if name != "" {
   255  					return fmt.Errorf("unexpected volumes %d found", len(lines))
   256  				}
   257  			}
   258  		}
   259  		return nil
   260  	})
   261  }
   262  
   263  func TestVolumeLsFilterSize(t *testing.T) {
   264  	base := testutil.NewBase(t)
   265  	tID := testutil.Identifier(t)
   266  	testutil.DockerIncompatible(t)
   267  
   268  	var vol1, vol2, vol3, vol4 = tID + "volsize-1", tID + "volsize-2", tID + "volsize-3", tID + "volsize-4"
   269  	var label1, label2, label3, label4 = tID + "=label-1", tID + "=label-2", tID + "=label-3", tID + "-group=label-4"
   270  	base.Cmd("volume", "create", "--label="+label1, "--label="+label4, vol1).AssertOK()
   271  	defer base.Cmd("volume", "rm", "-f", vol1).Run()
   272  
   273  	base.Cmd("volume", "create", "--label="+label2, "--label="+label4, vol2).AssertOK()
   274  	defer base.Cmd("volume", "rm", "-f", vol2).Run()
   275  
   276  	base.Cmd("volume", "create", "--label="+label3, vol3).AssertOK()
   277  	defer base.Cmd("volume", "rm", "-f", vol3).Run()
   278  
   279  	base.Cmd("volume", "create", vol4).AssertOK()
   280  	defer base.Cmd("volume", "rm", "-f", vol4).Run()
   281  
   282  	createFileWithSize(t, vol1, 409600)
   283  	createFileWithSize(t, vol2, 1024000)
   284  	createFileWithSize(t, vol3, 409600)
   285  	createFileWithSize(t, vol4, 1024000)
   286  
   287  	base.Cmd("volume", "ls", "--size", "--filter", "size=1024000").AssertOutWithFunc(func(stdout string) error {
   288  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   289  		if len(lines) < 3 {
   290  			return errors.New("expected at least 3 lines")
   291  		}
   292  
   293  		var tab = tabutil.NewReader("VOLUME NAME\tDIRECTORY\tSIZE")
   294  		var err = tab.ParseHeader(lines[0])
   295  		if err != nil {
   296  			return err
   297  		}
   298  		volNames := map[string]struct{}{
   299  			vol2: {},
   300  			vol4: {},
   301  		}
   302  
   303  		for _, line := range lines {
   304  			name, _ := tab.ReadRow(line, "VOLUME NAME")
   305  			if name == "VOLUME NAME" {
   306  				continue
   307  			}
   308  			_, ok := volNames[name]
   309  			if !ok {
   310  				return fmt.Errorf("unexpected volume %s found", name)
   311  			}
   312  		}
   313  		return nil
   314  	})
   315  
   316  	base.Cmd("volume", "ls", "--size", "--filter", "size>=1024000", "--filter", "size<=2048000").AssertOutWithFunc(func(stdout string) error {
   317  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   318  		if len(lines) < 3 {
   319  			return errors.New("expected at least 3 lines")
   320  		}
   321  
   322  		var tab = tabutil.NewReader("VOLUME NAME\tDIRECTORY\tSIZE")
   323  		var err = tab.ParseHeader(lines[0])
   324  		if err != nil {
   325  			return err
   326  		}
   327  		volNames := map[string]struct{}{
   328  			vol2: {},
   329  			vol4: {},
   330  		}
   331  
   332  		for _, line := range lines {
   333  			name, _ := tab.ReadRow(line, "VOLUME NAME")
   334  			if name == "VOLUME NAME" {
   335  				continue
   336  			}
   337  			_, ok := volNames[name]
   338  			if !ok {
   339  				return fmt.Errorf("unexpected volume %s found", name)
   340  			}
   341  		}
   342  		return nil
   343  	})
   344  
   345  	base.Cmd("volume", "ls", "--size", "--filter", "size>204800", "--filter", "size<1024000").AssertOutWithFunc(func(stdout string) error {
   346  		var lines = strings.Split(strings.TrimSpace(stdout), "\n")
   347  		if len(lines) < 3 {
   348  			return errors.New("expected at least 3 lines")
   349  		}
   350  
   351  		var tab = tabutil.NewReader("VOLUME NAME\tDIRECTORY\tSIZE")
   352  		var err = tab.ParseHeader(lines[0])
   353  		if err != nil {
   354  			return err
   355  		}
   356  		volNames := map[string]struct{}{
   357  			vol1: {},
   358  			vol3: {},
   359  		}
   360  
   361  		for _, line := range lines {
   362  			name, _ := tab.ReadRow(line, "VOLUME NAME")
   363  			if name == "VOLUME NAME" {
   364  				continue
   365  			}
   366  			_, ok := volNames[name]
   367  			if !ok {
   368  				return fmt.Errorf("unexpected volume %s found", name)
   369  			}
   370  		}
   371  		return nil
   372  	})
   373  }