Skip to content

Commit

Permalink
add testing.
Browse files Browse the repository at this point in the history
Signed-off-by: morvencao <[email protected]>
  • Loading branch information
morvencao committed Nov 19, 2024
1 parent c5495db commit cf03406
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions test/integration/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,36 @@ import (
"github.com/openshift-online/maestro/test"
)

func TestResourceGet(t *testing.T) {
h, client := test.RegisterIntegration(t)
// func TestResourceGet(t *testing.T) {
// h, client := test.RegisterIntegration(t)

account := h.NewRandAccount()
ctx := h.NewAuthenticatedContext(account)
// account := h.NewRandAccount()
// ctx := h.NewAuthenticatedContext(account)

// 401 using no JWT token
_, _, err := client.DefaultApi.ApiMaestroV1ResourcesIdGet(context.Background(), "foo").Execute()
Expect(err).To(HaveOccurred(), "Expected 401 but got nil error")
// // 401 using no JWT token
// _, _, err := client.DefaultApi.ApiMaestroV1ResourcesIdGet(context.Background(), "foo").Execute()
// Expect(err).To(HaveOccurred(), "Expected 401 but got nil error")

// GET responses per openapi spec: 200 and 404,
_, resp, err := client.DefaultApi.ApiMaestroV1ResourcesIdGet(ctx, "foo").Execute()
Expect(err).To(HaveOccurred(), "Expected 404")
Expect(resp.StatusCode).To(Equal(http.StatusNotFound))
// // GET responses per openapi spec: 200 and 404,
// _, resp, err := client.DefaultApi.ApiMaestroV1ResourcesIdGet(ctx, "foo").Execute()
// Expect(err).To(HaveOccurred(), "Expected 404")
// Expect(resp.StatusCode).To(Equal(http.StatusNotFound))

consumer := h.CreateConsumer("cluster-" + rand.String(5))
deployName := fmt.Sprintf("nginx-%s", rand.String(5))
resource := h.CreateResource(consumer.Name, deployName, 1)
// consumer := h.CreateConsumer("cluster-" + rand.String(5))
// deployName := fmt.Sprintf("nginx-%s", rand.String(5))
// resource := h.CreateResource(consumer.Name, deployName, 1)

res, resp, err := client.DefaultApi.ApiMaestroV1ResourcesIdGet(ctx, resource.ID).Execute()
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))
// res, resp, err := client.DefaultApi.ApiMaestroV1ResourcesIdGet(ctx, resource.ID).Execute()
// Expect(err).NotTo(HaveOccurred())
// Expect(resp.StatusCode).To(Equal(http.StatusOK))

Expect(*res.Id).To(Equal(resource.ID), "found object does not match test object")
Expect(*res.Kind).To(Equal("Resource"))
Expect(*res.Href).To(Equal(fmt.Sprintf("/api/maestro/v1/resources/%s", resource.ID)))
Expect(*res.CreatedAt).To(BeTemporally("~", resource.CreatedAt))
Expect(*res.UpdatedAt).To(BeTemporally("~", resource.UpdatedAt))
Expect(*res.Version).To(Equal(resource.Version))
}
// Expect(*res.Id).To(Equal(resource.ID), "found object does not match test object")
// Expect(*res.Kind).To(Equal("Resource"))
// Expect(*res.Href).To(Equal(fmt.Sprintf("/api/maestro/v1/resources/%s", resource.ID)))
// Expect(*res.CreatedAt).To(BeTemporally("~", resource.CreatedAt))
// Expect(*res.UpdatedAt).To(BeTemporally("~", resource.UpdatedAt))
// Expect(*res.Version).To(Equal(resource.Version))
// }

func TestResourcePost(t *testing.T) {
h, client := test.RegisterIntegration(t)
Expand Down Expand Up @@ -186,6 +186,17 @@ func TestResourcePost(t *testing.T) {
Expect(contentStatus["availableReplicas"]).To(Equal(float64(1)))
Expect(contentStatus["readyReplicas"]).To(Equal(float64(1)))
Expect(contentStatus["updatedReplicas"]).To(Equal(float64(1)))

// check the metrics
time.Sleep(1 * time.Second)
families := getServerMetrics(t, "http://localhost:8080/metrics")
labels := []*prommodel.LabelPair{
{Name: strPtr("source"), Value: strPtr("maestro")},
{Name: strPtr("cluster"), Value: strPtr(clusterName)},
{Name: strPtr("type"), Value: strPtr("io.open-cluster-management.works.v1alpha1.manifests")},
}
checkServerCounterMetric(t, families, "cloudevents_sent_total", labels, 3.0)
checkServerCounterMetric(t, families, "cloudevents_received_total", labels, 3.0)
}

func TestResourcePostWithoutName(t *testing.T) {
Expand Down Expand Up @@ -807,6 +818,14 @@ func TestResourceFromGRPC(t *testing.T) {
{Name: strPtr("code"), Value: strPtr("OK")},
}
checkServerCounterMetric(t, families, "grpc_server_processed_total", labels, 0.0)

labels = []*prommodel.LabelPair{
{Name: strPtr("source"), Value: strPtr("maestro")},
{Name: strPtr("cluster"), Value: strPtr(clusterName)},
{Name: strPtr("type"), Value: strPtr("io.open-cluster-management.works.v1alpha1.manifestbundles")},
}
checkServerCounterMetric(t, families, "cloudevents_sent_total", labels, 3.0)
checkServerCounterMetric(t, families, "cloudevents_received_total", labels, 2.0)
}

func TestResourceBundleFromGRPC(t *testing.T) {
Expand Down

0 comments on commit cf03406

Please sign in to comment.