|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + docker "github.com/fsouza/go-dockerclient" |
| 7 | +) |
| 8 | + |
| 9 | +// TestExecJob_InitializeRuntimeFields_NilClient tests that InitializeRuntimeFields |
| 10 | +// handles a nil client gracefully |
| 11 | +func TestExecJob_InitializeRuntimeFields_NilClient(t *testing.T) { |
| 12 | + job := &ExecJob{} |
| 13 | + job.InitializeRuntimeFields() |
| 14 | + |
| 15 | + // Should not panic and dockerOps should remain nil |
| 16 | + if job.dockerOps != nil { |
| 17 | + t.Error("Expected dockerOps to be nil when client is nil") |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// TestExecJob_InitializeRuntimeFields_WithClient tests that InitializeRuntimeFields |
| 22 | +// initializes dockerOps when a client is set |
| 23 | +func TestExecJob_InitializeRuntimeFields_WithClient(t *testing.T) { |
| 24 | + client, _ := docker.NewClient("unix:///var/run/docker.sock") |
| 25 | + job := &ExecJob{ |
| 26 | + Client: client, |
| 27 | + } |
| 28 | + |
| 29 | + job.InitializeRuntimeFields() |
| 30 | + |
| 31 | + // dockerOps should now be initialized |
| 32 | + if job.dockerOps == nil { |
| 33 | + t.Error("Expected dockerOps to be initialized when client is set") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// TestExecJob_InitializeRuntimeFields_Idempotent tests that InitializeRuntimeFields |
| 38 | +// can be called multiple times without side effects |
| 39 | +func TestExecJob_InitializeRuntimeFields_Idempotent(t *testing.T) { |
| 40 | + client, _ := docker.NewClient("unix:///var/run/docker.sock") |
| 41 | + job := &ExecJob{ |
| 42 | + Client: client, |
| 43 | + } |
| 44 | + |
| 45 | + job.InitializeRuntimeFields() |
| 46 | + firstOps := job.dockerOps |
| 47 | + |
| 48 | + job.InitializeRuntimeFields() |
| 49 | + secondOps := job.dockerOps |
| 50 | + |
| 51 | + // Should be the same instance |
| 52 | + if firstOps != secondOps { |
| 53 | + t.Error("Expected dockerOps to remain the same after multiple InitializeRuntimeFields calls") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestExecJob_NoNilPointerAfterInitialization verifies that an ExecJob |
| 58 | +// created without NewExecJob can still access dockerOps without panic |
| 59 | +// after calling InitializeRuntimeFields |
| 60 | +func TestExecJob_NoNilPointerAfterInitialization(t *testing.T) { |
| 61 | + client, _ := docker.NewClient("unix:///var/run/docker.sock") |
| 62 | + |
| 63 | + // Simulate how a job is created from config files/labels |
| 64 | + job := &ExecJob{ |
| 65 | + BareJob: BareJob{ |
| 66 | + Name: "test-job", |
| 67 | + Command: "echo hello", |
| 68 | + }, |
| 69 | + Client: client, |
| 70 | + Container: "test-container", |
| 71 | + User: "nobody", |
| 72 | + } |
| 73 | + |
| 74 | + // Initialize runtime fields (this is what the config loader should do) |
| 75 | + job.InitializeRuntimeFields() |
| 76 | + |
| 77 | + // Verify dockerOps is initialized |
| 78 | + if job.dockerOps == nil { |
| 79 | + t.Fatal("Expected dockerOps to be initialized after InitializeRuntimeFields") |
| 80 | + } |
| 81 | + |
| 82 | + // Create a context |
| 83 | + scheduler := NewScheduler(&SimpleLogger{}) |
| 84 | + exec, err := NewExecution() |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("Failed to create execution: %v", err) |
| 87 | + } |
| 88 | + defer exec.Cleanup() |
| 89 | + |
| 90 | + ctx := NewContext(scheduler, job, exec) |
| 91 | + |
| 92 | + // This should not panic even though job wasn't created with NewExecJob |
| 93 | + // We expect an error because the container doesn't exist, but not a panic |
| 94 | + _, err = job.buildExec(ctx) |
| 95 | + |
| 96 | + // Verify no nil pointer dereference occurred |
| 97 | + if job.dockerOps == nil { |
| 98 | + t.Error("dockerOps became nil during execution") |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// TestExecJob_RunWithoutNewExecJob_NoPanic is a critical regression test |
| 103 | +// that verifies the exact issue from the bug report is fixed: |
| 104 | +// ExecJob.Run() should not panic when the job was created via config deserialization |
| 105 | +func TestExecJob_RunWithoutNewExecJob_NoPanic(t *testing.T) { |
| 106 | + client, _ := docker.NewClient("unix:///var/run/docker.sock") |
| 107 | + |
| 108 | + // Simulate exactly how mapstructure creates an ExecJob from config |
| 109 | + job := &ExecJob{ |
| 110 | + BareJob: BareJob{ |
| 111 | + Name: "test-job", |
| 112 | + Command: "echo test", |
| 113 | + Schedule: "@every 1h", |
| 114 | + }, |
| 115 | + Client: client, |
| 116 | + Container: "nonexistent-container-for-test", |
| 117 | + User: "nobody", |
| 118 | + TTY: false, |
| 119 | + } |
| 120 | + |
| 121 | + // This is what the config loader does after deserialization |
| 122 | + job.InitializeRuntimeFields() |
| 123 | + |
| 124 | + // Verify critical preconditions |
| 125 | + if job.dockerOps == nil { |
| 126 | + t.Fatal("dockerOps should be initialized after InitializeRuntimeFields") |
| 127 | + } |
| 128 | + |
| 129 | + // Create execution context |
| 130 | + scheduler := NewScheduler(&SimpleLogger{}) |
| 131 | + exec, err := NewExecution() |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("Failed to create execution: %v", err) |
| 134 | + } |
| 135 | + defer exec.Cleanup() |
| 136 | + |
| 137 | + ctx := NewContext(scheduler, job, exec) |
| 138 | + |
| 139 | + // This is the critical test: Run() should not panic |
| 140 | + // We wrap in a recover to catch any panic |
| 141 | + didPanic := false |
| 142 | + func() { |
| 143 | + defer func() { |
| 144 | + if r := recover(); r != nil { |
| 145 | + didPanic = true |
| 146 | + t.Errorf("ExecJob.Run() panicked: %v", r) |
| 147 | + } |
| 148 | + }() |
| 149 | + // Call Run() - this was causing nil pointer panic before the fix |
| 150 | + _ = job.Run(ctx) |
| 151 | + }() |
| 152 | + |
| 153 | + if didPanic { |
| 154 | + t.Error("ExecJob.Run() should not panic even when container doesn't exist") |
| 155 | + } |
| 156 | + |
| 157 | + // Verify dockerOps is still valid after Run() |
| 158 | + if job.dockerOps == nil { |
| 159 | + t.Error("dockerOps should remain initialized after Run()") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// TestExecJob_StartExec_WithoutInitialization_Panics verifies that without |
| 164 | +// InitializeRuntimeFields(), the job would indeed panic (regression safety) |
| 165 | +func TestExecJob_StartExec_WithoutInitialization_Panics(t *testing.T) { |
| 166 | + client, _ := docker.NewClient("unix:///var/run/docker.sock") |
| 167 | + |
| 168 | + // Create job WITHOUT calling InitializeRuntimeFields() |
| 169 | + job := &ExecJob{ |
| 170 | + BareJob: BareJob{ |
| 171 | + Name: "uninit-job", |
| 172 | + Command: "echo test", |
| 173 | + }, |
| 174 | + Client: client, |
| 175 | + Container: "test", |
| 176 | + User: "nobody", |
| 177 | + } |
| 178 | + |
| 179 | + // Verify dockerOps is nil (not initialized) |
| 180 | + if job.dockerOps != nil { |
| 181 | + t.Fatal("dockerOps should be nil for this test to be valid") |
| 182 | + } |
| 183 | + |
| 184 | + // Create execution context |
| 185 | + scheduler := NewScheduler(&SimpleLogger{}) |
| 186 | + exec, err := NewExecution() |
| 187 | + if err != nil { |
| 188 | + t.Fatalf("Failed to create execution: %v", err) |
| 189 | + } |
| 190 | + defer exec.Cleanup() |
| 191 | + |
| 192 | + ctx := NewContext(scheduler, job, exec) |
| 193 | + |
| 194 | + // Verify that buildExec() WOULD panic without initialization |
| 195 | + didPanic := false |
| 196 | + func() { |
| 197 | + defer func() { |
| 198 | + if r := recover(); r != nil { |
| 199 | + didPanic = true |
| 200 | + } |
| 201 | + }() |
| 202 | + _, _ = job.buildExec(ctx) |
| 203 | + }() |
| 204 | + |
| 205 | + if !didPanic { |
| 206 | + t.Error("Expected buildExec() to panic when dockerOps is nil (verifies test validity)") |
| 207 | + } |
| 208 | +} |
0 commit comments