...

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

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

     1  package kclient
     2  
     3  import (
     4  	"testing"
     5  
     6  	appsv1 "k8s.io/api/apps/v1"
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     9  	"k8s.io/apimachinery/pkg/runtime"
    10  	"k8s.io/kubectl/pkg/scheme"
    11  )
    12  
    13  func TestClient_GetAllResourcesFromSelector(t *testing.T) {
    14  	type args struct {
    15  		selector string
    16  		ns       string
    17  	}
    18  	tests := []struct {
    19  		name        string
    20  		args        args
    21  		objects     func() []runtime.Object
    22  		checkResult func([]unstructured.Unstructured)
    23  		wantErr     bool
    24  	}{
    25  		{
    26  			name: "a deployment exists, matching labels",
    27  			args: args{
    28  				selector: "key1=value1",
    29  			},
    30  			objects: func() []runtime.Object {
    31  				dep1 := appsv1.Deployment{}
    32  				dep1.SetName("deploy1")
    33  				dep1.SetLabels(map[string]string{
    34  					"key1": "value1",
    35  					"key2": "value2",
    36  				})
    37  				return []runtime.Object{&dep1}
    38  			},
    39  			checkResult: func(u []unstructured.Unstructured) {
    40  				if len(u) != 1 {
    41  					t.Fatalf("len of result should be %d but is %d", 1, len(u))
    42  				}
    43  				if u[0].GetName() != "deploy1" {
    44  					t.Errorf("Name of 1st result should be %q but is %q", "deploy1", u[0].GetName())
    45  				}
    46  			},
    47  		},
    48  		{
    49  			name: "a deployment exists, not matching labels",
    50  			args: args{
    51  				selector: "key1=value1",
    52  			},
    53  			objects: func() []runtime.Object {
    54  				dep1 := appsv1.Deployment{}
    55  				dep1.SetName("deploy1")
    56  				dep1.SetLabels(map[string]string{
    57  					"key1": "value2",
    58  					"key2": "value1",
    59  				})
    60  				return []runtime.Object{&dep1}
    61  			},
    62  			checkResult: func(u []unstructured.Unstructured) {
    63  				if len(u) != 0 {
    64  					t.Fatalf("len of result should be %d but is %d", 0, len(u))
    65  				}
    66  			},
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  
    72  			fkclient, fkclientset := FakeNew()
    73  			fkclient.Namespace = "default"
    74  
    75  			objects := []runtime.Object{}
    76  			if tt.objects != nil {
    77  				objects = tt.objects()
    78  			}
    79  			fkclient.SetDynamicClient(scheme.Scheme, objects...)
    80  
    81  			fkclientset.Kubernetes.Fake.Resources = []*metav1.APIResourceList{
    82  				{
    83  					GroupVersion: "apps/v1",
    84  					APIResources: []metav1.APIResource{
    85  						{
    86  							Group:        "apps",
    87  							Version:      "v1",
    88  							Kind:         "Deployment",
    89  							Name:         "deployments",
    90  							SingularName: "deployment",
    91  							Namespaced:   true,
    92  							Verbs:        []string{"list"},
    93  						},
    94  					},
    95  				},
    96  			}
    97  
    98  			got, err := fkclient.GetAllResourcesFromSelector(tt.args.selector, tt.args.ns)
    99  			if (err != nil) != tt.wantErr {
   100  				t.Errorf("Client.GetAllResourcesFromSelector() error = %v, wantErr %v", err, tt.wantErr)
   101  				return
   102  			}
   103  			if tt.checkResult != nil {
   104  				tt.checkResult(got)
   105  			}
   106  		})
   107  	}
   108  }
   109  

View as plain text