33import pandas as pd
44import pytest
55
6- from ads .opctl .backend .marketplace .helm_helper import run_helm_install , _HELM_BINARY_ , HelmCommand , _get_as_flags_ , \
7- run_helm_list , run_helm_login , _check_if_chart_already_exists_ , check_helm_pull , HelmPullStatus
8- from unittest .mock import patch , Mock , create_autospec
6+ from ads .opctl .backend .marketplace .helm_helper import (
7+ run_helm_install ,
8+ _HELM_BINARY_ ,
9+ HelmCommand ,
10+ _get_as_flags_ ,
11+ run_helm_list ,
12+ run_helm_login ,
13+ _check_if_chart_already_exists_ ,
14+ check_helm_pull ,
15+ HelmPullStatus ,
16+ )
17+ from unittest .mock import patch , Mock
918
1019name = "NAME"
1120chart = "CHART_NAME"
@@ -28,9 +37,13 @@ def test_helm_install(mock_check_chart_exist: Mock, subprocess_mock: Mock):
2837 name ,
2938 chart ,
3039 * _get_as_flags_ (
31- namespace = namespace , values = values_yaml_path , version = version , timeout = "300s" , ** kwargs
40+ namespace = namespace ,
41+ values = values_yaml_path ,
42+ version = version ,
43+ timeout = "300s" ,
44+ ** kwargs ,
3245 ),
33- "--wait"
46+ "--wait" ,
3447 ]
3548
3649 subprocess_mock .assert_called_with (helm_cmd )
@@ -48,34 +61,65 @@ def test_helm_upgrade(mock_check_chart_exist: Mock, subprocess_mock: Mock):
4861 name ,
4962 chart ,
5063 * _get_as_flags_ (
51- namespace = namespace , values = values_yaml_path , version = version , timeout = "300s" , ** kwargs
64+ namespace = namespace ,
65+ values = values_yaml_path ,
66+ version = version ,
67+ timeout = "300s" ,
68+ ** kwargs ,
5269 ),
53- "--wait"
70+ "--wait" ,
5471 ]
5572 subprocess_mock .assert_called_with (helm_cmd )
5673
5774
5875@patch ("ads.opctl.backend.marketplace.helm_helper.subprocess.run" )
5976def test_helm_list_returns_single_value (subprocess_mock : Mock ):
60- header = ["NAME" , "NAMESPACE" , "REVISION" , "UPDATED" , "STATUS" , "CHART" , "APP VERSION" ]
61- data = ["fs-dp-api-test" , "feature-store" , "4" , "2023-11-22 10:22:13.425579296 +0530 IST" , "deployed" ,
62- "feature-store-dp-api-1.0" , "0.1.270.marketplace-vuls" ]
77+ header = [
78+ "NAME" ,
79+ "NAMESPACE" ,
80+ "REVISION" ,
81+ "UPDATED" ,
82+ "STATUS" ,
83+ "CHART" ,
84+ "APP VERSION" ,
85+ ]
86+ data = [
87+ "fs-dp-api-test" ,
88+ "feature-store" ,
89+ "4" ,
90+ "2023-11-22 10:22:13.425579296 +0530 IST" ,
91+ "deployed" ,
92+ "feature-store-dp-api-1.0" ,
93+ "0.1.270.marketplace-vuls" ,
94+ ]
6395 std_out = "\t " .join (header ) + "\n " + "\t " .join (data )
6496
65- list_result = subprocess .CompletedProcess (args = "" , returncode = 0 , stdout = std_out .encode ())
97+ list_result = subprocess .CompletedProcess (
98+ args = "" , returncode = 0 , stdout = std_out .encode ()
99+ )
66100 subprocess_mock .return_value = list_result
67101 result = run_helm_list (namespace , ** kwargs )
68102 assert len (result ) == 1
69103 assert "NAME" in result .columns
70- assert any (result [' NAME' ] == "fs-dp-api-test" )
104+ assert any (result [" NAME" ] == "fs-dp-api-test" )
71105
72106
73107@patch ("ads.opctl.backend.marketplace.helm_helper.subprocess.run" )
74108def test_helm_list_returns_no_value (subprocess_mock : Mock ):
75- header = ["NAME" , "NAMESPACE" , "REVISION" , "UPDATED" , "STATUS" , "CHART" , "APP VERSION" ]
109+ header = [
110+ "NAME" ,
111+ "NAMESPACE" ,
112+ "REVISION" ,
113+ "UPDATED" ,
114+ "STATUS" ,
115+ "CHART" ,
116+ "APP VERSION" ,
117+ ]
76118 std_out = "\t " .join (header )
77119
78- list_result = subprocess .CompletedProcess (args = "" , returncode = 0 , stdout = std_out .encode ())
120+ list_result = subprocess .CompletedProcess (
121+ args = "" , returncode = 0 , stdout = std_out .encode ()
122+ )
79123 subprocess_mock .return_value = list_result
80124 result = run_helm_list (namespace , ** kwargs )
81125 assert len (result ) == 0
@@ -84,7 +128,9 @@ def test_helm_list_returns_no_value(subprocess_mock: Mock):
84128
85129@patch ("ads.opctl.backend.marketplace.helm_helper.subprocess.run" )
86130def test_helm_list_throws_exception (subprocess_mock : Mock ):
87- list_result = subprocess .CompletedProcess (args = "" , returncode = 1 , stderr = "Some Exception" )
131+ list_result = subprocess .CompletedProcess (
132+ args = "" , returncode = 1 , stderr = "Some Exception"
133+ )
88134 subprocess_mock .return_value = list_result
89135 with pytest .raises (Exception ) as e_info :
90136 run_helm_list (namespace , ** kwargs )
@@ -94,75 +140,133 @@ def test_helm_list_throws_exception(subprocess_mock: Mock):
94140def test_helm_login_success (subprocess_mock : Mock ):
95141 subprocess_return = subprocess .CompletedProcess (args = "" , returncode = 0 )
96142 subprocess_mock .return_value = subprocess_return
97- run_helm_login (' oci_repo' , ' token' )
143+ run_helm_login (" oci_repo" , " token" )
98144 subprocess_mock .assert_called_with (
99- ['helm' , 'registry' , 'login' , 'oci_repo' , '--username' , 'BEARER_TOKEN' , '--password' , 'token' ],
100- capture_output = True )
145+ [
146+ "helm" ,
147+ "registry" ,
148+ "login" ,
149+ "oci_repo" ,
150+ "--username" ,
151+ "BEARER_TOKEN" ,
152+ "--password" ,
153+ "token" ,
154+ ],
155+ capture_output = True ,
156+ )
101157
102158
103159@patch ("ads.opctl.backend.marketplace.helm_helper.subprocess.run" )
104160def test_helm_login_throws_exception (subprocess_mock : Mock ):
105- subprocess_return = subprocess .CompletedProcess (args = "" , returncode = 1 , stderr = "Some Exception" )
161+ subprocess_return = subprocess .CompletedProcess (
162+ args = "" , returncode = 1 , stderr = "Some Exception"
163+ )
106164 subprocess_mock .return_value = subprocess_return
107165 with pytest .raises (Exception ) as e_info :
108- run_helm_login (' oci_repo' , ' token' )
166+ run_helm_login (" oci_repo" , " token" )
109167 subprocess_mock .assert_called_with (
110- ['helm' , 'registry' , 'login' , 'oci_repo' , '--username' , 'BEARER_TOKEN' , '--password' , 'token' ],
111- capture_output = True )
168+ [
169+ "helm" ,
170+ "registry" ,
171+ "login" ,
172+ "oci_repo" ,
173+ "--username" ,
174+ "BEARER_TOKEN" ,
175+ "--password" ,
176+ "token" ,
177+ ],
178+ capture_output = True ,
179+ )
112180
113181
114182@patch ("ads.opctl.backend.marketplace.helm_helper.subprocess.run" )
115183def test_helm_pull_success (subprocess_mock : Mock ):
116184 subprocess_return = subprocess .CompletedProcess (args = "" , returncode = 0 , stderr = b"" )
117185 subprocess_mock .return_value = subprocess_return
118- assert check_helm_pull (' helm_chart_url' , ' version' ) == HelmPullStatus .SUCCESS
186+ assert check_helm_pull (" helm_chart_url" , " version" ) == HelmPullStatus .SUCCESS
119187 subprocess_mock .assert_called_with (
120- [' helm' , ' pull' , ' helm_chart_url' , ' --version' , ' version' ],
121- capture_output = True )
188+ [" helm" , " pull" , " helm_chart_url" , " --version" , " version" ], capture_output = True
189+ )
122190
123191
124192@patch ("ads.opctl.backend.marketplace.helm_helper.subprocess.run" )
125193def test_helm_pull_unauthorized (subprocess_mock : Mock ):
126- subprocess_return = subprocess .CompletedProcess (args = "" , returncode = 1 , stderr = b"unauthorized" )
194+ subprocess_return = subprocess .CompletedProcess (
195+ args = "" , returncode = 1 , stderr = b"unauthorized"
196+ )
127197 subprocess_mock .return_value = subprocess_return
128- assert check_helm_pull ('helm_chart_url' , 'version' ) == HelmPullStatus .AUTHENTICATION_FAILURE
198+ assert (
199+ check_helm_pull ("helm_chart_url" , "version" )
200+ == HelmPullStatus .AUTHENTICATION_FAILURE
201+ )
129202 subprocess_mock .assert_called_with (
130- [' helm' , ' pull' , ' helm_chart_url' , ' --version' , ' version' ],
131- capture_output = True )
203+ [" helm" , " pull" , " helm_chart_url" , " --version" , " version" ], capture_output = True
204+ )
132205
133206
134207@patch ("ads.opctl.backend.marketplace.helm_helper.subprocess.run" )
135208def test_helm_pull_unknown_failure (subprocess_mock : Mock ):
136- subprocess_return = subprocess .CompletedProcess (args = "" , returncode = 1 , stderr = b"some failure" )
209+ subprocess_return = subprocess .CompletedProcess (
210+ args = "" , returncode = 1 , stderr = b"some failure"
211+ )
137212 subprocess_mock .return_value = subprocess_return
138- assert check_helm_pull ('helm_chart_url' , 'version' ) == HelmPullStatus .UNKNOWN_FAILURE
213+ assert (
214+ check_helm_pull ("helm_chart_url" , "version" ) == HelmPullStatus .UNKNOWN_FAILURE
215+ )
139216 subprocess_mock .assert_called_with (
140- [' helm' , ' pull' , ' helm_chart_url' , ' --version' , ' version' ],
141- capture_output = True )
217+ [" helm" , " pull" , " helm_chart_url" , " --version" , " version" ], capture_output = True
218+ )
142219
143220
144221def test_get_as_flags_ ():
145- args = {
146- "key1" : "value1" ,
147- "key2" : "value2"
148- }
222+ args = {"key1" : "value1" , "key2" : "value2" }
149223 assert _get_as_flags_ (** args ) == ["--key1" , "value1" , "--key2" , "value2" ]
150224
151225
152226@patch ("ads.opctl.backend.marketplace.helm_helper.run_helm_list" )
153227def test_check_helm_chart_exist_when_chart_do_exist (helm_list_cmd : Mock ):
154- header = ["NAME" , "NAMESPACE" , "REVISION" , "UPDATED" , "STATUS" , "CHART" , "APP VERSION" ]
155- data = ["fs-dp-api-test" , "feature-store" , "4" , "2023-11-22 10:22:13.425579296 +0530 IST" , "deployed" ,
156- "feature-store-dp-api-1.0" , "0.1.270.marketplace-vuls" ]
228+ header = [
229+ "NAME" ,
230+ "NAMESPACE" ,
231+ "REVISION" ,
232+ "UPDATED" ,
233+ "STATUS" ,
234+ "CHART" ,
235+ "APP VERSION" ,
236+ ]
237+ data = [
238+ "fs-dp-api-test" ,
239+ "feature-store" ,
240+ "4" ,
241+ "2023-11-22 10:22:13.425579296 +0530 IST" ,
242+ "deployed" ,
243+ "feature-store-dp-api-1.0" ,
244+ "0.1.270.marketplace-vuls" ,
245+ ]
157246 helm_list_cmd .return_value = pd .DataFrame ([data ], columns = header )
158247 assert _check_if_chart_already_exists_ ("fs-dp-api-test" , namespace )
159248
160249
161250@patch ("ads.opctl.backend.marketplace.helm_helper.run_helm_list" )
162251def test_check_helm_chart_exist_when_chart_do_not_exist (helm_list_cmd : Mock ):
163- header = ["NAME" , "NAMESPACE" , "REVISION" , "UPDATED" , "STATUS" , "CHART" , "APP VERSION" ]
164- data = ["some-other-chart" , "feature-store" , "4" , "2023-11-22 10:22:13.425579296 +0530 IST" , "deployed" ,
165- "feature-store-dp-api-1.0" , "0.1.270.marketplace-vuls" ]
252+ header = [
253+ "NAME" ,
254+ "NAMESPACE" ,
255+ "REVISION" ,
256+ "UPDATED" ,
257+ "STATUS" ,
258+ "CHART" ,
259+ "APP VERSION" ,
260+ ]
261+ data = [
262+ "some-other-chart" ,
263+ "feature-store" ,
264+ "4" ,
265+ "2023-11-22 10:22:13.425579296 +0530 IST" ,
266+ "deployed" ,
267+ "feature-store-dp-api-1.0" ,
268+ "0.1.270.marketplace-vuls" ,
269+ ]
166270 helm_list_cmd .return_value = pd .DataFrame ([data ], columns = header )
167271 assert not _check_if_chart_already_exists_ ("fs-dp-api-test" , namespace )
168272
0 commit comments