Skip to content

Commit

Permalink
!fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
cah-patrickthiem committed Nov 15, 2024
1 parent c9e8ced commit 1197227
Showing 1 changed file with 17 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scs_k8s_tests
import (
"context"
"fmt"
"reflect"
"strings"
"testing"
"time"
Expand All @@ -29,17 +28,6 @@ func setupClientset() (*kubernetes.Clientset, error) {
return kubernetes.NewForConfig(config)
}

// hasEtcdBackup is a helper to check for etcd backup in various resources
func hasEtcdBackup[T any](items []T, nameCheck func(string) bool) bool {
for _, item := range items {
name := reflect.ValueOf(item).FieldByName("Name").String()
if nameCheck(name) {
return true
}
}
return false
}

// ==================== Test Cases ====================

func Test_scs_0215_requestLimits(t *testing.T) {
Expand All @@ -52,12 +40,9 @@ func Test_scs_0215_requestLimits(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

pods, err := clientset.CoreV1().Pods("kube-system").List(
ctx,
metav1.ListOptions{
LabelSelector: "component=kube-apiserver",
},
)
pods, err := clientset.CoreV1().Pods("kube-system").List(ctx, metav1.ListOptions{
LabelSelector: "component=kube-apiserver",
})
if err != nil || len(pods.Items) == 0 {
t.Fatalf("Failed to find kube-apiserver pod: %v", err)
}
Expand Down Expand Up @@ -102,13 +87,9 @@ func Test_scs_0215_minRequestTimeout(t *testing.T) {
}

t.Run("Check_minRequestTimeout_Configuration", func(t *testing.T) {
// List all pods in kube-system namespace
pods, err := clientset.CoreV1().Pods("kube-system").List(
context.TODO(),
metav1.ListOptions{
LabelSelector: "component=kube-apiserver",
},
)
pods, err := clientset.CoreV1().Pods("kube-system").List(context.Background(), metav1.ListOptions{
LabelSelector: "component=kube-apiserver",
})
if err != nil || len(pods.Items) == 0 {
t.Fatalf("Failed to find kube-apiserver pod: %v", err)
}
Expand Down Expand Up @@ -150,11 +131,7 @@ func Test_scs_0215_eventRateLimit(t *testing.T) {
}

for _, loc := range configLocations {
config, err := clientset.CoreV1().ConfigMaps(loc.namespace).Get(
context.TODO(),
loc.name,
metav1.GetOptions{},
)
config, err := clientset.CoreV1().ConfigMaps(loc.namespace).Get(context.Background(), loc.name, metav1.GetOptions{})
if err == nil {
if data, ok := config.Data[loc.key]; ok {
if strings.Contains(data, "eventratelimit.admission.k8s.io") {
Expand All @@ -165,7 +142,7 @@ func Test_scs_0215_eventRateLimit(t *testing.T) {
}
}

configMaps, _ := clientset.CoreV1().ConfigMaps("kube-system").List(context.TODO(), metav1.ListOptions{})
configMaps, _ := clientset.CoreV1().ConfigMaps("kube-system").List(context.Background(), metav1.ListOptions{})
for _, cm := range configMaps.Items {
if strings.Contains(cm.Name, "event-rate-limit") {
t.Logf("Found standalone EventRateLimit configuration in ConfigMap: %s", cm.Name)
Expand All @@ -188,12 +165,9 @@ func Test_scs_0215_apiPriorityAndFairness(t *testing.T) {
}

t.Run("Check_APF_Configuration", func(t *testing.T) {
pods, err := clientset.CoreV1().Pods("kube-system").List(
context.TODO(),
metav1.ListOptions{
LabelSelector: "component=kube-apiserver",
},
)
pods, err := clientset.CoreV1().Pods("kube-system").List(context.Background(), metav1.ListOptions{
LabelSelector: "component=kube-apiserver",
})
if err != nil || len(pods.Items) == 0 {
t.Fatal("Failed to find kube-apiserver pod")
}
Expand Down Expand Up @@ -227,7 +201,7 @@ func Test_scs_0215_rateLimitValues(t *testing.T) {
"burst": "20000",
}

configMaps, _ := clientset.CoreV1().ConfigMaps("kube-system").List(context.TODO(), metav1.ListOptions{})
configMaps, _ := clientset.CoreV1().ConfigMaps("kube-system").List(context.Background(), metav1.ListOptions{})
for _, cm := range configMaps.Items {
var config string
switch {
Expand Down Expand Up @@ -268,12 +242,9 @@ func Test_scs_0215_etcdCompaction(t *testing.T) {
}

t.Run("Check_Etcd_Compaction_Settings", func(t *testing.T) {
pods, err := clientset.CoreV1().Pods("kube-system").List(
context.TODO(),
metav1.ListOptions{
LabelSelector: "component=etcd",
},
)
pods, err := clientset.CoreV1().Pods("kube-system").List(context.Background(), metav1.ListOptions{
LabelSelector: "component=etcd",
})
if err != nil || len(pods.Items) == 0 {
t.Skip("No etcd pods found")
return
Expand Down Expand Up @@ -317,7 +288,7 @@ func Test_scs_0215_etcdBackup(t *testing.T) {
}

t.Run("Check_Etcd_Backup_Configuration", func(t *testing.T) {
cronJobs, err := clientset.BatchV1().CronJobs("").List(context.TODO(), metav1.ListOptions{})
cronJobs, err := clientset.BatchV1().CronJobs("").List(context.Background(), metav1.ListOptions{})
if err == nil {
for _, job := range cronJobs.Items {
if strings.Contains(strings.ToLower(job.Name), "etcd") &&
Expand All @@ -338,11 +309,7 @@ func Test_scs_0215_certificateRotation(t *testing.T) {
}

t.Run("Check_Certificate_Controller", func(t *testing.T) {
_, err := clientset.AppsV1().Deployments("cert-manager").Get(
context.TODO(),
"cert-manager",
metav1.GetOptions{},
)
_, err := clientset.AppsV1().Deployments("cert-manager").Get(context.Background(), "cert-manager", metav1.GetOptions{})
if err != nil {
t.Error("cert-manager not found - certificate controller required")
} else {
Expand Down

0 comments on commit 1197227

Please sign in to comment.