github.com/release-engineering/exodus-rsync@v1.11.2/internal/cmd/cmd_rsync_exec_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/release-engineering/exodus-rsync/internal/args"
     9  	"github.com/release-engineering/exodus-rsync/internal/conf"
    10  	"github.com/release-engineering/exodus-rsync/internal/rsync"
    11  )
    12  
    13  func TestMainRawExecRsync(t *testing.T) {
    14  	ctrl := MockController(t)
    15  
    16  	mockRsync := rsync.NewMockInterface(ctrl)
    17  	ext.rsync = mockRsync
    18  
    19  	// Since --server is provided we should be able to run with just args, and
    20  	// the command name, i.e., "exodus-rsync", should be trimmed from args
    21  	// before reaching RawExec.
    22  	rawArgs := []string{"exodus-rsync", "--server", ".", "some-dest:/foo/bar"}
    23  	expectedArgv := []string{"--server", ".", "some-dest:/foo/bar"}
    24  
    25  	// We can't actually simulate the 'rsync successful' case because exec would not
    26  	// normally return if the process could be executed, so just force it to return
    27  	// an error.
    28  	rsyncError := fmt.Errorf("simulated error")
    29  
    30  	mockRsync.EXPECT().RawExec(gomock.Any(), expectedArgv).Return(rsyncError)
    31  
    32  	got := Main(rawArgs)
    33  
    34  	if got != 94 {
    35  		t.Error("returned incorrect exit code", got)
    36  	}
    37  }
    38  
    39  func TestMainExecRsync(t *testing.T) {
    40  	ctrl := MockController(t)
    41  
    42  	mockConf := conf.NewMockInterface(ctrl)
    43  	mockRsync := rsync.NewMockInterface(ctrl)
    44  	ext.conf = mockConf
    45  	ext.rsync = mockRsync
    46  
    47  	emptyConfig := conf.NewMockGlobalConfig(ctrl)
    48  
    49  	// Make it return an empty Config with no environments
    50  	mockConf.EXPECT().Load(gomock.Any(), gomock.Any()).Return(emptyConfig, nil)
    51  	emptyConfig.EXPECT().EnvironmentForDest(gomock.Any(), gomock.Any()).Return(nil)
    52  	emptyConfig.EXPECT().LogLevel().AnyTimes().Return("info")
    53  	emptyConfig.EXPECT().Logger().AnyTimes().Return("auto")
    54  	emptyConfig.EXPECT().Diag().AnyTimes().Return(false)
    55  
    56  	// Since no environment matches, we expect it to run rsync and it should pass
    57  	// through whatever arguments we're giving it.
    58  	args := args.Config{}
    59  	args.Recursive = true
    60  	args.Timeout = 1234
    61  	args.Src = "."
    62  	args.Dest = "some-dest:/foo/bar"
    63  
    64  	// We can't actually simulate the 'rsync successful' case because exec would not
    65  	// normally return if the process could be executed, so just force it to return
    66  	// an error.
    67  	rsyncError := fmt.Errorf("simulated error")
    68  
    69  	mockRsync.EXPECT().Exec(gomock.Any(), args).Return(rsyncError)
    70  
    71  	got := Main([]string{
    72  		"exodus-rsync", "--recursive", "--timeout", "1234",
    73  		".", "some-dest:/foo/bar",
    74  	})
    75  
    76  	if got != 94 {
    77  		t.Error("returned incorrect exit code", got)
    78  	}
    79  }