Skip to content

Commit ab09115

Browse files
author
Himani Anil Deshpande
committed
[UnitTest] Add unit test for cfn_hup_configuration resource
1 parent 1f33df3 commit ab09115

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
require 'spec_helper'
2+
3+
class ConvergeCfnHupConfiguration
4+
def self.configure(chef_run)
5+
chef_run.converge_dsl('aws-parallelcluster-environment') do
6+
cfn_hup_configuration 'configure' do
7+
action :configure
8+
end
9+
end
10+
end
11+
end
12+
13+
AWS_REGION = "AWS_REGION".freeze
14+
AWS_DOMAIN = "AWS_DOMAIN".freeze
15+
STACK_ID = "STACK_ID".freeze
16+
CLOUDFORMATION_URL = "https://cloudformation.#{AWS_REGION}.#{AWS_DOMAIN}".freeze
17+
INSTANCE_ROLE_NAME = "INSTANCE_ROLE_NAME".freeze
18+
LAUNCH_TEMPLATE_ID = "LAUNCH_TEMPLATE_ID".freeze
19+
SCRIPT_DIR = "SCRIPT_DIR".freeze
20+
MONITOR_SHARED_DIR = "MONITOR_SHARED_DIR".freeze
21+
22+
describe 'cfn_hup_configuration:configure' do
23+
for_all_oses do |platform, version|
24+
context "on #{platform}#{version}" do
25+
for_all_node_types do |node_type|
26+
context "when #{node_type}" do
27+
cached(:chef_run) do
28+
runner = runner(platform: platform, version: version, step_into: ['cfn_hup_configuration']) do |node|
29+
allow_any_instance_of(Object).to receive(:get_metadata_token).and_return("IMDS_TOKEN")
30+
allow_any_instance_of(Object).to receive(:get_metadata_with_token)
31+
.with("IMDS_TOKEN", URI("http://169.254.169.254/latest/meta-data/iam/security-credentials"))
32+
.and_return(INSTANCE_ROLE_NAME)
33+
node.override["cluster"]["node_type"] = node_type
34+
node.override["cluster"]["region"] = AWS_REGION
35+
node.override["cluster"]["aws_domain"] = AWS_DOMAIN
36+
# TODO: We inject the stack id into the attribute stack_arn when generating the dna.json in the CLI.
37+
# This should be fixed at the CLI level first and adapt the cookbook accordingly.
38+
node.override["cluster"]["stack_arn"] = STACK_ID
39+
node.override["cluster"]["launch_template_id"] = LAUNCH_TEMPLATE_ID
40+
node.override['cluster']['scripts_dir'] = SCRIPT_DIR
41+
node.override['cluster']['shared_dir'] = MONITOR_SHARED_DIR
42+
end
43+
ConvergeCfnHupConfiguration.configure(runner)
44+
end
45+
cached(:node) { chef_run.node }
46+
47+
%w(/etc/cfn /etc/cfn/hooks.d).each do |dir|
48+
it "creates the directory #{dir}" do
49+
is_expected.to create_directory(dir)
50+
.with(owner: 'root')
51+
.with(group: 'root')
52+
.with(mode: "0770")
53+
.with(recursive: true)
54+
end
55+
end
56+
57+
it "creates the file /etc/cfn/cfn-hup.conf" do
58+
is_expected.to create_template("/etc/cfn/cfn-hup.conf")
59+
.with(source: 'cfn_hup/cfn-hup.conf.erb')
60+
.with(user: "root")
61+
.with(group: "root")
62+
.with(mode: "0400")
63+
.with(variables: {
64+
stack_id: STACK_ID,
65+
region: AWS_REGION,
66+
cloudformation_url: CLOUDFORMATION_URL,
67+
cfn_init_role: INSTANCE_ROLE_NAME,
68+
})
69+
end
70+
71+
it "creates the file /etc/cfn/hooks.d/pcluster-update.conf" do
72+
is_expected.to create_template("/etc/cfn/hooks.d/pcluster-update.conf")
73+
.with(source: 'cfn_hup/cfn-hook-update.conf.erb')
74+
.with(user: "root")
75+
.with(group: "root")
76+
.with(mode: "0400")
77+
.with(variables: {
78+
stack_id: STACK_ID,
79+
region: AWS_REGION,
80+
cloudformation_url: CLOUDFORMATION_URL,
81+
cfn_init_role: INSTANCE_ROLE_NAME,
82+
launch_template_resource_id: LAUNCH_TEMPLATE_ID,
83+
update_hook_script_dir: SCRIPT_DIR,
84+
})
85+
end
86+
87+
if %(ComputeFleet).include?(node_type)
88+
it "creates the file #{SCRIPT_DIR}/cfn-hup-update-action.sh" do
89+
is_expected.to create_template("#{SCRIPT_DIR}/cfn-hup-update-action.sh")
90+
.with(source: "cfn_hup/#{node_type}/cfn-hup-update-action.sh.erb")
91+
.with(user: "root")
92+
.with(group: "root")
93+
.with(mode: "0744")
94+
.with(variables: {
95+
monitor_shared_dir: "#{MONITOR_SHARED_DIR}/dna",
96+
launch_template_resource_id: LAUNCH_TEMPLATE_ID,
97+
})
98+
end
99+
elsif node_type == 'HeadNode'
100+
it "creates #{SCRIPT_DIR}/get_compute_user_data.py" do
101+
is_expected.to create_if_missing_cookbook_file("#{SCRIPT_DIR}/get_compute_user_data.py")
102+
.with(source: 'cfn_hup/get_compute_user_data.py')
103+
.with(user: 'root')
104+
.with(group: 'root')
105+
.with(mode: '0755')
106+
end
107+
108+
it "creates the directory #{MONITOR_SHARED_DIR}/dna" do
109+
is_expected.to create_directory("#{MONITOR_SHARED_DIR}/dna")
110+
end
111+
end
112+
end
113+
end
114+
end
115+
end
116+
end

0 commit comments

Comments
 (0)