Skip to content

Commit

Permalink
Merge branch 'main' into release-please--branches--main
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexsJones committed Mar 14, 2024
2 parents 946b3b7 + 28e19a9 commit 50af671
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
106 changes: 106 additions & 0 deletions 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)
})
}
}
79 changes: 79 additions & 0 deletions 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())
}
16 changes: 16 additions & 0 deletions 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

0 comments on commit 50af671

Please sign in to comment.