...

Source file src/github.com/redhat-developer/odo/pkg/libdevfile/component_kubernetes_test.go

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

     1  package libdevfile
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     7  	devfilepkg "github.com/devfile/api/v2/pkg/devfile"
     8  	"github.com/devfile/library/v2/pkg/devfile/parser"
     9  	devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context"
    10  	"github.com/devfile/library/v2/pkg/devfile/parser/data"
    11  	devfileFileSystem "github.com/devfile/library/v2/pkg/testingutil/filesystem"
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/google/go-cmp/cmp/cmpopts"
    14  	"k8s.io/utils/pointer"
    15  
    16  	devfiletesting "github.com/redhat-developer/odo/pkg/devfile/testing"
    17  )
    18  
    19  func TestGetK8sAndOcComponentsToPush(t *testing.T) {
    20  	fs := devfileFileSystem.NewFakeFs()
    21  
    22  	buildK8sOrOcComponent := func(k8s bool, name string, deployByDefault *bool, referenced bool) (v1alpha2.Component, v1alpha2.Command) {
    23  		k8sLikeComponent := v1alpha2.K8sLikeComponent{
    24  			DeployByDefault: deployByDefault,
    25  			K8sLikeComponentLocation: v1alpha2.K8sLikeComponentLocation{
    26  				Inlined: name,
    27  			},
    28  		}
    29  		comp := v1alpha2.Component{Name: name}
    30  		if k8s {
    31  			comp.ComponentUnion.Kubernetes = &v1alpha2.KubernetesComponent{K8sLikeComponent: k8sLikeComponent}
    32  		} else {
    33  			comp.ComponentUnion.Openshift = &v1alpha2.OpenshiftComponent{K8sLikeComponent: k8sLikeComponent}
    34  		}
    35  		if referenced {
    36  			cmd := v1alpha2.Command{
    37  				Id: "apply-" + name,
    38  				CommandUnion: v1alpha2.CommandUnion{
    39  					Apply: &v1alpha2.ApplyCommand{
    40  						Component: name,
    41  					},
    42  				},
    43  			}
    44  			return comp, cmd
    45  		}
    46  		return comp, v1alpha2.Command{}
    47  	}
    48  
    49  	var (
    50  		k8sDeployByDefaultTrueReferenced, applyK8sDeployByDefaultTrueReferenced = buildK8sOrOcComponent(
    51  			true, "k8sDeployByDefaultTrueReferenced", pointer.Bool(true), true)
    52  		ocDeployByDefaultTrueReferenced, applyOcDeployByDefaultTrueReferenced = buildK8sOrOcComponent(
    53  			false, "ocDeployByDefaultTrueReferenced", pointer.Bool(true), true)
    54  
    55  		k8sDeployByDefaultTrueNotReferenced, _ = buildK8sOrOcComponent(
    56  			true, "k8sDeployByDefaultTrueNotReferenced", pointer.Bool(true), false)
    57  		ocDeployByDefaultTrueNotReferenced, _ = buildK8sOrOcComponent(
    58  			false, "ocDeployByDefaultTrueNotReferenced", pointer.Bool(true), false)
    59  
    60  		k8sDeployByDefaultFalseReferenced, applyK8sDeployByDefaultFalseReferenced = buildK8sOrOcComponent(
    61  			true, "k8sDeployByDefaultFalseReferenced", pointer.Bool(false), true)
    62  		ocDeployByDefaultFalseReferenced, applyOcDeployByDefaultFalseReferenced = buildK8sOrOcComponent(
    63  			false, "ocDeployByDefaultFalseReferenced", pointer.Bool(false), true)
    64  
    65  		k8sDeployByDefaultFalseNotReferenced, _ = buildK8sOrOcComponent(
    66  			true, "k8sDeployByDefaultFalseNotReferenced", pointer.Bool(false), false)
    67  		ocDeployByDefaultFalseNotReferenced, _ = buildK8sOrOcComponent(
    68  			false, "ocDeployByDefaultFalseNotReferenced", pointer.Bool(false), false)
    69  
    70  		k8sDeployByDefaultNotSetReferenced, applyK8sDeployByDefaultNotSetReferenced = buildK8sOrOcComponent(
    71  			true, "k8sDeployByDefaultNotSetReferenced", nil, true)
    72  		ocDeployByDefaultNotSetReferenced, applyOcDeployByDefaultNotSetReferenced = buildK8sOrOcComponent(
    73  			false, "ocDeployByDefaultNotSetReferenced", nil, true)
    74  
    75  		k8sDeployByDefaultNotSetNotReferenced, _ = buildK8sOrOcComponent(
    76  			true, "k8sDeployByDefaultNotSetNotReferenced", nil, false)
    77  		ocDeployByDefaultNotSetNotReferenced, _ = buildK8sOrOcComponent(
    78  			false, "ocDeployByDefaultNotSetNotReferenced", nil, false)
    79  	)
    80  
    81  	buildFullDevfile := func() (parser.DevfileObj, error) {
    82  		devfileData, err := data.NewDevfileData(string(data.APISchemaVersion220))
    83  		if err != nil {
    84  			return parser.DevfileObj{}, err
    85  		}
    86  		devfileData.SetMetadata(devfilepkg.DevfileMetadata{Name: "my-devfile"})
    87  		err = devfileData.AddComponents([]v1alpha2.Component{
    88  			k8sDeployByDefaultNotSetNotReferenced,
    89  			k8sDeployByDefaultNotSetReferenced,
    90  			ocDeployByDefaultNotSetReferenced,
    91  			ocDeployByDefaultNotSetNotReferenced,
    92  
    93  			k8sDeployByDefaultTrueNotReferenced,
    94  			k8sDeployByDefaultTrueReferenced,
    95  			ocDeployByDefaultTrueReferenced,
    96  			ocDeployByDefaultTrueNotReferenced,
    97  
    98  			k8sDeployByDefaultFalseNotReferenced,
    99  			k8sDeployByDefaultFalseReferenced,
   100  			ocDeployByDefaultFalseReferenced,
   101  			ocDeployByDefaultFalseNotReferenced,
   102  
   103  			//Add other kinds of components
   104  			{
   105  				Name: "my-image-component",
   106  				ComponentUnion: v1alpha2.ComponentUnion{
   107  					Image: &v1alpha2.ImageComponent{
   108  						Image: v1alpha2.Image{
   109  							ImageName: "image-component-1",
   110  						},
   111  					},
   112  				},
   113  			},
   114  			{
   115  				Name: "container-component",
   116  				ComponentUnion: v1alpha2.ComponentUnion{
   117  					Container: &v1alpha2.ContainerComponent{
   118  						Container: v1alpha2.Container{
   119  							DedicatedPod: pointer.Bool(true),
   120  							Image:        "my-container-image",
   121  						},
   122  					},
   123  				},
   124  			},
   125  		})
   126  		if err != nil {
   127  			return parser.DevfileObj{}, err
   128  		}
   129  		err = devfileData.AddCommands([]v1alpha2.Command{
   130  			applyK8sDeployByDefaultNotSetReferenced,
   131  			applyOcDeployByDefaultNotSetReferenced,
   132  			applyK8sDeployByDefaultTrueReferenced,
   133  			applyOcDeployByDefaultTrueReferenced,
   134  			applyK8sDeployByDefaultFalseReferenced,
   135  			applyOcDeployByDefaultFalseReferenced,
   136  
   137  			//Add other kinds of components
   138  			{
   139  				Id: "apply-image",
   140  				CommandUnion: v1alpha2.CommandUnion{
   141  					Apply: &v1alpha2.ApplyCommand{
   142  						Component: "my-image-component",
   143  					},
   144  				},
   145  			},
   146  			{
   147  				Id: "exec-command",
   148  				CommandUnion: v1alpha2.CommandUnion{
   149  					Apply: &v1alpha2.ApplyCommand{
   150  						Component: "my-image-component",
   151  					},
   152  					Exec: &v1alpha2.ExecCommand{
   153  						CommandLine: "/path/to/my/command -success",
   154  						Component:   "container-component",
   155  					},
   156  				},
   157  			},
   158  		})
   159  		if err != nil {
   160  			return parser.DevfileObj{}, err
   161  		}
   162  		return parser.DevfileObj{
   163  			Ctx:  devfileCtx.FakeContext(fs, parser.OutputDevfileYamlPath),
   164  			Data: devfileData,
   165  		}, nil
   166  	}
   167  
   168  	type args struct {
   169  		devfileObj func() (parser.DevfileObj, error)
   170  		allowApply bool
   171  	}
   172  
   173  	tests := []struct {
   174  		name    string
   175  		args    args
   176  		want    []v1alpha2.Component
   177  		wantErr bool
   178  	}{
   179  		{
   180  			name: "empty devfile",
   181  			args: args{
   182  				devfileObj: func() (parser.DevfileObj, error) {
   183  					return parser.DevfileObj{
   184  						Data: devfiletesting.GetDevfileData(t, nil, nil),
   185  						Ctx:  devfileCtx.FakeContext(fs, parser.OutputDevfileYamlPath),
   186  					}, nil
   187  				},
   188  			},
   189  			want:    []v1alpha2.Component{},
   190  			wantErr: false,
   191  		},
   192  		{
   193  			name: "allowApply=false => return components that need to be created automatically on startup",
   194  			args: args{
   195  				devfileObj: buildFullDevfile,
   196  				allowApply: false,
   197  			},
   198  			want: []v1alpha2.Component{
   199  				k8sDeployByDefaultTrueNotReferenced,
   200  				k8sDeployByDefaultTrueReferenced,
   201  				ocDeployByDefaultTrueNotReferenced,
   202  				ocDeployByDefaultTrueReferenced,
   203  				k8sDeployByDefaultNotSetNotReferenced,
   204  				ocDeployByDefaultNotSetNotReferenced,
   205  			},
   206  		},
   207  		{
   208  			name: "allowApply=true => return components that need to be created automatically on startup and those referenced",
   209  			args: args{
   210  				devfileObj: buildFullDevfile,
   211  				allowApply: true,
   212  			},
   213  			want: []v1alpha2.Component{
   214  				k8sDeployByDefaultTrueNotReferenced,
   215  				k8sDeployByDefaultTrueReferenced,
   216  				ocDeployByDefaultTrueNotReferenced,
   217  				ocDeployByDefaultTrueReferenced,
   218  				k8sDeployByDefaultNotSetNotReferenced,
   219  				ocDeployByDefaultNotSetNotReferenced,
   220  
   221  				k8sDeployByDefaultFalseReferenced,
   222  				ocDeployByDefaultFalseReferenced,
   223  				k8sDeployByDefaultNotSetReferenced,
   224  				ocDeployByDefaultNotSetReferenced,
   225  			},
   226  		},
   227  	}
   228  
   229  	for _, tt := range tests {
   230  		t.Run(tt.name, func(t *testing.T) {
   231  			devfileObj, err := tt.args.devfileObj()
   232  			if err != nil {
   233  				t.Errorf("unable to create Devfile object: %v", err)
   234  				return
   235  			}
   236  			got, err := GetK8sAndOcComponentsToPush(devfileObj, tt.args.allowApply)
   237  			gotErr := err != nil
   238  			if gotErr != tt.wantErr {
   239  				t.Errorf("Got error %v, expected %v\n", err, tt.wantErr)
   240  			}
   241  
   242  			if len(got) != len(tt.want) {
   243  				t.Errorf("Got %d components, expected %d\n", len(got), len(tt.want))
   244  			}
   245  
   246  			lessFn := func(x, y v1alpha2.Component) bool {
   247  				return x.Name < y.Name
   248  			}
   249  			if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty(), cmpopts.SortSlices(lessFn)); diff != "" {
   250  				t.Errorf("GetK8sAndOcComponentsToPush() mismatch (-want +got):\n%s", diff)
   251  			}
   252  		})
   253  	}
   254  }
   255  

View as plain text