-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: morvencao <lcao@redhat.com>
- Loading branch information
Showing
2 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/openshift-online/maestro/pkg/api" | ||
"github.com/openshift-online/maestro/pkg/dao" | ||
"github.com/openshift-online/maestro/test" | ||
prommodel "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func TestStatusDispatcher(t *testing.T) { | ||
broker := os.Getenv("BROKER") | ||
if broker == "grpc" { | ||
t.Skip("StatusDispatcher is not supported with gRPC broker") | ||
} | ||
|
||
h, _ := test.RegisterIntegration(t) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer func() { | ||
cancel() | ||
}() | ||
|
||
// two instances besides the current instance | ||
instance1 := "instance1" | ||
instance2 := "instance2" | ||
|
||
// create 3 consumers | ||
consumer1 := "plugh" | ||
consumer2 := "xyzzy" | ||
consumer3 := "thud" | ||
_ = h.CreateConsumer(consumer1) | ||
_ = h.CreateConsumer(consumer2) | ||
_ = h.CreateConsumer(consumer3) | ||
|
||
// should dispatch to all consumers for current instance | ||
Eventually(func() bool { | ||
return h.StatusDispatcher.Dispatch(consumer1) && | ||
h.StatusDispatcher.Dispatch(consumer2) && | ||
h.StatusDispatcher.Dispatch(consumer3) | ||
}, 6*time.Second, 1*time.Second).Should(BeTrue()) | ||
|
||
// insert a new instance | ||
instanceDao := dao.NewInstanceDao(&h.Env().Database.SessionFactory) | ||
_, err := instanceDao.Create(ctx, &api.ServerInstance{ | ||
Meta: api.Meta{ | ||
ID: instance1, | ||
}, | ||
LastHeartbeat: time.Now(), | ||
Ready: false, | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// should dispatch consumer based on the instance status | ||
Eventually(func() bool { | ||
return !h.StatusDispatcher.Dispatch(consumer1) && | ||
h.StatusDispatcher.Dispatch(consumer2) && | ||
!h.StatusDispatcher.Dispatch(consumer3) | ||
}, 5*time.Second, 1*time.Second).Should(BeTrue()) | ||
|
||
// create another instance, but will be marked as unready after next checkInstances | ||
_, err = instanceDao.Create(ctx, &api.ServerInstance{ | ||
Meta: api.Meta{ | ||
ID: instance2, | ||
}, | ||
// last heartbeat is 3 seconds ago | ||
LastHeartbeat: time.Now().Add(-3 * time.Second), | ||
Ready: true, | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// finally should dispatch to all consumers for current instance | ||
Eventually(func() bool { | ||
return !h.StatusDispatcher.Dispatch(consumer1) && | ||
h.StatusDispatcher.Dispatch(consumer2) && | ||
!h.StatusDispatcher.Dispatch(consumer3) | ||
}, 6*time.Second, 1*time.Second).Should(BeTrue()) | ||
|
||
// check metrics for status resync | ||
time.Sleep(3 * time.Second) | ||
families := getServerMetrics(t, "http://localhost:8080/metrics") | ||
labels := []*prommodel.LabelPair{ | ||
{Name: strPtr("source"), Value: strPtr("maestro")}, | ||
{Name: strPtr("cluster"), Value: strPtr(consumer1)}, | ||
{Name: strPtr("type"), Value: strPtr("io.open-cluster-management.works.v1alpha1.manifests")}, | ||
} | ||
checkServerCounterMetric(t, families, "cloudevents_sent_total", labels, 2.0) | ||
labels = []*prommodel.LabelPair{ | ||
{Name: strPtr("source"), Value: strPtr("maestro")}, | ||
{Name: strPtr("cluster"), Value: strPtr(consumer2)}, | ||
{Name: strPtr("type"), Value: strPtr("io.open-cluster-management.works.v1alpha1.manifests")}, | ||
} | ||
checkServerCounterMetric(t, families, "cloudevents_sent_total", labels, 1.0) | ||
labels = []*prommodel.LabelPair{ | ||
{Name: strPtr("source"), Value: strPtr("maestro")}, | ||
{Name: strPtr("cluster"), Value: strPtr(consumer3)}, | ||
{Name: strPtr("type"), Value: strPtr("io.open-cluster-management.works.v1alpha1.manifests")}, | ||
} | ||
checkServerCounterMetric(t, families, "cloudevents_sent_total", labels, 2.0) | ||
} |