Skip to content

Commit

Permalink
Adding e2e tests to validate fetching different CRD versions
Browse files Browse the repository at this point in the history
  • Loading branch information
marosset committed Sep 22, 2021
1 parent bb02b77 commit d903a63
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
79 changes: 78 additions & 1 deletion admission-webhook/integration_tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
Expand All @@ -29,6 +30,20 @@ const (
ymlExtension = ".yml"
)

var (
v1alpha1Resource = schema.GroupVersionResource{
Group: "windows.k8s.io",
Version: "v1alpha1",
Resource: "gmsacredentialspecs",
}

v1Resource = schema.GroupVersionResource{
Group: "windows.k8s.io",
Version: "v1",
Resource: "gmsacredentialspecs",
}
)

func TestHappyPathWithPodLevelCredSpec(t *testing.T) {
testName := "happy-path-with-pod-level-cred-spec"
credSpecTemplates := []string{"credspec-0"}
Expand Down Expand Up @@ -199,7 +214,7 @@ func TestCannotUpdateExistingPodLevelGMSASettings(t *testing.T) {
defer tearDownFunc()

// let's check that the pod has come up correctly, and has the correct GMSA cred inlined
pod, err := kubeClient(t).CoreV1().Pods(testConfig.Namespace).Get(context.Background() ,testName, metav1.GetOptions{})
pod, err := kubeClient(t).CoreV1().Pods(testConfig.Namespace).Get(context.Background(), testName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -273,6 +288,68 @@ func TestPossibleToUpdatePodWithExistingGMSASettings(t *testing.T) {
assert.True(t, success)
}

func TestDeployV1Alpha1CredSpecGetAllVersions(t *testing.T) {
testName := "deploy-v1alpha1-credspec-get-all-versions"
credSpecTemplates := []string{"credspec-0", "credspec-1"}

testConfig, tearDownFunc := integrationTestSetup(t, testName, credSpecTemplates, nil)
defer tearDownFunc()

// ensure CredSpec specified v1 CRD
templatePath := renderTemplate(t, testConfig, "credspec-1")
b, err := ioutil.ReadFile(templatePath)
if err != nil {
t.Fatal(err)
}
s := string(b)
assert.Contains(t, s, "apiVersion: windows.k8s.io/v1alpha1\n")

client := dynamicClient(t)
resourceName := "deploy-v1alpha1-credspec-get-all-versions-cred-spec-1"
v1alpha1CredSpec, err := client.Resource(v1alpha1Resource).Get(context.TODO(), resourceName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

v1CredSpec, err := client.Resource(v1Resource).Get(context.TODO(), resourceName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

assert.Equal(t, v1alpha1CredSpec.Object["credSpec"], v1CredSpec.Object["credSpec"])
}

func TestDeployV1CredSpecGetAllVersions(t *testing.T) {
testName := "deploy-v1-credspec-get-all-versions"
credSpecTemplates := []string{"credspec-0", "credspec-1"}

testConfig, tearDownFunc := integrationTestSetup(t, testName, credSpecTemplates, nil)
defer tearDownFunc()

// ensure CredSpec specified v1 CRD
templatePath := renderTemplate(t, testConfig, "credspec-0")
b, err := ioutil.ReadFile(templatePath)
if err != nil {
t.Fatal(err)
}
s := string(b)
assert.Contains(t, s, "apiVersion: windows.k8s.io/v1\n")

client := dynamicClient(t)
resourceName := "deploy-v1-credspec-get-all-versions-cred-spec-0"
v1alpha1CredSpec, err := client.Resource(v1alpha1Resource).Get(context.TODO(), resourceName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

v1CredSpec, err := client.Resource(v1Resource).Get(context.TODO(), resourceName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

assert.Equal(t, v1alpha1CredSpec.Object["credSpec"], v1CredSpec.Object["credSpec"])
}

/* Helpers */

type testConfig struct {
Expand Down
19 changes: 18 additions & 1 deletion admission-webhook/integration_tests/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

func kubeClient(t *testing.T) kubernetes.Interface {
func clientConfig(t *testing.T) *rest.Config {
kubeConfigPath, err := homedir.Expand(kubeconfig())
if err != nil {
t.Fatal(err)
Expand All @@ -26,6 +28,11 @@ func kubeClient(t *testing.T) kubernetes.Interface {
t.Fatal(err)
}

return config
}

func kubeClient(t *testing.T) kubernetes.Interface {
config := clientConfig(t)
client, err := kubernetes.NewForConfig(config)
if err != nil {
t.Fatal(err)
Expand All @@ -34,6 +41,16 @@ func kubeClient(t *testing.T) kubernetes.Interface {
return client
}

func dynamicClient(t *testing.T) dynamic.Interface {
config := clientConfig(t)
client, err := dynamic.NewForConfig(config)
if err != nil {
t.Fatal(err)
}

return client
}

// getNodes returns the nodes present in the cluster.
func getNodes(t *testing.T) []corev1.Node {
client := kubeClient(t)
Expand Down

0 comments on commit d903a63

Please sign in to comment.