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

k8snetworkplumbingwg / sriov-network-operator / 19331737185

13 Nov 2025 12:35PM UTC coverage: 62.137% (-0.2%) from 62.366%
19331737185

push

github

web-flow
Merge pull request #902 from SchSeba/create_platform_and_orchestrator_packages

Create platform and orchestrator packages

319 of 659 new or added lines in 25 files covered. (48.41%)

42 existing lines in 10 files now uncovered.

8770 of 14114 relevant lines covered (62.14%)

0.69 hits per line

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

33.77
/pkg/utils/utils.go
1
package utils
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "io"
7
        "io/ioutil"
8
        "net/http"
9
        "os"
10
        "os/exec"
11
        "path/filepath"
12
        "syscall"
13
        "time"
14

15
        "sigs.k8s.io/controller-runtime/pkg/log"
16

17
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
18
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
19
)
20

21
const (
22
        httpRequestTimeout = 5 * time.Second
23
)
24

25
//go:generate ../../bin/mockgen -destination mock/mock_utils.go -source utils.go
26
type CmdInterface interface {
27
        Chroot(string) (func() error, error)
28
        RunCommand(string, ...string) (string, string, error)
29
        HTTPGetFetchData(string) (string, error)
30
}
31

32
type utilsHelper struct {
33
}
34

35
func New() CmdInterface {
1✔
36
        return &utilsHelper{}
1✔
37
}
1✔
38

39
// Chroot run a chroot command on a specific path
40
func (u *utilsHelper) Chroot(path string) (func() error, error) {
×
41
        root, err := os.Open("/")
×
42
        if err != nil {
×
43
                return nil, err
×
44
        }
×
45

46
        if err := syscall.Chroot(path); err != nil {
×
47
                root.Close()
×
48
                return nil, err
×
49
        }
×
50
        vars.InChroot = true
×
51

×
52
        return func() error {
×
53
                defer root.Close()
×
54
                if err := root.Chdir(); err != nil {
×
55
                        return err
×
56
                }
×
57
                vars.InChroot = false
×
58
                return syscall.Chroot(".")
×
59
        }, nil
60
}
61

NEW
62
func (u *utilsHelper) HTTPGetFetchData(url string) (string, error) {
×
NEW
63
        // Initialize an HTTP client with a specific timeout.
×
NEW
64
        client := http.Client{
×
NEW
65
                Timeout: httpRequestTimeout,
×
NEW
66
        }
×
NEW
67

×
NEW
68
        // Perform the GET request.
×
NEW
69
        resp, err := client.Get(url)
×
NEW
70
        if err != nil {
×
NEW
71
                // This error typically indicates a network issue or that the server is unreachable.
×
NEW
72
                return "", fmt.Errorf("HTTP GET request to %s failed: %w", url, err)
×
NEW
73
        }
×
74
        // Ensure the response body is closed after the function returns.
NEW
75
        defer resp.Body.Close()
×
NEW
76

×
NEW
77
        // Check if the HTTP status code is OK (200).
×
NEW
78
        if resp.StatusCode != http.StatusOK {
×
NEW
79
                // Attempt to read the body for more detailed error information if available.
×
NEW
80
                errorBodyBytes, _ := ioutil.ReadAll(resp.Body) // ReadAll might return its own error, but we prioritize the status code error.
×
NEW
81
                return "", fmt.Errorf("request to %s returned status %s: %s", url, resp.Status, string(errorBodyBytes))
×
NEW
82
        }
×
83

84
        // Read the entire response body.
NEW
85
        bodyBytes, err := io.ReadAll(resp.Body)
×
NEW
86
        if err != nil {
×
NEW
87
                return "", fmt.Errorf("failed to read response body from %s: %w", url, err)
×
NEW
88
        }
×
NEW
89
        return string(bodyBytes), nil
×
90
}
91

92
// RunCommand runs a command
93
func (u *utilsHelper) RunCommand(command string, args ...string) (string, string, error) {
1✔
94
        log.Log.Info("RunCommand()", "command", command, "args", args)
1✔
95
        var stdout, stderr bytes.Buffer
1✔
96

1✔
97
        cmd := exec.Command(command, args...)
1✔
98
        cmd.Stdout = &stdout
1✔
99
        cmd.Stderr = &stderr
1✔
100

1✔
101
        err := cmd.Run()
1✔
102
        log.Log.V(2).Info("RunCommand()", "output", stdout.String(), "error", err)
1✔
103
        return stdout.String(), stderr.String(), err
1✔
104
}
1✔
105

106
func IsCommandNotFound(err error) bool {
×
107
        if exitErr, ok := err.(*exec.ExitError); ok {
×
108
                if status, ok := exitErr.Sys().(syscall.WaitStatus); ok && status.ExitStatus() == 127 {
×
109
                        return true
×
110
                }
×
111
        }
112
        return false
×
113
}
114

115
func GetHostExtension() string {
1✔
116
        if vars.InChroot {
2✔
117
                return vars.FilesystemRoot
1✔
118
        }
1✔
119
        return filepath.Join(vars.FilesystemRoot, consts.Host)
1✔
120
}
121

122
func GetHostExtensionPath(path string) string {
1✔
123
        return filepath.Join(GetHostExtension(), path)
1✔
124
}
1✔
125

126
func GetChrootExtension() string {
1✔
127
        if vars.InChroot {
1✔
128
                return vars.FilesystemRoot
×
129
        }
×
130
        return fmt.Sprintf("chroot %s%s", vars.FilesystemRoot, consts.Host)
1✔
131
}
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