Skip to content

Commit 2bbc367

Browse files
author
Amit Kumar Das
authored
feat(testing): initial integration test implementation (#184)
This commit uses container image to run integration tests. The tests are automated and run on a K3s installation. Integration tests are triggered as a make target. e.g. make integration-test-suite This approach provides the code coverage report in suite's run logs. This change fixes a labelling bug where resources that are not supposed to be labelled are labelled. GitHub actions has been modifed to accommodate integration testing. Signed-off-by: AmitKumarDas <[email protected]>
1 parent 87b9838 commit 2bbc367

Some content is hidden

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

44 files changed

+918
-102
lines changed

.github/workflows/test-release.yaml

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ jobs:
1616
with:
1717
go-version: 1.13.5
1818
- run: make ${{ matrix.test }}
19+
integrationtest:
20+
runs-on: ubuntu-18.04
21+
name: integrationtest
22+
steps:
23+
- name: Checkout Code
24+
uses: actions/checkout@v2
25+
- name: Setup Golang
26+
uses: actions/setup-go@v2
27+
with:
28+
go-version: 1.13.5
29+
- run: sudo make integration-test-suite
1930
e2etest:
2031
runs-on: ubuntu-18.04
2132
strategy:
@@ -33,7 +44,7 @@ jobs:
3344
release:
3445
name: Make Github Release
3546
runs-on: ubuntu-18.04
36-
needs: ['unittest', 'e2etest']
47+
needs: ['unittest', 'e2etest', 'integrationtest']
3748
steps:
3849
- name: Checkout Code
3950
uses: actions/checkout@v1

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ d-operators
44
test/bin/
55
test/kubebin/
66
test/e2e/uninstall-k3s.txt
7+
test/integration/uninstall-k3s.txt
78
uninstall-k3s.txt
89
dope
910
daction

Dockerfile.testing

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# --------------------------
2+
# Build d-operators binary
3+
# --------------------------
4+
FROM golang:1.13.5 as builder
5+
6+
WORKDIR /mayadata.io/d-operators/
7+
8+
# copy all manifests
9+
COPY . .

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ test:
3434
testv:
3535
@go test ./... -cover -v -args --logtostderr -v=2
3636

37+
.PHONY: integration-test
38+
integration-test:
39+
@go test ./... -cover --tags=integration
40+
3741
.PHONY: e2e-test
3842
e2e-test:
3943
@cd test/e2e && ./suite.sh
4044

45+
.PHONY: integration-test-suite
46+
integration-test-suite:
47+
@cd test/integration && ./suite.sh
48+
4149
.PHONY: image
4250
image:
4351
docker build -t $(IMG_REPO):$(PACKAGE_VERSION) -t $(IMG_REPO):latest .

common/labels/labels_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

common/metac/hook_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

common/metac/validation_test.go

-17
This file was deleted.

common/unstruct/list_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

common/unstruct/unstruct_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/command/reconciler_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/http/reconciler_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/run/assert_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/run/create_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/run/createordelete_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/run/delete_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/run/run_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/run/task_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

controller/run/update_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

pkg/command/job_builder_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

pkg/command/reconciler_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

pkg/kubernetes/utility.go

-74
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ import (
2525
"k8s.io/client-go/discovery"
2626
"k8s.io/client-go/kubernetes"
2727
"k8s.io/client-go/rest"
28-
"k8s.io/klog/v2"
2928

30-
apierrors "k8s.io/apimachinery/pkg/api/errors"
3129
"openebs.io/metac/dynamic/clientset"
3230
dynamicclientset "openebs.io/metac/dynamic/clientset"
3331
dynamicdiscovery "openebs.io/metac/dynamic/discovery"
@@ -38,7 +36,6 @@ type UtilityConfig struct {
3836
KubeConfig *rest.Config
3937
APIDiscovery *dynamicdiscovery.APIResourceDiscovery
4038
Retry *Retryable
41-
IsTeardown bool
4239
}
4340

4441
// UtilityFuncs exposes Utility fields as functional options
@@ -72,14 +69,6 @@ type Utility struct {
7269
// definitions aka CRDs
7370
crdClient apiextnv1beta1.ApiextensionsV1beta1Interface
7471

75-
// If resources created using this utility should be deleted
76-
// when this instance's Teardown method is invoked
77-
isTeardown bool
78-
79-
// list of teardown functions invoked when this instance's
80-
// Teardown method is invoked
81-
teardownFuncs []func() error
82-
8372
// error as value
8473
err error
8574
}
@@ -136,7 +125,6 @@ func NewUtility(config UtilityConfig) (*Utility, error) {
136125
// Following order needs to be maintained
137126
var setters = []func(UtilityConfig){
138127
// pre settings
139-
u.setTeardownFlag,
140128
u.setAPIResourceDiscoveryOrDefault,
141129

142130
// post settings
@@ -153,10 +141,6 @@ func NewUtility(config UtilityConfig) (*Utility, error) {
153141
return u, nil
154142
}
155143

156-
func (u *Utility) setTeardownFlag(config UtilityConfig) {
157-
u.isTeardown = config.IsTeardown
158-
}
159-
160144
func (u *Utility) setAPIResourceDiscoveryOrDefault(config UtilityConfig) {
161145
u.apiResourceDiscovery = config.APIDiscovery
162146
if u.apiResourceDiscovery != nil {
@@ -205,64 +189,6 @@ func (u *Utility) setKubeClientset(config UtilityConfig) {
205189
)
206190
}
207191

208-
// MustTeardown deletes resources created through this instance
209-
func (u *Utility) MustTeardown() {
210-
// cleanup in descending order
211-
for i := len(u.teardownFuncs) - 1; i >= 0; i-- {
212-
teardown := u.teardownFuncs[i]
213-
err := teardown()
214-
if err != nil {
215-
if apierrors.IsNotFound(err) {
216-
klog.V(3).Infof(
217-
"Teardown ignored: Resource not found: %+v",
218-
err,
219-
)
220-
continue
221-
}
222-
if apierrors.IsConflict(err) {
223-
klog.V(3).Infof(
224-
"Teardown ignored: Conflict: %+v",
225-
err,
226-
)
227-
continue
228-
}
229-
// we treat the teardown error as level 1 Info
230-
klog.V(1).Infof(
231-
"Teardown failed: %s: %+v",
232-
apierrors.ReasonForError(err),
233-
err,
234-
)
235-
}
236-
}
237-
}
238-
239-
// Teardown optionally deletes resources created through this
240-
// instance
241-
func (u *Utility) Teardown() {
242-
if !u.isTeardown {
243-
return
244-
}
245-
u.MustTeardown()
246-
}
247-
248-
// MustAddToTeardown adds the given teardown function to
249-
// the list of teardown functions
250-
func (u *Utility) MustAddToTeardown(teardown func() error) {
251-
if teardown == nil {
252-
return
253-
}
254-
u.teardownFuncs = append(u.teardownFuncs, teardown)
255-
}
256-
257-
// AddToTeardown optionally adds the given teardown function to
258-
// the list of teardown functions
259-
func (u *Utility) AddToTeardown(teardown func() error) {
260-
if !u.isTeardown {
261-
return
262-
}
263-
u.MustAddToTeardown(teardown)
264-
}
265-
266192
// GetClientForAPIVersionAndKind returns the dynamic client for the
267193
// given api version & kind
268194
func (u *Utility) GetClientForAPIVersionAndKind(

pkg/kubernetes/utility_int_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// +build integration
2+
3+
/*
4+
Copyright 2020 The MayaData Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package kubernetes
20+
21+
import (
22+
"testing"
23+
24+
"k8s.io/client-go/rest"
25+
)
26+
27+
func TestNewFixtureIT(t *testing.T) {
28+
var tests = map[string]struct {
29+
kubeConfig *rest.Config
30+
isErr bool
31+
}{
32+
"nil kubeconfig": {},
33+
}
34+
for name, mock := range tests {
35+
name := name
36+
mock := mock
37+
t.Run(name, func(t *testing.T) {
38+
_, err := NewUtility(UtilityConfig{
39+
KubeConfig: mock.kubeConfig,
40+
})
41+
if mock.isErr && err == nil {
42+
t.Fatal("Expected error got none")
43+
}
44+
if !mock.isErr && err != nil {
45+
t.Fatalf("Expected no error got %s", err.Error())
46+
}
47+
})
48+
}
49+
}

pkg/kubernetes/utility_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !integration
2+
13
/*
24
Copyright 2020 The MayaData Authors.
35

pkg/recipe/assert.go

+12
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,17 @@ func (a *Assertable) Run() (types.AssertStatus, error) {
158158
return types.AssertStatus{}, a.err
159159
}
160160
}
161+
var errorOnAssertFailure bool
162+
if a.Assert.ErrorOnAssertFailure != nil {
163+
errorOnAssertFailure = *a.Assert.ErrorOnAssertFailure
164+
}
165+
if errorOnAssertFailure &&
166+
a.status.Phase != types.AssertResultPassed {
167+
return types.AssertStatus{}, errors.Errorf(
168+
"%s: ErrorOnAssertFailure=%t",
169+
a.status,
170+
errorOnAssertFailure,
171+
)
172+
}
161173
return *a.status, nil
162174
}

0 commit comments

Comments
 (0)