cert-manager/internal/apis/config/webhook/v1alpha1/defaults_test.go
Jason Costello b363fd9b3f Applying API default tests to rest of configuration modules
Signed-off-by: Jason Costello <jason@jsnc.tl>
2024-04-21 09:15:38 -04:00

55 lines
1.3 KiB
Go

package v1alpha1
import (
"encoding/json"
"github.com/cert-manager/cert-manager/pkg/apis/config/webhook/v1alpha1"
"os"
"reflect"
"testing"
)
const TestFileLocation = "testdata/defaults.json"
func TestWebhookConfigurationDefaults(t *testing.T) {
if os.Getenv("UPDATE_DEFAULTS") == "true" {
config := &v1alpha1.WebhookConfiguration{}
SetObjectDefaults_WebhookConfiguration(config)
defaultData, err := json.Marshal(config)
if err != nil {
panic(err)
}
if err := os.WriteFile(TestFileLocation, defaultData, 0644); err != nil {
t.Fatal(err)
}
t.Log("webhook config api defaults updated")
}
tests := []struct {
name string
config *v1alpha1.WebhookConfiguration
}{
{
"v1alpha1",
&v1alpha1.WebhookConfiguration{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetObjectDefaults_WebhookConfiguration(tt.config)
var expected *v1alpha1.WebhookConfiguration
expectedData, err := os.ReadFile(TestFileLocation)
err = json.Unmarshal(expectedData, &expected)
if err != nil {
t.Fatal("testfile not found")
}
if !reflect.DeepEqual(tt.config, expected) {
prettyExpected, _ := json.MarshalIndent(expected, "", "\t")
prettyGot, _ := json.MarshalIndent(tt.config, "", "\t")
t.Errorf("expected defaults\n %v \n but got \n %v", string(prettyExpected), string(prettyGot))
}
})
}
}