...

Source file src/github.com/redhat-developer/odo/pkg/init/init_test.go

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

     1  package init
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     9  	"github.com/devfile/registry-support/registry-library/library"
    10  	"github.com/golang/mock/gomock"
    11  
    12  	"github.com/redhat-developer/odo/pkg/api"
    13  	"github.com/redhat-developer/odo/pkg/config"
    14  	envcontext "github.com/redhat-developer/odo/pkg/config/context"
    15  	"github.com/redhat-developer/odo/pkg/preference"
    16  	"github.com/redhat-developer/odo/pkg/registry"
    17  	"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
    18  )
    19  
    20  func TestInitClient_downloadFromRegistry(t *testing.T) {
    21  	type fields struct {
    22  		preferenceClient func(ctrl *gomock.Controller) preference.Client
    23  		registryClient   func(ctrl *gomock.Controller) registry.Client
    24  	}
    25  	type args struct {
    26  		registryName string
    27  		devfile      string
    28  		dest         string
    29  		archs        []string
    30  	}
    31  	tests := []struct {
    32  		name    string
    33  		fields  fields
    34  		args    args
    35  		wantErr bool
    36  	}{
    37  		{
    38  			name: "Download devfile from one specific Registry where devfile is present",
    39  			fields: fields{
    40  				preferenceClient: func(ctrl *gomock.Controller) preference.Client {
    41  					client := preference.NewMockClient(ctrl)
    42  					return client
    43  				},
    44  				registryClient: func(ctrl *gomock.Controller) registry.Client {
    45  					client := registry.NewMockClient(ctrl)
    46  					registryList := []api.Registry{
    47  						{
    48  							Name: "Registry0",
    49  							URL:  "http://registry0",
    50  						},
    51  						{
    52  							Name: "Registry1",
    53  							URL:  "http://registry1",
    54  						},
    55  					}
    56  					client.EXPECT().GetDevfileRegistries(gomock.Eq("Registry1")).Return(registryList, nil).Times(1)
    57  					client.EXPECT().PullStackFromRegistry("http://registry1", "java", gomock.Any(), library.RegistryOptions{
    58  						Telemetry: library.TelemetryData{
    59  							Client: "odo",
    60  						},
    61  						NewIndexSchema: true,
    62  					}).Return(nil).Times(1)
    63  					return client
    64  				},
    65  			},
    66  			args: args{
    67  				registryName: "Registry1",
    68  				devfile:      "java",
    69  				dest:         ".",
    70  			},
    71  			wantErr: false,
    72  		},
    73  		{
    74  			name: "Download devfile from one specific Registry where devfile is present and arch is passed",
    75  			fields: fields{
    76  				preferenceClient: func(ctrl *gomock.Controller) preference.Client {
    77  					client := preference.NewMockClient(ctrl)
    78  					return client
    79  				},
    80  				registryClient: func(ctrl *gomock.Controller) registry.Client {
    81  					client := registry.NewMockClient(ctrl)
    82  					registryList := []api.Registry{
    83  						{
    84  							Name: "Registry0",
    85  							URL:  "http://registry0",
    86  						},
    87  						{
    88  							Name: "Registry1",
    89  							URL:  "http://registry1",
    90  						},
    91  					}
    92  					client.EXPECT().GetDevfileRegistries(gomock.Eq("Registry1")).Return(registryList, nil).Times(1)
    93  					client.EXPECT().PullStackFromRegistry("http://registry1", "java", gomock.Any(), library.RegistryOptions{
    94  						Telemetry: library.TelemetryData{
    95  							Client: "odo",
    96  						},
    97  						Filter: library.RegistryFilter{
    98  							Architectures: []string{"arm64"},
    99  						},
   100  						NewIndexSchema: true,
   101  					}).Return(nil).Times(1)
   102  					return client
   103  				},
   104  			},
   105  			args: args{
   106  				registryName: "Registry1",
   107  				devfile:      "java",
   108  				dest:         ".",
   109  				archs:        []string{"arm64"},
   110  			},
   111  			wantErr: false,
   112  		},
   113  		{
   114  			name: "Fail to download devfile from one specific Registry where devfile is absent",
   115  			fields: fields{
   116  				preferenceClient: func(ctrl *gomock.Controller) preference.Client {
   117  					client := preference.NewMockClient(ctrl)
   118  					return client
   119  				},
   120  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   121  					client := registry.NewMockClient(ctrl)
   122  					registryList := []api.Registry{
   123  						{
   124  							Name: "Registry0",
   125  							URL:  "http://registry0",
   126  						},
   127  						{
   128  							Name: "Registry1",
   129  							URL:  "http://registry1",
   130  						},
   131  					}
   132  					client.EXPECT().GetDevfileRegistries(gomock.Eq("Registry1")).Return(registryList, nil).Times(1)
   133  					client.EXPECT().PullStackFromRegistry("http://registry1", "java", gomock.Any(), gomock.Any()).Return(errors.New("")).Times(1)
   134  					return client
   135  				},
   136  			},
   137  			args: args{
   138  				registryName: "Registry1",
   139  				devfile:      "java",
   140  				dest:         ".",
   141  			},
   142  			wantErr: true,
   143  		},
   144  		{
   145  			name: "Download devfile from all registries where devfile is present in second registry",
   146  			fields: fields{
   147  				preferenceClient: func(ctrl *gomock.Controller) preference.Client {
   148  					return preference.NewMockClient(ctrl)
   149  				},
   150  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   151  					client := registry.NewMockClient(ctrl)
   152  					registryList := []api.Registry{
   153  						{
   154  							Name: "Registry0",
   155  							URL:  "http://registry0",
   156  						},
   157  						{
   158  							Name: "Registry1",
   159  							URL:  "http://registry1",
   160  						},
   161  					}
   162  					client.EXPECT().GetDevfileRegistries(gomock.Eq("")).Return(registryList, nil).Times(1)
   163  					client.EXPECT().PullStackFromRegistry("http://registry0", "java", gomock.Any(), gomock.Any()).Return(errors.New("")).Times(1)
   164  					client.EXPECT().PullStackFromRegistry("http://registry1", "java", gomock.Any(), gomock.Any()).Return(nil).Times(1)
   165  					return client
   166  				},
   167  			},
   168  			args: args{
   169  				registryName: "",
   170  				devfile:      "java",
   171  				dest:         ".",
   172  			},
   173  			wantErr: false,
   174  		},
   175  		{
   176  			name: "Fail to download devfile from all registries where devfile is absent in all registries",
   177  			fields: fields{
   178  				preferenceClient: func(ctrl *gomock.Controller) preference.Client {
   179  					client := preference.NewMockClient(ctrl)
   180  					return client
   181  				},
   182  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   183  					client := registry.NewMockClient(ctrl)
   184  					registryList := []api.Registry{
   185  						{
   186  							Name: "Registry0",
   187  							URL:  "http://registry0",
   188  						},
   189  						{
   190  							Name: "Registry1",
   191  							URL:  "http://registry1",
   192  						},
   193  					}
   194  					client.EXPECT().GetDevfileRegistries(gomock.Eq("")).Return(registryList, nil).Times(1)
   195  					client.EXPECT().PullStackFromRegistry("http://registry0", "java", gomock.Any(), gomock.Any()).Return(errors.New("")).Times(1)
   196  					client.EXPECT().PullStackFromRegistry("http://registry1", "java", gomock.Any(), gomock.Any()).Return(errors.New("")).Times(1)
   197  					return client
   198  				},
   199  			},
   200  			args: args{
   201  				registryName: "",
   202  				devfile:      "java",
   203  				dest:         ".",
   204  			},
   205  			wantErr: true,
   206  		},
   207  	}
   208  	for _, tt := range tests {
   209  		t.Run(tt.name, func(t *testing.T) {
   210  			ctrl := gomock.NewController(t)
   211  			o := &InitClient{
   212  				preferenceClient: tt.fields.preferenceClient(ctrl),
   213  				registryClient:   tt.fields.registryClient(ctrl),
   214  			}
   215  			ctx := context.Background()
   216  			ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
   217  			if err := o.downloadFromRegistry(ctx, tt.args.registryName, tt.args.devfile, tt.args.dest, tt.args.archs); (err != nil) != tt.wantErr {
   218  				t.Errorf("InitClient.downloadFromRegistry() error = %v, wantErr %v", err, tt.wantErr)
   219  			}
   220  		})
   221  	}
   222  }
   223  
   224  func TestInitClient_downloadDirect(t *testing.T) {
   225  	type fields struct {
   226  		fsys           func(fs filesystem.Filesystem) filesystem.Filesystem
   227  		registryClient func(ctrl *gomock.Controller) registry.Client
   228  		InitParams     api.DetectionResult
   229  	}
   230  	type args struct {
   231  		URL  string
   232  		dest string
   233  	}
   234  	tests := []struct {
   235  		name    string
   236  		fields  fields
   237  		args    args
   238  		wantErr bool
   239  		want    func(fs filesystem.Filesystem) error
   240  	}{
   241  		{
   242  			name: "download an existing file",
   243  			fields: fields{
   244  				fsys: func(fs filesystem.Filesystem) filesystem.Filesystem {
   245  					_ = fs.WriteFile("/src/devfile.yaml", []byte("a content"), 0666)
   246  					return fs
   247  				},
   248  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   249  					return nil
   250  				},
   251  			},
   252  			args: args{
   253  				URL:  "/src/devfile.yaml",
   254  				dest: "/dest/file.yaml",
   255  			},
   256  			want: func(fs filesystem.Filesystem) error {
   257  				content, err := fs.ReadFile("/dest/file.yaml")
   258  				if err != nil {
   259  					return errors.New("error reading file")
   260  				}
   261  				if string(content) != "a content" {
   262  					return errors.New("content of file does not match")
   263  				}
   264  				info, err := fs.Stat("/dest/file.yaml")
   265  				if err != nil {
   266  					return errors.New("error executing Stat")
   267  				}
   268  				if info.Mode().Perm() != 0666 {
   269  					return errors.New("permissions of destination file do not match")
   270  				}
   271  				return nil
   272  			},
   273  			wantErr: false,
   274  		},
   275  		{
   276  			name: "non existing source file",
   277  			fields: fields{
   278  				fsys: func(fs filesystem.Filesystem) filesystem.Filesystem {
   279  					return fs
   280  				},
   281  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   282  					return nil
   283  				},
   284  			},
   285  			args: args{
   286  				URL:  "/src/devfile.yaml",
   287  				dest: "/dest/devfile.yaml",
   288  			},
   289  			want: func(fs filesystem.Filesystem) error {
   290  				return nil
   291  			},
   292  			wantErr: true,
   293  		},
   294  		{
   295  			name: "non existing URL",
   296  			fields: fields{
   297  				fsys: func(fs filesystem.Filesystem) filesystem.Filesystem {
   298  					return fs
   299  				},
   300  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   301  					client := registry.NewMockClient(ctrl)
   302  					client.EXPECT().DownloadFileInMemory(gomock.Any()).Return([]byte{}, errors.New(""))
   303  					return client
   304  				},
   305  			},
   306  			args: args{
   307  				URL:  "https://example.com/devfile.yaml",
   308  				dest: "/dest/devfile.yaml",
   309  			},
   310  			want: func(fs filesystem.Filesystem) error {
   311  				return nil
   312  			},
   313  			wantErr: true,
   314  		},
   315  		{
   316  			name: "existing URL",
   317  			fields: fields{
   318  				fsys: func(fs filesystem.Filesystem) filesystem.Filesystem {
   319  					return fs
   320  				},
   321  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   322  					client := registry.NewMockClient(ctrl)
   323  					client.EXPECT().DownloadFileInMemory(gomock.Any()).Return([]byte("a content"), nil)
   324  					return client
   325  				},
   326  			},
   327  			args: args{
   328  				URL:  "https://example.com/devfile.yaml",
   329  				dest: "/dest/devfile.yaml",
   330  			},
   331  			want: func(fs filesystem.Filesystem) error {
   332  				content, err := fs.ReadFile("/dest/devfile.yaml")
   333  				if err != nil {
   334  					return errors.New("error reading dest file")
   335  				}
   336  				if string(content) != "a content" {
   337  					return errors.New("unexpected file content")
   338  				}
   339  				return nil
   340  			},
   341  			wantErr: false,
   342  		},
   343  	}
   344  	for _, tt := range tests {
   345  		t.Run(tt.name, func(t *testing.T) {
   346  			fs := filesystem.NewFakeFs()
   347  			ctrl := gomock.NewController(t)
   348  			o := &InitClient{
   349  				fsys:           tt.fields.fsys(fs),
   350  				registryClient: tt.fields.registryClient(ctrl),
   351  			}
   352  			if err := o.downloadDirect(tt.args.URL, tt.args.dest); (err != nil) != tt.wantErr {
   353  				t.Errorf("InitClient.downloadDirect() error = %v, wantErr %v", err, tt.wantErr)
   354  			}
   355  			result := tt.want(fs)
   356  			if result != nil {
   357  				t.Errorf("unexpected error: %s", result)
   358  			}
   359  		})
   360  	}
   361  }
   362  
   363  func TestInitClient_downloadStarterProject(t *testing.T) {
   364  	type fields struct {
   365  		registryClient func(ctrl *gomock.Controller) registry.Client
   366  	}
   367  	type args struct {
   368  		project v1alpha2.StarterProject
   369  	}
   370  	tests := []struct {
   371  		name    string
   372  		fields  fields
   373  		args    args
   374  		wantErr bool
   375  	}{
   376  		{
   377  			name: "starter project defined",
   378  			fields: fields{
   379  				registryClient: func(ctrl *gomock.Controller) registry.Client {
   380  					client := registry.NewMockClient(ctrl)
   381  					client.EXPECT().DownloadStarterProject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any())
   382  					return client
   383  				},
   384  			},
   385  			args: args{
   386  				project: v1alpha2.StarterProject{
   387  					Name: "project1",
   388  				},
   389  			},
   390  			wantErr: false,
   391  		},
   392  	}
   393  	for _, tt := range tests {
   394  		t.Run(tt.name, func(t *testing.T) {
   395  			ctrl := gomock.NewController(t)
   396  			o := &InitClient{
   397  				registryClient: tt.fields.registryClient(ctrl),
   398  			}
   399  			if _, err := o.DownloadStarterProject(&tt.args.project, "dest"); (err != nil) != tt.wantErr {
   400  				t.Errorf("InitClient.downloadStarterProject() error = %v, wantErr %v", err, tt.wantErr)
   401  			}
   402  		})
   403  	}
   404  }
   405  

View as plain text