Skip to content

Commit

Permalink
chore: increase test coverage (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
shini4i authored Nov 22, 2023
1 parent b477b8c commit 3c49ddb
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static/
**/.env

# built binary file
argo-watcher
/argo-watcher

# swagger files
**/swagger.*
Expand Down
45 changes: 28 additions & 17 deletions cmd/argo-watcher/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,34 @@ import (
)

func TestNewServerConfig(t *testing.T) {
// Set up the required environment variables
t.Setenv("ARGO_URL", "https://example.com")
t.Setenv("ARGO_TOKEN", "secret-token")
t.Setenv("STATE_TYPE", "postgres")

// Call the NewServerConfig function
cfg, err := NewServerConfig()

// Assert that the configuration was parsed successfully
assert.NoError(t, err)
assert.NotNil(t, cfg)

// Assert specific field values
expectedUrl, _ := url.Parse("https://example.com")
assert.Equal(t, *expectedUrl, cfg.ArgoUrl)
assert.Equal(t, "secret-token", cfg.ArgoToken)
assert.Equal(t, "postgres", cfg.StateType)
t.Run("Success", func(t *testing.T) {
// Set up the required environment variables
t.Setenv("ARGO_URL", "https://example.com")
t.Setenv("ARGO_TOKEN", "secret-token")
t.Setenv("STATE_TYPE", "postgres")

// Call the NewServerConfig function
cfg, err := NewServerConfig()

// Assert that the configuration was parsed successfully
assert.NoError(t, err)
assert.NotNil(t, cfg)

// Assert specific field values
expectedUrl, _ := url.Parse("https://example.com")
assert.Equal(t, *expectedUrl, cfg.ArgoUrl)
assert.Equal(t, "secret-token", cfg.ArgoToken)
assert.Equal(t, "postgres", cfg.StateType)
})

t.Run("Invalid state type", func(t *testing.T) {
t.Setenv("ARGO_URL", "https://example.com")
t.Setenv("ARGO_TOKEN", "secret-token")
t.Setenv("STATE_TYPE", "invalid")

_, err := NewServerConfig()
assert.Error(t, err)
})
}

func TestNewServerConfig_RequiredFieldsMissing(t *testing.T) {
Expand Down
112 changes: 112 additions & 0 deletions cmd/argo-watcher/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"testing"

"github.com/rs/zerolog/log"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
)

func TestMetricsValuesChange(t *testing.T) {
metrics := &Metrics{}
metrics.Init()

app := "testApp"

t.Run("AddFailedDeployment", func(t *testing.T) {
// Call the method to test
metrics.AddFailedDeployment(app)

// Get the current value of the metric for the app
metric := testutil.ToFloat64(metrics.failedDeployment.With(prometheus.Labels{"app": app}))

// Check if the metric was incremented
assert.Equal(t, 1.0, metric)
})

t.Run("ResetFailedDeployment", func(t *testing.T) {
// Call the method to test
metrics.ResetFailedDeployment(app)

// Get the current value of the metric for the app
metric := testutil.ToFloat64(metrics.failedDeployment.With(prometheus.Labels{"app": app}))

// Check if the metric was reset
assert.Equal(t, 0.0, metric)
})

t.Run("IncrementProcessedDeployments", func(t *testing.T) {
for i := 0; i < 10; i++ {
metrics.AddProcessedDeployment()
}

// Get the current value of the metric for the app
metric := testutil.ToFloat64(metrics.processedDeployments)

// Check if the metric was incremented
assert.Equal(t, 10.0, metric)
})
}

func TestSetArgoUnavailable(t *testing.T) {
metrics := &Metrics{}
metrics.Init()

t.Run("SetArgoUnavailable to true", func(t *testing.T) {
// Call the method to test
metrics.SetArgoUnavailable(true)

// Get the current value of the metric
metric := testutil.ToFloat64(metrics.argocdUnavailable)

// Check if the metric was set to 1
assert.Equal(t, 1.0, metric)
})

t.Run("SetArgoUnavailable to false", func(t *testing.T) {
// Call the method to test
metrics.SetArgoUnavailable(false)

// Get the current value of the metric
metric := testutil.ToFloat64(metrics.argocdUnavailable)

// Check if the metric was set to 0
assert.Equal(t, 0.0, metric)
})
}

func TestRegister(t *testing.T) {
metrics := &Metrics{}
metrics.Init()

// Call the method to test
metrics.Register()

// adding a failed deployment to check if the metric was registered
metrics.AddFailedDeployment("testApp")

// Check if the metrics were registered
assert.True(t, testMetricRegistered("failed_deployment"))
assert.True(t, testMetricRegistered("processed_deployments"))
assert.True(t, testMetricRegistered("argocd_unavailable"))
}

// Helper function to check if a metric is registered.
func testMetricRegistered(metricName string) bool {
metricFamilies, err := prometheus.DefaultGatherer.Gather()
if err != nil {
log.Error().Msgf("Error gathering metrics: %v", err)
return false
}

for _, m := range metricFamilies {
if m.GetName() == metricName {
return true
}
}

return false
}
30 changes: 30 additions & 0 deletions cmd/argo-watcher/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

func TestGetVersion(t *testing.T) {
gin.SetMode(gin.TestMode)

router := gin.Default()
env := &Env{}
router.GET("/api/v1/version", env.getVersion)

req, err := http.NewRequest(http.MethodGet, "/api/v1/version", nil)
if err != nil {
t.Fatalf("Couldn't create request: %v\n", err)
}

w := httptest.NewRecorder()
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, fmt.Sprintf("\"%s\"", version), w.Body.String())
}

0 comments on commit 3c49ddb

Please sign in to comment.