...

Source file src/github.com/redhat-developer/odo/pkg/segment/context/context_test.go

Documentation: github.com/redhat-developer/odo/pkg/segment/context

     1  package context
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/spf13/pflag"
    11  )
    12  
    13  func TestGetContextProperties(t *testing.T) {
    14  	ckey, value := "preferenceKey", "consenttelemetry"
    15  	ctx := NewContext(context.Background())
    16  	setContextProperty(ctx, ckey, value)
    17  
    18  	got := GetContextProperties(ctx)
    19  	want := map[string]interface{}{ckey: value}
    20  
    21  	if diff := cmp.Diff(want, got); diff != "" {
    22  		t.Errorf("GetContextProperties() mismatch (-want +got):\n%s", diff)
    23  	}
    24  }
    25  
    26  func TestSetComponentType(t *testing.T) {
    27  	want := "java"
    28  	for _, value := range []string{"java", "java:8", "myproject/java:8"} {
    29  		ctx := NewContext(context.Background())
    30  		SetComponentType(ctx, value)
    31  
    32  		if got, contains := GetContextProperties(ctx)[ComponentType]; !contains || got != want {
    33  			t.Errorf("component type was not set. Got: %q, Want: %q", got, want)
    34  		}
    35  	}
    36  }
    37  
    38  // TODO(feloy) test with fake kclient implementation
    39  //func TestSetClusterType(t *testing.T) {
    40  //	tests := []struct {
    41  //		want   string
    42  //		groups []string
    43  //	}{
    44  //		{
    45  //			want:   "openshift3",
    46  //			groups: []string{"project.openshift.io/v1"},
    47  //		},
    48  //		{
    49  //			want:   "openshift4",
    50  //			groups: []string{"project.openshift.io/v1", "operators.coreos.com/v1alpha1"},
    51  //		},
    52  //		{
    53  //			want: "kubernetes",
    54  //		},
    55  //		{
    56  //			want: NOTFOUND,
    57  //		},
    58  //	}
    59  //
    60  //	for _, tt := range tests {
    61  //		var fakeClient *kclient.Client
    62  //		if tt.want != NOTFOUND {
    63  //			fakeClient, _ = kclient.FakeNew()
    64  //		}
    65  //		if tt.groups != nil {
    66  //			setupCluster(fakeClient, tt.groups)
    67  //		}
    68  //
    69  //		ctx := NewContext(context.Background())
    70  //		SetClusterType(ctx, fakeClient)
    71  //
    72  //		got := GetContextProperties(ctx)[ClusterType]
    73  //		if got != tt.want {
    74  //			t.Errorf("got: %q, want: %q", got, tt.want)
    75  //		}
    76  //	}
    77  //}
    78  
    79  func TestGetTelemetryStatus(t *testing.T) {
    80  	want := true
    81  	ctx := NewContext(context.Background())
    82  	setContextProperty(ctx, TelemetryStatus, want)
    83  	got := GetTelemetryStatus(ctx)
    84  	if got != want {
    85  		t.Errorf("got: %v, want: %v", got, want)
    86  	}
    87  }
    88  
    89  func TestSetTelemetryStatus(t *testing.T) {
    90  	want := false
    91  	ctx := NewContext(context.Background())
    92  	SetTelemetryStatus(ctx, want)
    93  	got := GetContextProperties(ctx)[TelemetryStatus]
    94  	if got != want {
    95  		t.Errorf("got: %v, want: %v", got, want)
    96  	}
    97  }
    98  
    99  //var apiResourceList = map[string]*metav1.APIResourceList{
   100  //	"operators.coreos.com/v1alpha1": {
   101  //		GroupVersion: "operators.coreos.com/v1alpha1",
   102  //		APIResources: []metav1.APIResource{{
   103  //			Name:         "clusterserviceversions",
   104  //			SingularName: "clusterserviceversion",
   105  //			Namespaced:   false,
   106  //			Kind:         "ClusterServiceVersion",
   107  //			ShortNames:   []string{"csv", "csvs"},
   108  //		}},
   109  //	},
   110  //	"project.openshift.io/v1": {
   111  //		GroupVersion: "project.openshift.io/v1",
   112  //		APIResources: []metav1.APIResource{{
   113  //			Name:         "projects",
   114  //			SingularName: "project",
   115  //			Namespaced:   false,
   116  //			Kind:         "Project",
   117  //			ShortNames:   []string{"proj"},
   118  //		}},
   119  //	},
   120  //}
   121  
   122  // setupCluster adds resource groups to the client
   123  //func setupCluster(fakeClient kclient.ClientInterface, groupVersion []string) {
   124  //	fd := odoFake.NewFakeDiscovery()
   125  //	for _, group := range groupVersion {
   126  //		fd.AddResourceList(group, apiResourceList[group])
   127  //	}
   128  //	fakeClient.SetDiscoveryInterface(fd)
   129  //}
   130  
   131  func TestSetFlags(t *testing.T) {
   132  	type args struct {
   133  		ctx   context.Context
   134  		flags *pflag.FlagSet
   135  	}
   136  	tests := []struct {
   137  		name string
   138  		args args
   139  		want string
   140  	}{
   141  		{
   142  			name: "no flags",
   143  			args: args{
   144  				ctx:   NewContext(context.Background()),
   145  				flags: &pflag.FlagSet{},
   146  			},
   147  			want: "",
   148  		},
   149  		{
   150  			name: "one changed flag",
   151  			args: args{
   152  				ctx: NewContext(context.Background()),
   153  				flags: func() *pflag.FlagSet {
   154  					f := &pflag.FlagSet{}
   155  					f.String("flag1", "", "")
   156  					//nolint:errcheck
   157  					f.Set("flag1", "value1")
   158  					return f
   159  				}(),
   160  			},
   161  			want: "flag1",
   162  		},
   163  		{
   164  			name: "one changed flag, one unchanged flag",
   165  			args: args{
   166  				ctx: NewContext(context.Background()),
   167  				flags: func() *pflag.FlagSet {
   168  					f := &pflag.FlagSet{}
   169  					f.String("flag1", "", "")
   170  					f.String("flag2", "", "")
   171  					//nolint:errcheck
   172  					f.Set("flag2", "value1")
   173  					return f
   174  				}(),
   175  			},
   176  			want: "flag2",
   177  		},
   178  		{
   179  			name: "two changed flags",
   180  			args: args{
   181  				ctx: NewContext(context.Background()),
   182  				flags: func() *pflag.FlagSet {
   183  					f := &pflag.FlagSet{}
   184  					f.String("flag1", "", "")
   185  					f.String("flag2", "", "")
   186  					//nolint:errcheck
   187  					f.Set("flag1", "value1")
   188  					//nolint:errcheck
   189  					f.Set("flag2", "value1")
   190  					return f
   191  				}(),
   192  			},
   193  			want: "flag1 flag2",
   194  		},
   195  	}
   196  	for _, tt := range tests {
   197  		t.Run(tt.name, func(t *testing.T) {
   198  			SetFlags(tt.args.ctx, tt.args.flags)
   199  			got := GetContextProperties(tt.args.ctx)[Flags]
   200  			if got != tt.want {
   201  				t.Errorf("SetFlags() = %v, want %v", got, tt.want)
   202  			}
   203  		})
   204  	}
   205  }
   206  
   207  func TestSetCaller(t *testing.T) {
   208  	type testScope struct {
   209  		name       string
   210  		callerType string
   211  		wantErr    bool
   212  		want       interface{}
   213  	}
   214  
   215  	tests := []testScope{
   216  		{
   217  			name:       "empty caller",
   218  			callerType: "",
   219  			want:       "",
   220  		},
   221  		{
   222  			name:       "unknown caller",
   223  			callerType: "an-unknown-caller",
   224  			wantErr:    true,
   225  			want:       "an-unknown-caller",
   226  		},
   227  		{
   228  			name:       "case-insensitive caller",
   229  			callerType: strings.ToUpper(IntelliJ),
   230  			want:       IntelliJ,
   231  		},
   232  		{
   233  			name:       "trimming space from caller",
   234  			callerType: fmt.Sprintf("   %s\t", VSCode),
   235  			want:       VSCode,
   236  		},
   237  	}
   238  	for _, c := range []string{VSCode, IntelliJ, JBoss} {
   239  		tests = append(tests, testScope{
   240  			name:       fmt.Sprintf("valid caller: %s", c),
   241  			callerType: c,
   242  			want:       c,
   243  		})
   244  	}
   245  
   246  	for _, tt := range tests {
   247  		t.Run(tt.name, func(t *testing.T) {
   248  			ctx := NewContext(context.Background())
   249  
   250  			err := SetCaller(ctx, tt.callerType)
   251  
   252  			if !tt.wantErr == (err != nil) {
   253  				t.Errorf("unexpected error %v, wantErr %v", err, tt.wantErr)
   254  			}
   255  
   256  			got := GetContextProperties(ctx)[Caller]
   257  			if got != tt.want {
   258  				t.Errorf("SetCaller() = %v, want %v", got, tt.want)
   259  			}
   260  		})
   261  	}
   262  }
   263  

View as plain text