Skip to content

Commit 1f8b4af

Browse files
Chief-Rishabravisuhag
authored andcommitted
test: improve coverage
1 parent e6830e4 commit 1f8b4af

File tree

71 files changed

+3576
-902
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3576
-902
lines changed

.github/workflows/test.yml

100644100755
+18-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,27 @@ jobs:
2626
name: coverage
2727
path: coverage.out
2828

29+
pretest:
30+
runs-on: ubuntu-latest
31+
outputs:
32+
plugins: ${{ steps.set-matrix.outputs.plugins }}
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v3
36+
- id: set-matrix
37+
run: |
38+
echo "plugins=$(find plugins -mindepth 2 -maxdepth 2 -type d | sed 's/plugins\///' | awk 'BEGIN{printf "["} {printf "%s\"%s\"",sep,$0; sep=","} END{print ",\".\"]"}')" >> $GITHUB_OUTPUT
39+
2940
plugins-test:
41+
needs: pretest
3042
runs-on: ubuntu-latest
3143
if: |
3244
github.ref == 'refs/heads/main' ||
3345
github.event_name == 'pull_request' ||
3446
github.event_name == 'workflow_dispatch'
47+
strategy:
48+
matrix:
49+
plugins: ${{ fromJson(needs.pretest.outputs.plugins) }}
3550
steps:
3651
- name: Checkout code
3752
uses: actions/checkout@v3
@@ -43,12 +58,12 @@ jobs:
4358
- name: Install dependencies
4459
run: sudo apt-get install build-essential
4560
- name: Run Test
46-
run: make test-plugins
61+
run: make test-plugins PLUGIN=${{ matrix.plugins }}
4762
- name: Upload coverage artifact
4863
uses: actions/upload-artifact@v3
4964
with:
5065
name: coverage-plugins
51-
path: coverage-plugins.out
66+
path: coverage-plugins*.out
5267

5368
coverage:
5469
runs-on: ubuntu-latest
@@ -74,4 +89,4 @@ jobs:
7489
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7590
run: |
7691
go install github.com/mattn/[email protected]
77-
goveralls -coverprofile=coverage.out,coverage-plugins.out -service=github
92+
goveralls -coverprofile=$(ls -1 coverage*.out | paste -sd "," -) -service=github

Makefile

100644100755
+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ copy-config:
1919
cp ./config/meteor.yaml.sample ./meteor.yaml
2020

2121
test:
22-
go test ./... -coverprofile=coverage.out
22+
go test $(shell go list ./... | grep -v 'test\|mocks\|plugins\|v1beta2\|cmd') -coverprofile=coverage.out
2323

2424
test-e2e:
2525
go test ./test/e2e -tags=integration -count=1
2626

2727
test-plugins:
2828
@echo " > Testing plugins with tag 'plugins'"
29-
go test ./plugins... -tags=plugins -coverprofile=coverage-plugins.out -parallel=1
29+
go test $(if $(filter .,$(PLUGIN)),./plugins,$(if $(PLUGIN),./plugins/$(PLUGIN)/...,./plugins/...)) -tags=plugins -coverprofile=coverage-plugins$(subst .,root,$(subst /,-,$(if $(PLUGIN),-$(PLUGIN),))).out -parallel=1
3030

3131
test-coverage: # test test-plugins
3232
cp coverage.out coverage-all.out

agent/stream.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
"fmt"
55
"sync"
66

7-
"github.com/pkg/errors"
87
"github.com/raystack/meteor/models"
98
)
109

11-
type streamMiddleware func(src models.Record) (dst models.Record, err error)
12-
type subscriber struct {
13-
callback func([]models.Record) error
14-
channel chan models.Record
15-
batchSize int
16-
}
10+
type (
11+
streamMiddleware func(src models.Record) (dst models.Record, err error)
12+
subscriber struct {
13+
callback func([]models.Record) error
14+
channel chan models.Record
15+
batchSize int
16+
}
17+
)
1718

1819
type stream struct {
1920
middlewares []streamMiddleware
@@ -94,7 +95,7 @@ func (s *stream) broadcast() error {
9495
func (s *stream) push(data models.Record) {
9596
data, err := s.runMiddlewares(data)
9697
if err != nil {
97-
s.closeWithError(errors.Wrap(err, "emitter: error running middleware"))
98+
s.closeWithError(fmt.Errorf("emitter: error running middleware: %w", err))
9899
return
99100
}
100101

config/config_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package config_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/raystack/meteor/config"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestLoad(t *testing.T) {
11+
type args struct {
12+
configFile string
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
expected config.Config
18+
expectedErr string
19+
}{
20+
{
21+
name: "should return a config",
22+
args: args{
23+
configFile: "testdata/valid-config.yaml",
24+
},
25+
expected: config.Config{
26+
LogLevel: "info",
27+
StatsdEnabled: false,
28+
StatsdHost: "localhost:8125",
29+
StatsdPrefix: "meteor",
30+
MaxRetries: 5,
31+
RetryInitialIntervalSeconds: 5,
32+
StopOnSinkError: false,
33+
},
34+
},
35+
{
36+
name: "config file not found",
37+
args: args{
38+
configFile: "not-found.yaml",
39+
},
40+
expected: config.Config{
41+
LogLevel: "info",
42+
StatsdEnabled: false,
43+
StatsdHost: "localhost:8125",
44+
StatsdPrefix: "meteor",
45+
MaxRetries: 5,
46+
RetryInitialIntervalSeconds: 5,
47+
},
48+
expectedErr: "",
49+
},
50+
{
51+
name: "config invalid",
52+
args: args{
53+
configFile: "testdata/invalid-config.yaml",
54+
},
55+
expected: config.Config{},
56+
expectedErr: "unable to load config to struct",
57+
},
58+
}
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
actual, err := config.Load(tt.args.configFile)
62+
if tt.expectedErr != "" {
63+
assert.ErrorContains(t, err, tt.expectedErr)
64+
return
65+
}
66+
67+
assert.NoError(t, err)
68+
assert.Equal(t, tt.expected, actual)
69+
})
70+
}
71+
}

config/testdata/invalid-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STATSD_ENABLED: not-a-boolean

config/testdata/valid-config.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
LOG_LEVEL: info
2+
STATSD_ENABLED: false
3+
STATSD_HOST: "localhost:8125"
4+
STATSD_PREFIX: meteor
5+
MAX_RETRIES: 5
6+
RETRY_INITIAL_INTERVAL_SECONDS: 5
7+
STOP_ON_SINK_ERROR: false

generator/recipe_test.go

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
package generator_test
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"testing"
7+
8+
"github.com/raystack/meteor/generator"
9+
"github.com/raystack/meteor/plugins"
10+
"github.com/raystack/meteor/registry"
11+
"github.com/raystack/meteor/test/mocks"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
var recipeVersions = [1]string{"v1beta1"}
16+
17+
func TestRecipe(t *testing.T) {
18+
var err error
19+
err = registry.Extractors.Register("test-extractor", func() plugins.Extractor {
20+
extr := mocks.NewExtractor()
21+
mockInfo := plugins.Info{
22+
Description: "Mock Extractor 1",
23+
}
24+
extr.On("Info").Return(mockInfo, nil).Once()
25+
return extr
26+
})
27+
assert.NoError(t, err)
28+
29+
err = registry.Sinks.Register("test-sink", func() plugins.Syncer {
30+
mockSink := mocks.NewSink()
31+
mockInfo := plugins.Info{
32+
Description: "Mock Sink 1",
33+
}
34+
mockSink.On("Info").Return(mockInfo, nil).Once()
35+
return mockSink
36+
})
37+
assert.NoError(t, err)
38+
39+
err = registry.Processors.Register("test-processor", func() plugins.Processor {
40+
mockProcessor := mocks.NewProcessor()
41+
mockInfo := plugins.Info{
42+
Description: "Mock Processor 1",
43+
}
44+
mockProcessor.On("Info").Return(mockInfo, nil).Once()
45+
return mockProcessor
46+
})
47+
assert.NoError(t, err)
48+
49+
type args struct {
50+
p generator.RecipeParams
51+
}
52+
tests := []struct {
53+
name string
54+
args args
55+
expected *generator.TemplateData
56+
expectedErr string
57+
}{
58+
{
59+
name: "success with minimal params",
60+
args: args{
61+
p: generator.RecipeParams{
62+
Name: "test-name",
63+
},
64+
},
65+
expected: &generator.TemplateData{
66+
Name: "test-name",
67+
Version: recipeVersions[len(recipeVersions)-1],
68+
},
69+
},
70+
{
71+
name: "success with full params",
72+
args: args{
73+
p: generator.RecipeParams{
74+
Name: "test-name",
75+
Source: "test-extractor",
76+
Sinks: []string{"test-sink"},
77+
Processors: []string{"test-processor"},
78+
},
79+
},
80+
expected: &generator.TemplateData{
81+
Name: "test-name",
82+
Version: recipeVersions[len(recipeVersions)-1],
83+
Source: struct {
84+
Name string
85+
Scope string
86+
SampleConfig string
87+
}{
88+
Name: "test-extractor",
89+
},
90+
Sinks: map[string]string{
91+
"test-sink": "",
92+
},
93+
Processors: map[string]string{
94+
"test-processor": "",
95+
},
96+
},
97+
},
98+
{
99+
name: "error with invalid source",
100+
args: args{
101+
p: generator.RecipeParams{
102+
Name: "test-name",
103+
Source: "invalid-source",
104+
},
105+
},
106+
expected: nil,
107+
expectedErr: "provide extractor information: could not find extractor",
108+
},
109+
{
110+
name: "error with invalid sinks",
111+
args: args{
112+
p: generator.RecipeParams{
113+
Name: "test-name",
114+
Sinks: []string{"invalid-sink"},
115+
},
116+
},
117+
expected: nil,
118+
expectedErr: "provide sink information: could not find sink",
119+
},
120+
{
121+
name: "error with invalid processors",
122+
args: args{
123+
p: generator.RecipeParams{
124+
Name: "test-name",
125+
Processors: []string{"invalid-processor"},
126+
},
127+
},
128+
expected: nil,
129+
expectedErr: "provide processor information: could not find processor",
130+
},
131+
}
132+
for _, tt := range tests {
133+
t.Run(tt.name, func(t *testing.T) {
134+
actual, err := generator.Recipe(tt.args.p)
135+
if tt.expectedErr != "" {
136+
assert.ErrorContains(t, err, tt.expectedErr)
137+
return
138+
}
139+
assert.NoError(t, err)
140+
141+
assert.Equal(t, actual, tt.expected)
142+
})
143+
}
144+
}
145+
146+
func TestRecipeWriteTo(t *testing.T) {
147+
type args struct {
148+
p generator.RecipeParams
149+
}
150+
tests := []struct {
151+
name string
152+
args args
153+
expectedWriter string
154+
expectedErr string
155+
}{
156+
{
157+
name: "success with minimal params",
158+
args: args{
159+
p: generator.RecipeParams{
160+
Name: "test-name",
161+
},
162+
},
163+
expectedWriter: `name: test-name
164+
version: v1beta1
165+
source:
166+
name:
167+
config:
168+
169+
`,
170+
expectedErr: "",
171+
},
172+
}
173+
for _, tt := range tests {
174+
t.Run(tt.name, func(t *testing.T) {
175+
writer := &bytes.Buffer{}
176+
err := generator.RecipeWriteTo(tt.args.p, writer)
177+
if tt.expectedErr != "" {
178+
assert.ErrorContains(t, err, tt.expectedErr)
179+
return
180+
}
181+
assert.Equal(t, tt.expectedWriter, writer.String())
182+
})
183+
}
184+
}
185+
186+
func TestGetRecipeVersions(t *testing.T) {
187+
tests := []struct {
188+
name string
189+
expected [1]string
190+
}{
191+
{
192+
name: "success",
193+
expected: recipeVersions,
194+
},
195+
}
196+
for _, tt := range tests {
197+
t.Run(tt.name, func(t *testing.T) {
198+
actual := generator.GetRecipeVersions()
199+
assert.Equal(t, tt.expected, actual)
200+
})
201+
}
202+
}

0 commit comments

Comments
 (0)