Skip to content

Commit

Permalink
added unit test with regional MIG
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwinhr716 committed Mar 13, 2024
1 parent f8b4727 commit 771e932
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
1 change: 1 addition & 0 deletions cluster-autoscaler/cloudprovider/gce/gce_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func GenerateMigUrl(domainUrl string, ref GceRef) string {
return fmt.Sprintf(migUrlTemplate, ref.Project, ref.Zone, ref.Name)
}

// IsInstanceTemplateRegional determines whether or not an instance template is regional based on the url
func IsInstanceTemplateRegional(templateUrl string) (bool, error) {
return regexp.MatchString("(/projects/.*[A-Za-z0-9]+.*/regions/)", templateUrl)
}
Expand Down
19 changes: 0 additions & 19 deletions cluster-autoscaler/cloudprovider/gce/kube_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,3 @@ func (ke KubeEnv) Var(name string) (string, bool) {
val, found := ke.env[name]
return val, found
}

// ParseKubeEnv parses kube-env from its string representation
func ParseKubeEnv(templateName, kubeEnvValue string) (KubeEnv, error) {
env := make(map[string]string)
err := yaml.Unmarshal([]byte(kubeEnvValue), &env)
if err != nil {
return KubeEnv{}, fmt.Errorf("error unmarshalling kubeEnv: %v", err)
}
return KubeEnv{templateName: templateName, env: env}, nil
}

// Var extracts variable from KubeEnv
func (ke KubeEnv) Var(name string) (string, bool) {
if ke.env == nil {
return "", false
}
val, found := ke.env[name]
return val, found
}
30 changes: 16 additions & 14 deletions cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type mockAutoscalingGceClient struct {
fetchMigTargetSize func(GceRef) (int64, error)
fetchMigBasename func(GceRef) (string, error)
fetchMigInstances func(GceRef) ([]GceInstance, error)
fetchMigTemplateName func(GceRef) (InstanceTemplateName, error)
fetchMigTemplate func(GceRef, string, bool) (*gce.InstanceTemplate, error)
fetchMigTemplate func(GceRef, string) (*gce.InstanceTemplate, error)
fetchMachineType func(string, string) (*gce.MachineType, error)
fetchListManagedInstancesResults func(GceRef) (string, error)
}
Expand Down Expand Up @@ -852,7 +852,13 @@ func TestGetMigInstanceTemplateName(t *testing.T) {
instanceGroupManager := &gce.InstanceGroupManager{
Zone: mig.GceRef().Zone,
Name: mig.GceRef().Name,
InstanceTemplate: templateName,
InstanceTemplate: "https://www.googleapis.com/compute/v1/projects/test-project/global/instanceTemplates/template-name",
}

instanceGroupManagerRegional := &gce.InstanceGroupManager{
Zone: mig.GceRef().Zone,
Name: mig.GceRef().Name,
InstanceTemplate: "https://www.googleapis.com/compute/v1/projects/test-project/regions/us-central1/instanceTemplates/template-name",
}

testCases := []struct {
Expand All @@ -861,6 +867,7 @@ func TestGetMigInstanceTemplateName(t *testing.T) {
fetchMigs func(string) ([]*gce.InstanceGroupManager, error)
fetchMigTemplateName func(GceRef) (InstanceTemplateName, error)
expectedTemplateName string
expectedRegion bool
expectedErr error
}{
{
Expand All @@ -877,6 +884,12 @@ func TestGetMigInstanceTemplateName(t *testing.T) {
fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{instanceGroupManager}),
expectedTemplateName: templateName,
},
{
name: "target size from cache fill, regional",
cache: emptyCache(),
fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{instanceGroupManagerRegional}),
expectedTemplateName: templateName,
},
{
name: "cache fill without mig, fallback success",
cache: emptyCache(),
Expand Down Expand Up @@ -1371,26 +1384,15 @@ func (f *fakeTime) Now() time.Time {

func emptyCache() *GceCache {
return &GceCache{
<<<<<<< HEAD
migs: map[GceRef]Mig{mig.GceRef(): mig},
instances: make(map[GceRef][]GceInstance),
instancesUpdateTime: make(map[GceRef]time.Time),
migTargetSizeCache: make(map[GceRef]int64),
migBaseNameCache: make(map[GceRef]string),
listManagedInstancesResultsCache: make(map[GceRef]string),
instanceTemplateNameCache: make(map[GceRef]InstanceTemplateNameType),
instanceTemplateNameCache: make(map[GceRef]InstanceTemplateName),
instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate),
instancesFromUnknownMig: make(map[GceRef]bool),
=======
migs: map[GceRef]Mig{mig.GceRef(): mig},
instances: make(map[GceRef][]GceInstance),
instancesUpdateTime: make(map[GceRef]time.Time),
migTargetSizeCache: make(map[GceRef]int64),
migBaseNameCache: make(map[GceRef]string),
instanceTemplateNameCache: make(map[GceRef]InstanceTemplateName),
instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate),
instancesFromUnknownMig: make(map[GceRef]bool),
>>>>>>> 2aeec4097 (changed InstanceTemplateNameType to InstanceTemplateName)
}
}

Expand Down

0 comments on commit 771e932

Please sign in to comment.