summaryrefslogtreecommitdiff
path: root/test/integration/targets/ec2_instance/roles/ec2_instance/tasks/checkmode_tests.yml
blob: b161eca636e7faa9974f4e944a1764e8cc7d0c50 (plain)
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
- block:
  - name: "Make basic instance"
    ec2_instance:
      state: present
      name: "{{ resource_prefix }}-checkmode-comparison"
      image_id: "{{ ec2_ami_image }}"
      security_groups: "{{ sg.group_id }}"
      instance_type: "{{ ec2_instance_type }}"
      vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
      wait: false
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
    register: basic_instance

  - name: "Make basic instance (check mode)"
    ec2_instance:
      state: present
      name: "{{ resource_prefix }}-checkmode-comparison-checkmode"
      image_id: "{{ ec2_ami_image }}"
      security_groups: "{{ sg.group_id }}"
      instance_type: "{{ ec2_instance_type }}"
      vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
    check_mode: yes

  - name: "fact presented ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
    register: presented_instance_fact

  - name: "fact checkmode ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison-checkmode"
    register: checkmode_instance_fact

  - name: "Confirm whether the check mode is working normally."
    assert:
      that:
        - "{{ presented_instance_fact.instances | length }} > 0"
        - "{{ checkmode_instance_fact.instances | length }} == 0"

  - name: "Stop instance (check mode)"
    ec2_instance:
      state: stopped
      name: "{{ resource_prefix }}-checkmode-comparison"
      vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
    check_mode: yes

  - name: "fact ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
    register: confirm_checkmode_stopinstance_fact

  - name: "Verify that it was not stopped."
    assert:
      that:
        - '"{{ confirm_checkmode_stopinstance_fact.instances[0].state.name }}" != "stopped"'

  - name: "Stop instance."
    ec2_instance:
      state: stopped
      name: "{{ resource_prefix }}-checkmode-comparison"
      vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
    register: instance_stop
    until: not instance_stop.failed
    retries: 10

  - name: "fact stopped ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
    register: confirm_stopinstance_fact

  - name: "Verify that it was stopped."
    assert:
      that:
        - '"{{ confirm_stopinstance_fact.instances[0].state.name }}" in ["stopped", "stopping"]'

  - name: "Running instance in check mode."
    ec2_instance:
      state: running
      name: "{{ resource_prefix }}-checkmode-comparison"
      vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
    check_mode: yes

  - name: "fact ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
    register: confirm_checkmode_runninginstance_fact

  - name: "Verify that it was not running."
    assert:
      that:
        - '"{{ confirm_checkmode_runninginstance_fact.instances[0].state.name }}" != "running"'

  - name: "Running instance."
    ec2_instance:
      state: running
      name: "{{ resource_prefix }}-checkmode-comparison"
      vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"

  - name: "fact ec2 instance."
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
    register: confirm_runninginstance_fact

  - name: "Verify that it was running."
    assert:
      that:
        - '"{{ confirm_runninginstance_fact.instances[0].state.name }}" == "running"'

  - name: "Terminate instance in check mode."
    ec2_instance:
      state: absent
      name: "{{ resource_prefix }}-checkmode-comparison"
      vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
    check_mode: yes

  - name: "fact ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
    register: confirm_checkmode_terminatedinstance_fact

  - name: "Verify that it was not terminated,"
    assert:
      that:
        - '"{{ confirm_checkmode_terminatedinstance_fact.instances[0].state.name }}" != "terminated"'

  - name: "Terminate instance."
    ec2_instance:
      state: absent
      name: "{{ resource_prefix }}-checkmode-comparison"
      vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"

  - name: "fact ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
    register: confirm_terminatedinstance_fact

  - name: "Verify that it was terminated,"
    assert:
      that:
        - '"{{ confirm_terminatedinstance_fact.instances[0].state.name }}" == "terminated"'

  always:
  - name: "Terminate checkmode instances"
    ec2_instance:
      state: absent
      filters:
        "tag:TestId": "{{ ec2_instance_tag_TestId }}"
      wait: yes
    ignore_errors: yes