github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/pkg/kubernetes/client/resources_test.go (about)

     1  package client
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func collectFQNs(resIntf interface{}) []string {
    12  	resPtr, ok := resIntf.(*Resources)
    13  	if !ok {
    14  		return nil
    15  	}
    16  	res := *resPtr
    17  	if len(res) == 0 {
    18  		return nil
    19  	}
    20  	out := make([]string, len(res))
    21  	for pos := range res {
    22  		out[pos] = res[pos].FQN()
    23  	}
    24  	return out
    25  }
    26  
    27  func TestUnmarshalTable(t *testing.T) {
    28  	cases := []struct {
    29  		name     string
    30  		tbl      string
    31  		dest     interface{}
    32  		want     interface{}
    33  		wantFQNs []string
    34  		err      error
    35  	}{
    36  		{
    37  			name: "normal",
    38  			tbl:  strings.TrimSpace(tblv126),
    39  			want: &Resources{
    40  				{APIVersion: "v1", Kind: "Binding", Name: "bindings", Shortnames: "", Namespaced: true, Verbs: "create"},
    41  				{APIVersion: "v1", Kind: "ComponentStatus", Name: "componentstatuses", Shortnames: "cs", Namespaced: false, Verbs: "get,list"},
    42  				{APIVersion: "v1", Kind: "ConfigMap", Name: "configmaps", Shortnames: "cm", Namespaced: true, Verbs: "create,delete,deletecollection,get,list,patch,update,watch"},
    43  				{APIVersion: "v1", Kind: "Pod", Name: "pods", Shortnames: "po", Namespaced: true, Verbs: "create,delete,deletecollection,get,list,patch,update,watch", Categories: "all"},
    44  			},
    45  			wantFQNs: []string{
    46  				"bindings",
    47  				"componentstatuses",
    48  				"configmaps",
    49  				"pods",
    50  			},
    51  			dest: &Resources{},
    52  		},
    53  		{
    54  			name: "normal-v1.18-to-v.1.25",
    55  			tbl:  strings.TrimSpace(tblv121),
    56  			want: &Resources{
    57  				{APIVersion: "v1", Kind: "Namespace", Name: "namespaces", Shortnames: "ns", Namespaced: false},
    58  				{APIVersion: "apps/v1", Kind: "DaemonSet", Name: "daemonsets", Shortnames: "ds", Namespaced: true},
    59  				{APIVersion: "apps/v1", Kind: "Deployment", Name: "deployments", Shortnames: "deploy", Namespaced: true},
    60  				{APIVersion: "networking.k8s.io/v1", Kind: "Ingress", Name: "ingresses", Shortnames: "ing", Namespaced: true},
    61  			},
    62  			wantFQNs: []string{
    63  				"namespaces",
    64  				"daemonsets.apps",
    65  				"deployments.apps",
    66  				"ingresses.networking.k8s.io",
    67  			},
    68  			dest: &Resources{},
    69  		},
    70  		{
    71  			name: "normal-v1.18-and-older",
    72  			tbl:  strings.TrimSpace(tblv118),
    73  			want: &Resources{
    74  				{APIGroup: "", Kind: "Namespace", Name: "namespaces", Shortnames: "ns", Namespaced: false},
    75  				{APIGroup: "apps", Kind: "DaemonSet", Name: "daemonsets", Shortnames: "ds", Namespaced: true},
    76  				{APIGroup: "apps", Kind: "Deployment", Name: "deployments", Shortnames: "deploy", Namespaced: true},
    77  				{APIGroup: "networking.k8s.io", Kind: "Ingress", Name: "ingresses", Shortnames: "ing", Namespaced: true},
    78  			},
    79  			wantFQNs: []string{
    80  				"namespaces",
    81  				"daemonsets.apps",
    82  				"deployments.apps",
    83  				"ingresses.networking.k8s.io",
    84  			},
    85  			dest: &Resources{},
    86  		},
    87  		{
    88  			name: "empty",
    89  			tbl:  strings.TrimSpace(tblEmpty),
    90  			want: &Resources{},
    91  			dest: &Resources{
    92  				{APIVersion: "apps/v1", Name: "Deployment", Namespaced: true},
    93  			},
    94  		},
    95  		{
    96  			name: "no-header",
    97  			tbl:  strings.TrimSpace(tblNoHeader),
    98  			err:  ErrorNoHeader,
    99  		},
   100  		{
   101  			name: "nothing",
   102  			tbl:  tblNothing,
   103  			err:  ErrorNoHeader,
   104  		},
   105  	}
   106  
   107  	for _, c := range cases {
   108  		t.Run(c.name, func(t *testing.T) {
   109  			err := UnmarshalTable(c.tbl, c.dest)
   110  			require.Equal(t, c.err, err)
   111  			assert.Equal(t, c.want, c.dest)
   112  			assert.Equal(t, c.wantFQNs, collectFQNs(c.dest))
   113  		})
   114  	}
   115  }
   116  
   117  // This is a snippet that was pulled from output generated with kubectl v1.26.0
   118  // $ kubectl api-resources --cached --output=wide
   119  var tblv126 = `
   120  NAME                              SHORTNAMES   APIVERSION                        NAMESPACED   KIND                             VERBS                                                        CATEGORIES
   121  bindings                                       v1                                true         Binding                          create
   122  componentstatuses                 cs           v1                                false        ComponentStatus                  get,list
   123  configmaps                        cm           v1                                true         ConfigMap                        create,delete,deletecollection,get,list,patch,update,watch
   124  pods                              po           v1                                true         Pod                              create,delete,deletecollection,get,list,patch,update,watch   all
   125  `
   126  
   127  // this output was generated with kubectl v1.21.1
   128  // $ kubectl api-resources | grep -e "Deployment\|DaemonSet\|Namespace\|networking.k8s.io.*Ingress$\|KIND"
   129  var tblv121 = `
   130  NAME                                SHORTNAMES                             APIVERSION                             NAMESPACED   KIND
   131  namespaces                          ns                                     v1                                     false        Namespace
   132  daemonsets                          ds                                     apps/v1                                true         DaemonSet
   133  deployments                         deploy                                 apps/v1                                true         Deployment
   134  ingresses                           ing                                    networking.k8s.io/v1                   true         Ingress
   135  `
   136  
   137  // this output was generated with kubectl v1.18.10
   138  // $ kubectl api-resources | grep -e "Deployment\|DaemonSet\|Namespace\|networking.k8s.io.*Ingress$\|KIND"
   139  var tblv118 = `
   140  NAME                                SHORTNAMES                             APIGROUP                       NAMESPACED   KIND
   141  namespaces                          ns                                                                    false        Namespace
   142  daemonsets                          ds                                     apps                           true         DaemonSet
   143  deployments                         deploy                                 apps                           true         Deployment
   144  ingresses                           ing                                    networking.k8s.io              true         Ingress
   145  `
   146  
   147  var tblEmpty = `
   148  APIVERSION    NAME        NAMESPACED
   149  `
   150  
   151  var tblNoHeader = `
   152  apps        Deployment  true
   153  networking  Ingress     true
   154  `
   155  
   156  var tblNothing = ``