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

k8snetworkplumbingwg / sriov-network-operator / 3751025296

pending completion
3751025296

Pull #365

github

GitHub
Merge 421284b55 into 788d76f7e
Pull Request #365: Implementation for new systemd configuration method

958 of 958 new or added lines in 18 files covered. (100.0%)

1971 of 8330 relevant lines covered (23.66%)

0.27 hits per line

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

0.0
/pkg/service/utils.go
1
package service
2

3
import (
4
        "io/ioutil"
5
        "strings"
6

7
        "github.com/coreos/go-systemd/v22/unit"
8
        "github.com/golang/glog"
9
        "gopkg.in/yaml.v2"
10
)
11

12
const systemdDir = "/usr/lib/systemd/system/"
13

14
// CompareServices compare 2 service and return true if serviceA has all the fields of serviceB
15
func CompareServices(serviceA, serviceB *Service) (bool, error) {
×
16
        optsA, err := unit.Deserialize(strings.NewReader(serviceA.Content))
×
17
        if err != nil {
×
18
                return false, err
×
19
        }
×
20
        optsB, err := unit.Deserialize(strings.NewReader(serviceB.Content))
×
21
        if err != nil {
×
22
                return false, err
×
23
        }
×
24

25
OUTER:
×
26
        for _, optB := range optsB {
×
27
                for _, optA := range optsA {
×
28
                        if optA.Match(optB) {
×
29
                                continue OUTER
×
30
                        }
31
                }
32
                glog.Infof("DEBUG: %+v %v", optsA, *optB)
×
33
                return true, nil
×
34
        }
35

36
        return false, nil
×
37
}
38

39
// RemoveFromService removes given fields from service
40
func RemoveFromService(service *Service, options ...*unit.UnitOption) (*Service, error) {
×
41
        opts, err := unit.Deserialize(strings.NewReader(service.Content))
×
42
        if err != nil {
×
43
                return nil, err
×
44
        }
×
45

46
        var newServiceOptions []*unit.UnitOption
×
47
OUTER:
×
48
        for _, opt := range opts {
×
49
                for _, optRemove := range options {
×
50
                        if opt.Match(optRemove) {
×
51
                                continue OUTER
×
52
                        }
53
                }
54

55
                newServiceOptions = append(newServiceOptions, opt)
×
56
        }
57

58
        data, err := ioutil.ReadAll(unit.Serialize(newServiceOptions))
×
59
        if err != nil {
×
60
                return nil, err
×
61
        }
×
62

63
        return &Service{
×
64
                Name:    service.Name,
×
65
                Path:    service.Path,
×
66
                Content: string(data),
×
67
        }, nil
×
68
}
69

70
// AppendToService appends given fields to service
71
func AppendToService(service *Service, options ...*unit.UnitOption) (*Service, error) {
×
72
        serviceOptions, err := unit.Deserialize(strings.NewReader(service.Content))
×
73
        if err != nil {
×
74
                return nil, err
×
75
        }
×
76

77
OUTER:
×
78
        for _, appendOpt := range options {
×
79
                for _, opt := range serviceOptions {
×
80
                        if opt.Match(appendOpt) {
×
81
                                continue OUTER
×
82
                        }
83
                }
84
                serviceOptions = append(serviceOptions, appendOpt)
×
85
        }
86

87
        data, err := ioutil.ReadAll(unit.Serialize(serviceOptions))
×
88
        if err != nil {
×
89
                return nil, err
×
90
        }
×
91

92
        return &Service{
×
93
                Name:    service.Name,
×
94
                Path:    service.Path,
×
95
                Content: string(data),
×
96
        }, nil
×
97
}
98

99
// ReadServiceInjectionManifestFile reads service injection file
100
func ReadServiceInjectionManifestFile(path string) (*Service, error) {
×
101
        data, err := ioutil.ReadFile(path)
×
102
        if err != nil {
×
103
                return nil, err
×
104
        }
×
105

106
        var serviceContent ServiceInjectionManifestFile
×
107
        if err := yaml.Unmarshal(data, &serviceContent); err != nil {
×
108
                return nil, err
×
109
        }
×
110

111
        return &Service{
×
112
                Name:    serviceContent.Name,
×
113
                Path:    systemdDir + serviceContent.Name,
×
114
                Content: serviceContent.Dropins[0].Contents,
×
115
        }, nil
×
116
}
117

118
// ReadServiceManifestFile reads service file
119
func ReadServiceManifestFile(path string) (*Service, error) {
×
120
        data, err := ioutil.ReadFile(path)
×
121
        if err != nil {
×
122
                return nil, err
×
123
        }
×
124

125
        var serviceFile *ServiceManifestFile
×
126
        if err := yaml.Unmarshal(data, &serviceFile); err != nil {
×
127
                return nil, err
×
128
        }
×
129

130
        return &Service{
×
131
                Name:    serviceFile.Name,
×
132
                Path:    "/etc/systemd/system/" + serviceFile.Name,
×
133
                Content: serviceFile.Contents,
×
134
        }, nil
×
135
}
136

137
// ReadScriptManifestFile reads script file
138
func ReadScriptManifestFile(path string) (*ScriptManifestFile, error) {
×
139
        data, err := ioutil.ReadFile(path)
×
140
        if err != nil {
×
141
                return nil, err
×
142
        }
×
143

144
        var scriptFile *ScriptManifestFile
×
145
        if err := yaml.Unmarshal(data, &scriptFile); err != nil {
×
146
                return nil, err
×
147
        }
×
148

149
        return scriptFile, nil
×
150
}
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

© 2025 Coveralls, Inc