-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathWskSdkTests.scala
More file actions
251 lines (221 loc) · 9.37 KB
/
WskSdkTests.scala
File metadata and controls
251 lines (221 loc) · 9.37 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package system.basic
import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import org.apache.commons.io.FileUtils
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import common.TestHelpers
import common.TestUtils.ERROR_EXIT
import common.TestUtils.SUCCESS_EXIT
import common.Wsk
import common.WskProps
import common.WskTestHelpers
import java.nio.charset.StandardCharsets
import scala.sys.process._
@RunWith(classOf[JUnitRunner])
class WskSdkTests extends TestHelpers with WskTestHelpers {
implicit val wskprops = WskProps()
val wsk = new Wsk
behavior of "Wsk SDK"
it should "prefix https to apihost if no scheme given" in {
val result = wsk.cli(Seq("--apihost", "localhost:54321", "sdk", "install", "docker"), expectedExitCode = ERROR_EXIT)
result.stderr should include regex ("""(?i)Get https://localhost:54321/""")
}
it should "not prefix https to http apihost" in {
val result =
wsk.cli(Seq("--apihost", "http://localhost:54321", "sdk", "install", "docker"), expectedExitCode = ERROR_EXIT)
result.stderr should include regex ("""(?i)Get http://localhost:54321/""")
}
it should "not double prefix https to https apihost" in {
val result =
wsk.cli(Seq("--apihost", "https://localhost:54321", "sdk", "install", "docker"), expectedExitCode = ERROR_EXIT)
result.stderr should include regex ("""(?i)Get https://localhost:54321/""")
}
it should "download docker action sdk" in {
val dir = File.createTempFile("wskinstall", ".tmp")
dir.delete()
dir.mkdir() should be(true)
try {
wsk.cli(wskprops.overrides ++ Seq("sdk", "install", "docker"), workingDir = dir).stdout should include(
"The docker skeleton is now installed at the current directory.")
val sdk = new File(dir, "dockerSkeleton")
sdk.exists() should be(true)
sdk.isDirectory() should be(true)
val dockerfile = new File(sdk, "Dockerfile")
dockerfile.exists() should be(true)
dockerfile.isFile() should be(true)
val lines = FileUtils.readLines(dockerfile, StandardCharsets.UTF_8)
// confirm that the image is correct
lines.get(1) shouldBe "FROM openwhisk/dockerskeleton"
val buildAndPushFile = new File(sdk, "buildAndPush.sh")
buildAndPushFile.canExecute() should be(true)
} finally {
FileUtils.deleteDirectory(dir)
}
}
it should "download docker sdk when blackbox.tar.gz exists, and docker name should be quoted in error." in {
val fileName = "blackbox.tar.gz"
val dir = File.createTempFile("wskinstall", ".tmp")
dir.delete()
dir.mkdir() should be(true)
val file = new File(dir, fileName)
file.createNewFile should be (true)
try {
wsk.cli(wskprops.overrides ++ Seq("sdk", "install", "docker"),
workingDir = dir,
expectedExitCode = 1).stderr should include(
s"""The file '${fileName}' already exists. Delete it and retry.""")
} finally {
file.delete()
FileUtils.deleteDirectory(dir)
}
}
it should "download ios sdk when OpenWhiskIOSStarterApp.zip exists, and ios name should be quoted in error." in {
val fileName = "OpenWhiskIOSStarterApp.zip"
val dir = File.createTempFile("wskinstall", ".tmp")
dir.delete()
dir.mkdir() should be(true)
val file = new File(dir, fileName)
file.createNewFile should be (true)
try {
wsk.cli(wskprops.overrides ++ Seq("sdk", "install", "ios"),
workingDir = dir,
expectedExitCode = 1).stderr should include(
s"""The file '${fileName}' already exists. Delete it and retry.""")
} finally {
file.delete()
FileUtils.deleteDirectory(dir)
}
}
it should "download ios sdk, check filename is quoted in error when create fails." in {
val fileName = "OpenWhiskIOSStarterApp.zip"
val dir = File.createTempFile("wskinstall", ".tmp")
dir.delete()
dir.mkdir() should be(true)
Seq("chmod", "555", dir.getAbsolutePath).!!
try {
wsk.cli(wskprops.overrides ++ Seq("sdk", "install", "ios"),
workingDir = dir,
expectedExitCode = 1).stderr should include(
s"""Error creating SDK file '${fileName}':""")
} finally {
FileUtils.deleteDirectory(dir)
}
}
it should "download docker sdk twice, check directory is quoted in error when create fails." in {
val dir = File.createTempFile("wskinstall", ".tmp")
dir.delete()
dir.mkdir() should be(true)
try {
wsk.cli(wskprops.overrides ++ Seq("sdk", "install", "docker"),
workingDir = dir).stdout should include(
s"""The docker skeleton is now installed at the current directory.""")
wsk.cli(wskprops.overrides ++ Seq("sdk", "install", "docker"),
workingDir = dir,
expectedExitCode = 1).stderr should include(
s"""The directory 'dockerSkeleton' already exists. Delete it and retry.""")
} finally {
FileUtils.deleteDirectory(dir)
}
}
it should "download iOS sdk" in {
val dir = File.createTempFile("wskinstall", ".tmp")
dir.delete()
dir.mkdir() should be(true)
wsk.cli(wskprops.overrides ++ Seq("sdk", "install", "iOS"), workingDir = dir).stdout should include(
"Downloaded OpenWhisk iOS starter app. Unzip 'OpenWhiskIOSStarterApp.zip' and open the project in Xcode.")
val sdk = new File(dir, "OpenWhiskIOSStarterApp.zip")
sdk.exists() should be(true)
sdk.isFile() should be(true)
FileUtils.sizeOf(sdk) should be > 20000L
FileUtils.deleteDirectory(dir)
}
it should "install the bash auto-completion bash script" in {
// Use a temp dir for testing to not disturb user's local folder
val dir = File.createTempFile("wskinstall", ".tmp")
dir.delete()
dir.mkdir() should be(true)
val scriptfilename = "wsk_cli_bash_completion.sh"
var scriptfile = new File(dir.getPath(), scriptfilename)
try {
val stdout = wsk.cli(Seq("sdk", "install", "bashauto"), workingDir = dir, expectedExitCode = SUCCESS_EXIT).stdout
stdout should include("is installed in the current directory")
val fileContent = FileUtils.readFileToString(scriptfile, StandardCharsets.UTF_8)
fileContent should include("bash completion for wsk")
} finally {
scriptfile.delete()
FileUtils.deleteDirectory(dir)
}
}
it should "print bash command completion script to STDOUT" in {
val msg = "bash completion for wsk" // Subject to change, dependent on Cobra script
val stdout = wsk.cli(Seq("sdk", "install", "bashauto", "--stdout")).stdout
stdout should include(msg)
}
def verifyMissingSecurityFile(config: String, fileName: String, expectedErrorMessage: String) = {
val tmpwskprops = File.createTempFile("wskprops", ".tmp")
val securityFile = File.createTempFile(fileName, ".pem")
try {
val writer = new BufferedWriter(new FileWriter(tmpwskprops))
writer.write(s"$config=${securityFile.getAbsolutePath()}\n")
writer.close()
val env = Map("WSK_CONFIG_FILE" -> tmpwskprops.getAbsolutePath())
val stderr = wsk
.cli(
Seq("sdk", "install", "docker", "--apihost", wskprops.apihost, "--apiversion", wskprops.apiversion),
env = env,
expectedExitCode = ERROR_EXIT)
.stderr
stderr should include regex (expectedErrorMessage)
} finally {
tmpwskprops.delete()
securityFile.delete()
}
}
it should "return configure the missing Key file" in {
verifyMissingSecurityFile("CERT", "cert", "The Key file is not configured. Please configure the missing Key file.")
}
it should "return configure the missing Cert file" in {
verifyMissingSecurityFile("KEY", "key", "The Cert file is not configured. Please configure the missing Cert file.")
}
it should "return unable to load the X509 key pair with both Cert and Key files missing" in {
val tmpwskprops = File.createTempFile("wskprops", ".tmp")
val certFile = File.createTempFile("cert", ".pem")
val keyFile = File.createTempFile("key", ".pem")
try {
val writer = new BufferedWriter(new FileWriter(tmpwskprops))
writer.write(s"CERT=${certFile.getAbsolutePath()}\n")
writer.write(s"KEY=${keyFile.getAbsolutePath()}\n")
writer.close()
val env = Map("WSK_CONFIG_FILE" -> tmpwskprops.getAbsolutePath())
val stderr = wsk
.cli(
Seq("sdk", "install", "docker", "--apihost", wskprops.apihost, "--apiversion", wskprops.apiversion),
env = env,
expectedExitCode = ERROR_EXIT)
.stderr
stderr should include regex ("""Unable to load the X509 key pair due to the following reason""")
} finally {
tmpwskprops.delete()
certFile.delete()
keyFile.delete()
}
}
}