diff --git a/pkg/kubernetes/apireference_test.go b/pkg/kubernetes/apireference_test.go new file mode 100644 index 0000000000..e60640f572 --- /dev/null +++ b/pkg/kubernetes/apireference_test.go @@ -0,0 +1,106 @@ +/* +Copyright 2024 The K8sGPT Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kubernetes + +import ( + "testing" + + openapi_v2 "github.com/google/gnostic/openapiv2" + "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func TestGetApiDocV2(t *testing.T) { + k8s := &K8sApiReference{ + ApiVersion: schema.GroupVersion{ + Group: "group.v1", + Version: "v1", + }, + OpenapiSchema: &openapi_v2.Document{ + Definitions: &openapi_v2.Definitions{ + AdditionalProperties: []*openapi_v2.NamedSchema{ + { + Name: "group.v1.kind", + Value: &openapi_v2.Schema{ + Title: "test", + Properties: &openapi_v2.Properties{ + AdditionalProperties: []*openapi_v2.NamedSchema{ + { + Name: "schema1", + Value: &openapi_v2.Schema{ + Title: "test", + Description: "schema1 description", + Type: &openapi_v2.TypeItem{ + Value: []string{"string"}, + }, + }, + }, + { + Name: "schema2", + Value: &openapi_v2.Schema{ + Items: &openapi_v2.ItemsItem{ + Schema: []*openapi_v2.Schema{ + { + Title: "random-schema", + }, + }, + }, + Title: "test", + XRef: "xref", + Description: "schema2 description", + Type: &openapi_v2.TypeItem{ + Value: []string{"bool"}, + }, + }, + }, + }, + }, + }, + }, + { + Name: "group", + }, + }, + }, + }, + Kind: "kind", + } + + tests := []struct { + name string + field string + expectedOutput string + }{ + { + name: "empty field", + }, + { + name: "2 schemas", + field: "schema2.schema1", + expectedOutput: "", + }, + { + name: "schema1 description", + field: "schema1", + expectedOutput: "schema1 description", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + output := k8s.GetApiDocV2(tt.field) + require.Equal(t, tt.expectedOutput, output) + }) + } +} diff --git a/pkg/kubernetes/kubernetes_test.go b/pkg/kubernetes/kubernetes_test.go new file mode 100644 index 0000000000..303ad52268 --- /dev/null +++ b/pkg/kubernetes/kubernetes_test.go @@ -0,0 +1,79 @@ +/* +Copyright 2024 The K8sGPT Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kubernetes + +import ( + "testing" + + "github.com/stretchr/testify/require" + "k8s.io/client-go/rest" +) + +func TestSliceContainsString(t *testing.T) { + tests := []struct { + name string + kubeContext string + kubeConfig string + expectedErr string + }{ + { + name: "empty config and empty context", + kubeContext: "", + kubeConfig: "", + expectedErr: "invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable", + }, + { + name: "non empty config and empty context", + kubeContext: "", + kubeConfig: "kube-config", + expectedErr: "stat kube-config: no such file or directory", + }, + { + name: "empty config and non empty context", + kubeContext: "some-context", + kubeConfig: "", + expectedErr: "context \"some-context\" does not exist", + }, + { + name: "non empty config and non empty context", + kubeContext: "minikube", + kubeConfig: "./testdata/kubeconfig", + expectedErr: "Get \"https://192.168.49.2:8443/version\": dial tcp 192.168.49.2:8443", + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + client, err := NewClient(tt.kubeContext, tt.kubeConfig) + if tt.expectedErr == "" { + require.NoError(t, err) + } else { + require.ErrorContains(t, err, tt.expectedErr) + require.Nil(t, client) + } + }) + } +} + +func TestKubernetesClient(t *testing.T) { + client := Client{ + Config: &rest.Config{ + Host: "host", + }, + } + + require.NotEmpty(t, client.GetConfig()) + require.Nil(t, client.GetClient()) + require.Nil(t, client.GetCtrlClient()) +} diff --git a/pkg/kubernetes/testdata/kubeconfig b/pkg/kubernetes/testdata/kubeconfig new file mode 100644 index 0000000000..9a51ab46d4 --- /dev/null +++ b/pkg/kubernetes/testdata/kubeconfig @@ -0,0 +1,16 @@ +apiVersion: v1 +clusters: +- cluster: + server: https://192.168.49.2:8443 + name: minikube +contexts: +- context: + cluster: minikube + namespace: default + user: minikube + name: minikube +current-context: minikube +kind: Config +preferences: {} +users: +- name: minikube \ No newline at end of file