Skip to content

Commit 9416906

Browse files
authored
Merge pull request #121 from rejoshed/tests/add-more-domain-account-tests
Tests/Account & Domain Isolation and More Domain/Account Tests
2 parents 4f11f52 + 697376c commit 9416906

24 files changed

+607
-187
lines changed

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ run:
3333
skip-dirs:
3434
- pkg/mocks
3535
- test
36+
37+
issues:
38+
# Excluding configuration per-path, per-linter, per-text and per-source
39+
exclude-rules:
40+
# Exclude some linters from running on tests files.
41+
- path: _test\.go
42+
linters:
43+
- gosec

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ RELEASE_DIR ?= out
2323
export ACK_GINKGO_DEPRECATIONS := 1.16.5
2424
export ACK_GINKGO_RC=true
2525

26-
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
26+
export PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
2727
export PATH := $(PROJECT_DIR)/bin:$(PATH)
2828

2929
all: build
@@ -177,9 +177,8 @@ export KUBEBUILDER_ASSETS=$(PROJECT_DIR)/bin
177177

178178
.PHONY: test
179179
test: generate-mocks lint bin/ginkgo bin/kubectl bin/kube-apiserver bin/etcd ## Run tests. At the moment this is only unit tests.
180-
@./hack/testing_ginkgo_recover_statements.sh --add # Add ginkgo.GinkgoRecover() statements to controllers.
181180
@# The following is a slightly funky way to make sure the ginkgo statements are removed regardless the test results.
182-
@ginkgo_v2 --cover -coverprofile cover.out --covermode=atomic -v ./api/... ./controllers/... ./pkg/...; EXIT_STATUS=$$?;\
181+
@ginkgo_v2 --label-filter="!integ" --cover -coverprofile cover.out --covermode=atomic -v ./api/... ./controllers/... ./pkg/...; EXIT_STATUS=$$?;\
183182
./hack/testing_ginkgo_recover_statements.sh --remove; exit $$EXIT_STATUS
184183

185184
.PHONY: generate-mocks

api/v1beta1/cloudstackmachine_webhook_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package v1beta1_test
1818

1919
import (
2020
"context"
21+
2122
"sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
23+
"sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies"
2224

2325
. "github.com/onsi/ginkgo/v2"
2426
. "github.com/onsi/gomega"
25-
"sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies"
2627
)
2728

2829
var _ = Describe("CloudStackMachine webhook", func() {

api/v1beta1/cloudstackmachinetemplate_webhook_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package v1beta1_test
1818

1919
import (
2020
"context"
21+
2122
"sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
23+
"sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies"
2224

2325
. "github.com/onsi/ginkgo/v2"
2426
. "github.com/onsi/gomega"
25-
"sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies"
2627
)
2728

2829
var _ = Describe("CloudStackMachineTemplate webhook", func() {

api/v1beta1/webhook_suite_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"fmt"
23-
"k8s.io/client-go/rest"
2423
"net"
2524
"path/filepath"
2625
"testing"
2726
"time"
2827

28+
"k8s.io/client-go/rest"
29+
2930
. "github.com/onsi/ginkgo/v2"
3031
. "github.com/onsi/gomega"
3132

@@ -57,10 +58,10 @@ var _ = BeforeSuite(func() {
5758

5859
By("bootstrapping test environment")
5960
testEnv = &envtest.Environment{
60-
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
61+
CRDDirectoryPaths: []string{filepath.Join("../../", "config", "crd", "bases")},
6162
ErrorIfCRDPathMissing: false,
6263
WebhookInstallOptions: envtest.WebhookInstallOptions{
63-
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
64+
Paths: []string{filepath.Join("../../", "config", "webhook")},
6465
},
6566
}
6667

controllers/controllers_suite_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ import (
2020
"context"
2121
"fmt"
2222
"go/build"
23-
"k8s.io/client-go/rest"
2423
"os"
2524
"os/exec"
26-
"path"
2725
"path/filepath"
2826
"regexp"
29-
goruntime "runtime"
3027
"strings"
3128
"testing"
3229
"time"
3330

31+
"k8s.io/client-go/rest"
32+
3433
"github.com/apache/cloudstack-go/v2/cloudstack"
3534
"github.com/go-logr/logr"
3635
"github.com/golang/mock/gomock"
3736
. "github.com/onsi/ginkgo/v2"
3837
. "github.com/onsi/gomega"
38+
"github.com/pkg/errors"
3939
"k8s.io/client-go/kubernetes/scheme"
4040
ctrl "sigs.k8s.io/controller-runtime"
4141
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -106,11 +106,13 @@ func TestAPIs(t *testing.T) {
106106

107107
var _ = BeforeSuite(func() {
108108

109-
// Check for ginkgo recover statements.
110-
cmd := exec.Command("../hack/testing_ginkgo_recover_statements.sh", "--contains")
109+
projectDir := os.Getenv("PROJECT_DIR")
110+
111+
// Add ginkgo recover statements to controllers.
112+
cmd := exec.Command(projectDir+"/hack/testing_ginkgo_recover_statements.sh", "--add")
111113
cmd.Stdout = os.Stdout
112114
if err := cmd.Run(); err != nil {
113-
fmt.Println("Refusing to run tests without ginkgo recover set.")
115+
fmt.Println(errors.Wrapf(err, "adding gingko statements"))
114116
os.Exit(1)
115117
}
116118

@@ -121,17 +123,13 @@ var _ = BeforeSuite(func() {
121123
CS = mocks.NewMockClient(mockCtrl)
122124

123125
By("bootstrapping test environment")
124-
// Get the root of the current file to use in CRD paths.
125-
_, filename, _, _ := goruntime.Caller(0) //nolint
126-
root := path.Join(path.Dir(filename), "..")
127-
fmt.Println(root)
128126

129127
crdPaths := []string{
130-
filepath.Join(root, "config", "crd", "bases"),
128+
filepath.Join(projectDir, "config", "crd", "bases"),
131129
}
132130

133131
// Append CAPI CRDs path
134-
if capiPath := getFilePathToCAPICRDs(root); capiPath != "" {
132+
if capiPath := getFilePathToCAPICRDs(projectDir); capiPath != "" {
135133
crdPaths = append(crdPaths, capiPath)
136134
}
137135

@@ -183,6 +181,14 @@ var _ = BeforeSuite(func() {
183181
})
184182

185183
var _ = AfterSuite(func() {
184+
projectDir := os.Getenv("PROJECT_DIR")
185+
// Add ginkgo recover statements to controllers.
186+
cmd := exec.Command(projectDir+"/hack/testing_ginkgo_recover_statements.sh", "--remove")
187+
cmd.Stdout = os.Stdout
188+
if err := cmd.Run(); err != nil {
189+
fmt.Println(errors.Wrapf(err, "cleaning up gingko statements"))
190+
os.Exit(1)
191+
}
186192
cancel()
187193
By("tearing down the test environment")
188194
Ω(testEnv.Stop()).Should(Succeed())

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/onsi/ginkgo/v2 v2.1.4
1111
github.com/onsi/gomega v1.19.0
1212
github.com/pkg/errors v0.9.1
13+
github.com/smallfish/simpleyaml v0.1.0
1314
github.com/spf13/pflag v1.0.5
1415
gopkg.in/ini.v1 v1.63.2
1516
k8s.io/api v0.23.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
517517
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
518518
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
519519
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
520+
github.com/smallfish/simpleyaml v0.1.0 h1:5uAZdLAiHxS9cmzkOxg7lH0dILXKTD7uRZbAhyHmyU0=
521+
github.com/smallfish/simpleyaml v0.1.0/go.mod h1:gU3WdNn44dQVAbVHD2SrSqKKCvmzFApWD2UURhgEj1M=
520522
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
521523
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
522524
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=

pkg/cloud/affinity_groups_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,32 @@ var _ = Describe("AffinityGroup Unit Tests", func() {
6666
Ω(client.GetOrCreateAffinityGroup(dummies.AffinityGroup)).Should(Succeed())
6767
})
6868

69-
Context("AffinityGroup Integ Tests", func() {
70-
client, connectionErr := cloud.NewClient("../../cloud-config")
69+
Context("AffinityGroup Integ Tests", Label("integ"), func() {
7170

7271
BeforeEach(func() {
73-
if connectionErr != nil { // Only do these tests if an actual ACS instance is available via cloud-config.
74-
Skip("Could not connect to ACS instance.")
75-
}
72+
client = realCloudClient
7673
dummies.AffinityGroup.ID = "" // Force name fetching.
7774
})
78-
AfterEach(func() {
79-
mockCtrl.Finish()
80-
})
8175

82-
It("Creates an affinity group.", func() {
83-
Ω(client.GetOrCreateAffinityGroup(dummies.AffinityGroup)).Should(Succeed())
84-
})
8576
It("Associates an affinity group.", func() {
86-
if err := client.GetOrCreateVMInstance(
77+
Ω(client.ResolveZone(dummies.CSZone1)).Should(Succeed())
78+
dummies.CSMachine1.Status.ZoneID = dummies.CSZone1.Spec.ID
79+
dummies.CSMachine1.Spec.DiskOffering.Name = ""
80+
81+
Ω(client.GetOrCreateVMInstance(
8782
dummies.CSMachine1, dummies.CAPIMachine, dummies.CSCluster, dummies.CSZone1, dummies.CSAffinityGroup, "",
88-
); err != nil {
89-
Skip("Could not create VM." + err.Error())
90-
}
83+
)).Should(Succeed())
84+
9185
Ω(client.GetOrCreateAffinityGroup(dummies.AffinityGroup)).Should(Succeed())
9286
Ω(client.AssociateAffinityGroup(dummies.CSMachine1, *dummies.AffinityGroup)).Should(Succeed())
87+
88+
// Make the created VM go away quickly by force stopping it.
89+
p := realCSClient.VirtualMachine.NewStopVirtualMachineParams(*dummies.CSMachine1.Spec.InstanceID)
90+
p.SetForced(true)
91+
Ω(realCSClient.VirtualMachine.StopVirtualMachine(p)).Should(Succeed())
9392
})
94-
It("Deletes an affinity group.", func() {
93+
94+
It("Creates and deletes an affinity group.", func() {
9595
Ω(client.DeleteAffinityGroup(dummies.AffinityGroup)).Should(Succeed())
9696
Ω(client.FetchAffinityGroup(dummies.AffinityGroup)).ShouldNot(Succeed())
9797
})

pkg/cloud/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626

2727
//go:generate mockgen -destination=../mocks/mock_client.go -package=mocks sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud Client
2828

29+
const GLOBAL = "Global"
30+
2931
type Client interface {
3032
VMIface
3133
NetworkIface
@@ -55,9 +57,9 @@ func NewClient(ccPath string) (Client, error) {
5557
c := &client{config: Config{VerifySSL: true}}
5658
if rawCfg, err := ini.Load(ccPath); err != nil {
5759
return nil, errors.Wrapf(err, "reading config at path %s", ccPath)
58-
} else if g := rawCfg.Section("Global"); len(g.Keys()) == 0 {
60+
} else if g := rawCfg.Section(GLOBAL); len(g.Keys()) == 0 {
5961
return nil, errors.New("section Global not found")
60-
} else if err = rawCfg.Section("Global").StrictMapTo(&c.config); err != nil {
62+
} else if err = rawCfg.Section(GLOBAL).StrictMapTo(&c.config); err != nil {
6163
return nil, errors.Wrapf(err, "parsing [Global] section from config at path %s", ccPath)
6264
}
6365

0 commit comments

Comments
 (0)