github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/image/save_test.go (about)

     1  package image
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/khulnasoft/cli/internal/test"
    10  	"github.com/pkg/errors"
    11  	"gotest.tools/v3/assert"
    12  	is "gotest.tools/v3/assert/cmp"
    13  )
    14  
    15  func TestNewSaveCommandErrors(t *testing.T) {
    16  	testCases := []struct {
    17  		name          string
    18  		args          []string
    19  		isTerminal    bool
    20  		expectedError string
    21  		imageSaveFunc func(images []string) (io.ReadCloser, error)
    22  	}{
    23  		{
    24  			name:          "wrong args",
    25  			args:          []string{},
    26  			expectedError: "requires at least 1 argument.",
    27  		},
    28  		{
    29  			name:          "output to terminal",
    30  			args:          []string{"output", "file", "arg1"},
    31  			isTerminal:    true,
    32  			expectedError: "cowardly refusing to save to a terminal. Use the -o flag or redirect",
    33  		},
    34  		{
    35  			name:          "ImageSave fail",
    36  			args:          []string{"arg1"},
    37  			isTerminal:    false,
    38  			expectedError: "error saving image",
    39  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    40  				return io.NopCloser(strings.NewReader("")), errors.Errorf("error saving image")
    41  			},
    42  		},
    43  		{
    44  			name:          "output directory does not exist",
    45  			args:          []string{"-o", "fakedir/out.tar", "arg1"},
    46  			expectedError: "failed to save image: invalid output path: directory \"fakedir\" does not exist",
    47  		},
    48  		{
    49  			name:          "output file is irregular",
    50  			args:          []string{"-o", "/dev/null", "arg1"},
    51  			expectedError: "failed to save image: invalid output path: \"/dev/null\" must be a directory or a regular file",
    52  		},
    53  	}
    54  	for _, tc := range testCases {
    55  		cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc})
    56  		cli.Out().SetIsTerminal(tc.isTerminal)
    57  		cmd := NewSaveCommand(cli)
    58  		cmd.SetOut(io.Discard)
    59  		cmd.SetArgs(tc.args)
    60  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    61  	}
    62  }
    63  
    64  func TestNewSaveCommandSuccess(t *testing.T) {
    65  	testCases := []struct {
    66  		args          []string
    67  		isTerminal    bool
    68  		imageSaveFunc func(images []string) (io.ReadCloser, error)
    69  		deferredFunc  func()
    70  	}{
    71  		{
    72  			args:       []string{"-o", "save_tmp_file", "arg1"},
    73  			isTerminal: true,
    74  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    75  				assert.Assert(t, is.Len(images, 1))
    76  				assert.Check(t, is.Equal("arg1", images[0]))
    77  				return io.NopCloser(strings.NewReader("")), nil
    78  			},
    79  			deferredFunc: func() {
    80  				os.Remove("save_tmp_file")
    81  			},
    82  		},
    83  		{
    84  			args:       []string{"arg1", "arg2"},
    85  			isTerminal: false,
    86  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    87  				assert.Assert(t, is.Len(images, 2))
    88  				assert.Check(t, is.Equal("arg1", images[0]))
    89  				assert.Check(t, is.Equal("arg2", images[1]))
    90  				return io.NopCloser(strings.NewReader("")), nil
    91  			},
    92  		},
    93  	}
    94  	for _, tc := range testCases {
    95  		cmd := NewSaveCommand(test.NewFakeCli(&fakeClient{
    96  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    97  				return io.NopCloser(strings.NewReader("")), nil
    98  			},
    99  		}))
   100  		cmd.SetOut(io.Discard)
   101  		cmd.SetArgs(tc.args)
   102  		assert.NilError(t, cmd.Execute())
   103  		if tc.deferredFunc != nil {
   104  			tc.deferredFunc()
   105  		}
   106  	}
   107  }