/
service.go
110 lines (90 loc) · 2.89 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright (C) 2022 alsritter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package taskserver
import (
"context"
"fmt"
"github.com/alsritter/middlebaby/pkg/apimanager"
"github.com/alsritter/middlebaby/pkg/pluginregistry"
"github.com/alsritter/middlebaby/pkg/protomanager"
"github.com/spf13/pflag"
"github.com/alsritter/middlebaby/pkg/caseprovider"
"github.com/alsritter/middlebaby/pkg/types/task"
"github.com/alsritter/middlebaby/pkg/util/logger"
)
type Config struct {
CloseTearDown bool `yaml:"closeTearDown"`
TargetServeAdder string `yaml:"targetServeAdder"`
}
func NewConfig() *Config {
return &Config{
CloseTearDown: false,
}
}
func (c *Config) Validate() error {
if c.TargetServeAdder == "" {
return fmt.Errorf("target Serve Adder cannot be empty")
}
return nil
}
// RegisterFlagsWithPrefix is used to register flags
func (c *Config) RegisterFlagsWithPrefix(prefix string, f *pflag.FlagSet) {}
type Provider interface {
RunSingleTaskCase(ctx context.Context, itfName, caseName string) (task.RunTaskReply, error)
}
type taskService struct {
logger.Logger
cfg *Config
caseProvider caseprovider.Provider
apiProvider apimanager.Provider
protoProvider protomanager.Provider
pluginRegistry pluginregistry.Registry
}
// New return a TaskService
func New(log logger.Logger, cfg *Config,
caseProvider caseprovider.Provider,
protoProvider protomanager.Provider,
apiProvider apimanager.Provider,
pluginRegistry pluginregistry.Registry,
) Provider {
return &taskService{
cfg: cfg,
caseProvider: caseProvider,
protoProvider: protoProvider,
apiProvider: apiProvider,
pluginRegistry: pluginRegistry,
Logger: log.NewLogger("task"),
}
}
// RunSingleTaskCase implements task.TaskServer
func (t *taskService) RunSingleTaskCase(ctx context.Context, itfName, caseName string) (task.RunTaskReply, error) {
if err := t.Run(ctx, itfName, caseName); err != nil {
t.Error(map[string]interface{}{
"InterfaceName": itfName,
"CaseName": caseName,
}, err.Error())
return task.RunTaskReply{
Status: 0,
FailedReason: err.Error(),
}, nil
}
t.Info(map[string]interface{}{
"InterfaceName": itfName,
"CaseName": caseName,
}, "case assert successful")
return task.RunTaskReply{
Status: 1,
FailedReason: "",
}, nil
}