Skip to content

Commit

Permalink
Correct the unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mvshao committed Aug 12, 2024
1 parent 7aedf80 commit db45325
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
40 changes: 36 additions & 4 deletions internal/gardener/shoot/extender/audit_log_test.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,75 @@
package extender

import (
"encoding/json"
gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v12 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"path/filepath"
"testing"
)

func TestAuditLogExtender(t *testing.T) {
t.Run("Should configure Audit Logs", func(t *testing.T) {
// given
auditLogPolicyName := "test-policy"
auditLogTenantConfigPath := "test-tenant"
seedName := "seedName"
auditLogConfig := &gardener.AuditConfig{
AuditPolicy: &gardener.AuditPolicy{
ConfigMapRef: &v12.ObjectReference{Name: auditLogPolicyName},
},
}
runtimeShoot := imv1.Runtime{
runtimeCR := imv1.Runtime{
Spec: imv1.RuntimeSpec{
Shoot: imv1.RuntimeShoot{
Name: "myshoot",
Provider: imv1.Provider{
Type: "azure",
},
},
},
}

shoot := gardener.Shoot{
ObjectMeta: v1.ObjectMeta{
Name: "test",
Namespace: "dev",
},
Spec: gardener.ShootSpec{
SeedName: &seedName,
Region: "westus2",
},
}

auditLogTenantConfigPath := filepath.Join("testdata", "config.json")
extender := NewAuditLogExtender(auditLogPolicyName, auditLogTenantConfigPath)
shoot := fixAuditLogGardenerShoot("test", "dev", "seedName")

// when
err := extender(runtimeShoot, &shoot)
err := extender(runtimeCR, &shoot)

expected := `
{
"providerConfig": {
"apiVersion": "service.auditlog.extensions.gardener.cloud/v1alpha1",
"kind": "AuditlogConfig",
"secretReferenceName": "auditlog-credentials",
"serviceURL": "https://auditlog.example.com:3000",
"tenantID": "a9be5aad-f855-4fd1-a8c8-e95683ec786b",
"type": "standard"
},
"type": "shoot-auditlog-service"
}`

// then
require.NoError(t, err)
require.NotNil(t, shoot.Spec.Kubernetes.KubeAPIServer)
require.NotNil(t, shoot.Spec.Kubernetes.KubeAPIServer.AuditConfig)
assert.Equal(t, auditLogConfig, shoot.Spec.Kubernetes.KubeAPIServer.AuditConfig)
actual, _ := json.Marshal(shoot.Spec.Extensions[0])
assert.JSONEq(t, expected, string(actual))
})
t.Run("Should not configure Audit Logs when shoot seed name is empty", func(t *testing.T) {
// given
Expand Down
14 changes: 14 additions & 0 deletions internal/gardener/shoot/extender/testdata/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"azure": {
"eu-central-1": {
"tenantID": "79c64792-9c1e-4c1b-9941-ef7560dd3eae",
"serviceURL": "https://auditlog.example.com:3001",
"secretName": "auditlog-secret2"
},
"westus2": {
"tenantID": "a9be5aad-f855-4fd1-a8c8-e95683ec786b",
"serviceURL": "https://auditlog.example.com:3000",
"secretName": "auditlog-secret"
}
}
}

0 comments on commit db45325

Please sign in to comment.