Skip to content

Commit 51b1d56

Browse files
authored
codecov, tests (#10)
1 parent c47b661 commit 51b1d56

File tree

5 files changed

+53
-42
lines changed

5 files changed

+53
-42
lines changed

.github/workflows/build.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ jobs:
2727
steps:
2828
- name: Checkout
2929
uses: actions/checkout@v3
30+
- name: Set up go
31+
uses: actions/setup-go@v2
32+
with:
33+
go-version: 1.19
34+
- name: Test
35+
run: make test
36+
- name: Upload coverage to Codecov
37+
uses: codecov/codecov-action@v3
3038
- name: Set up QEMU
3139
uses: docker/setup-qemu-action@v2
3240
- name: Set up Docker Buildx

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
cover.out

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ submit:
2626
.PHONY: clean
2727
clean:
2828
bash scripts/delete_cluster.sh
29+
30+
.PHONY: test
31+
test:
32+
go test -v ./... -coverprofile cover.out

internal/argocd_executor_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package argocd
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_durationStringToContext(t *testing.T) {
12+
t.Parallel()
13+
14+
t.Run("empty", func(t *testing.T) {
15+
ctx, cancel, err := durationStringToContext("")
16+
require.NoError(t, err)
17+
t.Cleanup(cancel)
18+
assert.Equal(t, context.Background(), ctx)
19+
})
20+
21+
t.Run("invalid", func(t *testing.T) {
22+
_, _, err := durationStringToContext("invalid")
23+
require.Error(t, err)
24+
})
25+
26+
t.Run("valid", func(t *testing.T) {
27+
ctx, cancel, err := durationStringToContext("1s")
28+
require.NoError(t, err)
29+
t.Cleanup(cancel)
30+
assert.NotEqual(t, context.Background(), ctx)
31+
})
32+
}

internal/plugin_test.go

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,21 @@ func (errReader) Read(_ []byte) (n int, err error) {
4343
}
4444

4545
type executorSpy struct {
46-
Called bool
46+
AuthorizeCalled bool
47+
ExecuteCalled bool
4748
}
4849

49-
func (e *executorSpy) Execute(_ executor.ExecuteTemplateArgs) executor.ExecuteTemplateReply {
50-
e.Called = true
50+
func (e *executorSpy) Authorize(_ *http.Request) error {
51+
e.AuthorizeCalled = true
52+
return nil
53+
}
5154

55+
func (e *executorSpy) Execute(_ executor.ExecuteTemplateArgs) executor.ExecuteTemplateReply {
56+
e.ExecuteCalled = true
5257
return executor.ExecuteTemplateReply{}
5358
}
5459

5560
func TestArgocdPlugin(t *testing.T) {
56-
// test returning correct result based on input
57-
5861
spy := executorSpy{}
5962
argocdPlugin := ArgocdPlugin(&spy)
6063
handler := http.HandlerFunc(argocdPlugin)
@@ -112,41 +115,4 @@ func TestArgocdPlugin(t *testing.T) {
112115
assert.Equal(t, gotStatus, tt.status)
113116
})
114117
}
115-
116-
var execTests = []struct {
117-
name string
118-
fail bool
119-
status int
120-
}{
121-
{
122-
name: "exec without fail",
123-
fail: false,
124-
status: http.StatusOK,
125-
},
126-
{
127-
name: "exec fails",
128-
fail: true,
129-
status: http.StatusInternalServerError,
130-
},
131-
}
132-
133-
body := validWorkflowBody
134-
for _, tt := range execTests {
135-
t.Run(tt.name, func(t *testing.T) {
136-
spy.Called = false
137-
request, _ := http.NewRequest(http.MethodPost, "/api/v1/template.execute", bytes.NewReader(body))
138-
request.Header.Set("Content-Type", "application/json")
139-
response := httptest.NewRecorder()
140-
handler.ServeHTTP(response, request)
141-
142-
if !spy.Called && !tt.fail {
143-
t.Error("ApiExecutor was not called")
144-
}
145-
146-
got := response.Result().StatusCode
147-
148-
assert.Equal(t, got, tt.status)
149-
assert.Equal(t, got, tt.status)
150-
})
151-
}
152118
}

0 commit comments

Comments
 (0)