github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/get/get_crd_counts_test.go (about)

     1  // +build unit
     2  
     3  package get
     4  
     5  import (
     6  	"testing"
     7  
     8  	"k8s.io/apimachinery/pkg/runtime/schema"
     9  
    10  	cmd_mocks "github.com/olli-ai/jx/v2/pkg/cmd/clients/mocks"
    11  	. "github.com/petergtz/pegomock"
    12  	"github.com/stretchr/testify/assert"
    13  	v1 "k8s.io/api/core/v1"
    14  	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
    15  	apiextentions_mocks "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
    16  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    17  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    18  	"k8s.io/apimachinery/pkg/runtime"
    19  	dymamic_mocks "k8s.io/client-go/dynamic/fake"
    20  	kube_mocks "k8s.io/client-go/kubernetes/fake"
    21  
    22  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    23  )
    24  
    25  const (
    26  	group = "wine.io"
    27  )
    28  
    29  func TestRun(t *testing.T) {
    30  	o := CRDCountOptions{
    31  		CommonOptions: &opts.CommonOptions{},
    32  	}
    33  
    34  	scheme := runtime.NewScheme()
    35  
    36  	// setup mocks
    37  	factory := cmd_mocks.NewMockFactory()
    38  	kubernetesInterface := kube_mocks.NewSimpleClientset(getNamespace("cellar"), getNamespace("cellarx"))
    39  	apiextensionsInterface := apiextentions_mocks.NewSimpleClientset(getClusterScopedCRD(), getNamespaceScopedCRD())
    40  	dynamicInterface := dymamic_mocks.NewSimpleDynamicClient(scheme)
    41  	r := schema.GroupVersionResource{Group: group, Version: "v1", Resource: "rioja"}
    42  
    43  	_, err := dynamicInterface.Resource(r).Namespace("cellar").Create(getNamespaceResource("test1"), metav1.CreateOptions{})
    44  	assert.NoError(t, err)
    45  	_, err = dynamicInterface.Resource(r).Namespace("cellarx").Create(getNamespaceResource("test2"), metav1.CreateOptions{})
    46  	assert.NoError(t, err)
    47  
    48  	r = schema.GroupVersionResource{Group: group, Version: "v1", Resource: "shiraz"}
    49  
    50  	_, err = dynamicInterface.Resource(r).Create(getClusterResource("test3"), metav1.CreateOptions{})
    51  	assert.NoError(t, err)
    52  
    53  	// return our fake kubernetes client in the test
    54  	When(factory.CreateKubeClient()).ThenReturn(kubernetesInterface, "cellar", nil)
    55  	When(factory.CreateApiExtensionsClient()).ThenReturn(apiextensionsInterface, nil)
    56  	When(factory.CreateDynamicClient()).ThenReturn(dynamicInterface, "cellar", nil)
    57  
    58  	o.SetFactory(factory)
    59  
    60  	// run the command
    61  	rs, err := o.getCustomResourceCounts()
    62  	assert.NoError(t, err)
    63  
    64  	// the order is important here, larger counts should appear at the bottom of the list so we can see them sooner
    65  	clusterScopedLine := rs[0]
    66  	namespace1ScopedLine := rs[1]
    67  	namespace2ScopedLine := rs[2]
    68  
    69  	assert.Equal(t, 1, clusterScopedLine.count)
    70  	assert.Equal(t, 1, namespace1ScopedLine.count)
    71  	assert.Equal(t, 1, namespace2ScopedLine.count)
    72  
    73  }
    74  
    75  func getNamespace(name string) *v1.Namespace {
    76  	return &v1.Namespace{
    77  		ObjectMeta: metav1.ObjectMeta{
    78  			Name:      name,
    79  			Namespace: name,
    80  		},
    81  	}
    82  }
    83  
    84  func getNamespaceScopedCRD() *v1beta1.CustomResourceDefinition {
    85  	return &v1beta1.CustomResourceDefinition{
    86  		ObjectMeta: metav1.ObjectMeta{
    87  			Name: "rioja",
    88  		},
    89  		Spec: v1beta1.CustomResourceDefinitionSpec{
    90  			Group: group,
    91  			Versions: []v1beta1.CustomResourceDefinitionVersion{
    92  				{
    93  					Name: "v1",
    94  				},
    95  			},
    96  			Scope: v1beta1.NamespaceScoped,
    97  			Names: v1beta1.CustomResourceDefinitionNames{
    98  				Plural: "rioja",
    99  			},
   100  		},
   101  	}
   102  }
   103  
   104  func getClusterScopedCRD() *v1beta1.CustomResourceDefinition {
   105  	return &v1beta1.CustomResourceDefinition{
   106  		ObjectMeta: metav1.ObjectMeta{
   107  			Name: "shiraz",
   108  		},
   109  		Spec: v1beta1.CustomResourceDefinitionSpec{
   110  			Group: group,
   111  			Versions: []v1beta1.CustomResourceDefinitionVersion{
   112  				{
   113  					Name: "v1",
   114  				},
   115  			},
   116  			Scope: v1beta1.ClusterScoped,
   117  			Names: v1beta1.CustomResourceDefinitionNames{
   118  				Plural: "shiraz",
   119  			},
   120  		},
   121  	}
   122  }
   123  
   124  func getNamespaceResource(name string) *unstructured.Unstructured {
   125  	return &unstructured.Unstructured{
   126  		Object: map[string]interface{}{
   127  			"apiVersion": "wine.io/v1",
   128  			"kind":       "rioja",
   129  			"metadata": map[string]interface{}{
   130  				"name": name,
   131  			},
   132  		},
   133  	}
   134  }
   135  
   136  func getClusterResource(name string) *unstructured.Unstructured {
   137  	return &unstructured.Unstructured{
   138  		Object: map[string]interface{}{
   139  			"apiVersion": "wine.io/v1",
   140  			"kind":       "shiraz",
   141  			"metadata": map[string]interface{}{
   142  				"name": name,
   143  			},
   144  		},
   145  	}
   146  }