github.com/hashicorp/go-getter/v2@v2.2.2/get_smbclient_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  	urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
    11  )
    12  
    13  // smbTestsPreCheck checks whether ACC_SMB_TEST is set before running any SMB tests.
    14  // SMB tests depends on a SMB server and should not run without the intention of it.
    15  func smbTestsPreCheck(t *testing.T) {
    16  	r := os.Getenv("ACC_SMB_TEST")
    17  	if r != "1" {
    18  		t.Skip("smb getter tests won't run. ACC_SMB_TEST not set")
    19  	}
    20  }
    21  
    22  func TestSmb_GetterImpl(t *testing.T) {
    23  	var _ Getter = new(SmbClientGetter)
    24  }
    25  
    26  func TestSmb_GetterGet(t *testing.T) {
    27  	smbTestsPreCheck(t)
    28  
    29  	tests := []struct {
    30  		name   string
    31  		rawURL string
    32  		file   string
    33  		fail   bool
    34  	}{
    35  		{
    36  			"smbclient with registered authentication in private share",
    37  			"smb://user:password@samba/private/subdir",
    38  			"file.txt",
    39  			false,
    40  		},
    41  		{
    42  			"smbclient with registered authentication with file in private share",
    43  			"smb://user:password@samba/private/subdir/file.txt",
    44  			"file.txt",
    45  			true,
    46  		},
    47  		{
    48  			"smbclient with only registered username authentication in private share",
    49  			"smb://user@samba/private/subdir",
    50  			"file.txt",
    51  			true,
    52  		},
    53  		{
    54  			"smbclient with non registered username authentication in public share",
    55  			"smb://username@samba/public/subdir",
    56  			"file.txt",
    57  			false,
    58  		},
    59  		{
    60  			"smbclient without authentication in private share",
    61  			"smb://samba/private/subdir",
    62  			"file.txt",
    63  			true,
    64  		},
    65  		{
    66  			"smbclient without authentication in public share",
    67  			"smb://samba/public/subdir",
    68  			"file.txt",
    69  			false,
    70  		},
    71  		{
    72  			"non existent directory in private share",
    73  			"smb://user:password@samba/private/invalid",
    74  			"",
    75  			true,
    76  		},
    77  		{
    78  			"non existent directory in public share",
    79  			"smb://samba/public/invalid",
    80  			"",
    81  			true,
    82  		},
    83  		{
    84  			"no hostname provided",
    85  			"smb://",
    86  			"",
    87  			true,
    88  		},
    89  		{
    90  			"no filepath provided",
    91  			"smb://samba",
    92  			"",
    93  			true,
    94  		},
    95  	}
    96  
    97  	for _, tt := range tests {
    98  		t.Run(tt.name, func(t *testing.T) {
    99  			dst := testing_helper.TempDir(t)
   100  			defer os.RemoveAll(dst)
   101  
   102  			url, err := urlhelper.Parse(tt.rawURL)
   103  			if err != nil {
   104  				t.Fatalf("err: %s", err.Error())
   105  			}
   106  			req := &Request{
   107  				Dst: dst,
   108  				u:   url,
   109  			}
   110  
   111  			g := new(SmbClientGetter)
   112  			err = g.Get(context.Background(), req)
   113  
   114  			fail := err != nil
   115  			if tt.fail != fail {
   116  				if fail {
   117  					t.Fatalf("err: unexpected error %s", err.Error())
   118  				}
   119  				t.Fatalf("err: expecting to fail but it did not")
   120  			}
   121  
   122  			if !tt.fail {
   123  				// Verify if the file was successfully downloaded
   124  				// and exists at the destination folder
   125  				testing_helper.AssertContents(t, filepath.Join(dst, tt.file), "Hello\n")
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func TestSmb_GetterGetFile(t *testing.T) {
   132  	smbTestsPreCheck(t)
   133  
   134  	tests := []struct {
   135  		name   string
   136  		rawURL string
   137  		file   string
   138  		fail   bool
   139  	}{
   140  		{
   141  			"smbclient with registered authentication in private share",
   142  			"smb://user:password@samba/private/file.txt",
   143  			"file.txt",
   144  			false,
   145  		},
   146  		{
   147  			"smbclient with registered authentication and subdirectory in private share",
   148  			"smb://user:password@samba/private/subdir/file.txt",
   149  			"file.txt",
   150  			false,
   151  		},
   152  		{
   153  			"smbclient with only registered username authentication in private share",
   154  			"smb://user@samba/private/file.txt",
   155  			"file.txt",
   156  			true,
   157  		},
   158  		{
   159  			"smbclient with non registered username authentication in public share",
   160  			"smb://username@samba/public/file.txt",
   161  			"file.txt",
   162  			false,
   163  		},
   164  		{
   165  			"smbclient without authentication in public share",
   166  			"smb://samba/public/file.txt",
   167  			"file.txt",
   168  			false,
   169  		},
   170  		{
   171  			"smbclient without authentication in private share",
   172  			"smb://samba/private/file.txt",
   173  			"file.txt",
   174  			true,
   175  		},
   176  		{
   177  			"smbclient get directory in private share",
   178  			"smb://user:password@samba/private/subdir",
   179  			"",
   180  			true,
   181  		},
   182  		{
   183  			"smbclient get directory in public share",
   184  			"smb://samba/public/subdir",
   185  			"",
   186  			true,
   187  		},
   188  		{
   189  			"non existent file in private share",
   190  			"smb://user:password@samba/private/invalidfile.txt",
   191  			"",
   192  			true,
   193  		},
   194  		{
   195  			"non existent file in public share",
   196  			"smb://samba/public/invalidfile.txt",
   197  			"",
   198  			true,
   199  		},
   200  		{
   201  			"no hostname provided",
   202  			"smb://",
   203  			"",
   204  			true,
   205  		},
   206  		{
   207  			"no filepath provided",
   208  			"smb://samba",
   209  			"",
   210  			true,
   211  		},
   212  	}
   213  
   214  	for _, tt := range tests {
   215  		t.Run(tt.name, func(t *testing.T) {
   216  			dst := testing_helper.TempDir(t)
   217  			defer os.RemoveAll(dst)
   218  
   219  			url, err := urlhelper.Parse(tt.rawURL)
   220  			if err != nil {
   221  				t.Fatalf("err: %s", err.Error())
   222  			}
   223  			req := &Request{
   224  				Dst: filepath.Join(dst, tt.file),
   225  				u:   url,
   226  			}
   227  
   228  			g := new(SmbClientGetter)
   229  			err = g.GetFile(context.Background(), req)
   230  
   231  			fail := err != nil
   232  			if tt.fail != fail {
   233  				if fail {
   234  					t.Fatalf("err: unexpected error %s", err.Error())
   235  				}
   236  				t.Fatalf("err: expecting to fail but it did not")
   237  			}
   238  
   239  			if !tt.fail {
   240  				// Verify if the file was successfully downloaded
   241  				// and exists at the destination folder
   242  				testing_helper.AssertContents(t, filepath.Join(dst, tt.file), "Hello\n")
   243  			}
   244  		})
   245  	}
   246  }
   247  
   248  func TestSmb_GetterMode(t *testing.T) {
   249  	smbTestsPreCheck(t)
   250  
   251  	tests := []struct {
   252  		name         string
   253  		rawURL       string
   254  		expectedMode Mode
   255  		fail         bool
   256  	}{
   257  		{
   258  			"smbclient modefile for existing file in authenticated private share",
   259  			"smb://user:password@samba/private/file.txt",
   260  			ModeFile,
   261  			false,
   262  		},
   263  		{
   264  			"smbclient modedir for existing directory in authenticated private share",
   265  			"smb://user:password@samba/private/subdir",
   266  			ModeDir,
   267  			false,
   268  		},
   269  		{
   270  			"mode fail for non existent directory in authenticated private share",
   271  			"smb://user:password@samba/private/invaliddir",
   272  			0,
   273  			true,
   274  		},
   275  		{
   276  			"mode fail for non existent file in authenticated private share",
   277  			"smb://user:password@samba/private/invalidfile.txt",
   278  			0,
   279  			true,
   280  		},
   281  		{
   282  			"smbclient modefile for existing file in public share",
   283  			"smb://samba/public/file.txt",
   284  			ModeFile,
   285  			false,
   286  		},
   287  		{
   288  			"smbclient modedir for existing directory in public share",
   289  			"smb://samba/public/subdir",
   290  			ModeDir,
   291  			false,
   292  		},
   293  		{
   294  			"mode fail for non existent directory in public share",
   295  			"smb://samba/public/invaliddir",
   296  			0,
   297  			true,
   298  		},
   299  		{
   300  			"mode fail for non existent file in public share",
   301  			"smb://samba/public/invalidfile.txt",
   302  			0,
   303  			true,
   304  		},
   305  	}
   306  
   307  	for _, tt := range tests {
   308  		t.Run(tt.name, func(t *testing.T) {
   309  			url, err := urlhelper.Parse(tt.rawURL)
   310  			if err != nil {
   311  				t.Fatalf("err: %s", err.Error())
   312  			}
   313  
   314  			g := new(SmbClientGetter)
   315  			mode, err := g.Mode(context.Background(), url)
   316  
   317  			fail := err != nil
   318  			if tt.fail != fail {
   319  				if fail {
   320  					t.Fatalf("err: unexpected error %s", err.Error())
   321  				}
   322  				t.Fatalf("err: expecting to fail but it did not")
   323  			}
   324  
   325  			if mode != tt.expectedMode {
   326  				t.Fatalf("err: expeting mode %d, actual mode %d", tt.expectedMode, mode)
   327  			}
   328  		})
   329  	}
   330  }