1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
package autoscaling
import (
"github.com/docker/goamz/autoscaling/astest"
"github.com/docker/goamz/aws"
"testing"
)
var testServer = astest.NewHTTPServer()
var mockTest bool
func TestBasicGroupRequest(t *testing.T) {
var as *AutoScaling
awsAuth, err := aws.EnvAuth()
if err != nil {
mockTest = true
t.Log("Running mock tests as AWS environment variables are not set")
awsAuth := aws.Auth{AccessKey: "abc", SecretKey: "123"}
as = New(awsAuth, aws.Region{AutoScalingEndpoint: testServer.URL})
testServer.Start()
go testServer.WaitRequest()
testServer.Response(200, nil, astest.BasicGroupResponse)
} else {
as = New(awsAuth, aws.USWest2)
}
groupResp, err := as.DescribeAutoScalingGroups(nil)
if err != nil {
t.Fatal(err)
}
if len(groupResp.AutoScalingGroups) > 0 {
firstGroup := groupResp.AutoScalingGroups[0]
if len(firstGroup.AutoScalingGroupName) > 0 {
t.Logf("Found AutoScaling group %s\n",
firstGroup.AutoScalingGroupName)
}
}
testServer.Flush()
}
func TestAutoScalingGroup(t *testing.T) {
var as *AutoScaling
// Launch configuration test config
var lc LaunchConfiguration
lc.LaunchConfigurationName = "LConf1"
lc.ImageId = "ami-03e47533" // Octave debian ami
lc.KernelId = "aki-98e26fa8"
lc.KeyName = "testAWS" // Replace with valid key for your account
lc.InstanceType = "m1.small"
// AutoScalingGroup test config
var asg AutoScalingGroup
asg.AutoScalingGroupName = "ASGTest1"
asg.LaunchConfigurationName = lc.LaunchConfigurationName
asg.DefaultCooldown = 300
asg.HealthCheckGracePeriod = 300
asg.DesiredCapacity = 1
asg.MinSize = 1
asg.MaxSize = 5
asg.AvailabilityZones = []string{"us-west-2a"}
// Parameters for setting desired capacity to 1
var sp1 SetDesiredCapacityRequestParams
sp1.AutoScalingGroupName = asg.AutoScalingGroupName
sp1.DesiredCapacity = 1
// Parameters for setting desired capacity to 2
var sp2 SetDesiredCapacityRequestParams
sp2.AutoScalingGroupName = asg.AutoScalingGroupName
sp2.DesiredCapacity = 2
awsAuth, err := aws.EnvAuth()
if err != nil {
mockTest = true
t.Log("Running mock tests as AWS environment variables are not set")
awsAuth := aws.Auth{AccessKey: "abc", SecretKey: "123"}
as = New(awsAuth, aws.Region{AutoScalingEndpoint: testServer.URL})
} else {
as = New(awsAuth, aws.USWest2)
}
// Create the launch configuration
if mockTest {
testServer.Response(200, nil, astest.CreateLaunchConfigurationResponse)
}
_, err = as.CreateLaunchConfiguration(lc)
if err != nil {
t.Fatal(err)
}
// Check that we can get the launch configuration details
if mockTest {
testServer.Response(200, nil, astest.DescribeLaunchConfigurationResponse)
}
_, err = as.DescribeLaunchConfigurations([]string{lc.LaunchConfigurationName})
if err != nil {
t.Fatal(err)
}
// Create the AutoScalingGroup
if mockTest {
testServer.Response(200, nil, astest.CreateAutoScalingGroupResponse)
}
_, err = as.CreateAutoScalingGroup(asg)
if err != nil {
t.Fatal(err)
}
// Check that we can get the autoscaling group details
if mockTest {
testServer.Response(200, nil, astest.DescribeAutoScalingGroupResponse)
}
_, err = as.DescribeAutoScalingGroups(nil)
if err != nil {
t.Fatal(err)
}
// Suspend the scaling processes for the test AutoScalingGroup
if mockTest {
testServer.Response(200, nil, astest.SuspendProcessesResponse)
}
_, err = as.SuspendProcesses(asg, nil)
if err != nil {
t.Fatal(err)
}
// Resume scaling processes for the test AutoScalingGroup
if mockTest {
testServer.Response(200, nil, astest.ResumeProcessesResponse)
}
_, err = as.ResumeProcesses(asg, nil)
if err != nil {
t.Fatal(err)
}
// Change the desired capacity from 1 to 2. This will launch a second instance
if mockTest {
testServer.Response(200, nil, astest.SetDesiredCapacityResponse)
}
_, err = as.SetDesiredCapacity(sp2)
if err != nil {
t.Fatal(err)
}
// Change the desired capacity from 2 to 1. This will terminate one of the instances
if mockTest {
testServer.Response(200, nil, astest.SetDesiredCapacityResponse)
}
_, err = as.SetDesiredCapacity(sp1)
if err != nil {
t.Fatal(err)
}
// Update the max capacity for the scaling group
if mockTest {
testServer.Response(200, nil, astest.UpdateAutoScalingGroupResponse)
}
asg.MaxSize = 6
_, err = as.UpdateAutoScalingGroup(asg)
if err != nil {
t.Fatal(err)
}
// Add a scheduled action to the group
var psar PutScheduledActionRequestParams
psar.AutoScalingGroupName = asg.AutoScalingGroupName
psar.MaxSize = 4
psar.ScheduledActionName = "SATest1"
psar.Recurrence = "30 0 1 1,6,12 *"
if mockTest {
testServer.Response(200, nil, astest.PutScheduledUpdateGroupActionResponse)
}
_, err = as.PutScheduledUpdateGroupAction(psar)
if err != nil {
t.Fatal(err)
}
// List the scheduled actions for the group
var sar ScheduledActionsRequestParams
sar.AutoScalingGroupName = asg.AutoScalingGroupName
if mockTest {
testServer.Response(200, nil, astest.DescribeScheduledActionsResponse)
}
_, err = as.DescribeScheduledActions(sar)
if err != nil {
t.Fatal(err)
}
// Delete the test scheduled action from the group
var dsar DeleteScheduledActionRequestParams
dsar.AutoScalingGroupName = asg.AutoScalingGroupName
dsar.ScheduledActionName = psar.ScheduledActionName
if mockTest {
testServer.Response(200, nil, astest.DeleteScheduledActionResponse)
}
_, err = as.DeleteScheduledAction(dsar)
if err != nil {
t.Fatal(err)
}
testServer.Flush()
}
|