1+ #
2+ # Licensed to the Apache Software Foundation (ASF) under one or more
3+ # contributor license agreements. See the NOTICE file distributed with
4+ # this work for additional information regarding copyright ownership.
5+ # The ASF licenses this file to You under the Apache License, Version 2.0
6+ # (the "License"); you may not use this file except in compliance with
7+ # the License. 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+ <#
18+ Pester tests for Solr zk command
19+ Ported from test/test_zk.bats
20+ #>
21+
22+ BeforeAll {
23+ # Get the Solr installation directory from environment variable
24+ $script :SolrTip = $env: SOLR_TIP
25+
26+ if (-not $SolrTip ) {
27+ throw " SOLR_TIP environment variable is not set"
28+ }
29+
30+ # Determine the Solr executable based on OS
31+ $script :SolrCmd = Join-Path $SolrTip " bin\solr.cmd"
32+
33+ if (-not (Test-Path $SolrCmd )) {
34+ throw " Solr executable not found at: $SolrCmd "
35+ }
36+
37+ Write-Host " Using Solr installation at: $SolrTip "
38+ Write-Host " Using Solr command: $SolrCmd "
39+
40+ # Get ZooKeeper port from environment or default
41+ $script :ZK_PORT = if ($env: ZK_PORT ) { $env: ZK_PORT } else { " 9983" }
42+ $script :SOLR_PORT = if ($env: SOLR_PORT ) { $env: SOLR_PORT } else { " 8983" }
43+
44+ function Test-CommandOutput {
45+ param (
46+ [string []]$Arguments ,
47+ [string ]$TestName
48+ )
49+
50+ Write-Host " Testing: $TestName "
51+ Write-Host " Running: $SolrCmd $ ( $Arguments -join ' ' ) "
52+
53+ $output = & $SolrCmd @Arguments 2>&1
54+ $outputStr = $output | Out-String
55+
56+ Write-Host " Exit Code: $LASTEXITCODE "
57+ if ($outputStr.Length -gt 0 ) {
58+ Write-Host " Output (first 500 chars): $ ( $outputStr.Substring (0 , [Math ]::Min(500 , $outputStr.Length ))) "
59+ } else {
60+ Write-Host " WARNING: Output is empty!"
61+ }
62+
63+ return $outputStr
64+ }
65+ }
66+
67+ Describe " Solr Zk Command" {
68+ Context " Help commands" {
69+ It " short help" {
70+ $output = Test-CommandOutput @ (" zk" , " ls" , " -h" ) " zk ls -h"
71+ $output | Should -Match " usage: bin/solr zk"
72+ }
73+
74+ It " short help is inferred" {
75+ $output = Test-CommandOutput @ (" zk" , " ls" ) " zk ls"
76+ $output | Should -Match " usage: bin/solr zk"
77+ }
78+
79+ It " long help" {
80+ $output = Test-CommandOutput @ (" zk" , " -h" ) " zk -h"
81+ $output | Should -Match " bin/solr zk ls"
82+ $output | Should -Match " bin/solr zk updateacls"
83+ $output | Should -Match " Pass --help or -h after any COMMAND"
84+ }
85+
86+ It " running subcommands with zk is prevented" {
87+ $output = Test-CommandOutput @ (" ls" , " /" , " -z" , " localhost:$ZK_PORT " ) " ls / -z localhost:$ZK_PORT "
88+ $output | Should -Match " You must invoke this subcommand using the zk command"
89+ }
90+ }
91+
92+ Context " Zk operations (requires running Solr with ZooKeeper)" - Skip:(-not (Test-Path (Join-Path $SolrTip " server\solr" ))) {
93+ BeforeAll {
94+ # Check if Solr is running by trying to connect to the port
95+ $solrRunning = $false
96+ try {
97+ $response = Invoke-WebRequest - Uri " http://localhost:$SOLR_PORT /solr/" - UseBasicParsing - ErrorAction SilentlyContinue
98+ if ($response.StatusCode -eq 200 ) {
99+ $solrRunning = $true
100+ }
101+ } catch {
102+ $solrRunning = $false
103+ }
104+
105+ $script :SolrRunning = $solrRunning
106+ if (-not $solrRunning ) {
107+ Write-Host " WARNING: Solr does not appear to be running on port $SOLR_PORT "
108+ }
109+ }
110+
111+ It " listing out files" - Skip:(-not $SolrRunning ) {
112+ Start-Sleep - Seconds 1
113+ $output = Test-CommandOutput @ (" zk" , " ls" , " /" , " -z" , " localhost:$ZK_PORT " , " --recursive" ) " zk ls / -z localhost:$ZK_PORT --recursive"
114+ $output | Should -Match " aliases\.json"
115+ }
116+
117+ It " connecting via solr-url" - Skip:(-not $SolrRunning ) {
118+ Start-Sleep - Seconds 1
119+ $output = Test-CommandOutput @ (" zk" , " ls" , " /" , " --solr-url" , " http://localhost:$SOLR_PORT " ) " zk ls / --solr-url http://localhost:$SOLR_PORT "
120+ $output | Should -Match " aliases\.json"
121+ }
122+
123+ It " connecting via -s flag" - Skip:(-not $SolrRunning ) {
124+ Start-Sleep - Seconds 1
125+ $output = Test-CommandOutput @ (" zk" , " ls" , " /" , " -s" , " http://localhost:$SOLR_PORT " ) " zk ls / -s http://localhost:$SOLR_PORT "
126+ $output | Should -Match " aliases\.json"
127+ }
128+
129+ It " connecting via default (localhost)" - Skip:(-not $SolrRunning ) {
130+ Start-Sleep - Seconds 1
131+ $output = Test-CommandOutput @ (" zk" , " ls" , " /" ) " zk ls /"
132+ $output | Should -Match " aliases\.json"
133+ }
134+
135+ It " connecting via -z flag" - Skip:(-not $SolrRunning ) {
136+ Start-Sleep - Seconds 1
137+ $output = Test-CommandOutput @ (" zk" , " ls" , " /" , " -z" , " localhost:$ZK_PORT " ) " zk ls / -z localhost:$ZK_PORT "
138+ $output | Should -Match " aliases\.json"
139+ }
140+
141+ It " connecting via --zk-host flag" - Skip:(-not $SolrRunning ) {
142+ Start-Sleep - Seconds 1
143+ $output = Test-CommandOutput @ (" zk" , " ls" , " /" , " --zk-host" , " localhost:$ZK_PORT " ) " zk ls / --zk-host localhost:$ZK_PORT "
144+ $output | Should -Match " aliases\.json"
145+ }
146+
147+ It " copying files around" - Skip:(-not $SolrRunning ) {
148+ $testFile = " testfile_$ ( [System.IO.Path ]::GetRandomFileName()) .txt"
149+ try {
150+ # Create test file
151+ " test content" | Out-File - FilePath $testFile - Encoding UTF8
152+
153+ # Copy to ZK
154+ $output = Test-CommandOutput @ (" zk" , " cp" , $testFile , " zk:/$testFile " , " -z" , " localhost:$ZK_PORT " ) " zk cp $testFile to ZK"
155+ $output | Should -Match " Copying from"
156+
157+ Start-Sleep - Seconds 1
158+
159+ # Verify file is in ZK
160+ $listOutput = Test-CommandOutput @ (" zk" , " ls" , " /" , " -z" , " localhost:$ZK_PORT " ) " zk ls / to verify copy"
161+ $listOutput | Should -Match $testFile
162+ } finally {
163+ # Cleanup
164+ if (Test-Path $testFile ) {
165+ Remove-Item $testFile - Force
166+ }
167+ }
168+ }
169+
170+ It " upconfig" - Skip:(-not $SolrRunning ) {
171+ $sourceConfigsetDir = Join-Path $SolrTip " server\solr\configsets\sample_techproducts_configs"
172+ if (Test-Path $sourceConfigsetDir ) {
173+ $output = Test-CommandOutput @ (" zk" , " upconfig" , " -d" , $sourceConfigsetDir , " -n" , " techproducts_ps_test" , " -z" , " localhost:$ZK_PORT " ) " zk upconfig"
174+ $output | Should -Match " Uploading"
175+ $output | Should -Not -Match " ERROR"
176+ } else {
177+ Set-ItResult - Skipped - Because " sample_techproducts_configs not found"
178+ }
179+ }
180+
181+ It " downconfig" - Skip:(-not $SolrRunning ) {
182+ $tempDir = [System.IO.Path ]::GetTempPath()
183+ $downloadDir = Join-Path $tempDir " downconfig_$ ( [System.IO.Path ]::GetRandomFileName()) "
184+ New-Item - ItemType Directory - Path $downloadDir - Force | Out-Null
185+
186+ try {
187+ $output = Test-CommandOutput @ (" zk" , " downconfig" , " -z" , " localhost:$ZK_PORT " , " -n" , " _default" , " -d" , $downloadDir ) " zk downconfig"
188+ $output | Should -Match " Downloading"
189+ $output | Should -Not -Match " ERROR"
190+ } finally {
191+ # Cleanup
192+ if (Test-Path $downloadDir ) {
193+ Remove-Item $downloadDir - Recurse - Force
194+ }
195+ }
196+ }
197+ }
198+ }
0 commit comments