Skip to content

Commit a4f2815

Browse files
committed
support to verify the template funcs
1 parent 664a559 commit a4f2815

File tree

8 files changed

+604
-465
lines changed

8 files changed

+604
-465
lines changed

cmd/function.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,27 @@ func createFunctionCmd() (c *cobra.Command) {
4444
}
4545
flags := c.Flags()
4646
flags.StringVarP(&opt.feature, "feature", "", "", "The feature query")
47+
flags.StringVarP(&opt.extensionFile, "extension-file", "", "", "The extension file")
4748
return
4849
}
4950

5051
type funcPrinterOption struct {
51-
feature string
52+
feature string
53+
extensionFile string
5254
}
5355

5456
func (o *funcPrinterOption) runE(cmd *cobra.Command, args []string) (err error) {
57+
if o.extensionFile != "" {
58+
var tpl *render.UserDefinedTemplates
59+
if tpl, err = render.ParseUserDefinedTemplatesFromFile(o.extensionFile); err != nil {
60+
return
61+
}
62+
63+
if err = tpl.Validate(); err != nil {
64+
return
65+
}
66+
}
67+
5568
if len(args) > 0 {
5669
name := args[0]
5770
filterAndPrint(cmd, name)

pkg/render/testdata/function.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
items:
2+
- name: randAge
3+
render: |
4+
{{ randInt 18 60 }}
5+
- name: randFruit
6+
render: |
7+
{{ randEnum "apple" "banana" "cherry" "date" "elderberry" }}

pkg/render/user_defined.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language 24 permissions and
14+
limitations under the License.
15+
*/
16+
package render
17+
18+
import (
19+
"errors"
20+
"os"
21+
22+
"gopkg.in/yaml.v3"
23+
)
24+
25+
type UserDefinedTemplates struct {
26+
Items []UserDefinedTemplate
27+
}
28+
29+
type UserDefinedTemplate struct {
30+
Name string
31+
Render string
32+
}
33+
34+
func (t *UserDefinedTemplates) Validate() (err error) {
35+
for _, item := range t.Items {
36+
_, rErr := Render(item.Name, item.Render, nil)
37+
err = errors.Join(err, rErr)
38+
}
39+
return
40+
}
41+
42+
func ParseUserDefinedTemplates(data []byte) (templates *UserDefinedTemplates, err error) {
43+
templates = &UserDefinedTemplates{}
44+
err = yaml.Unmarshal(data, templates)
45+
return
46+
}
47+
48+
func ParseUserDefinedTemplatesFromFile(filePath string) (templates *UserDefinedTemplates, err error) {
49+
var data []byte
50+
data, err = os.ReadFile(filePath)
51+
if err == nil {
52+
templates, err = ParseUserDefinedTemplates(data)
53+
if err == nil {
54+
err = templates.Validate()
55+
}
56+
}
57+
return
58+
}

pkg/render/user_defined_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language 24 permissions and
14+
limitations under the License.
15+
*/
16+
package render_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/linuxsuren/api-testing/pkg/render"
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestUserDefinedTemplates(t *testing.T) {
26+
tpl, err := render.ParseUserDefinedTemplatesFromFile("testdata/function.yaml")
27+
assert.NoError(t, err)
28+
assert.NotNil(t, tpl)
29+
err = tpl.Validate()
30+
assert.NoError(t, err)
31+
}

pkg/server/server.pb.go

Lines changed: 471 additions & 460 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/server/server.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ message StoreKind {
613613
string name = 1;
614614
string url = 2;
615615
bool enabled = 3;
616+
repeated string dependencies = 4;
616617
}
617618

618619
message CommonResult {

pkg/server/store_ext_manager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"syscall"
2929
"time"
3030

31+
"github.com/linuxsuren/api-testing/pkg/testing"
3132
"github.com/linuxsuren/api-testing/pkg/util/home"
3233

3334
"github.com/linuxsuren/api-testing/pkg/downloader"
@@ -90,6 +91,12 @@ func NewStoreExtManagerInstance(execer fakeruntime.Execer) ExtManager {
9091
return ss
9192
}
9293

94+
func (s *storeExtManager) StartPlugin(storeKind testing.StoreKind) {
95+
for _, plugin := range storeKind.Dependencies {
96+
s.Start(plugin, fmt.Sprintf("unix://%s", home.GetExtensionSocketPath(plugin)))
97+
}
98+
}
99+
93100
func (s *storeExtManager) Start(name, socket string) (err error) {
94101
if v, ok := s.extStatusMap[name]; ok && v {
95102
return

pkg/testing/store.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ package testing
2626

2727
import (
2828
"fmt"
29-
"github.com/linuxsuren/api-testing/pkg/util"
3029
"os"
3130
"path"
3231
"strings"
3332

33+
"github.com/linuxsuren/api-testing/pkg/util"
34+
3435
"gopkg.in/yaml.v3"
3536
)
3637

@@ -97,9 +98,19 @@ func MapToStore(data map[string]string) (store Store) {
9798

9899
// StoreKind represents a gRPC-based store
99100
type StoreKind struct {
100-
Name string
101-
URL string
102-
Enabled bool
101+
Name string
102+
Dependencies []string
103+
URL string
104+
Params []StoreKindParam
105+
Link string
106+
Enabled bool
107+
}
108+
109+
type StoreKindParam struct {
110+
Key string
111+
DefaultValue string
112+
Enum []string
113+
Description string
103114
}
104115

105116
type StoreGetterAndSetter interface {

0 commit comments

Comments
 (0)