...

Source file src/github.com/redhat-developer/odo/pkg/sync/copy_test.go

Documentation: github.com/redhat-developer/odo/pkg/sync

     1  package sync
     2  
     3  import (
     4  	taro "archive/tar"
     5  	"bytes"
     6  	"io"
     7  	"path"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
    12  	"github.com/redhat-developer/odo/pkg/util"
    13  )
    14  
    15  func Test_linearTar(t *testing.T) {
    16  	// FileType custom type to indicate type of file
    17  	type FileType int
    18  
    19  	const (
    20  		// RegularFile enum to represent regular file
    21  		RegularFile FileType = 0
    22  		// Directory enum to represent directory
    23  		Directory FileType = 1
    24  	)
    25  
    26  	fs := filesystem.NewFakeFs()
    27  
    28  	type args struct {
    29  		srcBase  string
    30  		srcFile  string
    31  		destBase string
    32  		destFile string
    33  		data     string
    34  	}
    35  	tests := []struct {
    36  		name          string
    37  		args          args
    38  		fileType      FileType
    39  		notExistError bool
    40  		wantErr       bool
    41  	}{
    42  		{
    43  			name: "case 1: write a regular file",
    44  			args: args{
    45  				srcBase:  filepath.Join("tmp", "dir1"),
    46  				srcFile:  "red.js",
    47  				destBase: filepath.Join("tmp1", "dir2"),
    48  				destFile: "red.js",
    49  				data:     "hi",
    50  			},
    51  			fileType: RegularFile,
    52  			wantErr:  false,
    53  		},
    54  		{
    55  			name: "case 2: write a folder",
    56  			args: args{
    57  				srcBase:  filepath.Join("tmp", "dir1"),
    58  				srcFile:  "dir0",
    59  				destBase: filepath.Join("tmp1", "dir2"),
    60  				destFile: "dir2",
    61  			},
    62  			fileType: Directory,
    63  			wantErr:  false,
    64  		},
    65  		{
    66  			name: "case 3: file source doesn't exist",
    67  			args: args{
    68  				srcBase:  filepath.Join("tmp", "dir1"),
    69  				srcFile:  "red.js",
    70  				destBase: filepath.Join("tmp1", "dir2"),
    71  				destFile: "red.js",
    72  				data:     "hi",
    73  			},
    74  			fileType:      RegularFile,
    75  			notExistError: true,
    76  			wantErr:       true,
    77  		},
    78  		{
    79  			name: "case 4: folder source doesn't exist",
    80  			args: args{
    81  				srcBase:  filepath.Join("tmp", "dir1"),
    82  				srcFile:  "dir0",
    83  				destBase: filepath.Join("tmp1", "dir2"),
    84  				destFile: "dir2",
    85  			},
    86  			fileType:      Directory,
    87  			notExistError: true,
    88  			wantErr:       true,
    89  		},
    90  		{
    91  			name: "case 5: dest is empty",
    92  			args: args{
    93  				srcBase:  filepath.Join("tmp", "dir1"),
    94  				srcFile:  "dir0",
    95  				destBase: "",
    96  				destFile: "",
    97  			},
    98  			fileType: Directory,
    99  			wantErr:  true,
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			filepath := path.Join(tt.args.srcBase, tt.args.srcFile)
   105  
   106  			if tt.fileType == RegularFile {
   107  				f, err := fs.Create(filepath)
   108  				if err != nil {
   109  					t.Fatalf("unexpected error: %v", err)
   110  				}
   111  				if _, err := io.Copy(f, bytes.NewBuffer([]byte(tt.args.data))); err != nil {
   112  					t.Fatalf("unexpected error: %v", err)
   113  				}
   114  				defer f.Close()
   115  			} else {
   116  				if err := fs.MkdirAll(filepath, 0755); err != nil {
   117  					t.Errorf("unexpected error: %v", err)
   118  				}
   119  			}
   120  
   121  			if tt.notExistError == true {
   122  				tt.args.srcBase += "blah"
   123  			}
   124  
   125  			reader, writer := io.Pipe()
   126  			defer reader.Close()
   127  			defer writer.Close()
   128  
   129  			tarWriter := taro.NewWriter(writer)
   130  
   131  			go func() {
   132  				defer tarWriter.Close()
   133  				if err := linearTar(tt.args.srcBase, tt.args.srcFile, tt.args.destBase, tt.args.destFile, tarWriter, fs); (err != nil) != tt.wantErr {
   134  					t.Errorf("linearTar() error = %v, wantErr %v", err, tt.wantErr)
   135  				}
   136  			}()
   137  
   138  			tarReader := taro.NewReader(reader)
   139  			for {
   140  				hdr, err := tarReader.Next()
   141  				if err == io.EOF {
   142  					break
   143  				} else if err != nil {
   144  					t.Errorf("unexpected error: %v", err)
   145  				}
   146  
   147  				if hdr.Name != tt.args.destFile {
   148  					t.Errorf("expected %q as destination filename, saw: %q", tt.args.destFile, hdr.Name)
   149  				}
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func Test_makeTar(t *testing.T) {
   156  	fs := filesystem.NewFakeFs()
   157  
   158  	dir0, err := fs.TempDir("", "dir0")
   159  	if err != nil {
   160  		t.Errorf("unexpected error: %v", err)
   161  	}
   162  
   163  	_, err = fs.Create(filepath.Join(dir0, "red.js"))
   164  	if err != nil {
   165  		t.Errorf("unexpected error: %v", err)
   166  	}
   167  	_, err = fs.Create(filepath.Join(dir0, "README.txt"))
   168  	if err != nil {
   169  		t.Errorf("unexpected error: %v", err)
   170  	}
   171  	err = fs.MkdirAll(filepath.Join(dir0, "views"), 0644)
   172  	if err != nil {
   173  		t.Errorf("unexpected error: %v", err)
   174  	}
   175  	_, err = fs.Create(filepath.Join(dir0, "views", "view.html"))
   176  	if err != nil {
   177  		t.Errorf("unexpected error: %v", err)
   178  	}
   179  
   180  	type args struct {
   181  		srcPath  string
   182  		destPath string
   183  		files    []string
   184  		globExps []string
   185  		ret      util.IndexerRet
   186  	}
   187  	tests := []struct {
   188  		name      string
   189  		args      args
   190  		wantFiles map[string]bool
   191  		wantErr   bool
   192  	}{
   193  		{
   194  			name: "case 1: normal tar making",
   195  			args: args{
   196  				srcPath:  dir0,
   197  				destPath: filepath.Join("tmp", "dir1"),
   198  				files: []string{
   199  					filepath.Join(dir0, "red.js"),
   200  					filepath.Join(dir0, "README.txt"),
   201  					filepath.Join(dir0, "views"),
   202  					filepath.Join(dir0, "views", "view.html")},
   203  				globExps: []string{},
   204  				ret: util.IndexerRet{
   205  					NewFileMap: map[string]util.FileData{
   206  						"red.js": {
   207  							RemoteAttribute: "red.js",
   208  						},
   209  						"README.txt": {
   210  							RemoteAttribute: "README.txt",
   211  						},
   212  						"views": {
   213  							RemoteAttribute: "views",
   214  						},
   215  						filepath.Join("views", "view.html"): {
   216  							RemoteAttribute: "views/view.html",
   217  						},
   218  					},
   219  				},
   220  			},
   221  			wantFiles: map[string]bool{
   222  				"red.js":          true,
   223  				"views/view.html": true,
   224  				"README.txt":      true,
   225  			},
   226  		},
   227  		{
   228  			name: "case 2: normal tar making with matching glob expression",
   229  			args: args{
   230  				srcPath:  dir0,
   231  				destPath: filepath.Join("tmp", "dir1"),
   232  				files: []string{
   233  					filepath.Join(dir0, "red.js"),
   234  					filepath.Join(dir0, "README.txt"),
   235  					filepath.Join(dir0, "views"),
   236  					filepath.Join(dir0, "views", "view.html")},
   237  				globExps: []string{"README.txt"},
   238  				ret: util.IndexerRet{
   239  					NewFileMap: map[string]util.FileData{
   240  						"red.js": {
   241  							RemoteAttribute: "red.js",
   242  						},
   243  						"README.txt": {
   244  							RemoteAttribute: "README.txt",
   245  						},
   246  						"views": {
   247  							RemoteAttribute: "views",
   248  						},
   249  						filepath.Join("views", "view.html"): {
   250  							RemoteAttribute: "views/view.html",
   251  						},
   252  					},
   253  				},
   254  			},
   255  			wantFiles: map[string]bool{
   256  				"red.js":          true,
   257  				"views/view.html": true,
   258  			},
   259  		},
   260  		{
   261  			name: "case 3: normal tar making different remote than local",
   262  			args: args{
   263  				srcPath:  dir0,
   264  				destPath: filepath.Join("tmp", "dir1"),
   265  				files: []string{
   266  					filepath.Join(dir0, "red.js"),
   267  					filepath.Join(dir0, "README.txt"),
   268  					filepath.Join(dir0, "views"),
   269  					filepath.Join(dir0, "views", "view.html")},
   270  				globExps: []string{},
   271  				ret: util.IndexerRet{
   272  					NewFileMap: map[string]util.FileData{
   273  						"red.js": {
   274  							RemoteAttribute: "red.js",
   275  						},
   276  						"README.txt": {
   277  							RemoteAttribute: "text/README.txt",
   278  						},
   279  						"views": {
   280  							RemoteAttribute: "views",
   281  						},
   282  						filepath.Join("views", "view.html"): {
   283  							RemoteAttribute: "views/view.html",
   284  						},
   285  					},
   286  				},
   287  			},
   288  			wantFiles: map[string]bool{
   289  				"red.js":          true,
   290  				"views/view.html": true,
   291  				"text/README.txt": true,
   292  			},
   293  		},
   294  		{
   295  			name: "case 4: ignore no existent file or folder",
   296  			args: args{
   297  				srcPath:  dir0,
   298  				destPath: filepath.Join("tmp", "dir1"),
   299  				files: []string{
   300  					filepath.Join(dir0, "red.js"),
   301  					filepath.Join(dir0, "README.txt"),
   302  					filepath.Join("blah", "views"),
   303  					filepath.Join(dir0, "views", "view.html")},
   304  				globExps: []string{},
   305  				ret: util.IndexerRet{
   306  					NewFileMap: map[string]util.FileData{
   307  						"red.js": {
   308  							RemoteAttribute: "red.js",
   309  						},
   310  						"README.txt": {
   311  							RemoteAttribute: "text/README.txt",
   312  						},
   313  						"views": {
   314  							RemoteAttribute: "views",
   315  						},
   316  						filepath.Join("views", "view.html"): {
   317  							RemoteAttribute: "views/view.html",
   318  						},
   319  					},
   320  				},
   321  			},
   322  			wantFiles: map[string]bool{
   323  				"red.js":          true,
   324  				"views/view.html": true,
   325  				"text/README.txt": true,
   326  			},
   327  		},
   328  	}
   329  	for _, tt := range tests {
   330  		t.Run(tt.name, func(t *testing.T) {
   331  			reader, writer := io.Pipe()
   332  			defer reader.Close()
   333  			defer writer.Close()
   334  
   335  			tarWriter := taro.NewWriter(writer)
   336  			go func() {
   337  				defer tarWriter.Close()
   338  				wantErr := tt.wantErr
   339  				if err := makeTar(tt.args.srcPath, tt.args.destPath, writer, tt.args.files, tt.args.globExps, tt.args.ret, fs); (err != nil) != wantErr {
   340  					t.Errorf("makeTar() error = %v, wantErr %v", err, tt.wantErr)
   341  					return
   342  				}
   343  			}()
   344  
   345  			gotFiles := make(map[string]bool)
   346  			tarReader := taro.NewReader(reader)
   347  			for {
   348  				hdr, err := tarReader.Next()
   349  				if err == io.EOF {
   350  					break
   351  				} else if err != nil {
   352  					t.Errorf("unexpected error: %v", err)
   353  				}
   354  
   355  				if _, ok := tt.wantFiles[hdr.Name]; !ok {
   356  					t.Errorf("unexpected file name in tar, : %q", hdr.Name)
   357  				}
   358  
   359  				gotFiles[hdr.Name] = true
   360  			}
   361  
   362  			for fileName := range tt.wantFiles {
   363  				if _, ok := gotFiles[fileName]; !ok {
   364  					t.Errorf("missed file, : %q", fileName)
   365  				}
   366  			}
   367  		})
   368  	}
   369  }
   370  

View as plain text