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

     1  // Copyright 2018 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  	"time"
    21  
    22  	"go.chromium.org/luci/config/validation"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func TestConfig(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	Convey("ComputeAmount", t, func() {
    32  		cfg := &Config{
    33  			Amount: &Amount{
    34  				Min: 1,
    35  				Max: 3,
    36  			},
    37  		}
    38  		amt, err := cfg.ComputeAmount(2, time.Time{})
    39  		So(err, ShouldBeNil)
    40  		So(amt, ShouldEqual, 2)
    41  	})
    42  
    43  	Convey("Validate", t, func() {
    44  		c := &validation.Context{Context: context.Background()}
    45  
    46  		Convey("invalid", func() {
    47  			Convey("empty", func() {
    48  				cfg := &Config{}
    49  				cfg.Validate(c)
    50  				errs := c.Finalize().(*validation.Error).Errors
    51  				So(errs, ShouldContainErr, "at least one disk is required")
    52  				So(errs, ShouldContainErr, "prefix is required")
    53  				So(errs, ShouldContainErr, "duration or seconds is required")
    54  			})
    55  
    56  			Convey("current amount", func() {
    57  				cfg := &Config{
    58  					CurrentAmount: 1,
    59  				}
    60  				cfg.Validate(c)
    61  				errs := c.Finalize().(*validation.Error).Errors
    62  				So(errs, ShouldContainErr, "current amount must not be specified")
    63  			})
    64  
    65  			Convey("revision", func() {
    66  				cfg := &Config{
    67  					Revision: "revision",
    68  				}
    69  				cfg.Validate(c)
    70  				errs := c.Finalize().(*validation.Error).Errors
    71  				So(errs, ShouldContainErr, "revision must not be specified")
    72  			})
    73  		})
    74  
    75  		Convey("valid", func() {
    76  			cfg := &Config{
    77  				Attributes: &VM{
    78  					Disk: []*Disk{
    79  						{
    80  							Image: "global/images/image",
    81  						},
    82  					},
    83  					MachineType: "type",
    84  					NetworkInterface: []*NetworkInterface{
    85  						{},
    86  					},
    87  					Project: "project",
    88  					Zone:    "zone",
    89  				},
    90  				Lifetime: &TimePeriod{
    91  					Time: &TimePeriod_Seconds{
    92  						Seconds: 3600,
    93  					},
    94  				},
    95  				Prefix: "prefix",
    96  			}
    97  			cfg.Validate(c)
    98  			So(c.Finalize(), ShouldBeNil)
    99  		})
   100  	})
   101  }