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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/golang/mock/gomock"
    10  	"github.com/release-engineering/exodus-rsync/internal/gw"
    11  )
    12  
    13  func TestMainSyncIgnoreExisting(t *testing.T) {
    14  	SetConfig(t, CONFIG)
    15  
    16  	logs := CaptureLogger(t)
    17  
    18  	ctrl := MockController(t)
    19  
    20  	mockGw := gw.NewMockInterface(ctrl)
    21  	ext.gw = mockGw
    22  
    23  	client := FakeClient{blobs: make(map[string]string)}
    24  
    25  	t.Run("ignore-existing OK if no files", func(t *testing.T) {
    26  		os.Mkdir("nofiles", 0755)
    27  		os.Mkdir("nofiles/subdir", 0755)
    28  
    29  		args := []string{"rsync", "--ignore-existing", "nofiles", "exodus:/some/target"}
    30  
    31  		mockGw.EXPECT().NewClient(gomock.Any(), EnvMatcher{"best-env"}).Return(&client, nil)
    32  		got := Main(args)
    33  
    34  		// It should succeed, though not actually do anything.
    35  		if got != 0 {
    36  			t.Errorf("unexpectedly failed with code = %v", got)
    37  		}
    38  	})
    39  
    40  	t.Run("ignore-existing fails if files exist", func(t *testing.T) {
    41  		os.Mkdir("files", 0755)
    42  		os.WriteFile("files/file1", []byte("hello"), 0644)
    43  
    44  		args := []string{"rsync", "--ignore-existing", "files", "exodus:/some/target"}
    45  
    46  		mockGw.EXPECT().NewClient(gomock.Any(), EnvMatcher{"best-env"}).Return(&client, nil)
    47  		got := Main(args)
    48  
    49  		// It should fail
    50  		if got != 73 {
    51  			t.Errorf("got unexpected exit code = %v", got)
    52  		}
    53  
    54  		// It should tell us why
    55  		entry := FindEntry(logs, "can't read files for sync")
    56  		if entry == nil {
    57  			t.Error("missing expected log message")
    58  		}
    59  
    60  		err := fmt.Sprint(entry.Fields["error"])
    61  		if !strings.Contains(err, "--ignore-existing is not supported") {
    62  			t.Error("unexpected error message", err)
    63  		}
    64  	})
    65  
    66  }