• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openshift-kni / lifecycle-agent / 567839dabd85027fcc379712ab73d3ce3dd9d8a8

20 May 2026 04:08PM UTC coverage: 30.271% (+0.5%) from 29.8%
567839dabd85027fcc379712ab73d3ce3dd9d8a8

push

web-flow
Merge pull request #6318 from sebrandon1/CNF-23417

CNF-23417: Add unit tests for rollback handlers

4920 of 16253 relevant lines covered (30.27%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lca-cli/cmd/ibuPrecacheWorkload.go
1
/*
2
 * Copyright 2023 Red Hat, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *   http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package cmd
18

19
import (
20
        "fmt"
21
        "os"
22
        "strings"
23
        "syscall"
24

25
        "github.com/openshift-kni/lifecycle-agent/internal/common"
26
        "github.com/openshift-kni/lifecycle-agent/internal/precache"
27
        "github.com/openshift-kni/lifecycle-agent/internal/precache/workload"
28

29
        "github.com/spf13/cobra"
30
)
31

32
// ibuPrecacheWorkloadCmd represents the ibuPrecacheWorkload command
33
var ibuPrecacheWorkloadCmd = &cobra.Command{
34
        Use:     "ibuPrecacheWorkload",
35
        Aliases: []string{"ibu-precache-workload"},
36
        Short:   "Start precache during IBU",
37
        Long:    `Start precache during IBU. This is to be called from a k8s job!`,
38
        Run: func(cmd *cobra.Command, args []string) {
×
39
                ibuPrecacheWorkloadRun()
×
40
        },
×
41
}
42

43
func init() {
×
44
        rootCmd.AddCommand(ibuPrecacheWorkloadCmd)
×
45
}
×
46

47
// Failure Exit codes
48
const (
49
        Failure int = 1
50
)
51

52
// terminateOnError Logs a "terminating job" + error message and terminates the pre-caching job with the given exit code
53
func terminateOnError(err error) {
×
54
        log.Errorf("terminating pre-caching job due to error: %v", err)
×
55
        os.Exit(Failure)
×
56
}
×
57

58
// readPrecacheSpecFile returns the list of images to be precached as specified in the precache spec file
59
func readPrecacheSpecFile() (precacheSpec []string, err error) {
×
60
        precacheSpecFile := os.Getenv(precache.EnvPrecacheSpecFile)
×
61
        if precacheSpecFile == "" {
×
62
                return precacheSpec, fmt.Errorf("environment variable %s is not set", precache.EnvPrecacheSpecFile)
×
63
        }
×
64

65
        // Check if precacheSpecFile exists
66
        if _, err := os.Stat(precacheSpecFile); os.IsNotExist(err) { //nolint:gosec // path is validated
×
67
                return precacheSpec, fmt.Errorf("missing precache spec file")
×
68
        }
×
69
        log.Info("Precache spec file found.")
×
70

×
71
        var content []byte
×
72
        content, err = os.ReadFile(precacheSpecFile) // nolint:gosec // path is validated
×
73
        if err != nil {
×
74
                return
×
75
        }
×
76

77
        lines := strings.Split(string(content), "\n")
×
78

×
79
        // Filter out empty lines
×
80
        for _, line := range lines {
×
81
                if line != "" {
×
82
                        precacheSpec = append(precacheSpec, line)
×
83
                }
×
84
        }
85

86
        return precacheSpec, nil
×
87
}
88

89
func ibuPrecacheWorkloadRun() {
×
90
        log.Info("Starting to execute pre-cache workload")
×
91

×
92
        bestEffort := false
×
93
        str := os.Getenv(precache.EnvPrecacheBestEffort)
×
94
        if str == "TRUE" {
×
95
                bestEffort = true
×
96
                log.Info("pre-caching set to 'best-effort'")
×
97
        }
×
98

99
        // Load precache spec file which is outside /host filesystem
100
        precacheSpec, err := readPrecacheSpecFile()
×
101
        if err != nil {
×
102
                terminateOnError(err)
×
103
        }
×
104

105
        log.Info("Loaded precache spec file.")
×
106

×
107
        // Change root directory to /host
×
108
        if err := syscall.Chroot(common.Host); err != nil {
×
109
                terminateOnError(fmt.Errorf("failed to chroot to %s, err: %w", common.Host, err))
×
110
        }
×
111
        log.Infof("chroot %s successful", common.Host)
×
112

×
113
        // Pre-check: Verify podman is running
×
114
        if !workload.CheckPodman() {
×
115
                terminateOnError(fmt.Errorf("failed to execute podman command"))
×
116
        }
×
117
        log.Info("podman is running, proceeding to pre-cache images!")
×
118
        // Get auth file for Podman
×
119
        authFile, err := workload.GetAuthFile()
×
120
        if err != nil {
×
121
                terminateOnError(err)
×
122
        }
×
123
        if err := workload.Precache(precacheSpec, authFile, bestEffort); err != nil {
×
124
                terminateOnError(err)
×
125
        }
×
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc