...

Source file src/github.com/redhat-developer/odo/pkg/exec/exec_test.go

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

     1  package exec
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	corev1 "k8s.io/api/core/v1"
    11  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    12  	"k8s.io/apimachinery/pkg/watch"
    13  )
    14  
    15  const (
    16  	_podName       = "my-pod"
    17  	_containerName = "my-container"
    18  )
    19  
    20  type fakePlatform struct {
    21  	execCMDInContainer func(containerName, podName string, cmd []string, stdout io.Writer, stderr io.Writer, stdin io.Reader, tty bool) error
    22  }
    23  
    24  func (o fakePlatform) ExecCMDInContainer(ctx context.Context, containerName, podName string, cmd []string, stdout, stderr io.Writer, stdin io.Reader, tty bool) error {
    25  	return o.execCMDInContainer(containerName, podName, cmd, stdout, stderr, stdin, tty)
    26  }
    27  
    28  func (o fakePlatform) GetPodLogs(podName, containerName string, followLog bool) (io.ReadCloser, error) {
    29  	panic("not implemented yet")
    30  }
    31  
    32  func (o fakePlatform) GetPodsMatchingSelector(selector string) (*corev1.PodList, error) {
    33  	panic("not implemented yet")
    34  }
    35  
    36  func (o fakePlatform) GetAllResourcesFromSelector(selector string, ns string) ([]unstructured.Unstructured, error) {
    37  	panic("not implemented yet")
    38  }
    39  
    40  func (o fakePlatform) GetAllPodsInNamespaceMatchingSelector(selector string, ns string) (*corev1.PodList, error) {
    41  	panic("not implemented yet")
    42  }
    43  
    44  func (o fakePlatform) GetRunningPodFromSelector(selector string) (*corev1.Pod, error) {
    45  	panic("not implemented yet")
    46  }
    47  
    48  func (o fakePlatform) GetPodUsingComponentName(componentName string) (*corev1.Pod, error) {
    49  	panic("not implemented yet")
    50  }
    51  
    52  func (o fakePlatform) PodWatcher(ctx context.Context, selector string) (watch.Interface, error) {
    53  	return nil, nil
    54  }
    55  
    56  func TestExecuteCommand(t *testing.T) {
    57  	for _, tt := range []struct {
    58  		name               string
    59  		cmd                []string
    60  		execCMDInContainer func(containerName, podName string, cmd []string, stdout io.Writer, stderr io.Writer, stdin io.Reader, tty bool) error
    61  		wantErr            bool
    62  		wantStdout         []string
    63  		wantStderr         []string
    64  	}{
    65  		{
    66  			name: "command returning an error",
    67  			cmd:  []string{"unknown-cmd"},
    68  			execCMDInContainer: func(containerName, podName string, cmd []string, stdout io.Writer, stderr io.Writer, stdin io.Reader, tty bool) error {
    69  				_, _ = stderr.Write([]byte("error running command\nanother message"))
    70  				return errors.New("some error")
    71  			},
    72  			wantErr:    true,
    73  			wantStderr: []string{"error running command", "another message"},
    74  		},
    75  		{
    76  			name: "command not returning an error",
    77  			cmd:  []string{"cat", "/path/to/my/file"},
    78  			execCMDInContainer: func(containerName, podName string, cmd []string, stdout io.Writer, stderr io.Writer, stdin io.Reader, tty bool) error {
    79  				_, _ = stdout.Write([]byte("Hello World\n\n\n"))
    80  				_, _ = stdout.Write([]byte("Lorem Ipsum Dolor Sit Amet\n"))
    81  				_, _ = stderr.Write([]byte("some message written to stderr"))
    82  				return nil
    83  			},
    84  			wantStdout: []string{"Hello World", "", "", "Lorem Ipsum Dolor Sit Amet"},
    85  			wantStderr: []string{"some message written to stderr"},
    86  		},
    87  	} {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			platformClient := fakePlatform{
    90  				execCMDInContainer: tt.execCMDInContainer,
    91  			}
    92  
    93  			execClient := NewExecClient(platformClient)
    94  			stdout, stderr, err := execClient.ExecuteCommand(context.Background(), tt.cmd, _podName, _containerName, false, nil, nil)
    95  
    96  			if tt.wantErr != (err != nil) {
    97  				t.Errorf("unexpected error %v, wantErr %v", err, tt.wantErr)
    98  			}
    99  
   100  			if diff := cmp.Diff(tt.wantStdout, stdout); diff != "" {
   101  				t.Errorf("ExecClient.ExecuteCommand() wantStdout mismatch (-want +got):\n%s", diff)
   102  			}
   103  			if diff := cmp.Diff(tt.wantStderr, stderr); diff != "" {
   104  				t.Errorf("ExecClient.ExecuteCommand() wantStderr mismatch (-want +got):\n%s", diff)
   105  			}
   106  		})
   107  	}
   108  }
   109  

View as plain text