Skip to content

Commit 0505243

Browse files
committed
Review: Add tests for failure paths when querying the k8s API for NIM job list
1 parent 2726bc3 commit 0505243

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

pkg/jobs/nim_job_list_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,125 @@ func TestNIMJobList_DqliteDump_SkippedVsPodListError(t *testing.T) {
497497
})
498498
}
499499
}
500+
501+
func TestNIMJobList_CommandExecutionFailure(t *testing.T) {
502+
tests := []struct {
503+
name string
504+
jobName string
505+
jobIndex int
506+
podNamePattern string
507+
expectedContainer string
508+
expectedCommands [][]string
509+
}{
510+
{
511+
name: "exec-apigw-nginx-t command failure",
512+
jobName: "exec-apigw-nginx-t",
513+
jobIndex: 0,
514+
podNamePattern: "apigw",
515+
expectedContainer: "apigw",
516+
expectedCommands: [][]string{{"/usr/sbin/nginx", "-T"}},
517+
},
518+
{
519+
name: "exec-apigw-nginx-version command failure",
520+
jobName: "exec-apigw-nginx-version",
521+
jobIndex: 1,
522+
podNamePattern: "apigw",
523+
expectedContainer: "apigw",
524+
expectedCommands: [][]string{{"/usr/sbin/nginx", "-v"}},
525+
},
526+
{
527+
name: "exec-clickhouse-version command failure",
528+
jobName: "exec-clickhouse-version",
529+
jobIndex: 2,
530+
podNamePattern: "clickhouse",
531+
expectedContainer: "clickhouse-server",
532+
expectedCommands: [][]string{{"clickhouse-server", "--version"}},
533+
},
534+
{
535+
name: "exec-dqlite-dump command failure",
536+
jobName: "exec-dqlite-dump",
537+
jobIndex: 4,
538+
podNamePattern: "core",
539+
expectedContainer: "core",
540+
expectedCommands: [][]string{
541+
{"/etc/nms/scripts/dqlite-backup", "-n", "core", "-c", "/etc/nms/nms.conf", "-a", "0.0.0.0:7891", "-o", "/tmp/core.sql", "-k"},
542+
},
543+
},
544+
}
545+
546+
for _, tt := range tests {
547+
t.Run(tt.name, func(t *testing.T) {
548+
tmpDir := t.TempDir()
549+
var logOutput bytes.Buffer
550+
551+
// Create pod matching the pattern
552+
pod := &corev1.Pod{
553+
ObjectMeta: metav1.ObjectMeta{
554+
Name: tt.podNamePattern + "-test-pod",
555+
Namespace: "default",
556+
},
557+
Spec: corev1.PodSpec{
558+
Containers: []corev1.Container{
559+
{Name: tt.expectedContainer, Image: "test:latest"},
560+
},
561+
},
562+
}
563+
564+
client := fake.NewSimpleClientset(pod)
565+
566+
var executedCommands [][]string
567+
dc := &data_collector.DataCollector{
568+
BaseDir: tmpDir,
569+
Namespaces: []string{"default"},
570+
Logger: log.New(&logOutput, "", 0),
571+
K8sCoreClientSet: client,
572+
PodExecutor: func(namespace, podName, containerName string, command []string, ctx context.Context) ([]byte, error) {
573+
// Track executed commands
574+
executedCommands = append(executedCommands, command)
575+
576+
// Verify correct parameters
577+
assert.Equal(t, "default", namespace)
578+
assert.Equal(t, tt.podNamePattern+"-test-pod", podName)
579+
assert.Equal(t, tt.expectedContainer, containerName)
580+
581+
// Return error to test failure path
582+
return nil, fmt.Errorf("command execution failed: %v", command)
583+
},
584+
}
585+
586+
// Execute the specific job
587+
jobs := NIMJobList()
588+
job := jobs[tt.jobIndex]
589+
assert.Equal(t, tt.jobName, job.Name)
590+
591+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
592+
defer cancel()
593+
594+
ch := make(chan JobResult, 1)
595+
go job.Execute(dc, ctx, ch)
596+
597+
select {
598+
case result := <-ch:
599+
logContent := logOutput.String()
600+
601+
// Verify the error was set
602+
assert.NotNil(t, result.Error, "Job should have error when command execution fails")
603+
assert.Contains(t, result.Error.Error(), "command execution failed")
604+
605+
// Verify the error was logged
606+
assert.Contains(t, logContent, "Command execution")
607+
assert.Contains(t, logContent, "failed for pod")
608+
assert.Contains(t, logContent, "command execution failed")
609+
610+
// Verify no files were created when command execution fails
611+
assert.Empty(t, result.Files, "No files should be created when command execution fails")
612+
613+
// Verify expected commands were called
614+
assert.NotEmpty(t, executedCommands, "Should have executed commands")
615+
616+
case <-ctx.Done():
617+
t.Fatalf("Job %s timed out", tt.jobName)
618+
}
619+
})
620+
}
621+
}

0 commit comments

Comments
 (0)