...

Source file src/github.com/redhat-developer/odo/pkg/devfile/image/image_test.go

Documentation: github.com/redhat-developer/odo/pkg/devfile/image

     1  package image
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	devfile "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
    10  	gomock "github.com/golang/mock/gomock"
    11  
    12  	"github.com/redhat-developer/odo/pkg/config"
    13  	envcontext "github.com/redhat-developer/odo/pkg/config/context"
    14  	"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
    15  )
    16  
    17  func TestBuildPushImage(t *testing.T) {
    18  	fakeFs := filesystem.NewFakeFs()
    19  	tests := []struct {
    20  		name            string
    21  		devfilePath     string
    22  		image           *devfile.ImageComponent
    23  		push            bool
    24  		BuildReturns    error
    25  		PushReturns     error
    26  		wantErr         bool
    27  		wantBuildCalled bool
    28  		wantPushCalled  bool
    29  	}{
    30  		{
    31  			name:            "nil image and no push should return an error",
    32  			push:            false,
    33  			wantErr:         true,
    34  			wantBuildCalled: false,
    35  			wantPushCalled:  false,
    36  		},
    37  		{
    38  			name:            "nil image and push should return an error",
    39  			push:            true,
    40  			wantErr:         true,
    41  			wantBuildCalled: false,
    42  			wantPushCalled:  false,
    43  		},
    44  		{
    45  			name: "image and no push should call Build and not Push",
    46  			image: &devfile.ImageComponent{
    47  				Image: devfile.Image{
    48  					ImageName: "a name",
    49  				},
    50  			},
    51  			push:            false,
    52  			wantErr:         false,
    53  			wantBuildCalled: true,
    54  			wantPushCalled:  false,
    55  		},
    56  		{
    57  			name: "image and push should call Build and Push",
    58  			image: &devfile.ImageComponent{
    59  				Image: devfile.Image{
    60  					ImageName: "a name",
    61  				},
    62  			},
    63  			push:            true,
    64  			wantErr:         false,
    65  			wantBuildCalled: true,
    66  			wantPushCalled:  true,
    67  		},
    68  		{
    69  			name: "Build returns err",
    70  			image: &devfile.ImageComponent{
    71  				Image: devfile.Image{
    72  					ImageName: "a name",
    73  				},
    74  			},
    75  			push:            true,
    76  			BuildReturns:    errors.New(""),
    77  			PushReturns:     nil,
    78  			wantErr:         true,
    79  			wantBuildCalled: true,
    80  			wantPushCalled:  false,
    81  		},
    82  		{
    83  			name: "Push returns err",
    84  			image: &devfile.ImageComponent{
    85  				Image: devfile.Image{
    86  					ImageName: "a name",
    87  				},
    88  			},
    89  			push:            true,
    90  			BuildReturns:    nil,
    91  			PushReturns:     errors.New(""),
    92  			wantErr:         true,
    93  			wantBuildCalled: true,
    94  			wantPushCalled:  true,
    95  		},
    96  	}
    97  
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			ctrl := gomock.NewController(t)
   101  			backend := NewMockBackend(ctrl)
   102  			if tt.wantBuildCalled {
   103  				backend.EXPECT().Build(fakeFs, tt.image, tt.devfilePath).Return(tt.BuildReturns).Times(1)
   104  			} else {
   105  				backend.EXPECT().Build(fakeFs, nil, tt.devfilePath).Times(0)
   106  			}
   107  			if tt.wantPushCalled {
   108  				backend.EXPECT().Push(tt.image.ImageName).Return(tt.PushReturns).Times(1)
   109  			} else {
   110  				backend.EXPECT().Push(nil).Times(0)
   111  			}
   112  			err := buildPushImage(backend, fakeFs, tt.image, "", tt.push)
   113  
   114  			if tt.wantErr != (err != nil) {
   115  				t.Errorf("%s: Error result wanted %v, got %v", tt.name, tt.wantErr, err != nil)
   116  			}
   117  			ctrl.Finish()
   118  		})
   119  	}
   120  }
   121  
   122  func TestSelectBackend(t *testing.T) {
   123  	tests := []struct {
   124  		name        string
   125  		envConfig   config.Configuration
   126  		lookPathCmd func(string) (string, error)
   127  		wantType    string
   128  		wantErr     bool
   129  	}{
   130  		{
   131  			name: "all backends are present",
   132  			envConfig: config.Configuration{
   133  				DockerCmd: "docker",
   134  				PodmanCmd: "podman",
   135  			},
   136  			lookPathCmd: func(string) (string, error) {
   137  				return "", nil
   138  			},
   139  			wantErr:  false,
   140  			wantType: "podman",
   141  		},
   142  		{
   143  			name: "no backend are present",
   144  			envConfig: config.Configuration{
   145  				DockerCmd: "docker",
   146  				PodmanCmd: "podman",
   147  			},
   148  			lookPathCmd: func(string) (string, error) {
   149  				return "", errors.New("")
   150  			},
   151  			wantErr: true,
   152  		},
   153  		{
   154  			name: "only docker is present",
   155  			envConfig: config.Configuration{
   156  				DockerCmd: "docker",
   157  				PodmanCmd: "podman",
   158  			},
   159  			lookPathCmd: func(name string) (string, error) {
   160  				if name == "docker" {
   161  					return "docker", nil
   162  				}
   163  				return "", errors.New("")
   164  			},
   165  			wantErr:  false,
   166  			wantType: "docker",
   167  		},
   168  		{
   169  			name: "only podman is present",
   170  			envConfig: config.Configuration{
   171  				DockerCmd: "docker",
   172  				PodmanCmd: "podman",
   173  			},
   174  			lookPathCmd: func(name string) (string, error) {
   175  				if name == "podman" {
   176  					return "podman", nil
   177  				}
   178  				return "", errors.New("")
   179  			},
   180  			wantErr:  false,
   181  			wantType: "podman",
   182  		},
   183  		{
   184  			name: "value of PODMAN_CMD envvar is returned if it points to a valid command",
   185  			envConfig: config.Configuration{
   186  				DockerCmd: "docker",
   187  				PodmanCmd: "my-alternate-podman-command",
   188  			},
   189  			lookPathCmd: func(name string) (string, error) {
   190  				if name == "my-alternate-podman-command" {
   191  					return "my-alternate-podman-command", nil
   192  				}
   193  				return "", errors.New("")
   194  			},
   195  			wantErr:  false,
   196  			wantType: "my-alternate-podman-command",
   197  		},
   198  		{
   199  			name: "docker if PODMAN_CMD points to an invalid command",
   200  			envConfig: config.Configuration{
   201  				PodmanCmd: "no-such-command",
   202  				DockerCmd: "docker",
   203  			},
   204  			lookPathCmd: func(name string) (string, error) {
   205  				if name == "docker" {
   206  					return "docker", nil
   207  				}
   208  				return "", errors.New("")
   209  			},
   210  			wantErr:  false,
   211  			wantType: "docker",
   212  		},
   213  	}
   214  
   215  	for _, tt := range tests {
   216  		t.Run(tt.name, func(t *testing.T) {
   217  			lookPathCmd = tt.lookPathCmd
   218  			defer func() { lookPathCmd = exec.LookPath }()
   219  			ctx := context.Background()
   220  			ctx = envcontext.WithEnvConfig(ctx, tt.envConfig)
   221  			backend := SelectBackend(ctx)
   222  			if tt.wantErr != (backend == nil) {
   223  				t.Errorf("%s: Error result wanted %v, got %v", tt.name, tt.wantErr, backend == nil)
   224  			}
   225  			if tt.wantErr == false {
   226  				if tt.wantType != backend.String() {
   227  					t.Errorf("%s: Error backend wanted %v, got %v", tt.name, tt.wantType, backend.String())
   228  				}
   229  			}
   230  		})
   231  	}
   232  }
   233  

View as plain text