Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vareversat committed Jan 23, 2024
1 parent 7f8ebea commit 8c34886
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
89 changes: 89 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package collector

import (
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
)

func TestNewDigicertCollectorWithoutMockedData(t *testing.T) {

logger := log.NewNopLogger()
digicertURL := "https://digicert.com"
digicertAPIKey := "API_KEY"

// Create a new DigicertCollector
collector, err := NewDigicertCollector(logger, digicertURL, digicertAPIKey, true)

assert.NoError(t, err)
assert.False(t, collector.useMockedData)
}

func TestNewDigicertCollectorWithMockedData(t *testing.T) {

logger := log.NewNopLogger()
digicertURL := ""
digicertAPIKey := ""

// Create a new DigicertCollector
collector, err := NewDigicertCollector(logger, digicertURL, digicertAPIKey, true)

assert.NoError(t, err)
assert.True(t, collector.useMockedData)
}

func TestUpdateMetricsApiDown(t *testing.T) {
logger := log.NewNopLogger()
digicertURL := "https://digicert.com"
digicertAPIKey := "test"

// Create a new DigicertCollector
collector, err := NewDigicertCollector(logger, digicertURL, digicertAPIKey, true)
assert.NoError(t, err)

promChan := make(chan prometheus.Metric)

go collector.UpdateMetrics(promChan)

expected := strings.NewReader(
`# HELP digicert_api_up Was the last Digicert API query successful.
# TYPE digicert_api_up gauge
digicert_api_up 0
`)

err = testutil.CollectAndCompare(
collector,
expected,
"digicert_api_up",
)
}

func TestUpdateMetricsUse(t *testing.T) {
logger := log.NewNopLogger()
digicertURL := ""
digicertAPIKey := ""

// Create a new DigicertCollector
collector, err := NewDigicertCollector(logger, digicertURL, digicertAPIKey, true)
assert.NoError(t, err)

promChan := make(chan prometheus.Metric)

go collector.UpdateMetrics(promChan)

expected := strings.NewReader(
`# HELP digicert_api_up Was the last Digicert API query successful.
# TYPE digicert_api_up gauge
digicert_api_up 0
`)

err = testutil.CollectAndCompare(
collector,
expected,
"digicert_api_up",
)
}
2 changes: 1 addition & 1 deletion collector/digicert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c *DigicertCollector) FetchDigicertData() (*OrderList, error) {

if c.useMockedData {
// Open data file
jsonFile, err := os.Open("mock.json")
jsonFile, err := os.Open("../mock.json")
if err != nil {
level.Error(c.logger).Log("err", fmt.Sprintf("cannot open json file : %s", err))
return nil, err
Expand Down
50 changes: 50 additions & 0 deletions mock.json → collector/mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,56 @@
"number_of_sans": 2,
"is_multi_year_plan": "1"
},
{
"id": 10000000,
"certificate": {
"id": 177479,
"serial_number": "4HLQLH7IHKBKUOCXZNZNT9MGGKLQP",
"common_name": "*.amazon.com",
"dns_names": [
"*.amazon.com",
"amazon.com"
],
"valid_from": "2025-11-02",
"valid_till": "2025-11-02",
"days_remaining": 391,
"cert_validity": {
"years": 1
},
"signature_hash": "sha256"
},
"status": "expired",
"is_renewed": true,
"date_created": "2021-11-02T10:52:41+00:00",
"organization": {
"id": 11111111,
"name": "My Org."
},
"validity_years": 1,
"order_validity": {
"years": 1
},
"order_valid_till": "2024-11-02",
"disable_renewal_notifications": false,
"container": {
"id": 454737,
"name": "MY_CONTAINER",
"is_active": false
},
"product": {
"name_id": "ssl_basic",
"name": "Basic OV",
"type": "ssl_certificate"
},
"duplicates_count": 0,
"has_duplicates": false,
"reissues_count": 0,
"has_reissues": false,
"price": 788,
"product_name_id": "ssl_basic",
"number_of_sans": 2,
"is_multi_year_plan": "1"
},
{
"id": 176250000,
"certificate": {
Expand Down

0 comments on commit 8c34886

Please sign in to comment.