Skip to content

Commit

Permalink
refac: Include e2e monitoring stack tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khalifaa55 committed Sep 17, 2024
1 parent 07390d3 commit b53a947
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 207 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ test: generate ## run tests
@go test -coverprofile=coverage/coverage.out -covermode=count -timeout 25m ./...

e2e-test: generate ## Run e2e tests
@go test -timeout 20m -count=1 ./e2e/...
@go test -timeout 25m -count=1 ./e2e/...

e2e-test-windows: generate ## Run e2e tests on Windows
@go test -timeout 20m -count=1 -skip TestE2E_MonitoringStack ./e2e/...
@go test -timeout 25m -count=1 -skip TestE2E_MonitoringStack ./e2e/...

test-no-e2e: generate ## run tests excluding e2e
@mkdir -p coverage
Expand Down
34 changes: 25 additions & 9 deletions e2e/checks.go → e2e/sedge/checks.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
Copyright 2022 Nethermind
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e

import (
Expand All @@ -9,6 +24,7 @@ import (
"testing"
"time"

base "github.com/NethermindEth/sedge/e2e"
"github.com/NethermindEth/sedge/internal/pkg/services"
"github.com/cenkalti/backoff"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -90,28 +106,28 @@ func checkPrometheusTargetsUp(t *testing.T, targets ...string) {
logPrefix := fmt.Sprintf("checkPrometheusTargetsUp (%d)", tries)
promTargets, err = prometheusTargets(t)
if err != nil {
return logAndPipeError(t, logPrefix, err)
return base.LogAndPipeError(t, logPrefix, err)
}
if promTargets.Status != "success" {
return logAndPipeError(t, logPrefix, fmt.Errorf("expected status success, got %s", promTargets.Status))
return base.LogAndPipeError(t, logPrefix, fmt.Errorf("expected status success, got %s", promTargets.Status))
}
if len(promTargets.Data.ActiveTargets) != len(targets) {
return logAndPipeError(t, logPrefix, fmt.Errorf("expected %d targets, got %d", len(targets), len(promTargets.Data.ActiveTargets)))
return base.LogAndPipeError(t, logPrefix, fmt.Errorf("expected %d targets, got %d", len(targets), len(promTargets.Data.ActiveTargets)))
}
for i, target := range promTargets.Data.ActiveTargets {
var labels []string
for label := range target.Labels {
labels = append(labels, label)
}
if !slices.Contains(labels, "instance") {
return logAndPipeError(t, logPrefix, fmt.Errorf("target %d does not have instance label", i))
return base.LogAndPipeError(t, logPrefix, fmt.Errorf("target %d does not have instance label", i))
}
instanceLabel := target.Labels["instance"]
if !slices.Contains(targets, instanceLabel) {
return logAndPipeError(t, logPrefix, fmt.Errorf("target %d instance label is not expected", i))
return base.LogAndPipeError(t, logPrefix, fmt.Errorf("target %d instance label is not expected", i))
}
if target.Health == "unknown" {
return logAndPipeError(t, logPrefix, fmt.Errorf("target %d health is unknown", i))
return base.LogAndPipeError(t, logPrefix, fmt.Errorf("target %d health is unknown", i))
}
}
return nil
Expand All @@ -134,14 +150,14 @@ func checkGrafanaHealth(t *testing.T) {
BasicAuth: url.UserPassword("admin", "admin"),
})
if err != nil {
return logAndPipeError(t, logPrefix, err)
return base.LogAndPipeError(t, logPrefix, err)
}
healthResponse, err := gClient.Health()
if err != nil {
return logAndPipeError(t, logPrefix, err)
return base.LogAndPipeError(t, logPrefix, err)
}
if healthResponse.Database != "ok" {
return logAndPipeError(t, logPrefix, fmt.Errorf("expected database ok, got %s", healthResponse.Database))
return base.LogAndPipeError(t, logPrefix, fmt.Errorf("expected database ok, got %s", healthResponse.Database))
}
return nil
}, b)
Expand Down
40 changes: 28 additions & 12 deletions e2e/monitoring_stack_test.go → e2e/sedge/monitoring_stack_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
/*
Copyright 2022 Nethermind
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e

import (
"testing"

base "github.com/NethermindEth/sedge/e2e"
"github.com/stretchr/testify/assert"
)

Expand All @@ -13,13 +29,13 @@ func TestE2E_MonitoringStack_NotInitialized(t *testing.T) {
runErr error
)
// Build test case
e2eTest := newE2ETestCase(
e2eTest := newE2ESedgeTestCase(
t,
// Arrange
nil,
// Act
func(t *testing.T, binaryPath string, dataDirPath string) {
runErr = runCommand(t, binaryPath, "--help")
runErr = base.RunCommand(t, binaryPath, "--help")
},
// Assert
func(t *testing.T, dataDirPath string) {
Expand All @@ -40,13 +56,13 @@ func TestE2E_MonitoringStack_Init(t *testing.T) {
runErr error
)
// Build test case
e2eTest := newE2ETestCase(
e2eTest := newE2ESedgeTestCase(
t,
// Arrange
nil,
// Act
func(t *testing.T, binaryPath string, dataDirPath string) {
runErr = runCommand(t, binaryPath, "monitoring", "init")
runErr = base.RunCommand(t, binaryPath, "monitoring", "init")
},
// Assert
func(t *testing.T, dataDirPath string) {
Expand All @@ -70,11 +86,11 @@ func TestE2E_MonitoringStack_NotReinstalled(t *testing.T) {
runErr error
)
// Build test case
e2eTest := newE2ETestCase(
e2eTest := newE2ESedgeTestCase(
t,
// Arrange
func(t *testing.T, sedgePath string) error {
err := runCommand(t, sedgePath, "monitoring", "init")
err := base.RunCommand(t, sedgePath, "monitoring", "init")
if err != nil {
return err
}
Expand All @@ -91,7 +107,7 @@ func TestE2E_MonitoringStack_NotReinstalled(t *testing.T) {
},
// Act
func(t *testing.T, binaryPath string, dataDirPath string) {
runErr = runCommand(t, binaryPath, "monitoring", "init")
runErr = base.RunCommand(t, binaryPath, "monitoring", "init")
},
// Assert
func(t *testing.T, dataDirPath string) {
Expand Down Expand Up @@ -121,15 +137,15 @@ func TestE2E_MonitoringStack_Clean(t *testing.T) {
runErr error
)
// Build test case
e2eTest := newE2ETestCase(
e2eTest := newE2ESedgeTestCase(
t,
// Arrange
func(t *testing.T, sedgePath string) error {
return runCommand(t, sedgePath, "monitoring", "init")
return base.RunCommand(t, sedgePath, "monitoring", "init")
},
// Act
func(t *testing.T, binaryPath string, dataDirPath string) {
runErr = runCommand(t, binaryPath, "monitoring", "clean")
runErr = base.RunCommand(t, binaryPath, "monitoring", "clean")
},
// Assert
func(t *testing.T, dataDirPath string) {
Expand All @@ -152,13 +168,13 @@ func TestE2E_MonitoringStack_CleanNonExistent(t *testing.T) {
runErr error
)
// Build test case
e2eTest := newE2ETestCase(
e2eTest := newE2ESedgeTestCase(
t,
// Arrange
nil,
// Act
func(t *testing.T, binaryPath string, dataDirPath string) {
runErr = runCommand(t, binaryPath, "monitoring", "clean")
runErr = base.RunCommand(t, binaryPath, "monitoring", "clean")
},
// Assert
func(t *testing.T, dataDirPath string) {
Expand Down
98 changes: 98 additions & 0 deletions e2e/sedge/monitoring_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2022 Nethermind
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e

import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"

"github.com/NethermindEth/sedge/internal/pkg/services"
"github.com/NethermindEth/sedge/internal/utils"
"github.com/docker/docker/client"
)

func dataDirPath() (string, error) {
userDataHome := os.Getenv("XDG_DATA_HOME")
if userDataHome == "" {
userHome, err := os.UserHomeDir()
if err != nil {
return "", err
}
userDataHome = filepath.Join(userHome, ".local", "share")
}
return filepath.Join(userDataHome, ".sedge"), nil
}

type Target struct {
Labels Labels `json:"labels"`
Health string `json:"health"`
}

type Labels map[string]string

type Data struct {
ActiveTargets []Target `json:"activeTargets"`
}

type PrometheusTargetsResponse struct {
Status string `json:"status"`
Data Data `json:"data"`
}

func prometheusTargets(t *testing.T) (*PrometheusTargetsResponse, error) {
response, err := utils.GetRequest("http://localhost:9090/api/v1/targets", time.Second)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("prometheus targets status code should be 200")
}
var r PrometheusTargetsResponse
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &r)
if err != nil {
return nil, err
}
return &r, nil
}

func getContainerIDByName(containerName string) (string, error) {
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return "", err
}
defer cli.Close()

dockerServiceManager := services.NewDockerServiceManager(cli)

containerID, err := dockerServiceManager.ContainerID(containerName)
if err != nil {
return "", err
}

return containerID, nil
}
Loading

0 comments on commit b53a947

Please sign in to comment.