github.com/splunk/dan1-qbec@v0.7.3/internal/remote/k8smeta/meta_test.go (about)

     1  /*
     2     Copyright 2019 Splunk Inc.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package k8smeta
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/splunk/qbec/internal/model"
    28  	"github.com/splunk/qbec/internal/sio"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"k8s.io/apimachinery/pkg/runtime/schema"
    33  )
    34  
    35  type disco struct {
    36  	Groups        *metav1.APIGroupList               `json:"groups"`
    37  	ResourceLists map[string]*metav1.APIResourceList `json:"resourceLists"`
    38  }
    39  
    40  func (d *disco) ServerGroups() (*metav1.APIGroupList, error) {
    41  	return d.Groups, nil
    42  }
    43  
    44  func (d *disco) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
    45  	parts := strings.SplitN(groupVersion, "/", 2)
    46  	var group, version string
    47  	if len(parts) == 2 {
    48  		group, version = parts[0], parts[1]
    49  	} else {
    50  		version = parts[0]
    51  	}
    52  	key := fmt.Sprintf("%s:%s", group, version)
    53  	rl := d.ResourceLists[key]
    54  	if rl == nil {
    55  		return nil, fmt.Errorf("no resources for %s", groupVersion)
    56  	}
    57  	return rl, nil
    58  }
    59  
    60  func getServerMetadata(t *testing.T, verbosity int) *Resources {
    61  	var d disco
    62  	b, err := ioutil.ReadFile(filepath.Join("testdata", "metadata.json"))
    63  	require.Nil(t, err)
    64  	err = json.Unmarshal(b, &d)
    65  	require.Nil(t, err)
    66  	sm, err := NewResources(&d, ResourceOpts{})
    67  	require.Nil(t, err)
    68  	if verbosity > 0 {
    69  		sm.Dump(sio.Debugln)
    70  	}
    71  	return sm
    72  }
    73  
    74  func loadObject(t *testing.T, file string) model.K8sObject {
    75  	b, err := ioutil.ReadFile(filepath.Join("testdata", file))
    76  	require.Nil(t, err)
    77  	var d map[string]interface{}
    78  	err = json.Unmarshal(b, &d)
    79  	require.Nil(t, err)
    80  	return model.NewK8sObject(d)
    81  }
    82  
    83  func TestMetadataCanonical(t *testing.T) {
    84  	a := assert.New(t)
    85  	sm := getServerMetadata(t, 2)
    86  
    87  	canonDeployment := schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "Deployment"}
    88  
    89  	tests := []struct {
    90  		name     string
    91  		expected schema.GroupVersionKind
    92  		input    schema.GroupVersionKind
    93  	}{
    94  		{
    95  			name:     "v1beta1-deployment",
    96  			expected: canonDeployment,
    97  			input:    schema.GroupVersionKind{Group: "apps", Version: "v1beta1", Kind: "Deployment"},
    98  		},
    99  		{
   100  			name:     "v1-deployment",
   101  			expected: canonDeployment,
   102  			input:    schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"},
   103  		},
   104  		{
   105  			name:     "self-deployment",
   106  			expected: canonDeployment,
   107  			input:    canonDeployment,
   108  		},
   109  		{
   110  			name:     "v1beta2-replicaset",
   111  			expected: schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "ReplicaSet"},
   112  			input:    schema.GroupVersionKind{Group: "apps", Version: "v1beta2", Kind: "ReplicaSet"},
   113  		},
   114  		{
   115  			name:     "self-replicaset",
   116  			expected: schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "ReplicaSet"},
   117  			input:    schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "ReplicaSet"},
   118  		},
   119  		{
   120  			name:     "self-cronjob",
   121  			expected: schema.GroupVersionKind{Group: "batch", Version: "v1beta1", Kind: "CronJob"},
   122  			input:    schema.GroupVersionKind{Group: "batch", Version: "v1beta1", Kind: "CronJob"},
   123  		},
   124  		{
   125  			name:     "self-job",
   126  			expected: schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"},
   127  			input:    schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"},
   128  		},
   129  	}
   130  
   131  	for _, test := range tests {
   132  		t.Run(test.name, func(t *testing.T) {
   133  			canon, err := sm.CanonicalGroupVersionKind(test.input)
   134  			require.Nil(t, err)
   135  			a.EqualValues(test.expected, canon)
   136  		})
   137  	}
   138  }
   139  
   140  func TestMetadataOther(t *testing.T) {
   141  	sm := getServerMetadata(t, 0)
   142  	res := sm.APIResource(schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "Deployment"})
   143  	require.NotNil(t, res)
   144  
   145  	res = sm.APIResource(schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"})
   146  	require.NotNil(t, res)
   147  
   148  	res = sm.APIResource(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "FooBar"})
   149  	require.Nil(t, res)
   150  
   151  	_, err := sm.CanonicalGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "FooBar"})
   152  	require.NotNil(t, err)
   153  }