github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/gce/instance_information_test.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package gce
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/clock/testclock"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/core/constraints"
    14  	"github.com/juju/juju/provider/gce/google"
    15  )
    16  
    17  type instanceInformationSuite struct {
    18  	BaseSuite
    19  }
    20  
    21  var _ = gc.Suite(&instanceInformationSuite{})
    22  
    23  func (s *instanceInformationSuite) TestInstanceTypesCacheExpiration(c *gc.C) {
    24  	zone := google.NewZone("a-zone", google.StatusUp, "", "")
    25  	s.FakeConn.Zones = []google.AvailabilityZone{zone}
    26  
    27  	now := time.Now()
    28  	clk := testclock.NewClock(now)
    29  	allInstTypes, err := s.Env.getAllInstanceTypes(s.CallCtx, clk)
    30  	c.Assert(err, jc.ErrorIsNil)
    31  
    32  	// Cache miss
    33  	cacheExpAt := s.Env.instCacheExpireAt
    34  	c.Assert(cacheExpAt.After(now), jc.IsTrue, gc.Commentf("expected a cache expiration time to be set"))
    35  
    36  	// Cache hit
    37  	cachedInstTypes, err := s.Env.getAllInstanceTypes(s.CallCtx, clk)
    38  	c.Assert(err, jc.ErrorIsNil)
    39  	c.Assert(allInstTypes, gc.DeepEquals, cachedInstTypes, gc.Commentf("expected to get cached instance list"))
    40  	c.Assert(s.Env.instCacheExpireAt, gc.Equals, cacheExpAt, gc.Commentf("expected cache expiration timestamp not to be modified"))
    41  
    42  	// Forced cache-miss after expiry.
    43  	// NOTE(achilleasa): this will trigger a "advancing a clock with nothing waiting"
    44  	// warning but that's a false positive; we just want to advance the clock
    45  	// to test the cache expiry logic.
    46  	clk.Advance(11 * time.Minute)
    47  	_, err = s.Env.getAllInstanceTypes(s.CallCtx, clk)
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	c.Assert(s.Env.instCacheExpireAt.After(cacheExpAt), jc.IsTrue, gc.Commentf("expected cache expiration to be updated"))
    50  	c.Assert(s.Env.instCacheExpireAt.After(clk.Now()), jc.IsTrue, gc.Commentf("expected cache expiration to be in the future"))
    51  }
    52  
    53  func (s *instanceInformationSuite) TestEnsureDefaultConstraints(c *gc.C) {
    54  	// Fill default cores and mem.
    55  	cons := constraints.Value{}
    56  	c.Assert(cons.String(), gc.Equals, ``)
    57  	cons = ensureDefaultConstraints(cons)
    58  	c.Assert(cons.String(), gc.Equals, `cores=2`)
    59  
    60  	var err error
    61  	// Do not fill default cores and mem if instance type is provided.
    62  	cons, err = constraints.Parse(`instance-type=e2-medium`)
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	c.Assert(cons.String(), gc.Equals, `instance-type=e2-medium`)
    65  	cons = ensureDefaultConstraints(cons)
    66  	c.Assert(cons.String(), gc.Equals, `instance-type=e2-medium`)
    67  
    68  	// Do not fill default cores and mem if cores or/and mem are provided.
    69  	cons, err = constraints.Parse(`cores=1 mem=1024M`) // smaller than defaults
    70  	c.Assert(err, jc.ErrorIsNil)
    71  	cons = ensureDefaultConstraints(cons)
    72  	c.Assert(cons.String(), gc.Equals, `cores=1 mem=1024M`)
    73  
    74  	cons, err = constraints.Parse(`cores=4 mem=4096M`)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	cons = ensureDefaultConstraints(cons)
    77  	c.Assert(cons.String(), gc.Equals, `cores=4 mem=4096M`)
    78  
    79  	cons, err = constraints.Parse(`cores=4`)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	cons = ensureDefaultConstraints(cons)
    82  	c.Assert(cons.String(), gc.Equals, `cores=4`)
    83  
    84  	cons, err = constraints.Parse(`mem=4096M`)
    85  	c.Assert(err, jc.ErrorIsNil)
    86  	cons = ensureDefaultConstraints(cons)
    87  	c.Assert(cons.String(), gc.Equals, `cores=2 mem=4096M`)
    88  }