Пример #1
0
func (s *AvailabilityZoneSuite) TestDistributeInstancesGroupErrors(c *gc.C) {
	resultErr := fmt.Errorf("whatever")
	s.PatchValue(common.InternalAvailabilityZoneAllocations, func(_ common.ZonedEnviron, group []instance.Id) ([]common.AvailabilityZoneInstances, error) {
		return nil, resultErr
	})
	_, err := common.DistributeInstances(&s.env, nil, nil)
	c.Assert(err, gc.Equals, resultErr)
}
Пример #2
0
func (s *AvailabilityZoneSuite) TestDistributeInstances(c *gc.C) {
	var bestAvailabilityZones map[string][]instance.Id
	s.PatchValue(common.InternalBestAvailabilityZoneAllocations, func(_ common.ZonedEnviron, group []instance.Id) (map[string][]instance.Id, error) {
		return bestAvailabilityZones, nil
	})

	type distributeInstancesTest struct {
		bestAvailabilityZones map[string][]instance.Id
		candidates            []instance.Id
		eligible              []instance.Id
	}

	tests := []distributeInstancesTest{{
		bestAvailabilityZones: map[string][]instance.Id{
			"az0": []instance.Id{"i0"},
			"az1": []instance.Id{"i1"},
			"az2": []instance.Id{"i2"},
		},
		candidates: []instance.Id{"i2", "i3", "i4"},
		eligible:   []instance.Id{"i2"},
	}, {
		bestAvailabilityZones: map[string][]instance.Id{
			"az0": []instance.Id{"i0"},
			"az1": []instance.Id{"i1"},
			"az2": []instance.Id{"i2"},
		},
		candidates: []instance.Id{"i0", "i1", "i2"},
		eligible:   []instance.Id{"i0", "i1", "i2"},
	}, {
		bestAvailabilityZones: map[string][]instance.Id{
			"az0": []instance.Id{"i0"},
			"az1": []instance.Id{"i1"},
			"az2": []instance.Id{"i2"},
		},
		candidates: []instance.Id{"i3", "i4", "i5"},
		eligible:   []instance.Id{},
	}, {
		bestAvailabilityZones: map[string][]instance.Id{
			"az0": []instance.Id{"i0"},
			"az1": []instance.Id{"i1"},
			"az2": []instance.Id{"i2"},
		},
		candidates: []instance.Id{},
		eligible:   []instance.Id{},
	}, {
		bestAvailabilityZones: map[string][]instance.Id{},
		candidates:            []instance.Id{"i0"},
		eligible:              []instance.Id{},
	}}

	for i, test := range tests {
		c.Logf("test %d", i)
		bestAvailabilityZones = test.bestAvailabilityZones
		eligible, err := common.DistributeInstances(&s.env, test.candidates, nil)
		c.Assert(err, gc.IsNil)
		c.Assert(eligible, jc.SameContents, test.eligible)
	}
}
Пример #3
0
func (s *AvailabilityZoneSuite) TestDistributeInstancesGroup(c *gc.C) {
	expectedGroup := []instance.Id{"0", "1", "2"}
	var called bool
	s.PatchValue(common.InternalAvailabilityZoneAllocations, func(_ common.ZonedEnviron, group []instance.Id) ([]common.AvailabilityZoneInstances, error) {
		c.Assert(group, gc.DeepEquals, expectedGroup)
		called = true
		return nil, nil
	})
	common.DistributeInstances(&s.env, nil, expectedGroup)
	c.Assert(called, jc.IsTrue)
}
Пример #4
0
// DistributeInstances implements the state.InstanceDistributor policy.
func (e *Environ) DistributeInstances(candidates, distributionGroup []instance.Id) ([]instance.Id, error) {
	return common.DistributeInstances(e, candidates, distributionGroup)
}
Пример #5
0
func (s *AvailabilityZoneSuite) TestDistributeInstances(c *gc.C) {
	var zoneInstances []common.AvailabilityZoneInstances
	s.PatchValue(common.InternalAvailabilityZoneAllocations, func(_ common.ZonedEnviron, group []instance.Id) ([]common.AvailabilityZoneInstances, error) {
		return zoneInstances, nil
	})

	type distributeInstancesTest struct {
		zoneInstances []common.AvailabilityZoneInstances
		candidates    []instance.Id
		eligible      []instance.Id
	}

	tests := []distributeInstancesTest{{
		zoneInstances: []common.AvailabilityZoneInstances{{
			ZoneName:  "az0",
			Instances: []instance.Id{"i0"},
		}, {
			ZoneName:  "az1",
			Instances: []instance.Id{"i1"},
		}, {
			ZoneName:  "az2",
			Instances: []instance.Id{"i2"},
		}},
		candidates: []instance.Id{"i2", "i3", "i4"},
		eligible:   []instance.Id{"i2"},
	}, {
		zoneInstances: []common.AvailabilityZoneInstances{{
			ZoneName:  "az0",
			Instances: []instance.Id{"i0"},
		}, {
			ZoneName:  "az1",
			Instances: []instance.Id{"i1"},
		}, {
			ZoneName:  "az2",
			Instances: []instance.Id{"i2"},
		}},
		candidates: []instance.Id{"i0", "i1", "i2"},
		eligible:   []instance.Id{"i0", "i1", "i2"},
	}, {
		zoneInstances: []common.AvailabilityZoneInstances{{
			ZoneName:  "az0",
			Instances: []instance.Id{"i0"},
		}, {
			ZoneName:  "az1",
			Instances: []instance.Id{"i1"},
		}, {
			ZoneName:  "az2",
			Instances: []instance.Id{"i2"},
		}},
		candidates: []instance.Id{"i3", "i4", "i5"},
		eligible:   []instance.Id{},
	}, {
		zoneInstances: []common.AvailabilityZoneInstances{{
			ZoneName:  "az0",
			Instances: []instance.Id{"i0"},
		}, {
			ZoneName:  "az1",
			Instances: []instance.Id{"i1"},
		}, {
			ZoneName:  "az2",
			Instances: []instance.Id{"i2"},
		}},
		candidates: []instance.Id{},
		eligible:   []instance.Id{},
	}, {
		zoneInstances: []common.AvailabilityZoneInstances{},
		candidates:    []instance.Id{"i0"},
		eligible:      []instance.Id{},
	}}

	for i, test := range tests {
		c.Logf("test %d", i)
		zoneInstances = test.zoneInstances
		eligible, err := common.DistributeInstances(&s.env, test.candidates, nil)
		c.Assert(err, jc.ErrorIsNil)
		c.Assert(eligible, jc.SameContents, test.eligible)
	}
}