github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_autoscaling_group_waiting_test.go (about)

     1  package aws
     2  
     3  import "testing"
     4  
     5  func TestCapacitySatisfiedCreate(t *testing.T) {
     6  	cases := map[string]struct {
     7  		Data            map[string]interface{}
     8  		HaveASG         int
     9  		HaveELB         int
    10  		ExpectSatisfied bool
    11  		ExpectReason    string
    12  	}{
    13  		"min_size, have less": {
    14  			Data: map[string]interface{}{
    15  				"min_size": 5,
    16  			},
    17  			HaveASG:         2,
    18  			ExpectSatisfied: false,
    19  			ExpectReason:    "Need at least 5 healthy instances in ASG, have 2",
    20  		},
    21  		"min_size, got it": {
    22  			Data: map[string]interface{}{
    23  				"min_size": 5,
    24  			},
    25  			HaveASG:         5,
    26  			ExpectSatisfied: true,
    27  		},
    28  		"min_size, have more": {
    29  			Data: map[string]interface{}{
    30  				"min_size": 5,
    31  			},
    32  			HaveASG:         10,
    33  			ExpectSatisfied: true,
    34  		},
    35  		"desired_capacity, have less": {
    36  			Data: map[string]interface{}{
    37  				"desired_capacity": 5,
    38  			},
    39  			HaveASG:         2,
    40  			ExpectSatisfied: false,
    41  			ExpectReason:    "Need at least 5 healthy instances in ASG, have 2",
    42  		},
    43  		"desired_capacity overrides min_size": {
    44  			Data: map[string]interface{}{
    45  				"min_size":         2,
    46  				"desired_capacity": 5,
    47  			},
    48  			HaveASG:         2,
    49  			ExpectSatisfied: false,
    50  			ExpectReason:    "Need at least 5 healthy instances in ASG, have 2",
    51  		},
    52  		"desired_capacity, got it": {
    53  			Data: map[string]interface{}{
    54  				"desired_capacity": 5,
    55  			},
    56  			HaveASG:         5,
    57  			ExpectSatisfied: true,
    58  		},
    59  		"desired_capacity, have more": {
    60  			Data: map[string]interface{}{
    61  				"desired_capacity": 5,
    62  			},
    63  			HaveASG:         10,
    64  			ExpectSatisfied: true,
    65  		},
    66  
    67  		"min_elb_capacity, have less": {
    68  			Data: map[string]interface{}{
    69  				"min_elb_capacity": 5,
    70  			},
    71  			HaveELB:         2,
    72  			ExpectSatisfied: false,
    73  			ExpectReason:    "Need at least 5 healthy instances in ELB, have 2",
    74  		},
    75  		"min_elb_capacity, got it": {
    76  			Data: map[string]interface{}{
    77  				"min_elb_capacity": 5,
    78  			},
    79  			HaveELB:         5,
    80  			ExpectSatisfied: true,
    81  		},
    82  		"min_elb_capacity, have more": {
    83  			Data: map[string]interface{}{
    84  				"min_elb_capacity": 5,
    85  			},
    86  			HaveELB:         10,
    87  			ExpectSatisfied: true,
    88  		},
    89  		"wait_for_elb_capacity, have less": {
    90  			Data: map[string]interface{}{
    91  				"wait_for_elb_capacity": 5,
    92  			},
    93  			HaveELB:         2,
    94  			ExpectSatisfied: false,
    95  			ExpectReason:    "Need at least 5 healthy instances in ELB, have 2",
    96  		},
    97  		"wait_for_elb_capacity, got it": {
    98  			Data: map[string]interface{}{
    99  				"wait_for_elb_capacity": 5,
   100  			},
   101  			HaveELB:         5,
   102  			ExpectSatisfied: true,
   103  		},
   104  		"wait_for_elb_capacity, have more": {
   105  			Data: map[string]interface{}{
   106  				"wait_for_elb_capacity": 5,
   107  			},
   108  			HaveELB:         10,
   109  			ExpectSatisfied: true,
   110  		},
   111  		"wait_for_elb_capacity overrides min_elb_capacity": {
   112  			Data: map[string]interface{}{
   113  				"min_elb_capacity":      2,
   114  				"wait_for_elb_capacity": 5,
   115  			},
   116  			HaveELB:         2,
   117  			ExpectSatisfied: false,
   118  			ExpectReason:    "Need at least 5 healthy instances in ELB, have 2",
   119  		},
   120  	}
   121  
   122  	r := resourceAwsAutoscalingGroup()
   123  	for tn, tc := range cases {
   124  		d := r.TestResourceData()
   125  		for k, v := range tc.Data {
   126  			if err := d.Set(k, v); err != nil {
   127  				t.Fatalf("err: %s", err)
   128  			}
   129  		}
   130  		gotSatisfied, gotReason := capacitySatisfiedCreate(d, tc.HaveASG, tc.HaveELB)
   131  
   132  		if gotSatisfied != tc.ExpectSatisfied {
   133  			t.Fatalf("%s: expected satisfied: %t, got: %t (reason: %s)",
   134  				tn, tc.ExpectSatisfied, gotSatisfied, gotReason)
   135  		}
   136  
   137  		if gotReason != tc.ExpectReason {
   138  			t.Fatalf("%s: expected reason: %s, got: %s",
   139  				tn, tc.ExpectReason, gotReason)
   140  		}
   141  	}
   142  }
   143  
   144  func TestCapacitySatisfiedUpdate(t *testing.T) {
   145  	cases := map[string]struct {
   146  		Data            map[string]interface{}
   147  		HaveASG         int
   148  		HaveELB         int
   149  		ExpectSatisfied bool
   150  		ExpectReason    string
   151  	}{
   152  		"default is satisfied": {
   153  			Data:            map[string]interface{}{},
   154  			ExpectSatisfied: true,
   155  		},
   156  		"desired_capacity, have less": {
   157  			Data: map[string]interface{}{
   158  				"desired_capacity": 5,
   159  			},
   160  			HaveASG:         2,
   161  			ExpectSatisfied: false,
   162  			ExpectReason:    "Need exactly 5 healthy instances in ASG, have 2",
   163  		},
   164  		"desired_capacity, got it": {
   165  			Data: map[string]interface{}{
   166  				"desired_capacity": 5,
   167  			},
   168  			HaveASG:         5,
   169  			ExpectSatisfied: true,
   170  		},
   171  		"desired_capacity, have more": {
   172  			Data: map[string]interface{}{
   173  				"desired_capacity": 5,
   174  			},
   175  			HaveASG:         10,
   176  			ExpectSatisfied: false,
   177  			ExpectReason:    "Need exactly 5 healthy instances in ASG, have 10",
   178  		},
   179  		"wait_for_elb_capacity, have less": {
   180  			Data: map[string]interface{}{
   181  				"wait_for_elb_capacity": 5,
   182  			},
   183  			HaveELB:         2,
   184  			ExpectSatisfied: false,
   185  			ExpectReason:    "Need exactly 5 healthy instances in ELB, have 2",
   186  		},
   187  		"wait_for_elb_capacity, got it": {
   188  			Data: map[string]interface{}{
   189  				"wait_for_elb_capacity": 5,
   190  			},
   191  			HaveELB:         5,
   192  			ExpectSatisfied: true,
   193  		},
   194  		"wait_for_elb_capacity, have more": {
   195  			Data: map[string]interface{}{
   196  				"wait_for_elb_capacity": 5,
   197  			},
   198  			HaveELB:         10,
   199  			ExpectSatisfied: false,
   200  			ExpectReason:    "Need exactly 5 healthy instances in ELB, have 10",
   201  		},
   202  	}
   203  
   204  	r := resourceAwsAutoscalingGroup()
   205  	for tn, tc := range cases {
   206  		d := r.TestResourceData()
   207  		for k, v := range tc.Data {
   208  			if err := d.Set(k, v); err != nil {
   209  				t.Fatalf("err: %s", err)
   210  			}
   211  		}
   212  		gotSatisfied, gotReason := capacitySatisfiedUpdate(d, tc.HaveASG, tc.HaveELB)
   213  
   214  		if gotSatisfied != tc.ExpectSatisfied {
   215  			t.Fatalf("%s: expected satisfied: %t, got: %t (reason: %s)",
   216  				tn, tc.ExpectSatisfied, gotSatisfied, gotReason)
   217  		}
   218  
   219  		if gotReason != tc.ExpectReason {
   220  			t.Fatalf("%s: expected reason: %s, got: %s",
   221  				tn, tc.ExpectReason, gotReason)
   222  		}
   223  	}
   224  }