Change validation tests such that they accept valid values from a

provided TZ database and fail on any other values
This commit is contained in:
Maciej Szulik 2022-07-26 12:38:22 +02:00
parent 7fb3714fa1
commit 130845c937
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4

View File

@ -17,6 +17,10 @@ limitations under the License.
package validation
import (
"archive/zip"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
@ -36,11 +40,10 @@ var (
timeZoneEmpty = ""
timeZoneLocal = "LOCAL"
timeZoneUTC = "UTC"
timeZoneCorrectCasing = "America/New_York"
timeZoneBadCasing = "AMERICA/new_york"
timeZoneBadPrefix = " America/New_York"
timeZoneBadSuffix = "America/New_York "
timeZoneBadName = "America/New York"
timeZoneCorrect = "Continent/Zone"
timeZoneBadPrefix = " Continent/Zone"
timeZoneBadSuffix = "Continent/Zone "
timeZoneBadName = "Continent/InvalidZone"
timeZoneEmptySpace = " "
)
@ -882,6 +885,12 @@ func TestValidateCronJob(t *testing.T) {
validPodTemplateSpec := getValidPodTemplateSpecForGenerated(getValidGeneratedSelector())
validPodTemplateSpec.Labels = map[string]string{}
zoneDir := t.TempDir()
if err := setupFakeTimeZoneDatabase(zoneDir); err != nil {
t.Fatalf("Unexpected error setting up fake timezone database: %v", err)
}
t.Setenv("ZONEINFO", zoneDir)
successCases := map[string]batch.CronJob{
"basic scheduled job": {
ObjectMeta: metav1.ObjectMeta{
@ -915,7 +924,7 @@ func TestValidateCronJob(t *testing.T) {
},
},
},
"correct timeZone value casing": {
"correct timeZone value": {
ObjectMeta: metav1.ObjectMeta{
Name: "mycronjob",
Namespace: metav1.NamespaceDefault,
@ -923,7 +932,7 @@ func TestValidateCronJob(t *testing.T) {
},
Spec: batch.CronJobSpec{
Schedule: "0 * * * *",
TimeZone: &timeZoneCorrectCasing,
TimeZone: &timeZoneCorrect,
ConcurrencyPolicy: batch.AllowConcurrent,
JobTemplate: batch.JobTemplateSpec{
Spec: batch.JobSpec{
@ -934,6 +943,7 @@ func TestValidateCronJob(t *testing.T) {
},
}
for k, v := range successCases {
t.Run(k, func(t *testing.T) {
if errs := ValidateCronJobCreate(&v, corevalidation.PodValidationOptions{}); len(errs) != 0 {
t.Errorf("expected success for %s: %v", k, errs)
}
@ -945,6 +955,7 @@ func TestValidateCronJob(t *testing.T) {
if errs := ValidateCronJobUpdate(&v, &v, corevalidation.PodValidationOptions{}); len(errs) != 0 {
t.Errorf("expected success for %s: %v", k, errs)
}
})
}
negative := int32(-1)
@ -1034,7 +1045,7 @@ func TestValidateCronJob(t *testing.T) {
},
},
},
"spec.timeZone: Invalid value: \" America/New_York\": unknown time zone America/New_York": {
"spec.timeZone: Invalid value: \" Continent/Zone\": unknown time zone Continent/Zone": {
ObjectMeta: metav1.ObjectMeta{
Name: "mycronjob",
Namespace: metav1.NamespaceDefault,
@ -1051,7 +1062,7 @@ func TestValidateCronJob(t *testing.T) {
},
},
},
"spec.timeZone: Invalid value: \"America/New_York \": unknown time zone America/New_York ": {
"spec.timeZone: Invalid value: \"Continent/Zone \": unknown time zone Continent/Zone ": {
ObjectMeta: metav1.ObjectMeta{
Name: "mycronjob",
Namespace: metav1.NamespaceDefault,
@ -1068,7 +1079,7 @@ func TestValidateCronJob(t *testing.T) {
},
},
},
"spec.timeZone: Invalid value: \"America/New York\": unknown time zone America/New York": {
"spec.timeZone: Invalid value: \"Continent/InvalidZone\": unknown time zone Continent/InvalidZone": {
ObjectMeta: metav1.ObjectMeta{
Name: "mycronjob",
Namespace: metav1.NamespaceDefault,
@ -1314,8 +1325,7 @@ func TestValidateCronJob(t *testing.T) {
},
},
},
}
errorCases["spec.jobTemplate.spec.ttlSecondsAfterFinished:must be greater than or equal to 0"] = batch.CronJob{
"spec.jobTemplate.spec.ttlSecondsAfterFinished:must be greater than or equal to 0": {
ObjectMeta: metav1.ObjectMeta{
Name: "mycronjob",
Namespace: metav1.NamespaceDefault,
@ -1331,29 +1341,11 @@ func TestValidateCronJob(t *testing.T) {
},
},
},
}
if runtime.GOOS != "darwin" {
// Skip this error case on darwin, see https://github.com/golang/go/issues/21512
errorCases["spec.timeZone: Invalid value: \"AMERICA/new_york\": unknown time zone AMERICA/new_york"] = batch.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: "mycronjob",
Namespace: metav1.NamespaceDefault,
UID: types.UID("1a2b3c"),
},
Spec: batch.CronJobSpec{
Schedule: "0 * * * *",
TimeZone: &timeZoneBadCasing,
ConcurrencyPolicy: batch.AllowConcurrent,
JobTemplate: batch.JobTemplateSpec{
Spec: batch.JobSpec{
Template: validPodTemplateSpec,
},
},
},
}
}
for k, v := range errorCases {
t.Run(k, func(t *testing.T) {
errs := ValidateCronJobCreate(&v, corevalidation.PodValidationOptions{})
if len(errs) == 0 {
t.Errorf("expected failure for %s", k)
@ -1377,7 +1369,7 @@ func TestValidateCronJob(t *testing.T) {
errs = ValidateCronJobUpdate(&newSpec, &oldSpec, corevalidation.PodValidationOptions{})
if len(errs) == 0 {
if k == "metadata.name: must be no more than 52 characters" {
continue
return
}
t.Errorf("expected failure for %s", k)
} else {
@ -1387,9 +1379,46 @@ func TestValidateCronJob(t *testing.T) {
t.Errorf("unexpected error: %v, expected: %s", err, k)
}
}
})
}
}
// Sets up fake timezone database in a zoneDir directory with a single valid
// time zone called "Continent/Zone" by copying UTC metadata from golang's
// built-in databse. Returns an error in case of problems.
func setupFakeTimeZoneDatabase(zoneDir string) error {
reader, err := zip.OpenReader(runtime.GOROOT() + "/lib/time/zoneinfo.zip")
if err != nil {
return err
}
defer reader.Close()
if err := os.Mkdir(filepath.Join(zoneDir, "Continent"), os.ModePerm); err != nil {
return err
}
zoneFile, err := os.OpenFile(filepath.Join(zoneDir, "Continent", "Zone"), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
}
defer zoneFile.Close()
for _, file := range reader.File {
if file.Name != "UTC" {
continue
}
rc, err := file.Open()
if err != nil {
return err
}
if _, err := io.Copy(zoneFile, rc); err != nil {
return err
}
rc.Close()
break
}
return nil
}
func TestValidateCronJobSpec(t *testing.T) {
validPodTemplateSpec := getValidPodTemplateSpecForGenerated(getValidGeneratedSelector())
validPodTemplateSpec.Labels = map[string]string{}