...

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

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

     1  package libdevfile
     2  
     3  import (
     4  	"testing"
     5  
     6  	devfilev1 "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 TestGetImageComponentsToPushAutomatically(t *testing.T) {
    20  	fs := devfileFileSystem.NewFakeFs()
    21  
    22  	buildImageComponent := func(name string, autoBuild *bool, referenced bool) (devfilev1.Component, devfilev1.Command) {
    23  		comp := devfilev1.Component{
    24  			Name: name,
    25  			ComponentUnion: devfilev1.ComponentUnion{
    26  				Image: &devfilev1.ImageComponent{
    27  					Image: devfilev1.Image{
    28  						ImageName: "my-image:" + name,
    29  						ImageUnion: devfilev1.ImageUnion{
    30  							AutoBuild: autoBuild,
    31  						},
    32  					},
    33  				},
    34  			},
    35  		}
    36  		if referenced {
    37  			cmd := devfilev1.Command{
    38  				Id: "apply-" + name,
    39  				CommandUnion: devfilev1.CommandUnion{
    40  					Apply: &devfilev1.ApplyCommand{
    41  						Component: name,
    42  					},
    43  				},
    44  			}
    45  			return comp, cmd
    46  		}
    47  		return comp, devfilev1.Command{}
    48  	}
    49  
    50  	var (
    51  		autoBuildTrueReferenced, applyAutoBuildTrueReferenced = buildImageComponent(
    52  			"autoBuildTrueReferenced", pointer.Bool(true), true)
    53  		autoBuildTrueNotReferenced, _ = buildImageComponent(
    54  			"autoBuildTrueNotReferenced", pointer.Bool(true), false)
    55  		autoBuildFalseReferenced, applyAutoBuildFalseReferenced = buildImageComponent(
    56  			"autoBuildFalseReferenced", pointer.Bool(false), true)
    57  		autoBuildFalseNotReferenced, _ = buildImageComponent(
    58  			"autoBuildFalseNotReferenced", pointer.Bool(false), false)
    59  		autoBuildNotSetReferenced, applyAutoBuildNotSetReferenced = buildImageComponent(
    60  			"autoBuildNotSetReferenced", nil, true)
    61  		autoBuildNotSetNotReferenced, _ = buildImageComponent(
    62  			"autoBuildNotSetNotReferenced", nil, false)
    63  	)
    64  
    65  	buildFullDevfile := func() (parser.DevfileObj, error) {
    66  		devfileData, err := data.NewDevfileData(string(data.APISchemaVersion220))
    67  		if err != nil {
    68  			return parser.DevfileObj{}, err
    69  		}
    70  		devfileData.SetMetadata(devfilepkg.DevfileMetadata{Name: "my-devfile"})
    71  		err = devfileData.AddComponents([]devfilev1.Component{
    72  			autoBuildTrueReferenced,
    73  			autoBuildTrueNotReferenced,
    74  			autoBuildFalseReferenced,
    75  			autoBuildFalseNotReferenced,
    76  			autoBuildNotSetReferenced,
    77  			autoBuildNotSetNotReferenced,
    78  
    79  			//Add other kinds of components
    80  			{
    81  				Name: "my-k8s-component",
    82  				ComponentUnion: devfilev1.ComponentUnion{
    83  					Kubernetes: &devfilev1.KubernetesComponent{
    84  						K8sLikeComponent: devfilev1.K8sLikeComponent{
    85  							K8sLikeComponentLocation: devfilev1.K8sLikeComponentLocation{
    86  								Inlined: "my-k8s-component-inlined",
    87  							},
    88  						},
    89  					},
    90  				},
    91  			},
    92  			{
    93  				Name: "container-component",
    94  				ComponentUnion: devfilev1.ComponentUnion{
    95  					Container: &devfilev1.ContainerComponent{
    96  						Container: devfilev1.Container{
    97  							DedicatedPod: pointer.Bool(true),
    98  							Image:        "my-container-image",
    99  						},
   100  					},
   101  				},
   102  			},
   103  		})
   104  		if err != nil {
   105  			return parser.DevfileObj{}, err
   106  		}
   107  		err = devfileData.AddCommands([]devfilev1.Command{
   108  			applyAutoBuildTrueReferenced,
   109  			applyAutoBuildFalseReferenced,
   110  			applyAutoBuildNotSetReferenced,
   111  
   112  			//Add other kinds of components
   113  			{
   114  				Id: "apply-k8s-component",
   115  				CommandUnion: devfilev1.CommandUnion{
   116  					Apply: &devfilev1.ApplyCommand{
   117  						Component: "my-k8s-component",
   118  					},
   119  				},
   120  			},
   121  			{
   122  				Id: "exec-command",
   123  				CommandUnion: devfilev1.CommandUnion{
   124  					Apply: &devfilev1.ApplyCommand{
   125  						Component: "my-image-component",
   126  					},
   127  					Exec: &devfilev1.ExecCommand{
   128  						CommandLine: "/path/to/my/command -success",
   129  						Component:   "container-component",
   130  					},
   131  				},
   132  			},
   133  		})
   134  		if err != nil {
   135  			return parser.DevfileObj{}, err
   136  		}
   137  		return parser.DevfileObj{
   138  			Ctx:  devfileCtx.FakeContext(fs, parser.OutputDevfileYamlPath),
   139  			Data: devfileData,
   140  		}, nil
   141  	}
   142  
   143  	type args struct {
   144  		devfileObj func() (parser.DevfileObj, error)
   145  	}
   146  	tests := []struct {
   147  		name    string
   148  		args    args
   149  		want    []devfilev1.Component
   150  		wantErr bool
   151  	}{
   152  		{
   153  			name: "empty devfile",
   154  			args: args{
   155  				devfileObj: func() (parser.DevfileObj, error) {
   156  					return parser.DevfileObj{
   157  						Data: devfiletesting.GetDevfileData(t, nil, nil),
   158  						Ctx:  devfileCtx.FakeContext(fs, parser.OutputDevfileYamlPath),
   159  					}, nil
   160  				},
   161  			},
   162  			want:    []devfilev1.Component{},
   163  			wantErr: false,
   164  		},
   165  		{
   166  			name: "return components that need to be created automatically on startup",
   167  			args: args{
   168  				devfileObj: buildFullDevfile,
   169  			},
   170  			want: []devfilev1.Component{
   171  				autoBuildTrueReferenced,
   172  				autoBuildTrueNotReferenced,
   173  				autoBuildNotSetNotReferenced,
   174  			},
   175  		},
   176  	}
   177  	for _, tt := range tests {
   178  		t.Run(tt.name, func(t *testing.T) {
   179  			devfileObj, err := tt.args.devfileObj()
   180  			if err != nil {
   181  				t.Errorf("unable to create Devfile object: %v", err)
   182  				return
   183  			}
   184  
   185  			got, err := GetImageComponentsToPushAutomatically(devfileObj)
   186  			if (err != nil) != tt.wantErr {
   187  				t.Errorf("GetImageComponentsToPushAutomatically() error = %v, wantErr %v", err, tt.wantErr)
   188  				return
   189  			}
   190  			if len(got) != len(tt.want) {
   191  				t.Errorf("Got %d components, expected %d\n", len(got), len(tt.want))
   192  			}
   193  
   194  			lessFn := func(x, y devfilev1.Component) bool {
   195  				return x.Name < y.Name
   196  			}
   197  			if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty(), cmpopts.SortSlices(lessFn)); diff != "" {
   198  				t.Errorf("GetImageComponentsToPushAutomatically() mismatch (-want +got):\n%s", diff)
   199  			}
   200  		})
   201  	}
   202  }
   203  

View as plain text