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

mendersoftware / workflows / 1565759653

29 Nov 2024 07:56AM UTC coverage: 67.786% (-14.5%) from 82.255%
1565759653

push

gitlab-ci

web-flow
Merge pull request #336 from alfrunes/2.6.x

chore(deps): Update golang builder images to latest

1050 of 1549 relevant lines covered (67.79%)

5.01 hits per line

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

84.62
/model/job.go
1
// Copyright 2021 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 model
16

17
import (
18
        "time"
19

20
        "github.com/pkg/errors"
21
)
22

23
// Status
24
const (
25
        StatusDone = int32(iota)
26
        StatusPending
27
        StatusProcessing
28
        StatusFailure
29
)
30

31
// ErrMsgMissingParamF is the error message for missing input parameters
32
const ErrMsgMissingParamF = "Missing input parameters: %s"
33

34
var (
35
        // ErrInvalidStatus is the error for invalid status
36
        ErrInvalidStatus = errors.New("Invalid status")
37
)
38

39
// Job defines the execution job a workflow
40
type Job struct {
41
        // Id is the ID of the job
42
        ID string `json:"id" bson:"_id"`
43

44
        // WorkflowName contains the name of the workflow
45
        WorkflowName string `json:"workflowName" bson:"workflow_name"`
46

47
        // InputParameters contains the name of the workflow
48
        InputParameters InputParameters `json:"inputParameters" bson:"input_parameters"`
49

50
        // Enumerated status of the Job and string field used for unmarshalling
51
        Status       int32  `json:"-" bson:"status"`
52
        StatusString string `json:"status,omitempty" bson:"-"`
53

54
        // Results produced by a finished job. If status is not "done" this
55
        // field will always be nil.
56
        Results []TaskResult `json:"results" bson:"results,omitempty"`
57

58
        // insert time
59
        InsertTime time.Time `json:"insert_time" bson:"insert_time,omitempty"`
60

61
        //workflow version
62
        WorkflowVersion string `json:"version" bson:"version,omitempty"`
63
}
64

65
// InputParameter defines the input parameter of a job
66
type InputParameter struct {
67
        // Name of the parameter
68
        Name string `json:"name" bson:"name"`
69

70
        // Value of the input parameter
71
        Value string `json:"value" bson:"value"`
72

73
        // Raw value of the input parameter
74
        Raw interface{} `json:"raw,omitempty" bson:"raw"`
75
}
76

77
type InputParameters []InputParameter
78

79
func (param InputParameters) Map() map[string]interface{} {
×
80
        var ret = map[string]interface{}{}
×
81
        for _, val := range param {
×
82
                ret[val.Name] = val.Value
×
83
        }
×
84
        return ret
×
85
}
86

87
// TaskResult contains the result of the execution of a task
88
type TaskResult struct {
89
        Name        string                 `json:"name" bson:"name"`
90
        Type        string                 `json:"type" bson:"type"`
91
        Success     bool                   `json:"success" bson:"success"`
92
        Skipped     bool                   `json:"skipped" bson:"skipped"`
93
        CLI         *TaskResultCLI         `json:"cli,omitempty" bson:"cli,omitempty"`
94
        HTTPRequest *TaskResultHTTPRequest `json:"httpRequest,omitempty" bson:"httpRequest,omitempty"`
95
        //nolint:lll
96
        HTTPResponse *TaskResultHTTPResponse `json:"httpResponse,omitempty" bson:"httpResponse,omitempty"`
97
        NATS         *TaskResultNATS         `json:"nats,omitempty" bson:"nats,omitempty"`
98
        SMTP         *TaskResultSMTP         `json:"smtp,omitempty" bson:"smtp,omitempty"`
99
}
100

101
// TaskResultCLI contains the CLI command, the output and the exit status
102
type TaskResultCLI struct {
103
        Command  []string `json:"command" bson:"command"`
104
        Output   string   `json:"output" bson:"output"`
105
        Error    string   `json:"error" bson:"error"`
106
        ExitCode int      `json:"exitCode" bson:"exitCode"`
107
}
108

109
type TaskResultNATS struct {
110
        Error string `json:"error" bson:"error"`
111
}
112

113
// TaskResultHTTPRequest contains the request
114
type TaskResultHTTPRequest struct {
115
        URI     string   `json:"uri" bson:"uri"`
116
        Method  string   `json:"method" bson:"method"`
117
        Body    string   `json:"body" bson:"body"`
118
        Headers []string `json:"headers" bson:"headers"`
119
}
120

121
// TaskResultHTTPResponse contains the response
122
type TaskResultHTTPResponse struct {
123
        StatusCode int    `json:"statusCode" bson:"status_code"`
124
        Body       string `json:"body" bson:"body"`
125
}
126

127
// TaskResultSMTP contains the SMTP message, the output and the exit status
128
type TaskResultSMTP struct {
129
        Sender     string   `json:"sender" bson:"sender"`
130
        Recipients []string `json:"recipients" bson:"recipients"`
131
        Message    string   `json:"message" bson:"message"`
132
        Error      string   `json:"error" bson:"error"`
133
}
134

135
// Validate job against workflow. Check that all required parameters are present.
136
func (job *Job) Validate(workflow *Workflow) error {
2✔
137
        var missing []string
2✔
138
        inputParameters := make(map[string]string)
2✔
139
        for _, param := range job.InputParameters {
4✔
140
                inputParameters[param.Name] = param.Value
2✔
141
        }
2✔
142
        for _, key := range workflow.InputParameters {
5✔
143
                if _, ok := inputParameters[key]; !ok {
4✔
144
                        missing = append(missing, key)
1✔
145
                }
1✔
146
        }
147
        if len(missing) > 0 {
3✔
148
                return errors.Errorf(ErrMsgMissingParamF, missing)
1✔
149
        }
1✔
150
        return nil
1✔
151
}
152

153
func (job *Job) PrepareForJSONMarshalling() {
1✔
154
        job.StatusString = StatusToString(job.Status)
1✔
155
        for i := range job.InputParameters {
2✔
156
                job.InputParameters[i].Raw = nil
1✔
157
        }
1✔
158
}
159

160
// StatusToString returns the job's status as a string
161
func StatusToString(status int32) string {
7✔
162
        var ret string
7✔
163
        switch status {
7✔
164
        case StatusPending:
3✔
165
                ret = "pending"
3✔
166
        case StatusProcessing:
1✔
167
                ret = "processing"
1✔
168
        case StatusDone:
1✔
169
                ret = "done"
1✔
170
        case StatusFailure:
1✔
171
                ret = "failed"
1✔
172
        default:
1✔
173
                ret = "unknown"
1✔
174
        }
175
        return ret
7✔
176
}
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