mirror of
https://github.com/fluxcd/flux2.git
synced 2025-10-28 05:04:48 +08:00
make tests for notifications provider agnostic
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
committed by
Sunny
parent
7c1b897919
commit
e63ddb99de
@ -21,7 +21,6 @@ package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@ -29,7 +28,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
eventhub "github.com/Azure/azure-event-hubs-go/v3"
|
||||
"github.com/microsoft/azure-devops-go-api/azuredevops"
|
||||
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
|
||||
. "github.com/onsi/gomega"
|
||||
@ -37,185 +35,14 @@ import (
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
|
||||
notiv1 "github.com/fluxcd/notification-controller/api/v1"
|
||||
notiv1beta2 "github.com/fluxcd/notification-controller/api/v1beta2"
|
||||
events "github.com/fluxcd/pkg/apis/event/v1beta1"
|
||||
"github.com/fluxcd/pkg/apis/meta"
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1"
|
||||
)
|
||||
|
||||
func TestEventHubNotification(t *testing.T) {
|
||||
g := NewWithT(t)
|
||||
|
||||
ctx := context.TODO()
|
||||
branchName := "test-notification"
|
||||
testID := branchName + "-" + randStringRunes(5)
|
||||
|
||||
// Start listening to eventhub with latest offset
|
||||
// TODO(somtochiama): Make here provider agnostic
|
||||
hub, err := eventhub.NewHubFromConnectionString(cfg.notificationURL)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
c := make(chan string, 10)
|
||||
handler := func(ctx context.Context, event *eventhub.Event) error {
|
||||
c <- string(event.Data)
|
||||
return nil
|
||||
}
|
||||
runtimeInfo, err := hub.GetRuntimeInformation(ctx)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(len(runtimeInfo.PartitionIDs)).To(Equal(1))
|
||||
listenerHandler, err := hub.Receive(ctx, runtimeInfo.PartitionIDs[0], handler, eventhub.ReceiveWithLatestOffset())
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Setup Flux resources
|
||||
manifest := `apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: foobar`
|
||||
repoUrl := getTransportURL(cfg.applicationRepository)
|
||||
client, err := getRepository(ctx, t.TempDir(), repoUrl, defaultBranch, cfg.defaultAuthOpts)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
files := make(map[string]io.Reader)
|
||||
files["configmap.yaml"] = strings.NewReader(manifest)
|
||||
err = commitAndPushAll(ctx, client, files, branchName)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
namespace := corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testID,
|
||||
},
|
||||
}
|
||||
g.Expect(testEnv.Create(ctx, &namespace)).To(Succeed())
|
||||
defer testEnv.Delete(ctx, &namespace)
|
||||
|
||||
secret := corev1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testID,
|
||||
Namespace: testID,
|
||||
},
|
||||
StringData: map[string]string{
|
||||
"address": cfg.notificationURL,
|
||||
},
|
||||
}
|
||||
g.Expect(testEnv.Create(ctx, &secret)).To(Succeed())
|
||||
defer testEnv.Delete(ctx, &secret)
|
||||
|
||||
provider := notiv1beta2.Provider{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testID,
|
||||
Namespace: testID,
|
||||
},
|
||||
Spec: notiv1beta2.ProviderSpec{
|
||||
Type: "azureeventhub",
|
||||
Address: repoUrl,
|
||||
SecretRef: &meta.LocalObjectReference{
|
||||
Name: testID,
|
||||
},
|
||||
},
|
||||
}
|
||||
g.Expect(testEnv.Create(ctx, &provider)).To(Succeed())
|
||||
defer testEnv.Delete(ctx, &provider)
|
||||
|
||||
alert := notiv1beta2.Alert{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testID,
|
||||
Namespace: testID,
|
||||
},
|
||||
Spec: notiv1beta2.AlertSpec{
|
||||
ProviderRef: meta.LocalObjectReference{
|
||||
Name: provider.Name,
|
||||
},
|
||||
EventSources: []notiv1.CrossNamespaceObjectReference{
|
||||
{
|
||||
Kind: "Kustomization",
|
||||
Name: testID,
|
||||
Namespace: testID,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
g.Expect(testEnv.Create(ctx, &alert)).ToNot(HaveOccurred())
|
||||
defer testEnv.Delete(ctx, &alert)
|
||||
|
||||
g.Eventually(func() bool {
|
||||
nn := types.NamespacedName{Name: alert.Name, Namespace: alert.Namespace}
|
||||
alertObj := ¬iv1beta2.Alert{}
|
||||
err := testEnv.Get(ctx, nn, alertObj)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if err := checkReadyCondition(alertObj); err != nil {
|
||||
t.Log(err)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}, testTimeout, testInterval).Should(BeTrue())
|
||||
|
||||
modifyKsSpec := func(spec *kustomizev1.KustomizationSpec) {
|
||||
spec.Interval = metav1.Duration{Duration: 30 * time.Second}
|
||||
spec.HealthChecks = []meta.NamespacedObjectKindReference{
|
||||
{
|
||||
APIVersion: "v1",
|
||||
Kind: "ConfigMap",
|
||||
Name: "foobar",
|
||||
Namespace: testID,
|
||||
},
|
||||
}
|
||||
}
|
||||
g.Expect(setUpFluxConfig(ctx, testID, nsConfig{
|
||||
repoURL: repoUrl,
|
||||
ref: &sourcev1.GitRepositoryRef{
|
||||
Branch: branchName,
|
||||
},
|
||||
path: "./",
|
||||
modifyKsSpec: modifyKsSpec,
|
||||
})).To(Succeed())
|
||||
t.Cleanup(func() {
|
||||
err := tearDownFluxConfig(ctx, testID)
|
||||
if err != nil {
|
||||
t.Logf("failed to delete resources in '%s' namespace: %s", testID, err)
|
||||
}
|
||||
})
|
||||
|
||||
g.Eventually(func() bool {
|
||||
err := verifyGitAndKustomization(ctx, testEnv, testID, testID)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}, testTimeout, testInterval).Should(BeTrue())
|
||||
|
||||
// Wait to read even from event hub
|
||||
g.Eventually(func() bool {
|
||||
select {
|
||||
case eventJson := <-c:
|
||||
event := &events.Event{}
|
||||
err := json.Unmarshal([]byte(eventJson), event)
|
||||
if err != nil {
|
||||
t.Logf("the received event type does not match Flux format, error: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
if event.InvolvedObject.Kind == kustomizev1.KustomizationKind &&
|
||||
event.InvolvedObject.Name == testID && event.InvolvedObject.Namespace == testID {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}, testTimeout, 1*time.Second).Should(BeTrue())
|
||||
err = listenerHandler.Close(ctx)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
err = hub.Close(ctx)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
}
|
||||
|
||||
func TestAzureDevOpsCommitStatus(t *testing.T) {
|
||||
g := NewWithT(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user