@@ -8,13 +8,17 @@ import (
88
99. "github.com/onsi/ginkgo/v2"
1010. "github.com/onsi/gomega"
11+ format "github.com/onsi/gomega/format"
1112batchv1 "k8s.io/api/batch/v1"
1213v1 "k8s.io/api/core/v1"
14+ "k8s.io/utils/pointer"
1315"k8s.io/utils/ptr"
1416
1517"github.com/rancher/system-upgrade-controller/e2e/framework"
1618upgradeapiv1 "github.com/rancher/system-upgrade-controller/pkg/apis/upgrade.cattle.io/v1"
1719metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+ _ "k8s.io/kubernetes/test/utils/format"
1822)
1923
2024var _ = Describe ("Job Generation" , func () {
@@ -137,29 +141,7 @@ var _ = Describe("Job Generation", func() {
137141Expect (jobs [0 ].Spec .Template .Spec .InitContainers [0 ].Args ).To (ContainElement (ContainSubstring ("!upgrade.cattle.io/controller" )))
138142Expect (jobs [0 ].Spec .Template .Spec .InitContainers [0 ].Args ).To (ContainElement (ContainSubstring ("component notin (sonobuoy)" )))
139143})
140- AfterEach (func () {
141- if CurrentSpecReport ().Failed () {
142- podList , _ := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).List (context .Background (), metav1.ListOptions {})
143- for _ , pod := range podList .Items {
144- containerNames := []string {}
145- for _ , container := range pod .Spec .InitContainers {
146- containerNames = append (containerNames , container .Name )
147- }
148- for _ , container := range pod .Spec .Containers {
149- containerNames = append (containerNames , container .Name )
150- }
151- for _ , container := range containerNames {
152- reportName := fmt .Sprintf ("podlogs-%s-%s" , pod .Name , container )
153- logs := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).GetLogs (pod .Name , & v1.PodLogOptions {Container : container })
154- if logStreamer , err := logs .Stream (context .Background ()); err == nil {
155- if podLogs , err := io .ReadAll (logStreamer ); err == nil {
156- AddReportEntry (reportName , string (podLogs ))
157- }
158- }
159- }
160- }
161- }
162- })
144+ AfterEach (CollectLogsOnFailure (e2e ))
163145})
164146
165147When ("fails because of invalid time window" , func () {
@@ -206,29 +188,7 @@ var _ = Describe("Job Generation", func() {
206188Expect (jobs [0 ].Status .Active ).To (BeNumerically ("==" , 0 ))
207189Expect (jobs [0 ].Status .Failed ).To (BeNumerically ("==" , 0 ))
208190})
209- AfterEach (func () {
210- if CurrentSpecReport ().Failed () {
211- podList , _ := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).List (context .Background (), metav1.ListOptions {})
212- for _ , pod := range podList .Items {
213- containerNames := []string {}
214- for _ , container := range pod .Spec .InitContainers {
215- containerNames = append (containerNames , container .Name )
216- }
217- for _ , container := range pod .Spec .Containers {
218- containerNames = append (containerNames , container .Name )
219- }
220- for _ , container := range containerNames {
221- reportName := fmt .Sprintf ("podlogs-%s-%s" , pod .Name , container )
222- logs := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).GetLogs (pod .Name , & v1.PodLogOptions {Container : container })
223- if logStreamer , err := logs .Stream (context .Background ()); err == nil {
224- if podLogs , err := io .ReadAll (logStreamer ); err == nil {
225- AddReportEntry (reportName , string (podLogs ))
226- }
227- }
228- }
229- }
230- }
231- })
191+ AfterEach (CollectLogsOnFailure (e2e ))
232192})
233193
234194When ("fails because of invalid post complete delay" , func () {
@@ -275,32 +235,10 @@ var _ = Describe("Job Generation", func() {
275235Expect (jobs [0 ].Status .Active ).To (BeNumerically ("==" , 0 ))
276236Expect (jobs [0 ].Status .Failed ).To (BeNumerically ("==" , 0 ))
277237})
278- AfterEach (func () {
279- if CurrentSpecReport ().Failed () {
280- podList , _ := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).List (context .Background (), metav1.ListOptions {})
281- for _ , pod := range podList .Items {
282- containerNames := []string {}
283- for _ , container := range pod .Spec .InitContainers {
284- containerNames = append (containerNames , container .Name )
285- }
286- for _ , container := range pod .Spec .Containers {
287- containerNames = append (containerNames , container .Name )
288- }
289- for _ , container := range containerNames {
290- reportName := fmt .Sprintf ("podlogs-%s-%s" , pod .Name , container )
291- logs := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).GetLogs (pod .Name , & v1.PodLogOptions {Container : container })
292- if logStreamer , err := logs .Stream (context .Background ()); err == nil {
293- if podLogs , err := io .ReadAll (logStreamer ); err == nil {
294- AddReportEntry (reportName , string (podLogs ))
295- }
296- }
297- }
298- }
299- }
300- })
238+ AfterEach (CollectLogsOnFailure (e2e ))
301239})
302240
303- When ("updated secret should not change hash" , func () {
241+ When ("updated secret does not change hash" , func () {
304242var (
305243err error
306244plan * upgradeapiv1.Plan
@@ -347,5 +285,82 @@ var _ = Describe("Job Generation", func() {
347285It ("hash should be equal" , func () {
348286Expect (plan .Status .LatestHash ).Should (Equal (hash ))
349287})
288+ AfterEach (CollectLogsOnFailure (e2e ))
289+ })
290+
291+ When ("job failure message is reflected in plan status condition" , func () {
292+ var (
293+ err error
294+ plan * upgradeapiv1.Plan
295+ jobs []batchv1.Job
296+ )
297+ BeforeEach (func () {
298+ plan = e2e .NewPlan ("job-deadline-" , "library/alpine:3.18" , []string {"sh" , "-c" }, "sleep 3600" )
299+ plan .Spec .JobActiveDeadlineSecs = pointer .Int64 (15 )
300+ plan .Spec .Version = "latest"
301+ plan .Spec .Concurrency = 1
302+ plan .Spec .ServiceAccountName = e2e .Namespace .Name
303+ plan .Spec .NodeSelector = & metav1.LabelSelector {
304+ MatchExpressions : []metav1.LabelSelectorRequirement {{
305+ Key : "node-role.kubernetes.io/control-plane" ,
306+ Operator : metav1 .LabelSelectorOpDoesNotExist ,
307+ }},
308+ }
309+ plan , err = e2e .CreatePlan (plan )
310+ Expect (err ).ToNot (HaveOccurred ())
311+
312+ plan , err = e2e .WaitForPlanCondition (plan .Name , upgradeapiv1 .PlanLatestResolved , 30 * time .Second )
313+ Expect (err ).ToNot (HaveOccurred ())
314+ })
315+ It ("message should contain deadline reason and message" , func () {
316+ jobs , err = e2e .WaitForPlanJobs (plan , 1 , 120 * time .Second )
317+ Expect (err ).ToNot (HaveOccurred ())
318+ Expect (jobs ).To (HaveLen (1 ))
319+ Expect (jobs [0 ].Status .Succeeded ).To (BeNumerically ("==" , 0 ))
320+ Expect (jobs [0 ].Status .Active ).To (BeNumerically ("==" , 0 ))
321+ Expect (jobs [0 ].Status .Failed ).To (BeNumerically (">=" , 1 ))
322+
323+ Eventually (e2e .GetPlan ).
324+ WithArguments (plan .Name , metav1.GetOptions {}).
325+ WithTimeout (45 * time .Second ).
326+ Should (SatisfyAll (
327+ WithTransform (upgradeapiv1 .PlanComplete .IsTrue , BeFalse ()),
328+ WithTransform (upgradeapiv1 .PlanComplete .GetReason , Equal ("JobFailed" )),
329+ WithTransform (upgradeapiv1 .PlanComplete .GetMessage , ContainSubstring ("DeadlineExceeded: Job was active longer than specified deadline" )),
330+ ))
331+ })
332+ AfterEach (CollectLogsOnFailure (e2e ))
350333})
351334})
335+
336+ func CollectLogsOnFailure (e2e * framework.Client ) func () {
337+ return func () {
338+ if CurrentSpecReport ().Failed () {
339+ planList , _ := e2e .UpgradeClientSet .UpgradeV1 ().Plans (e2e .Namespace .Name ).List (context .Background (), metav1.ListOptions {})
340+ AddReportEntry ("plans" , format .Object (planList , 0 ))
341+
342+ jobList , _ := e2e .ClientSet .BatchV1 ().Jobs (e2e .Namespace .Name ).List (context .Background (), metav1.ListOptions {})
343+ AddReportEntry ("jobs" , format .Object (jobList , 0 ))
344+
345+ podList , _ := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).List (context .Background (), metav1.ListOptions {})
346+ for _ , pod := range podList .Items {
347+ containerNames := []string {}
348+ for _ , container := range pod .Spec .InitContainers {
349+ containerNames = append (containerNames , container .Name )
350+ }
351+ for _ , container := range pod .Spec .Containers {
352+ containerNames = append (containerNames , container .Name )
353+ }
354+ for _ , container := range containerNames {
355+ reportName := fmt .Sprintf ("podlogs-%s-%s" , pod .Name , container )
356+ logs := e2e .ClientSet .CoreV1 ().Pods (e2e .Namespace .Name ).GetLogs (pod .Name , & v1.PodLogOptions {Container : container })
357+ if logStreamer , err := logs .Stream (context .Background ()); err == nil {
358+ if podLogs , err := io .ReadAll (logStreamer ); err == nil {
359+ AddReportEntry (reportName , string (podLogs ))
360+ }
361+ }
362+ }
363+ }
364+ }
365+ }
366+ }
0 commit comments