Skip to content

Commit e01d3d6

Browse files
author
nam
committed
instancetype test: additional unit tests
1 parent 4c05a31 commit e01d3d6

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

environs/instances/instancetype.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (bt ByName) Less(i, j int) bool {
230230
}
231231
// Name is equal, so use cost as a tie breaker.
232232
// Result is in ascending order of cost so instance with lowest cost is first.
233-
return inst0.Cost > inst1.Cost
233+
return inst0.Cost < inst1.Cost
234234

235235
}
236236

environs/instances/instancetype_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,74 @@ func (s *instanceTypeSuite) TestSortByCost(c *gc.C) {
409409
c.Check(names, gc.DeepEquals, t.expectedItypes)
410410
}
411411
}
412+
413+
var byNameTests = []struct {
414+
about string
415+
itypesToUse []InstanceType
416+
expectedItypes []string
417+
}{
418+
{
419+
about: "when different Name, pick lowest Name after delimiter",
420+
itypesToUse: []InstanceType{
421+
{Id: "2", Name: "a1.xLarge", CpuCores: 2, Mem: 4096, Cost: 240},
422+
{Id: "1", Name: "c2.12xLarge", CpuCores: 1, Mem: 2048, Cost: 241},
423+
},
424+
expectedItypes: []string{
425+
"a1.xLarge", "c2.12xLarge",
426+
},
427+
}, {
428+
about: "when differentsame Name before delimiter, pick lowest cost",
429+
itypesToUse: []InstanceType{
430+
{Id: "2", Name: "a1.xLarge", CpuCores: 2, Mem: 4096, Cost: 240},
431+
{Id: "3", Name: "a1.6xLarge", CpuCores: 2, Mem: 4096, Cost: 440},
432+
{Id: "1", Name: "a1.12xLarge", CpuCores: 1, Mem: 2048, Cost: 800},
433+
},
434+
expectedItypes: []string{
435+
"a1.xLarge", "a1.6xLarge", "a1.12xLarge",
436+
},
437+
},
438+
{
439+
about: "when name mixture, same and different, before delimiter, pick lowest cost",
440+
itypesToUse: []InstanceType{
441+
{Id: "2", Name: "a1.xLarge", CpuCores: 2, Mem: 4096, Cost: 240},
442+
{Id: "3", Name: "a1.6xLarge", CpuCores: 2, Mem: 4096, Cost: 440},
443+
{Id: "4", Name: "b1.4xLarge", CpuCores: 2, Mem: 4096, Cost: 500},
444+
{Id: "5", Name: "b1.2xLarge", CpuCores: 2, Mem: 4096, Cost: 400},
445+
{Id: "1", Name: "a1.12xLarge", CpuCores: 1, Mem: 2048, Cost: 800},
446+
},
447+
expectedItypes: []string{
448+
"a1.xLarge", "a1.6xLarge", "a1.12xLarge", "b1.2xLarge", "b1.4xLarge",
449+
},
450+
},
451+
{
452+
about: "when different no delimiter, base to lexical sort",
453+
itypesToUse: []InstanceType{
454+
{Id: "2", Name: "a1xLarge", CpuCores: 2, Mem: 4096, Cost: 300},
455+
{Id: "1", Name: "c212xLarge", CpuCores: 1, Mem: 2048, Cost: 241},
456+
},
457+
expectedItypes: []string{
458+
"a1xLarge", "c212xLarge",
459+
},
460+
}, {
461+
about: "when different not defined delimiter, base to lexical sort",
462+
itypesToUse: []InstanceType{
463+
{Id: "2", Name: "a1+12xlarge", CpuCores: 2, Mem: 4096, Cost: 890},
464+
{Id: "1", Name: "c2+12xLarge", CpuCores: 1, Mem: 2048, Cost: 800},
465+
},
466+
expectedItypes: []string{
467+
"a1+12xlarge", "c2+12xLarge",
468+
},
469+
},
470+
}
471+
472+
func (s *instanceTypeSuite) TestSortByName(c *gc.C) {
473+
for i, t := range byNameTests {
474+
c.Logf("test %d: %s", i, t.about)
475+
sort.Sort(ByName(t.itypesToUse))
476+
names := make([]string, len(t.itypesToUse))
477+
for i, itype := range t.itypesToUse {
478+
names[i] = itype.Name
479+
}
480+
c.Check(names, gc.DeepEquals, t.expectedItypes)
481+
}
482+
}

0 commit comments

Comments
 (0)