...

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

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

     1  package backend
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/google/go-cmp/cmp"
     9  
    10  	"github.com/redhat-developer/odo/pkg/alizer"
    11  	"github.com/redhat-developer/odo/pkg/api"
    12  	"github.com/redhat-developer/odo/pkg/init/asker"
    13  	"github.com/redhat-developer/odo/pkg/registry"
    14  	"github.com/redhat-developer/odo/pkg/testingutil"
    15  
    16  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
    17  	"github.com/devfile/library/v2/pkg/devfile/parser"
    18  	parsercontext "github.com/devfile/library/v2/pkg/devfile/parser/context"
    19  	"github.com/devfile/library/v2/pkg/devfile/parser/data"
    20  	"github.com/devfile/library/v2/pkg/testingutil/filesystem"
    21  )
    22  
    23  func TestInteractiveBackend_SelectDevfile(t *testing.T) {
    24  	type fields struct {
    25  		buildAsker         func(ctrl *gomock.Controller) asker.Asker
    26  		buildCatalogClient func(ctrl *gomock.Controller) registry.Client
    27  	}
    28  	tests := []struct {
    29  		name    string
    30  		fields  fields
    31  		want    *api.DetectionResult
    32  		wantErr bool
    33  	}{
    34  		{
    35  			name: "direct selection",
    36  			fields: fields{
    37  				buildAsker: func(ctrl *gomock.Controller) asker.Asker {
    38  					client := asker.NewMockAsker(ctrl)
    39  					client.EXPECT().AskArchitectures(knownArchitectures, []string{"amd64"}).Return([]string{"amd64"}, nil)
    40  					client.EXPECT().AskLanguage(gomock.Any()).Return(false, "java", nil)
    41  					client.EXPECT().AskType(gomock.Any()).Return(false, api.DevfileStack{
    42  						Name: "a-devfile-name",
    43  						Registry: api.Registry{
    44  							Name: "MyRegistry1",
    45  						},
    46  					}, nil)
    47  					return client
    48  				},
    49  				buildCatalogClient: func(ctrl *gomock.Controller) registry.Client {
    50  					client := registry.NewMockClient(ctrl)
    51  					client.EXPECT().ListDevfileStacks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any())
    52  					return client
    53  				},
    54  			},
    55  			want: &api.DetectionResult{
    56  				Devfile:         "a-devfile-name",
    57  				DevfileRegistry: "MyRegistry1",
    58  			},
    59  		},
    60  		{
    61  			name: "selection with back on language selection",
    62  			fields: fields{
    63  				buildAsker: func(ctrl *gomock.Controller) asker.Asker {
    64  					client := asker.NewMockAsker(ctrl)
    65  					client.EXPECT().AskArchitectures(knownArchitectures, []string{"amd64"}).Return([]string{"amd64", "arm64"}, nil)
    66  					client.EXPECT().AskLanguage(gomock.Any()).Return(true, "", nil)
    67  					client.EXPECT().AskArchitectures(knownArchitectures, []string{"amd64", "arm64"}).Return([]string{"arm64"}, nil)
    68  					client.EXPECT().AskLanguage(gomock.Any()).Return(false, "go", nil)
    69  					client.EXPECT().AskType(gomock.Any()).Return(false, api.DevfileStack{
    70  						Name: "a-devfile-name",
    71  						Registry: api.Registry{
    72  							Name: "MyRegistry1",
    73  						},
    74  					}, nil)
    75  					return client
    76  				},
    77  				buildCatalogClient: func(ctrl *gomock.Controller) registry.Client {
    78  					client := registry.NewMockClient(ctrl)
    79  					client.EXPECT().ListDevfileStacks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2)
    80  					return client
    81  				},
    82  			},
    83  			want: &api.DetectionResult{
    84  				Devfile:         "a-devfile-name",
    85  				DevfileRegistry: "MyRegistry1",
    86  			},
    87  		},
    88  		{
    89  			name: "selection with back on type selection",
    90  			fields: fields{
    91  				buildAsker: func(ctrl *gomock.Controller) asker.Asker {
    92  					client := asker.NewMockAsker(ctrl)
    93  					client.EXPECT().AskArchitectures(knownArchitectures, []string{"amd64"}).Return([]string{"amd64"}, nil)
    94  					client.EXPECT().AskLanguage(gomock.Any()).Return(false, "java", nil)
    95  					client.EXPECT().AskType(gomock.Any()).Return(true, api.DevfileStack{}, nil)
    96  					client.EXPECT().AskLanguage(gomock.Any()).Return(false, "go", nil)
    97  					client.EXPECT().AskType(gomock.Any()).Return(false, api.DevfileStack{
    98  						Name: "a-devfile-name",
    99  						Registry: api.Registry{
   100  							Name: "MyRegistry1",
   101  						},
   102  					}, nil)
   103  					return client
   104  				},
   105  				buildCatalogClient: func(ctrl *gomock.Controller) registry.Client {
   106  					client := registry.NewMockClient(ctrl)
   107  					client.EXPECT().ListDevfileStacks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2)
   108  					return client
   109  				},
   110  			},
   111  			want: &api.DetectionResult{
   112  				Devfile:         "a-devfile-name",
   113  				DevfileRegistry: "MyRegistry1",
   114  			},
   115  		},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			ctrl := gomock.NewController(t)
   120  			o := &InteractiveBackend{
   121  				askerClient:    tt.fields.buildAsker(ctrl),
   122  				registryClient: tt.fields.buildCatalogClient(ctrl),
   123  			}
   124  			ctx := context.Background()
   125  			got, err := o.SelectDevfile(ctx, map[string]string{}, nil, "")
   126  			if (err != nil) != tt.wantErr {
   127  				t.Errorf("InteractiveBackend.SelectDevfile() error = %v, wantErr %v", err, tt.wantErr)
   128  				return
   129  			}
   130  			if diff := cmp.Diff(tt.want, got); diff != "" {
   131  				t.Errorf("InteractiveBackend.SelectDevfile() mismatch (-want +got):\n%s", diff)
   132  			}
   133  		})
   134  	}
   135  }
   136  
   137  func TestInteractiveBackend_SelectStarterProject(t *testing.T) {
   138  	type fields struct {
   139  		asker          func(ctrl *gomock.Controller) asker.Asker
   140  		registryClient registry.Client
   141  	}
   142  	type args struct {
   143  		devfile func() parser.DevfileObj
   144  		flags   map[string]string
   145  	}
   146  	tests := []struct {
   147  		name    string
   148  		fields  fields
   149  		args    args
   150  		want    *v1alpha2.StarterProject
   151  		wantErr bool
   152  	}{
   153  		{
   154  			name: "no flags, no starter selected",
   155  			fields: fields{
   156  				asker: func(ctrl *gomock.Controller) asker.Asker {
   157  					client := asker.NewMockAsker(ctrl)
   158  					client.EXPECT().AskStarterProject(gomock.Any()).Return(false, 0, nil)
   159  					return client
   160  				},
   161  			},
   162  			args: args{
   163  				devfile: func() parser.DevfileObj {
   164  					devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
   165  					return parser.DevfileObj{
   166  						Data: devfileData,
   167  					}
   168  				},
   169  				flags: map[string]string{},
   170  			},
   171  			want:    nil,
   172  			wantErr: false,
   173  		},
   174  		{
   175  			name: "no flags, starter selected",
   176  			fields: fields{
   177  				asker: func(ctrl *gomock.Controller) asker.Asker {
   178  					client := asker.NewMockAsker(ctrl)
   179  					client.EXPECT().AskStarterProject(gomock.Any()).Return(true, 1, nil)
   180  					return client
   181  				},
   182  			},
   183  			args: args{
   184  				devfile: func() parser.DevfileObj {
   185  					devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
   186  					_ = devfileData.AddStarterProjects([]v1alpha2.StarterProject{
   187  						{
   188  							Name: "starter1",
   189  						},
   190  						{
   191  							Name: "starter2",
   192  						},
   193  						{
   194  							Name: "starter3",
   195  						},
   196  					})
   197  					return parser.DevfileObj{
   198  						Data: devfileData,
   199  					}
   200  				},
   201  				flags: map[string]string{},
   202  			},
   203  			want: &v1alpha2.StarterProject{
   204  				Name: "starter2",
   205  			},
   206  			wantErr: false,
   207  		},
   208  		// TODO: Add test cases.
   209  	}
   210  	for _, tt := range tests {
   211  		t.Run(tt.name, func(t *testing.T) {
   212  			ctrl := gomock.NewController(t)
   213  			var askerClient asker.Asker
   214  			if tt.fields.asker != nil {
   215  				askerClient = tt.fields.asker(ctrl)
   216  			}
   217  			o := &InteractiveBackend{
   218  				askerClient:    askerClient,
   219  				registryClient: tt.fields.registryClient,
   220  			}
   221  			got1, err := o.SelectStarterProject(tt.args.devfile(), tt.args.flags)
   222  			if (err != nil) != tt.wantErr {
   223  				t.Errorf("InteractiveBackend.SelectStarterProject() error = %v, wantErr %v", err, tt.wantErr)
   224  				return
   225  			}
   226  
   227  			if diff := cmp.Diff(tt.want, got1); diff != "" {
   228  				t.Errorf("InteractiveBackend.SelectStarterProject() mismatch (-want +got):\n%s", diff)
   229  			}
   230  		})
   231  	}
   232  }
   233  
   234  func TestInteractiveBackend_PersonalizeName(t *testing.T) {
   235  
   236  	type fields struct {
   237  		asker          func(ctrl *gomock.Controller) asker.Asker
   238  		registryClient registry.Client
   239  		alizer         func(ctrl *gomock.Controller) alizer.Client
   240  	}
   241  	type args struct {
   242  		devfile func(fs filesystem.Filesystem) parser.DevfileObj
   243  		flags   map[string]string
   244  	}
   245  	tests := []struct {
   246  		name        string
   247  		fields      fields
   248  		args        args
   249  		wantErr     bool
   250  		checkResult func(newName string, args args) bool
   251  	}{
   252  		{
   253  			name: "no flag",
   254  			fields: fields{
   255  				asker: func(ctrl *gomock.Controller) asker.Asker {
   256  					client := asker.NewMockAsker(ctrl)
   257  					client.EXPECT().AskName(gomock.Any()).Return("aname", nil)
   258  					return client
   259  				},
   260  				alizer: func(ctrl *gomock.Controller) alizer.Client {
   261  					client := alizer.NewMockClient(ctrl)
   262  					client.EXPECT().DetectName(gomock.Any()).Return("name1", nil)
   263  					return client
   264  				},
   265  			},
   266  			args: args{
   267  				devfile: func(fs filesystem.Filesystem) parser.DevfileObj {
   268  					devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
   269  
   270  					obj := parser.DevfileObj{
   271  						Ctx:  parsercontext.FakeContext(fs, "/tmp/devfile.yaml"),
   272  						Data: devfileData,
   273  					}
   274  					return obj
   275  				},
   276  				flags: map[string]string{},
   277  			},
   278  			wantErr: false,
   279  			checkResult: func(newName string, args args) bool {
   280  				return newName == "aname"
   281  			},
   282  		},
   283  		{
   284  			name: "invalid name",
   285  			fields: fields{
   286  				asker: func(ctrl *gomock.Controller) asker.Asker {
   287  					client := asker.NewMockAsker(ctrl)
   288  					client.EXPECT().AskName(gomock.Any()).Return("ls;aname", nil)
   289  					client.EXPECT().AskName(gomock.Any()).Return("aname", nil)
   290  					return client
   291  				},
   292  				alizer: func(ctrl *gomock.Controller) alizer.Client {
   293  					client := alizer.NewMockClient(ctrl)
   294  					client.EXPECT().DetectName(gomock.Any()).Return("name1", nil)
   295  					return client
   296  				},
   297  			},
   298  			args: args{
   299  				devfile: func(fs filesystem.Filesystem) parser.DevfileObj {
   300  					devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
   301  
   302  					obj := parser.DevfileObj{
   303  						Ctx:  parsercontext.FakeContext(fs, "/tmp/devfile.yaml"),
   304  						Data: devfileData,
   305  					}
   306  					return obj
   307  				},
   308  				flags: map[string]string{},
   309  			},
   310  			wantErr: false,
   311  			checkResult: func(newName string, args args) bool {
   312  				return newName == "aname"
   313  			},
   314  		},
   315  	}
   316  	for _, tt := range tests {
   317  		t.Run(tt.name, func(t *testing.T) {
   318  			ctrl := gomock.NewController(t)
   319  			var askerClient asker.Asker
   320  			if tt.fields.asker != nil {
   321  				askerClient = tt.fields.asker(ctrl)
   322  			}
   323  			var alizerClient alizer.Client
   324  			if tt.fields.alizer != nil {
   325  				alizerClient = tt.fields.alizer(ctrl)
   326  			}
   327  			o := &InteractiveBackend{
   328  				askerClient:    askerClient,
   329  				registryClient: tt.fields.registryClient,
   330  				alizerClient:   alizerClient,
   331  			}
   332  			fs := filesystem.NewFakeFs()
   333  			newName, err := o.PersonalizeName(tt.args.devfile(fs), tt.args.flags)
   334  			if (err != nil) != tt.wantErr {
   335  				t.Errorf("InteractiveBackend.PersonalizeName() error = %v, wantErr %v", err, tt.wantErr)
   336  				return
   337  			}
   338  
   339  			if tt.checkResult != nil && !tt.checkResult(newName, tt.args) {
   340  				t.Errorf("InteractiveBackend.PersonalizeName(), checking result failed")
   341  			}
   342  		})
   343  	}
   344  }
   345  
   346  func TestInteractiveBackend_PersonalizeDevfileconfig(t *testing.T) {
   347  	container1 := "runtime"
   348  
   349  	type fields struct {
   350  		asker          func(ctrl *gomock.Controller, configuration asker.DevfileConfiguration) asker.Asker
   351  		registryClient registry.Client
   352  	}
   353  	type args struct {
   354  		devfileobj func(fs filesystem.Filesystem) parser.DevfileObj
   355  		key        string
   356  		value      string
   357  	}
   358  	tests := []struct {
   359  		name        string
   360  		fields      fields
   361  		args        args
   362  		wantErr     bool
   363  		checkResult func(config asker.ContainerConfiguration, key string, value string) bool
   364  	}{
   365  		{
   366  			name: "Add new port",
   367  			fields: fields{
   368  				asker: func(ctrl *gomock.Controller, configuration asker.DevfileConfiguration) asker.Asker {
   369  					client := asker.NewMockAsker(ctrl)
   370  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return(container1, nil)
   371  					containerConfig := configuration[container1]
   372  					selectContainer := client.EXPECT().AskPersonalizeConfiguration(containerConfig).Return(asker.OperationOnContainer{
   373  						Ops:  "Add",
   374  						Kind: "Port",
   375  					}, nil).MaxTimes(1)
   376  					addPort := client.EXPECT().AskAddPort().Return("5000", nil).After(selectContainer)
   377  					containerConfig.Ports = append(containerConfig.Ports, "5000")
   378  					containerConfigDone := client.EXPECT().AskPersonalizeConfiguration(containerConfig).Return(asker.OperationOnContainer{Ops: "Nothing"}, nil).After(addPort)
   379  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return("NONE - configuration is correct", nil).After(containerConfigDone)
   380  					return client
   381  				},
   382  				registryClient: nil,
   383  			},
   384  			args: args{
   385  				key: "5000",
   386  				devfileobj: func(fs filesystem.Filesystem) parser.DevfileObj {
   387  					ports := []string{"7000", "8000"}
   388  					envVars := []v1alpha2.EnvVar{{Name: "env1", Value: "val1"}, {Name: "env2", Value: "val2"}}
   389  					return getDevfileObj(fs, container1, ports, envVars)
   390  				},
   391  			},
   392  			wantErr: false,
   393  			checkResult: func(config asker.ContainerConfiguration, key string, value string) bool {
   394  				for _, port := range config.Ports {
   395  					if port == key {
   396  						return true
   397  					}
   398  				}
   399  				return false
   400  			},
   401  		},
   402  		{
   403  			name: "Add new environment variable",
   404  			fields: fields{
   405  				asker: func(ctrl *gomock.Controller, configuration asker.DevfileConfiguration) asker.Asker {
   406  					client := asker.NewMockAsker(ctrl)
   407  					askContainerName := client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return(container1, nil)
   408  					containerConfig := configuration[container1]
   409  					selectContainer := client.EXPECT().AskPersonalizeConfiguration(containerConfig).Return(asker.OperationOnContainer{
   410  						Ops:  "Add",
   411  						Kind: "EnvVar",
   412  					}, nil).After(askContainerName)
   413  					key, val := "env3", "val3"
   414  					addEnvVar := client.EXPECT().AskAddEnvVar().Return(key, val, nil).After(selectContainer)
   415  					// containerConfig.Envs[key] = val
   416  					containerConfigDone := client.EXPECT().AskPersonalizeConfiguration(gomock.Any()).Return(asker.OperationOnContainer{Ops: "Nothing"}, nil).After(addEnvVar)
   417  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return("NONE - configuration is correct", nil).After(containerConfigDone)
   418  					return client
   419  				},
   420  				registryClient: nil,
   421  			},
   422  			args: args{
   423  				devfileobj: func(fs filesystem.Filesystem) parser.DevfileObj {
   424  					ports := []string{"7000", "8000"}
   425  					envVars := []v1alpha2.EnvVar{{Name: "env1", Value: "val1"}, {Name: "env2", Value: "val2"}}
   426  					return getDevfileObj(fs, container1, ports, envVars)
   427  				},
   428  				key:   "env3",
   429  				value: "val3",
   430  			},
   431  			wantErr: false,
   432  			checkResult: func(config asker.ContainerConfiguration, key string, value string) bool {
   433  				if val, ok := config.Envs[key]; ok && val == value {
   434  					return true
   435  				}
   436  				return false
   437  			},
   438  		},
   439  		{
   440  			name: "Delete port",
   441  			fields: fields{
   442  				asker: func(ctrl *gomock.Controller, configuration asker.DevfileConfiguration) asker.Asker {
   443  					client := asker.NewMockAsker(ctrl)
   444  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return(container1, nil)
   445  					containerConfig := configuration[container1]
   446  					selectContainer := client.EXPECT().AskPersonalizeConfiguration(containerConfig).Return(asker.OperationOnContainer{
   447  						Ops:  "Delete",
   448  						Kind: "Port",
   449  						Key:  "7000",
   450  					}, nil).MaxTimes(1)
   451  					containerConfig.Ports = []string{"8000"}
   452  					containerConfigDone := client.EXPECT().AskPersonalizeConfiguration(containerConfig).Return(asker.OperationOnContainer{Ops: "Nothing"}, nil).After(selectContainer)
   453  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return("NONE - configuration is correct", nil).After(containerConfigDone)
   454  					return client
   455  				},
   456  				registryClient: nil,
   457  			},
   458  			args: args{
   459  				devfileobj: func(fs filesystem.Filesystem) parser.DevfileObj {
   460  					ports := []string{"7000", "8000"}
   461  					envVars := []v1alpha2.EnvVar{{Name: "env1", Value: "val1"}, {Name: "env2", Value: "val2"}}
   462  					return getDevfileObj(fs, container1, ports, envVars)
   463  				},
   464  				key:   "7000",
   465  				value: "",
   466  			},
   467  			checkResult: func(config asker.ContainerConfiguration, key string, value string) bool {
   468  				for _, port := range config.Ports {
   469  					if port == key {
   470  						return false
   471  					}
   472  				}
   473  				return true
   474  			},
   475  			wantErr: false,
   476  		},
   477  		{
   478  			name: "Delete environment variable",
   479  			fields: fields{
   480  				asker: func(ctrl *gomock.Controller, configuration asker.DevfileConfiguration) asker.Asker {
   481  					client := asker.NewMockAsker(ctrl)
   482  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return(container1, nil)
   483  					containerConfig := configuration[container1]
   484  					selectContainer := client.EXPECT().AskPersonalizeConfiguration(containerConfig).Return(asker.OperationOnContainer{
   485  						Ops:  "Delete",
   486  						Kind: "EnvVar",
   487  						Key:  "env2",
   488  					}, nil).MaxTimes(1)
   489  
   490  					// delete(containerConfig.Envs, "env2")
   491  					containerConfigDone := client.EXPECT().AskPersonalizeConfiguration(gomock.Any()).Return(asker.OperationOnContainer{Ops: "Nothing"}, nil).After(selectContainer)
   492  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return("NONE - configuration is correct", nil).After(containerConfigDone)
   493  					return client
   494  				},
   495  				registryClient: nil,
   496  			},
   497  			args: args{
   498  				devfileobj: func(fs filesystem.Filesystem) parser.DevfileObj {
   499  					ports := []string{"7000", "8000"}
   500  					envVars := []v1alpha2.EnvVar{{Name: "env1", Value: "val1"}, {Name: "env2", Value: "val2"}}
   501  					return getDevfileObj(fs, container1, ports, envVars)
   502  				},
   503  				key:   "env2",
   504  				value: "",
   505  			},
   506  			wantErr: false,
   507  			checkResult: func(config asker.ContainerConfiguration, key string, value string) bool {
   508  				if _, ok := config.Envs[key]; ok {
   509  					return false
   510  				}
   511  				return true
   512  			},
   513  		},
   514  		{
   515  			name: "None - Configuration is correct",
   516  			fields: fields{
   517  				asker: func(ctrl *gomock.Controller, configuration asker.DevfileConfiguration) asker.Asker {
   518  					client := asker.NewMockAsker(ctrl)
   519  					client.EXPECT().AskContainerName(append(configuration.GetContainers(), "NONE - configuration is correct")).Return("NONE - configuration is correct", nil)
   520  					containerConfig := configuration[container1]
   521  					client.EXPECT().AskPersonalizeConfiguration(containerConfig).Return(asker.OperationOnContainer{
   522  						Ops: "Nothing",
   523  					}, nil).MaxTimes(1)
   524  					return client
   525  				},
   526  				registryClient: nil,
   527  			},
   528  			args: args{
   529  				devfileobj: func(fs filesystem.Filesystem) parser.DevfileObj {
   530  					ports := []string{"7000", "8000"}
   531  					envVars := []v1alpha2.EnvVar{{Name: "env1", Value: "val1"}, {Name: "env2", Value: "val2"}}
   532  					return getDevfileObj(fs, container1, ports, envVars)
   533  				},
   534  				key:   "",
   535  				value: "",
   536  			},
   537  			wantErr: false,
   538  			checkResult: func(config asker.ContainerConfiguration, key string, value string) bool {
   539  				checkConfig := asker.ContainerConfiguration{
   540  					Ports: []string{"7000", "8000"},
   541  					Envs:  map[string]string{"env1": "val1", "env2": "val2"},
   542  				}
   543  				return cmp.Diff(checkConfig, config) == ""
   544  			},
   545  		},
   546  	}
   547  	for _, tt := range tests {
   548  		t.Run(tt.name, func(t *testing.T) {
   549  			devfile := tt.args.devfileobj(filesystem.NewFakeFs())
   550  			config, err := getPortsAndEnvVar(devfile)
   551  			if err != nil {
   552  				t.Errorf("getPortsAndEnvVar() error = %v", err)
   553  			}
   554  
   555  			ctrl := gomock.NewController(t)
   556  			var askerClient asker.Asker
   557  			if tt.fields.asker != nil {
   558  				askerClient = tt.fields.asker(ctrl, config)
   559  			}
   560  
   561  			o := &InteractiveBackend{
   562  				askerClient:    askerClient,
   563  				registryClient: tt.fields.registryClient,
   564  			}
   565  			devfile, err = o.PersonalizeDevfileConfig(devfile)
   566  			if (err != nil) != tt.wantErr {
   567  				t.Errorf("PersonalizeDevfileConfig() error = %v, wantErr %v", err, tt.wantErr)
   568  			}
   569  			config, err = getPortsAndEnvVar(devfile)
   570  			if err != nil {
   571  				t.Errorf("getPortsAndEnvVar() error = %v", err)
   572  			}
   573  			if tt.checkResult != nil && !tt.checkResult(config[container1], tt.args.key, tt.args.value) {
   574  				t.Errorf("InteractiveBackend.PersonalizeName(), checking result failed")
   575  			}
   576  		})
   577  	}
   578  }
   579  
   580  func getDevfileObj(fs filesystem.Filesystem, containerName string, ports []string, envVars []v1alpha2.EnvVar) parser.DevfileObj {
   581  	devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
   582  	_ = devfileData.AddComponents([]v1alpha2.Component{
   583  		testingutil.GetFakeContainerComponent(containerName),
   584  	})
   585  	obj := parser.DevfileObj{
   586  		Ctx:  parsercontext.FakeContext(fs, "/tmp/devfile.yaml"),
   587  		Data: devfileData,
   588  	}
   589  	_ = obj.SetPorts(map[string][]string{containerName: ports})
   590  	_ = obj.AddEnvVars(map[string][]v1alpha2.EnvVar{containerName: envVars})
   591  	return obj
   592  }
   593  
   594  func Test_getPortsAndEnvVar(t *testing.T) {
   595  	ports := []string{"7000", "8000"}
   596  	envVars := []v1alpha2.EnvVar{{Name: "env1", Value: "val1"}, {Name: "env2", Value: "val2"}}
   597  	containerTypeName := "runtime"
   598  	volumeTypeName := "m2"
   599  	type args struct {
   600  		obj parser.DevfileObj
   601  	}
   602  	tests := []struct {
   603  		name    string
   604  		args    args
   605  		want    asker.DevfileConfiguration
   606  		wantErr bool
   607  	}{
   608  		{
   609  			name: "only detect Container type components",
   610  			args: args{obj: func() parser.DevfileObj {
   611  				obj := getDevfileObj(filesystem.NewFakeFs(), containerTypeName, ports, envVars)
   612  				_ = obj.Data.AddComponents([]v1alpha2.Component{testingutil.GetFakeVolumeComponent(volumeTypeName, "10Mi")})
   613  				return obj
   614  			}()},
   615  			want: asker.DevfileConfiguration{containerTypeName: asker.ContainerConfiguration{
   616  				Ports: ports,
   617  				Envs:  map[string]string{"env1": "val1", "env2": "val2"},
   618  			}},
   619  			wantErr: false,
   620  		},
   621  		{
   622  			name: "no Container type component found",
   623  			args: args{
   624  				obj: func() parser.DevfileObj {
   625  					devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
   626  					_ = devfileData.AddComponents([]v1alpha2.Component{
   627  						testingutil.GetFakeVolumeComponent(volumeTypeName, "10Mi"),
   628  					})
   629  					obj := parser.DevfileObj{
   630  						Ctx:  parsercontext.FakeContext(filesystem.NewFakeFs(), "/tmp/devfile.yaml"),
   631  						Data: devfileData,
   632  					}
   633  					return obj
   634  				}(),
   635  			},
   636  			want:    asker.DevfileConfiguration{},
   637  			wantErr: false,
   638  		},
   639  	}
   640  	for _, tt := range tests {
   641  		t.Run(tt.name, func(t *testing.T) {
   642  			got, err := getPortsAndEnvVar(tt.args.obj)
   643  			if (err != nil) != tt.wantErr {
   644  				t.Errorf("getPortsAndEnvVar() error = %v, wantErr %v", err, tt.wantErr)
   645  				return
   646  			}
   647  			if diff := cmp.Diff(tt.want, got); diff != "" {
   648  				t.Errorf("getPortsAndEnvVar mismatch (-want +got):\n%s", diff)
   649  			}
   650  		})
   651  	}
   652  }
   653  

View as plain text