Skip to content

Commit e2151f5

Browse files
committed
Migrate tests to Gomega
Signed-off-by: Stefan Prodan <[email protected]>
1 parent a6983bc commit e2151f5

31 files changed

+545
-437
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ require (
3939
github.com/sethvargo/go-limiter v1.0.0
4040
github.com/slok/go-http-metrics v0.13.0
4141
github.com/spf13/pflag v1.0.7
42-
github.com/stretchr/testify v1.11.1
4342
gitlab.com/gitlab-org/api/client-go v0.137.0
4443
golang.org/x/oauth2 v0.30.0
4544
golang.org/x/text v0.28.0

internal/notifier/alertmanager_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,24 @@ import (
2424
"net/http/httptest"
2525
"testing"
2626

27-
"github.com/stretchr/testify/require"
27+
. "github.com/onsi/gomega"
2828
)
2929

3030
func TestAlertmanager_Post(t *testing.T) {
31+
g := NewWithT(t)
3132
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3233
b, err := io.ReadAll(r.Body)
33-
require.NoError(t, err)
34+
g.Expect(err).ToNot(HaveOccurred())
3435
var payload []AlertManagerAlert
3536
err = json.Unmarshal(b, &payload)
36-
require.NoError(t, err)
37+
g.Expect(err).ToNot(HaveOccurred())
3738

3839
}))
3940
defer ts.Close()
4041

4142
alertmanager, err := NewAlertmanager(ts.URL, "", nil, "", "", "")
42-
require.NoError(t, err)
43+
g.Expect(err).ToNot(HaveOccurred())
4344

4445
err = alertmanager.Post(context.TODO(), testEvent())
45-
require.NoError(t, err)
46+
g.Expect(err).ToNot(HaveOccurred())
4647
}

internal/notifier/azure_devops_test.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,50 @@ package notifier
1919
import (
2020
"context"
2121
"testing"
22+
"time"
2223

2324
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
2425
"github.com/microsoft/azure-devops-go-api/azuredevops/v6/git"
25-
"github.com/stretchr/testify/assert"
26+
. "github.com/onsi/gomega"
2627
corev1 "k8s.io/api/core/v1"
2728
)
2829

2930
func TestNewAzureDevOpsBasic(t *testing.T) {
30-
a, err := NewAzureDevOps(context.TODO(), "kustomization/gitops-system/0c9c2e41", "https://dev.azure.com/foo/bar/_git/baz", "foo", nil, "", "", "", "", nil, nil)
31-
assert.Nil(t, err)
32-
assert.Equal(t, a.Project, "bar")
33-
assert.Equal(t, a.Repo, "baz")
31+
g := NewWithT(t)
32+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
33+
defer cancel()
34+
a, err := NewAzureDevOps(ctx, "kustomization/gitops-system/0c9c2e41", "https://dev.azure.com/foo/bar/_git/baz", "foo", nil, "", "", "", "", nil, nil)
35+
g.Expect(err).ToNot(HaveOccurred())
36+
g.Expect(a.Project).To(Equal("bar"))
37+
g.Expect(a.Repo).To(Equal("baz"))
3438
}
3539

3640
func TestNewAzureDevOpsInvalidUrl(t *testing.T) {
37-
_, err := NewAzureDevOps(context.TODO(), "kustomization/gitops-system/0c9c2e41", "https://dev.azure.com/foo/bar/baz", "foo", nil, "", "", "", "", nil, nil)
38-
assert.NotNil(t, err)
41+
g := NewWithT(t)
42+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
43+
defer cancel()
44+
_, err := NewAzureDevOps(ctx, "kustomization/gitops-system/0c9c2e41", "https://dev.azure.com/foo/bar/baz", "foo", nil, "", "", "", "", nil, nil)
45+
g.Expect(err).To(HaveOccurred())
3946
}
4047

4148
func TestNewAzureDevOpsMissingToken(t *testing.T) {
42-
_, err := NewAzureDevOps(context.TODO(), "kustomization/gitops-system/0c9c2e41", "https://dev.azure.com/foo/bar/baz", "", nil, "", "", "", "", nil, nil)
43-
assert.NotNil(t, err)
49+
g := NewWithT(t)
50+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
51+
defer cancel()
52+
_, err := NewAzureDevOps(ctx, "kustomization/gitops-system/0c9c2e41", "https://dev.azure.com/foo/bar/baz", "", nil, "", "", "", "", nil, nil)
53+
g.Expect(err).To(HaveOccurred())
4454
}
4555

4656
func TestNewAzureDevOpsEmptyCommitStatus(t *testing.T) {
47-
_, err := NewAzureDevOps(context.TODO(), "", "https://dev.azure.com/foo/bar/_git/baz", "foo", nil, "", "", "", "", nil, nil)
48-
assert.NotNil(t, err)
57+
g := NewWithT(t)
58+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
59+
defer cancel()
60+
_, err := NewAzureDevOps(ctx, "", "https://dev.azure.com/foo/bar/_git/baz", "foo", nil, "", "", "", "", nil, nil)
61+
g.Expect(err).To(HaveOccurred())
4962
}
5063

5164
func TestDuplicateAzureDevOpsStatus(t *testing.T) {
52-
assert := assert.New(t)
65+
g := NewWithT(t)
5366

5467
var tests = []struct {
5568
ss *[]git.GitStatus
@@ -64,7 +77,7 @@ func TestDuplicateAzureDevOpsStatus(t *testing.T) {
6477
}
6578

6679
for _, test := range tests {
67-
assert.Equal(test.dup, duplicateAzureDevOpsStatus(test.ss, test.s))
80+
g.Expect(duplicateAzureDevOpsStatus(test.ss, test.s)).To(Equal(test.dup))
6881
}
6982
}
7083

@@ -165,16 +178,19 @@ func TestAzureDevOps_Post(t *testing.T) {
165178

166179
for _, tt := range postTests {
167180
t.Run(tt.name, func(t *testing.T) {
168-
a, err := NewAzureDevOps(context.TODO(), "kustomization/gitops-system/0c9c2e41", "https://example.com/foo/bar/_git/baz", "foo", nil, "", "", "", "", nil, nil)
181+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
182+
defer cancel()
183+
a, err := NewAzureDevOps(ctx, "kustomization/gitops-system/0c9c2e41", "https://example.com/foo/bar/_git/baz", "foo", nil, "", "", "", "", nil, nil)
169184
fakeClient := &fakeDevOpsClient{}
170185
a.Client = fakeClient
171-
assert.Nil(t, err)
186+
g := NewWithT(t)
187+
g.Expect(err).ToNot(HaveOccurred())
172188

173-
err = a.Post(context.TODO(), tt.event)
174-
assert.Nil(t, err)
189+
err = a.Post(ctx, tt.event)
190+
g.Expect(err).ToNot(HaveOccurred())
175191

176192
want := []git.CreateCommitStatusArgs{tt.want}
177-
assert.Equal(t, want, fakeClient.created)
193+
g.Expect(fakeClient.created).To(Equal(want))
178194
})
179195
}
180196
}

internal/notifier/azure_eventhub.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/*
2-
Copyright 2021 The Flux authors
2+
Copyright 2025 The Flux authors
3+
34
Licensed under the Apache License, Version 2.0 (the "License");
45
you may not use this file except in compliance with the License.
56
You may obtain a copy of the License at
7+
68
http://www.apache.org/licenses/LICENSE-2.0
9+
710
Unless required by applicable law or agreed to in writing, software
811
distributed under the License is distributed on an "AS IS" BASIS,
912
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -40,15 +43,15 @@ type AzureEventHub struct {
4043
func NewAzureEventHub(ctx context.Context, endpointURL, token, eventHubNamespace, proxy,
4144
serviceAccountName, providerName, providerNamespace string, tokenClient client.Client,
4245
tokenCache *cache.TokenCache) (*AzureEventHub, error) {
43-
var client *eventhub.ProducerClient
46+
var producerClient *eventhub.ProducerClient
4447
var err error
4548

4649
if err := validateAuthOptions(endpointURL, token, serviceAccountName); err != nil {
4750
return nil, fmt.Errorf("invalid authentication options: %v", err)
4851
}
4952

5053
if isSASAuth(endpointURL) {
51-
client, err = newSASHub(endpointURL)
54+
producerClient, err = newSASHub(endpointURL)
5255
if err != nil {
5356
return nil, fmt.Errorf("failed to create a eventhub using SAS: %w", err)
5457
}
@@ -63,14 +66,14 @@ func NewAzureEventHub(ctx context.Context, endpointURL, token, eventHubNamespace
6366
} else {
6467
log.FromContext(ctx).Error(nil, "warning: static JWT authentication is deprecated and will be removed in the future, prefer workload identity: https://fluxcd.io/flux/components/notification/providers/#managed-identity")
6568
}
66-
client, err = newJWTHub(endpointURL, token, eventHubNamespace)
69+
producerClient, err = newJWTHub(endpointURL, token, eventHubNamespace)
6770
if err != nil {
6871
return nil, fmt.Errorf("failed to create a eventhub using authentication token: %w", err)
6972
}
7073
}
7174

7275
return &AzureEventHub{
73-
ProducerClient: client,
76+
ProducerClient: producerClient,
7477
}, nil
7578
}
7679

@@ -140,12 +143,12 @@ func newJWTHub(eventhubName, token, eventHubNamespace string) (*eventhub.Produce
140143

141144
// newSASHub used when address is a SAS ConnectionString
142145
func newSASHub(address string) (*eventhub.ProducerClient, error) {
143-
client, err := eventhub.NewProducerClientFromConnectionString(address, "", nil)
146+
producerClient, err := eventhub.NewProducerClientFromConnectionString(address, "", nil)
144147
if err != nil {
145148
return nil, err
146149
}
147150

148-
return client, nil
151+
return producerClient, nil
149152
}
150153

151154
// validateAuthOptions checks if the authentication options are valid

internal/notifier/azure_eventhub_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/*
2-
Copyright 2021 The Flux authors
2+
Copyright 2025 The Flux authors
3+
34
Licensed under the Apache License, Version 2.0 (the "License");
45
you may not use this file except in compliance with the License.
56
You may obtain a copy of the License at
7+
68
http://www.apache.org/licenses/LICENSE-2.0
9+
710
Unless required by applicable law or agreed to in writing, software
811
distributed under the License is distributed on an "AS IS" BASIS,
912
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,8 +20,9 @@ import (
1720
"context"
1821
"errors"
1922
"testing"
23+
"time"
2024

21-
"github.com/stretchr/testify/assert"
25+
. "github.com/onsi/gomega"
2226
)
2327

2428
func TestNewAzureEventHub(t *testing.T) {
@@ -54,7 +58,7 @@ func TestNewAzureEventHub(t *testing.T) {
5458
endpointURL: "azure-nc-eventhub",
5559
token: "",
5660
eventHubNamespace: "namespace",
57-
err: errors.New("failed to create a eventhub using managed identity: failed to get token: failed to create provider access token for the controller: ManagedIdentityCredential: failed to authenticate a system assigned identity. The endpoint responded with {\"error\":\"invalid_request\",\"error_description\":\"Identity not found\"}"),
61+
err: errors.New("failed to create a eventhub using managed identity: failed to get token: failed to create provider access token for the controller: ManagedIdentityCredential"),
5862
},
5963
{
6064
name: "SAS auth with serviceAccountName set",
@@ -90,13 +94,16 @@ func TestNewAzureEventHub(t *testing.T) {
9094

9195
for _, tt := range tests {
9296
t.Run(tt.name, func(t *testing.T) {
93-
client, err := NewAzureEventHub(context.TODO(), tt.endpointURL, tt.token, tt.eventHubNamespace, "", tt.serviceAccountName, "", "", nil, nil)
97+
g := NewWithT(t)
98+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
99+
defer cancel()
100+
client, err := NewAzureEventHub(ctx, tt.endpointURL, tt.token, tt.eventHubNamespace, "", tt.serviceAccountName, "", "", nil, nil)
94101
if tt.err != nil {
95-
assert.Error(t, err)
96-
assert.ErrorContains(t, err, tt.err.Error())
102+
g.Expect(err).To(HaveOccurred())
103+
g.Expect(err).To(MatchError(ContainSubstring(tt.err.Error())))
97104
} else {
98-
assert.NoError(t, err)
99-
assert.NotNil(t, client)
105+
g.Expect(err).ToNot(HaveOccurred())
106+
g.Expect(client).ToNot(BeNil())
100107
}
101108
})
102109
}

internal/notifier/bitbucket_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,32 @@ package notifier
1919
import (
2020
"testing"
2121

22-
"github.com/stretchr/testify/assert"
22+
. "github.com/onsi/gomega"
2323
)
2424

2525
func TestNewBitbucketBasic(t *testing.T) {
26+
g := NewWithT(t)
2627
b, err := NewBitbucket("kustomization/gitops-system/0c9c2e41", "https://bitbucket.org/foo/bar", "foo:bar", nil)
27-
assert.Nil(t, err)
28-
assert.Equal(t, b.Owner, "foo")
29-
assert.Equal(t, b.Repo, "bar")
30-
assert.Equal(t, b.CommitStatus, "kustomization/gitops-system/0c9c2e41")
28+
g.Expect(err).ToNot(HaveOccurred())
29+
g.Expect(b.Owner).To(Equal("foo"))
30+
g.Expect(b.Repo).To(Equal("bar"))
31+
g.Expect(b.CommitStatus).To(Equal("kustomization/gitops-system/0c9c2e41"))
3132
}
3233

3334
func TestNewBitbucketEmptyCommitStatus(t *testing.T) {
35+
g := NewWithT(t)
3436
_, err := NewBitbucket("", "https://bitbucket.org/foo/bar", "foo:bar", nil)
35-
assert.NotNil(t, err)
37+
g.Expect(err).To(HaveOccurred())
3638
}
3739

3840
func TestNewBitbucketInvalidUrl(t *testing.T) {
41+
g := NewWithT(t)
3942
_, err := NewBitbucket("kustomization/gitops-system/0c9c2e41", "https://bitbucket.org/foo/bar/baz", "foo:bar", nil)
40-
assert.NotNil(t, err)
43+
g.Expect(err).To(HaveOccurred())
4144
}
4245

4346
func TestNewBitbucketInvalidToken(t *testing.T) {
47+
g := NewWithT(t)
4448
_, err := NewBitbucket("kustomization/gitops-system/0c9c2e41", "https://bitbucket.org/foo/bar", "bar", nil)
45-
assert.NotNil(t, err)
49+
g.Expect(err).To(HaveOccurred())
4650
}

0 commit comments

Comments
 (0)