-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
robertlestak
committed
Jul 13, 2024
1 parent
025c8d6
commit 938175e
Showing
25 changed files
with
1,333 additions
and
33 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
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
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
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,59 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInitMetrics(t *testing.T) { | ||
// Reset the global registry to ensure a clean state | ||
prometheus.DefaultRegisterer = prometheus.NewRegistry() | ||
|
||
InitMetrics() | ||
|
||
// Check if SyncStatus is registered | ||
metricFamilies, err := prometheus.DefaultGatherer.Gather() | ||
assert.NoError(t, err) | ||
// ensure we have at least one metric family | ||
assert.Greater(t, len(metricFamilies), 0) | ||
// ensure the metric family is the one we expect | ||
for _, mf := range metricFamilies { | ||
if *mf.Name == "cert_manager_sync_status" { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func TestServe(t *testing.T) { | ||
// Mock environment variable | ||
os.Setenv("METRICS_PORT", "9091") | ||
|
||
// Mock log output to prevent actual logging during tests | ||
logrus.SetOutput(os.Stdout) | ||
logrus.SetLevel(logrus.PanicLevel) | ||
|
||
// Start the metrics server in a separate goroutine | ||
go Serve() | ||
|
||
// Wait a moment for the server to start | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
// Test /healthz endpoint | ||
resp, err := http.Get("http://localhost:9091/healthz") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
// Test /metrics endpoint | ||
resp, err = http.Get("http://localhost:9091/metrics") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
// Cleanup: reset the environment variable | ||
os.Unsetenv("METRICS_PORT") | ||
} |
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,148 @@ | ||
package certmanagersync | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/robertlestak/cert-manager-sync/pkg/state" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestNewStore(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
storeType StoreType | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Test ACMStoreType", | ||
storeType: ACMStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test CloudflareStoreType", | ||
storeType: CloudflareStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test DigitalOceanStoreType", | ||
storeType: DigitalOceanStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test FilepathStoreType", | ||
storeType: FilepathStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test GCPStoreType", | ||
storeType: GCPStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test HerokuStoreType", | ||
storeType: HerokuStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test IncapsulaStoreType", | ||
storeType: IncapsulaStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test ThreatxStoreType", | ||
storeType: ThreatxStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test VaultStoreType", | ||
storeType: VaultStoreType, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test invalid store type", | ||
storeType: "invalid", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := NewStore(tt.storeType) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NewStore() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if err != nil && tt.wantErr { | ||
assert.True(t, errors.Is(err, ErrInvalidStoreType)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestEnabledStores(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
annotations map[string]string | ||
want []StoreType | ||
}{ | ||
{ | ||
name: "Test with no enabled stores", | ||
annotations: map[string]string{ | ||
state.OperatorName + "/sync-enabled": "false", | ||
}, | ||
want: nil, | ||
}, | ||
{ | ||
name: "Test with ACM and Cloudflare enabled", | ||
annotations: map[string]string{ | ||
state.OperatorName + "/sync-enabled": "true", | ||
state.OperatorName + "/acm-enabled": "true", | ||
state.OperatorName + "/cloudflare-enabled": "true", | ||
}, | ||
want: []StoreType{ACMStoreType, CloudflareStoreType}, | ||
}, | ||
{ | ||
name: "Test with all stores enabled", | ||
annotations: map[string]string{ | ||
state.OperatorName + "/sync-enabled": "true", | ||
state.OperatorName + "/acm-enabled": "true", | ||
state.OperatorName + "/cloudflare-enabled": "true", | ||
state.OperatorName + "/digitalocean-enabled": "true", | ||
state.OperatorName + "/filepath-enabled": "true", | ||
state.OperatorName + "/gcp-enabled": "true", | ||
state.OperatorName + "/heroku-enabled": "true", | ||
state.OperatorName + "/incapsula-site-id": "1234", | ||
state.OperatorName + "/threatx-hostname": "example.com", | ||
state.OperatorName + "/vault-addr": "https://vault.example.com", | ||
}, | ||
want: []StoreType{ | ||
ACMStoreType, | ||
CloudflareStoreType, | ||
DigitalOceanStoreType, | ||
FilepathStoreType, | ||
GCPStoreType, | ||
HerokuStoreType, | ||
IncapsulaStoreType, | ||
ThreatxStoreType, | ||
VaultStoreType, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: tt.annotations, | ||
}, | ||
} | ||
if got := EnabledStores(s); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("EnabledStores() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.