...

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

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

     1  package kclient
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	projectv1 "github.com/openshift/api/project/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/fields"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  	"k8s.io/apimachinery/pkg/watch"
    13  	ktesting "k8s.io/client-go/testing"
    14  
    15  	"github.com/redhat-developer/odo/pkg/testingutil"
    16  )
    17  
    18  func TestCreateNewProject(t *testing.T) {
    19  	tests := []struct {
    20  		name     string
    21  		projName string
    22  		wait     bool
    23  		wantErr  bool
    24  	}{
    25  		{
    26  			name:     "Case 1: valid project name, not waiting",
    27  			projName: "testing",
    28  			wait:     false,
    29  			wantErr:  false,
    30  		},
    31  		{
    32  			name:     "Case 2: valid project name, waiting",
    33  			projName: "testing2",
    34  			wait:     true,
    35  			wantErr:  false,
    36  		},
    37  	}
    38  
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			fkclient, fkclientset := FakeNew()
    42  
    43  			fkclientset.ProjClientset.PrependReactor("create", "projectrequests", func(action ktesting.Action) (bool, runtime.Object, error) {
    44  				proj := projectv1.Project{
    45  					ObjectMeta: metav1.ObjectMeta{
    46  						Name: tt.projName,
    47  					},
    48  				}
    49  				return true, &proj, nil
    50  			})
    51  
    52  			if tt.wait {
    53  				fkWatch := watch.NewFake()
    54  				// Change the status
    55  				go func() {
    56  					fkWatch.Add(&projectv1.Project{
    57  						ObjectMeta: metav1.ObjectMeta{
    58  							Name: tt.projName,
    59  						},
    60  						Status: projectv1.ProjectStatus{Phase: "Active"},
    61  					})
    62  				}()
    63  				fkclientset.ProjClientset.PrependWatchReactor("projects", func(action ktesting.Action) (handled bool, ret watch.Interface, err error) {
    64  					if len(tt.projName) == 0 {
    65  						return true, nil, fmt.Errorf("error watching project")
    66  					}
    67  					return true, fkWatch, nil
    68  				})
    69  			}
    70  
    71  			err := fkclient.CreateNewProject(tt.projName, tt.wait)
    72  			if !tt.wantErr == (err != nil) {
    73  				t.Errorf("client.CreateNewProject(string) unexpected error %v, wantErr %v", err, tt.wantErr)
    74  			}
    75  
    76  			actions := fkclientset.ProjClientset.Actions()
    77  			actionsLen := len(actions)
    78  			if !tt.wait && actionsLen != 1 {
    79  				t.Errorf("expected 1 action in CreateNewProject got: %v", actions)
    80  			}
    81  			if tt.wait && actionsLen != 2 {
    82  				t.Errorf("expected 2 actions in CreateNewProject when waiting for project creation got: %v", actions)
    83  			}
    84  
    85  			if err == nil {
    86  				createdProj := actions[actionsLen-1].(ktesting.CreateAction).GetObject().(*projectv1.ProjectRequest)
    87  
    88  				if createdProj.Name != tt.projName {
    89  					t.Errorf("project name does not match the expected name, expected: %s, got: %s", tt.projName, createdProj.Name)
    90  				}
    91  
    92  				if tt.wait {
    93  					expectedFields := fields.OneTermEqualSelector("metadata.name", tt.projName)
    94  					expectedFieldsReq := expectedFields.Requirements()
    95  
    96  					gotFields := actions[0].(ktesting.WatchAction).GetWatchRestrictions().Fields
    97  					gotFieldsReq := gotFields.Requirements()
    98  
    99  					if diff := cmp.Diff(expectedFieldsReq, gotFieldsReq); diff != "" {
   100  						t.Errorf("OneTermEqualSelector() fieldsReq mismatch (-want +got):\n%s", diff)
   101  					}
   102  				}
   103  			}
   104  
   105  		})
   106  	}
   107  }
   108  
   109  func TestListProjects(t *testing.T) {
   110  	tests := []struct {
   111  		name             string
   112  		returnedProjects *projectv1.ProjectList
   113  		want             *projectv1.ProjectList
   114  		wantErr          bool
   115  	}{
   116  		{
   117  			name:             "case 1: three projects returned",
   118  			returnedProjects: testingutil.FakeProjects(),
   119  			want:             testingutil.FakeProjects(),
   120  			wantErr:          false,
   121  		},
   122  		{
   123  			name:             "case 2: no projects present",
   124  			returnedProjects: &projectv1.ProjectList{},
   125  			want:             &projectv1.ProjectList{},
   126  			wantErr:          false,
   127  		},
   128  	}
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			fkclient, fkclientset := FakeNew()
   132  
   133  			fkclientset.ProjClientset.PrependReactor("list", "projects", func(action ktesting.Action) (bool, runtime.Object, error) {
   134  				return true, tt.returnedProjects, nil
   135  			})
   136  
   137  			got, err := fkclient.ListProjects()
   138  			if (err != nil) != tt.wantErr {
   139  				t.Errorf("ListProjects() error = %v, wantErr %v", err, tt.wantErr)
   140  				return
   141  			}
   142  
   143  			if diff := cmp.Diff(tt.want, got); diff != "" {
   144  				t.Errorf("Client.ListProjects() mismatch (-want +got):\n%s", diff)
   145  			}
   146  		})
   147  	}
   148  }
   149  
   150  func TestListProjectNames(t *testing.T) {
   151  	tests := []struct {
   152  		name             string
   153  		returnedProjects *projectv1.ProjectList
   154  		want             []string
   155  		wantErr          bool
   156  	}{
   157  		{
   158  			name:             "case 1: three projects returned",
   159  			returnedProjects: testingutil.FakeProjects(),
   160  			want:             []string{"testing", "prj1", "prj2"},
   161  			wantErr:          false,
   162  		},
   163  		{
   164  			name:             "case 2: no projects present",
   165  			returnedProjects: &projectv1.ProjectList{},
   166  			want:             nil,
   167  			wantErr:          false,
   168  		},
   169  	}
   170  	for _, tt := range tests {
   171  		t.Run(tt.name, func(t *testing.T) {
   172  			fkclient, fkclientset := FakeNew()
   173  
   174  			fkclientset.ProjClientset.PrependReactor("list", "projects", func(action ktesting.Action) (bool, runtime.Object, error) {
   175  				return true, tt.returnedProjects, nil
   176  			})
   177  
   178  			got, err := fkclient.ListProjectNames()
   179  			if (err != nil) != tt.wantErr {
   180  				t.Errorf("ListProjectNames() error = %v, wantErr %v", err, tt.wantErr)
   181  				return
   182  			}
   183  			if diff := cmp.Diff(tt.want, got); diff != "" {
   184  				t.Errorf("Client.ListProjectNames() mismatch (-want +got):\n%s", diff)
   185  			}
   186  		})
   187  	}
   188  }
   189  

View as plain text