8000 🧪 Fix Kind E2E audit scenario failures by abahmed · Pull Request #520 · abahmed/kwatch · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion test/e2e/harness/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func matchingEntries(entries []AuditEntry, match AuditMatch) []AuditEntry {
if match.Namespace != "" && entry.Namespace != match.Namespace {
continue
}
if match.Resource != "" && entry.Name != match.Resource {
if match.Resource != "" && !resourceMatches(entry, match) {
continue
}
if match.Reason != "" && entry.Reason != match.Reason {
Expand All @@ -127,3 +127,11 @@ func matchingEntries(entries []AuditEntry, match AuditMatch) []AuditEntry {
}
return result
}

func resourceMatches(entry AuditEntry, match AuditMatch) bool {
if entry.Name == match.Resource {
return true
}
return match.Namespace != "" &&
entry.Name == match.Namespace+"/"+match.Resource
}
53 changes: 53 additions & 0 deletions test/e2e/harness/audit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build e2e

package harness

import "testing"

func TestMatchingEntriesAcceptsNamespacedResourceNames(t *testing.T) {
entries := []AuditEntry{{
Namespace: "apps",
Name: "apps/api",
Reason: "DeploymentUnavailable",
Action: "create",
}}

tests := []struct {
name string
match AuditMatch
want int
}{
{
name: "short name with namespace",
match: AuditMatch{
Namespace: "apps", Resource: "api",
Reason: "DeploymentUnavailable", Count: 1,
},
want: 1,
},
{
name: "fully qualified name",
match: AuditMatch{
Namespace: "apps", Resource: "apps/api",
Reason: "DeploymentUnavailable", Count: 1,
},
want: 1,
},
{
name: "wrong namespace",
match: AuditMatch{
Namespace: "other", Resource: "api",
Reason: "DeploymentUnavailable", Count: 1,
},
want: 0,
},
}

< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker="+"> for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := len(matchingEntries(entries, tt.match)); got != tt.want {
t.Fatalf("matching entries = %d, want %d", got, tt.want)
}
})
}
}
16 changes: 15 additions & 1 deletion test/e2e/scenarios/configuration_reload_test.go
< 8000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ func TestScenarioInvalidLiveConfiguration(t *testing.T) {
}()
waitCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
if err := e.AssertHealthy(waitCtx); err != nil {
if err := wait.PollUntilContextTimeout(
waitCtx, 500*time.Millisecond, 2*time.Minute, true,
func(ctx context.Context) (bool, error) {
return e.AssertHealthy(ctx) == nil, nil
},
); err != nil {
t.Fatal(err)
}
if err := e.AssertNoRuntimePanic(ctx); err != nil {
Expand Down Expand Up @@ -146,6 +151,15 @@ func TestScenarioInvalidStartupConfiguration(t *testing.T) {
{Name: "KWATCH_LEADER_ELECTION_NAME",
Value: "kwatch-invalid-startup"},
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: boolPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
VolumeMounts: []corev1.VolumeMount{{
Name: "config", MountPath: "/config",
}},
Expand Down
20 changes: 19 additions & 1 deletion test/e2e/scenarios/edge_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,14 @@ func TestScenarioMissingServiceAccountReference(t *testing.T) {
t.Fatal(err)
}
defer cleanupNamespace(t, e, namespace)
_, err := e.Client.CoreV1().Pods(namespace).Create(ctx, &corev1.Pod{
_, err := e.Client.CoreV1().ServiceAccounts(namespace).Create(ctx,
&corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{Name: "missing-service-account"},
}, metav1.CreateOptions{})
if err != nil {
t.Fatal(err)
}
pod, err := e.Client.CoreV1().Pods(namespace).Create(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "missing-service-account"},
Spec: corev1.PodSpec{
ServiceAccountName: "missing-service-account",
Expand All @@ -198,6 +205,17 @@ func TestScenarioMissingServiceAccountReference(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := e.Client.CoreV1().ServiceAccounts(namespace).Delete(
ctx, "missing-service-account", metav1.DeleteOptions{},
); err != nil {
t.Fatal(err)
}
pod.Labels = map[string]string{"kwatch-e2e": "missing-service-account"}
if _, err := e.Client.CoreV1().Pods(namespace).Update(
ctx, pod, metav1.UpdateOptions{},
); err != nil {
t.Fatal(err)
}
if _, err := e.Audit.WaitFor(ctx, harness.AuditMatch{
Namespace: namespace, Resource: "missing-service-account",
Reason: "ServiceAccountMissing", Count: 1,
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/testdata/kind-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: kindest/node:v1.31.2
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
scheduler:
extraArgs:
bind-address: 0.0.0.0
- role: worker
image: kindest/node:v1.31.2
- role: worker
Expand Down
Loading
0