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

mendersoftware / mender-server / 1979462190

12 Aug 2025 09:25AM UTC coverage: 65.751% (+0.3%) from 65.495%
1979462190

Pull #856

gitlab-ci

alfrunes
test(workflows): Clean up compose file command override

The command override was a workaround for a test that no longer exists.

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #856: fix(workflows): move inventory to end of decommission workflow

29256 of 44495 relevant lines covered (65.75%)

1.45 hits per line

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

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

15
package worker
16

17
import (
18
        "encoding/json"
19
        "fmt"
20
        "io"
21
        "net/http"
22
        "net/url"
23
        "strings"
24
        "time"
25

26
        "github.com/mendersoftware/mender-server/pkg/log"
27

28
        "github.com/mendersoftware/mender-server/services/workflows/app/processor"
29
        "github.com/mendersoftware/mender-server/services/workflows/model"
30
)
31

32
var makeHTTPRequest = func(req *http.Request, timeout time.Duration) (*http.Response, error) {
2✔
33
        var httpClient = &http.Client{
2✔
34
                Timeout: timeout,
2✔
35
        }
2✔
36
        res, err := httpClient.Do(req)
2✔
37
        if err != nil {
2✔
38
                return nil, err
×
39
        }
×
40
        return res, nil
2✔
41
}
42

43
func processHTTPTask(
44
        httpTask *model.HTTPTask,
45
        ps *processor.JobStringProcessor,
46
        jp *processor.JobProcessor,
47
        l *log.Logger,
48
) (*model.TaskResult, error) {
3✔
49
        uri := ps.ProcessJobString(httpTask.URI)
3✔
50
        l.Infof("processHTTPTask: starting with: method=%s uri=%s",
3✔
51
                httpTask.Method,
3✔
52
                uri,
3✔
53
        )
3✔
54

3✔
55
        var payloadString string
3✔
56
        if len(httpTask.FormData) > 0 {
4✔
57
                form := url.Values{}
1✔
58
                for key, value := range httpTask.FormData {
2✔
59
                        key = ps.ProcessJobString(key)
1✔
60
                        value = ps.ProcessJobString(value)
1✔
61
                        form.Add(key, value)
1✔
62
                }
1✔
63
                payloadString = form.Encode()
1✔
64
        } else if httpTask.JSON != nil {
6✔
65
                payloadJSON := jp.ProcessJSON(httpTask.JSON, ps)
3✔
66
                payloadBytes, err := json.Marshal(payloadJSON)
3✔
67
                if err != nil {
3✔
68
                        return nil, err
×
69
                }
×
70
                payloadString = string(payloadBytes)
3✔
71
        } else {
3✔
72
                payloadString = ps.ProcessJobString(httpTask.Body)
3✔
73
        }
3✔
74
        payload := strings.NewReader(payloadString)
3✔
75

3✔
76
        req, err := http.NewRequest(httpTask.Method, uri, payload)
3✔
77
        if err != nil {
3✔
78
                return nil, err
×
79
        }
×
80

81
        if httpTask.ContentType != "" {
5✔
82
                req.Header.Add("Content-Type", httpTask.ContentType)
2✔
83
        }
2✔
84

85
        var headersToBeSent []string
3✔
86
        for name, value := range httpTask.Headers {
6✔
87
                headerValue := ps.ProcessJobString(value)
3✔
88
                req.Header.Add(name, headerValue)
3✔
89
                headersToBeSent = append(headersToBeSent,
3✔
90
                        fmt.Sprintf("%s: %s", name, headerValue))
3✔
91
        }
3✔
92

93
        l.Debugf("processHTTPTask makeHTTPRequest '%v'", req)
3✔
94
        res, err := makeHTTPRequest(req, time.Duration(httpTask.ReadTimeOut)*time.Second)
3✔
95
        l.Debugf("processHTTPTask makeHTTPRequest returned '%v','%v'", res, err)
3✔
96
        if err != nil {
3✔
97
                return nil, err
×
98
        }
×
99

100
        defer res.Body.Close()
3✔
101
        resBody, _ := io.ReadAll(res.Body)
3✔
102

3✔
103
        var success bool
3✔
104
        if len(httpTask.StatusCodes) == 0 {
6✔
105
                success = true
3✔
106
                if res.StatusCode >= 400 {
4✔
107
                        success = false
1✔
108
                }
1✔
109
        } else {
3✔
110
                success = false
3✔
111
                for _, statusCode := range httpTask.StatusCodes {
6✔
112
                        if statusCode == res.StatusCode {
6✔
113
                                success = true
3✔
114
                                break
3✔
115
                        }
116
                }
117
        }
118

119
        result := &model.TaskResult{
3✔
120
                Success: success,
3✔
121
                HTTPRequest: &model.TaskResultHTTPRequest{
3✔
122
                        URI:     uri,
3✔
123
                        Method:  httpTask.Method,
3✔
124
                        Body:    payloadString,
3✔
125
                        Headers: headersToBeSent,
3✔
126
                },
3✔
127
                HTTPResponse: &model.TaskResultHTTPResponse{
3✔
128
                        StatusCode: res.StatusCode,
3✔
129
                        Body:       string(resBody),
3✔
130
                },
3✔
131
        }
3✔
132

3✔
133
        return result, nil
3✔
134
}
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