...

Source file src/github.com/redhat-developer/odo/pkg/kclient/utils_test.go

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

     1  package kclient
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	corev1 "k8s.io/api/core/v1"
     9  )
    10  
    11  func TestGetInputEnvVarsFromStrings(t *testing.T) {
    12  	tests := []struct {
    13  		name          string
    14  		envVars       []string
    15  		wantedEnvVars []corev1.EnvVar
    16  		wantErr       bool
    17  	}{
    18  		{
    19  			name:    "Test case 1: with valid two key value pairs",
    20  			envVars: []string{"key=value", "key1=value1"},
    21  			wantedEnvVars: []corev1.EnvVar{
    22  				{
    23  					Name:  "key",
    24  					Value: "value",
    25  				},
    26  				{
    27  					Name:  "key1",
    28  					Value: "value1",
    29  				},
    30  			},
    31  			wantErr: false,
    32  		},
    33  		{
    34  			name:    "Test case 2: one env var with missing value",
    35  			envVars: []string{"key=value", "key1="},
    36  			wantedEnvVars: []corev1.EnvVar{
    37  				{
    38  					Name:  "key",
    39  					Value: "value",
    40  				},
    41  				{
    42  					Name:  "key1",
    43  					Value: "",
    44  				},
    45  			},
    46  			wantErr: false,
    47  		},
    48  		{
    49  			name:    "Test case 3: one env var with no value and no =",
    50  			envVars: []string{"key=value", "key1"},
    51  			wantErr: true,
    52  		},
    53  		{
    54  			name:    "Test case 4: one env var with multiple values",
    55  			envVars: []string{"key=value", "key1=value1=value2"},
    56  			wantedEnvVars: []corev1.EnvVar{
    57  				{
    58  					Name:  "key",
    59  					Value: "value",
    60  				},
    61  				{
    62  					Name:  "key1",
    63  					Value: "value1=value2",
    64  				},
    65  			},
    66  			wantErr: false,
    67  		},
    68  		{
    69  			name:    "Test case 5: two env var with same key",
    70  			envVars: []string{"key=value", "key=value1"},
    71  			wantErr: true,
    72  		},
    73  		{
    74  			name:    "Test case 6: one env var with base64 encoded value",
    75  			envVars: []string{"key=value", "key1=SSd2ZSBnb3QgYSBsb3ZlbHkgYnVuY2ggb2YgY29jb251dHMhCg=="},
    76  			wantedEnvVars: []corev1.EnvVar{
    77  				{
    78  					Name:  "key",
    79  					Value: "value",
    80  				},
    81  				{
    82  					Name:  "key1",
    83  					Value: "SSd2ZSBnb3QgYSBsb3ZlbHkgYnVuY2ggb2YgY29jb251dHMhCg==",
    84  				},
    85  			},
    86  			wantErr: false,
    87  		},
    88  	}
    89  
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			envVars, err := GetInputEnvVarsFromStrings(tt.envVars)
    93  
    94  			if err == nil && !tt.wantErr {
    95  				if diff := cmp.Diff(tt.wantedEnvVars, envVars); diff != "" {
    96  					t.Errorf("GetInputEnvVarsFromStrings() wantedEnvVars mismatch (-want +got):\n%s", diff)
    97  				}
    98  			} else if err == nil && tt.wantErr {
    99  				t.Error("error was expected, but no error was returned")
   100  			} else if err != nil && !tt.wantErr {
   101  				t.Errorf("test failed, no error was expected, but got unexpected error: %s", err)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func Test_getErrorMessageFromEvents(t *testing.T) {
   108  	type args struct {
   109  		failedEvents map[string]corev1.Event
   110  	}
   111  	tests := []struct {
   112  		name string
   113  		args args
   114  		want []string
   115  	}{
   116  		{
   117  			name: "case 1: three failed events",
   118  			args: args{
   119  				failedEvents: map[string]corev1.Event{
   120  					"pvc": {
   121  						Reason:  "pvc not bound",
   122  						Message: "pvc not bound",
   123  						Count:   5,
   124  					},
   125  					"deployment": {
   126  						Reason:  "deployment not running",
   127  						Message: "deployment not running",
   128  						Count:   3,
   129  					},
   130  					"pod": {
   131  						Reason:  "pod not running",
   132  						Message: "pod not running",
   133  						Count:   8,
   134  					},
   135  				},
   136  			},
   137  			want: []string{"pvc not bound", "deployment not running", "pod not running", "8", "5", "3"},
   138  		},
   139  		{
   140  			name: "case 2: no failed events",
   141  			args: args{
   142  				failedEvents: map[string]corev1.Event{},
   143  			},
   144  			want: []string{},
   145  		},
   146  	}
   147  	for _, tt := range tests {
   148  		t.Run(tt.name, func(t *testing.T) {
   149  			got := getErrorMessageFromEvents(tt.args.failedEvents)
   150  
   151  			for _, wantString := range tt.want {
   152  				if !strings.Contains(got.String(), wantString) {
   153  					t.Errorf("getErrorMessageFromEvents() out: %s, doesn't contain: %s", got.String(), wantString)
   154  				}
   155  			}
   156  		})
   157  	}
   158  }
   159  

View as plain text