github.com/hashicorp/go-getter/v2@v2.2.2/client_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	testing_helper "github.com/hashicorp/go-getter/v2/helper/testing"
    10  )
    11  
    12  func TestSmb_ClientGet(t *testing.T) {
    13  	smbTestsPreCheck(t)
    14  
    15  	tests := []struct {
    16  		name   string
    17  		rawURL string
    18  		mode   Mode
    19  		file   string
    20  		fail   bool
    21  	}{
    22  		{
    23  			"smb scheme subdir with registered authentication in private share",
    24  			"smb://user:password@samba/private/subdir",
    25  			ModeDir,
    26  			"file.txt",
    27  			false,
    28  		},
    29  		{
    30  			"smb scheme file with registered authentication with file in private share",
    31  			"smb://user:password@samba/private/subdir/file.txt",
    32  			ModeFile,
    33  			"file.txt",
    34  			false,
    35  		},
    36  		{
    37  			"smb scheme file without authentication in public share",
    38  			"smb://samba/public/subdir/file.txt",
    39  			ModeFile,
    40  			"file.txt",
    41  			false,
    42  		},
    43  	}
    44  
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			dst := testing_helper.TempDir(t)
    48  			defer os.RemoveAll(dst)
    49  
    50  			if tt.mode == ModeFile {
    51  				dst = filepath.Join(dst, tt.file)
    52  			}
    53  
    54  			req := &Request{
    55  				Dst:     dst,
    56  				Src:     tt.rawURL,
    57  				GetMode: tt.mode,
    58  			}
    59  
    60  			result, err := DefaultClient.Get(context.Background(), req)
    61  
    62  			fail := err != nil
    63  			if tt.fail != fail {
    64  				if fail {
    65  					t.Fatalf("err: unexpected error %s", err.Error())
    66  				}
    67  				t.Fatalf("err: expecting to fail but it did not")
    68  			}
    69  
    70  			if !tt.fail {
    71  				if result == nil {
    72  					t.Fatalf("err: get result should not be nil")
    73  				}
    74  				if result.Dst != dst {
    75  					t.Fatalf("err: expected destination: %s \n actual destination: %s", dst, result.Dst)
    76  				}
    77  				if tt.mode == ModeDir {
    78  					dst = filepath.Join(dst, tt.file)
    79  				}
    80  				// Verify if the file was successfully downloaded
    81  				// and exists at the destination folder
    82  				testing_helper.AssertContents(t, dst, "Hello\n")
    83  			}
    84  		})
    85  	}
    86  }