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

trento-project / agent / 15566159639

10 Jun 2025 05:26PM UTC coverage: 72.807% (-0.6%) from 73.44%
15566159639

Pull #449

github

balanza
refine log
Pull Request #449: Replace logrus logger with built-in log/slogs

215 of 384 new or added lines in 62 files covered. (55.99%)

5 existing lines in 3 files now uncovered.

4806 of 6601 relevant lines covered (72.81%)

19.34 hits per line

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

81.48
/internal/core/sapsystem/sapinstance.go
1
package sapsystem
2

3
import (
4
        "context"
5
        "fmt"
6
        "log/slog"
7
        "os"
8
        "path"
9
        "regexp"
10
        "strings"
11

12
        "github.com/pkg/errors"
13
        "github.com/spf13/afero"
14
        "github.com/trento-project/agent/internal/core/sapsystem/sapcontrolapi"
15
        "github.com/trento-project/agent/pkg/utils"
16
)
17

18
var (
19
        databaseFeatures         = regexp.MustCompile("HDB.*")
20
        applicationFeatures      = regexp.MustCompile("MESSAGESERVER.*|ENQREP|ABAP.*|J2EE.*")
21
        diagnosticsAgentFeatures = regexp.MustCompile("SMDAGENT")
22
)
23

24
type SystemReplication map[string]interface{}
25
type HostConfiguration map[string]interface{}
26
type HdbnsutilSRstate map[string]interface{}
27

28
type SAPInstance struct {
29
        Name       string
30
        Type       SystemType
31
        Host       string
32
        SAPControl *SAPControl
33
        // Only for Database type
34
        SystemReplication SystemReplication
35
        HostConfiguration HostConfiguration
36
        HdbnsutilSRstate  HdbnsutilSRstate
37
}
38

39
func NewSAPInstance(
40
        ctx context.Context,
41
        w sapcontrolapi.WebService,
42
        executor utils.CommandExecutor,
43
        fs afero.Fs,
44
) (*SAPInstance, error) {
19✔
45
        host, err := os.Hostname()
19✔
46
        if err != nil {
19✔
47
                return nil, err
×
48
        }
×
49

50
        scontrol, err := NewSAPControl(ctx, w, fs, host)
19✔
51
        if err != nil {
21✔
52
                return nil, err
2✔
53
        }
2✔
54

55
        instanceName, err := scontrol.findProperty("INSTANCE_NAME")
17✔
56
        if err != nil {
17✔
57
                return nil, err
×
58
        }
×
59

60
        instanceType, err := detectType(scontrol)
17✔
61
        if err != nil {
17✔
62
                return nil, err
×
63
        }
×
64

65
        sapInstance := &SAPInstance{
17✔
66
                Host:              host,
17✔
67
                SAPControl:        scontrol,
17✔
68
                Name:              instanceName,
17✔
69
                Type:              instanceType,
17✔
70
                SystemReplication: nil,
17✔
71
                HostConfiguration: nil,
17✔
72
                HdbnsutilSRstate:  nil,
17✔
73
        }
17✔
74

17✔
75
        if instanceType == Database {
20✔
76
                sid, err := sapInstance.SAPControl.findProperty("SAPSYSTEMNAME")
3✔
77
                if err != nil {
3✔
78
                        return nil, errors.Wrap(err, "Error finding the SAP instance sid")
×
79
                }
×
80

81
                sapInstance.SystemReplication = systemReplicationStatus(executor, sid, sapInstance.Name)
3✔
82
                sapInstance.HostConfiguration = landscapeHostConfiguration(executor, sid, sapInstance.Name)
3✔
83
                sapInstance.HdbnsutilSRstate = hdbnsutilSrstate(executor, sid, sapInstance.Name)
3✔
84
        }
85

86
        return sapInstance, nil
17✔
87
}
88

89
func detectType(sapControl *SAPControl) (SystemType, error) {
17✔
90
        sapLocalhost, err := sapControl.findProperty("SAPLOCALHOST")
17✔
91
        if err != nil {
17✔
92
                return Unknown, err
×
93
        }
×
94

95
        for _, instance := range sapControl.Instances {
36✔
96
                if instance.Hostname == sapLocalhost {
36✔
97
                        switch {
17✔
98
                        case databaseFeatures.MatchString(instance.Features):
3✔
99
                                return Database, nil
3✔
100
                        case applicationFeatures.MatchString(instance.Features):
8✔
101
                                return Application, nil
8✔
102
                        case diagnosticsAgentFeatures.MatchString(instance.Features):
2✔
103
                                return DiagnosticsAgent, nil
2✔
104
                        default:
4✔
105
                                return Unknown, nil
4✔
106
                        }
107
                }
108
        }
109

110
        return Unknown, nil
×
111
}
112

113
func runPythonSupport(executor utils.CommandExecutor, sid, instance, script string) map[string]interface{} {
6✔
114
        user := fmt.Sprintf("%sadm", strings.ToLower(sid))
6✔
115
        cmdPath := path.Join(sapInstallationPath, sid, instance, "exe/python_support", script)
6✔
116
        cmd := fmt.Sprintf("python %s --sapcontrol=1", cmdPath)
6✔
117
        // Even with a error return code, some data is available
6✔
118
        srData, err := executor.Exec("/usr/bin/su", "-lc", cmd, user)
6✔
119
        if err != nil {
6✔
NEW
120
                slog.Warn("Error running python_support command", "error", err)
×
121
        }
×
122
        dataMap := utils.FindMatches(`(\S+)=(.*)`, srData)
6✔
123

6✔
124
        return dataMap
6✔
125
}
126

127
func systemReplicationStatus(executor utils.CommandExecutor, sid, instance string) map[string]interface{} {
3✔
128
        return runPythonSupport(executor, sid, instance, "systemReplicationStatus.py")
3✔
129
}
3✔
130

131
func landscapeHostConfiguration(executor utils.CommandExecutor, sid, instance string) map[string]interface{} {
3✔
132
        return runPythonSupport(executor, sid, instance, "landscapeHostConfiguration.py")
3✔
133
}
3✔
134

135
func hdbnsutilSrstate(executor utils.CommandExecutor, sid, instance string) map[string]interface{} {
3✔
136
        user := fmt.Sprintf("%sadm", strings.ToLower(sid))
3✔
137
        cmdPath := path.Join(sapInstallationPath, sid, instance, "exe", "hdbnsutil")
3✔
138
        cmd := fmt.Sprintf("%s -sr_state -sapcontrol=1", cmdPath)
3✔
139
        srData, err := executor.Exec("/usr/bin/su", "-lc", cmd, user)
3✔
140
        if err != nil {
3✔
NEW
141
                slog.Warn("Error running hdbnsutil command", "error", err)
×
142
        }
×
143
        dataMap := utils.FindMatches(`(.+)=(.*)`, srData)
3✔
144
        return dataMap
3✔
145
}
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