go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/api/config/v1/timeperiod_test.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/config/validation"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  	. "go.chromium.org/luci/common/testing/assertions"
    25  )
    26  
    27  func TestTimePeriod(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	Convey("normalize", t, func() {
    31  		Convey("invalid", func() {
    32  			tp := &TimePeriod{
    33  				Time: &TimePeriod_Duration{
    34  					Duration: "-1h",
    35  				},
    36  			}
    37  			So(tp.Normalize(), ShouldErrLike, "invalid duration")
    38  		})
    39  
    40  		Convey("valid", func() {
    41  			Convey("empty", func() {
    42  				tp := &TimePeriod{}
    43  				So(tp.Normalize(), ShouldBeNil)
    44  				So(tp.Time, ShouldBeNil)
    45  			})
    46  
    47  			Convey("normalizes", func() {
    48  				Convey("zero", func() {
    49  					tp := &TimePeriod{
    50  						Time: &TimePeriod_Duration{
    51  							Duration: "0h",
    52  						},
    53  					}
    54  					So(tp.Normalize(), ShouldBeNil)
    55  					So(tp.Time, ShouldHaveSameTypeAs, &TimePeriod_Seconds{})
    56  					So(tp.Time.(*TimePeriod_Seconds).Seconds, ShouldEqual, 0)
    57  				})
    58  
    59  				Convey("nonzero", func() {
    60  					tp := &TimePeriod{
    61  						Time: &TimePeriod_Duration{
    62  							Duration: "1h",
    63  						},
    64  					}
    65  					So(tp.Normalize(), ShouldBeNil)
    66  					So(tp.Time, ShouldHaveSameTypeAs, &TimePeriod_Seconds{})
    67  					So(tp.Time.(*TimePeriod_Seconds).Seconds, ShouldEqual, 3600)
    68  				})
    69  			})
    70  
    71  			Convey("normalized", func() {
    72  				tp := &TimePeriod{
    73  					Time: &TimePeriod_Seconds{
    74  						Seconds: 3600,
    75  					},
    76  				}
    77  				So(tp.Normalize(), ShouldBeNil)
    78  				So(tp.Time, ShouldHaveSameTypeAs, &TimePeriod_Seconds{})
    79  				So(tp.Time.(*TimePeriod_Seconds).Seconds, ShouldEqual, 3600)
    80  			})
    81  		})
    82  	})
    83  
    84  	Convey("validate", t, func() {
    85  		c := &validation.Context{Context: context.Background()}
    86  
    87  		Convey("invalid", func() {
    88  			Convey("duration", func() {
    89  				tp := &TimePeriod{
    90  					Time: &TimePeriod_Duration{
    91  						Duration: "1yr",
    92  					},
    93  				}
    94  				tp.Validate(c)
    95  				errs := c.Finalize().(*validation.Error).Errors
    96  				So(errs, ShouldErrLike, "duration must match regex")
    97  			})
    98  
    99  			Convey("seconds", func() {
   100  				tp := &TimePeriod{
   101  					Time: &TimePeriod_Seconds{
   102  						Seconds: -1,
   103  					},
   104  				}
   105  				tp.Validate(c)
   106  				errs := c.Finalize().(*validation.Error).Errors
   107  				So(errs, ShouldContainErr, "seconds must be non-negative")
   108  			})
   109  		})
   110  
   111  		Convey("valid", func() {
   112  			Convey("empty", func() {
   113  				tp := &TimePeriod{}
   114  				tp.Validate(c)
   115  				So(c.Finalize(), ShouldBeNil)
   116  			})
   117  
   118  			Convey("duration", func() {
   119  				tp := &TimePeriod{
   120  					Time: &TimePeriod_Duration{
   121  						Duration: "1h",
   122  					},
   123  				}
   124  				tp.Validate(c)
   125  				So(c.Finalize(), ShouldBeNil)
   126  			})
   127  
   128  			Convey("seconds", func() {
   129  				tp := &TimePeriod{
   130  					Time: &TimePeriod_Seconds{
   131  						Seconds: 3600,
   132  					},
   133  				}
   134  				tp.Validate(c)
   135  				So(c.Finalize(), ShouldBeNil)
   136  			})
   137  		})
   138  	})
   139  }