1+ # -*- coding: utf-8 -*-
2+
3+ # Copyright 2022 Cloudera, Inc. All Rights Reserved.
4+ #
5+ # Licensed under the Apache License, Version 2.0 (the "License");
6+ # you may not use this file except in compliance with the License.
7+ # You may obtain a copy of the License at
8+ #
9+ # http://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # Unless required by applicable law or agreed to in writing, software
12+ # distributed under the License is distributed on an "AS IS" BASIS,
13+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ # See the License for the specific language governing permissions and
15+ # limitations under the License.
16+
17+ from __future__ import (absolute_import , division , print_function )
18+ __metaclass__ = type
19+
20+ import pytest
21+ import unittest
22+
23+ from unittest .mock import patch
24+
25+ from ansible_collections .cloudera .cloud .plugins .modules import env
26+ from ansible_collections .cloudera .cloud .tests .unit .plugins .modules .utils import AnsibleExitJson , ModuleTestCase , setup_module_args
27+
28+
29+ class TestEnvironment (ModuleTestCase ):
30+
31+ def test_freeipa_specified (self ):
32+ setup_module_args ({
33+ 'name' : "unit-test" ,
34+ 'cloud' : 'aws' ,
35+ 'region' : 'fake_region' ,
36+ 'credential' : 'fake_credential' ,
37+ 'public_key_id' : 'fake_key' ,
38+ 'vpc_id' : 'fake_vpc' ,
39+ 'subnet_ids' : [ 'fake_subnet' ],
40+ 'default_sg' : 'fake_default_sg' ,
41+ 'knox_sg' : 'fake_knox_sg' ,
42+ 'log_location' : 'fake_log_location' ,
43+ 'log_identity' : 'fake_log_identity' ,
44+ 'wait' : False ,
45+ 'freeipa' : { 'instanceCountByGroup' : 3 }
46+ })
47+
48+ expected = dict (
49+ environmentName = 'unit-test' ,
50+ credentialName = 'fake_credential' ,
51+ region = 'fake_region' ,
52+ enableTunnel = False ,
53+ workloadAnalytics = True ,
54+ logStorage = dict (
55+ instanceProfile = 'fake_log_identity' ,
56+ storageLocationBase = 'fake_log_location'
57+ ),
58+ authentication = dict (
59+ publicKeyId = 'fake_key'
60+ ),
61+ vpcId = 'fake_vpc' ,
62+ subnetIds = ['fake_subnet' ],
63+ securityAccess = dict (
64+ defaultSecurityGroupId = 'fake_default_sg' ,
65+ securityGroupIdForKnox = 'fake_knox_sg'
66+ ),
67+ freeIpa = dict (instanceCountByGroup = 3 )
68+ )
69+
70+ with patch ('cdpy.cdpy.CdpyEnvironments' ) as mocked_cdp :
71+ mocked_cdp .return_value .describe_environment .return_value = None
72+ mocked_cdp .return_value .create_aws_environment .return_value = { 'name' : 'Successful test' }
73+
74+ with pytest .raises (AnsibleExitJson ) as e :
75+ env .main ()
76+
77+ print ("Returned: " , str (e .value ))
78+
79+ mocked_cdp .return_value .describe_environment .assert_called_once_with ('unit-test' )
80+ mocked_cdp .return_value .create_aws_environment .assert_called_once_with (** expected )
81+
82+ def test_freeipa_default (self ):
83+ setup_module_args ({
84+ 'name' : "unit-test" ,
85+ 'cloud' : 'aws' ,
86+ 'region' : 'fake_region' ,
87+ 'credential' : 'fake_credential' ,
88+ 'public_key_id' : 'fake_key' ,
89+ 'vpc_id' : 'fake_vpc' ,
90+ 'subnet_ids' : [ 'fake_subnet' ],
91+ 'default_sg' : 'fake_default_sg' ,
92+ 'knox_sg' : 'fake_knox_sg' ,
93+ 'log_location' : 'fake_log_location' ,
94+ 'log_identity' : 'fake_log_identity' ,
95+ 'wait' : False
96+ })
97+
98+ expected = dict (
99+ environmentName = 'unit-test' ,
100+ credentialName = 'fake_credential' ,
101+ region = 'fake_region' ,
102+ enableTunnel = False ,
103+ workloadAnalytics = True ,
104+ logStorage = dict (
105+ instanceProfile = 'fake_log_identity' ,
106+ storageLocationBase = 'fake_log_location'
107+ ),
108+ authentication = dict (
109+ publicKeyId = 'fake_key'
110+ ),
111+ vpcId = 'fake_vpc' ,
112+ subnetIds = ['fake_subnet' ],
113+ securityAccess = dict (
114+ defaultSecurityGroupId = 'fake_default_sg' ,
115+ securityGroupIdForKnox = 'fake_knox_sg'
116+ ),
117+ freeIpa = dict (instanceCountByGroup = 2 )
118+ )
119+
120+ with patch ('cdpy.cdpy.CdpyEnvironments' ) as mocked_cdp :
121+ mocked_cdp .return_value .describe_environment .return_value = None
122+ mocked_cdp .return_value .create_aws_environment .return_value = { 'name' : 'Successful test' }
123+
124+ with pytest .raises (AnsibleExitJson ) as e :
125+ env .main ()
126+
127+ print ("Returned: " , str (e .value ))
128+
129+ mocked_cdp .return_value .describe_environment .assert_called_once_with ('unit-test' )
130+ mocked_cdp .return_value .create_aws_environment .assert_called_once_with (** expected )
131+
132+ if __name__ == '__main__' :
133+ unittest .main ()
0 commit comments