This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
script.clj
executable file
·150 lines (131 loc) · 6.24 KB
/
script.clj
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
#!/usr/bin/env bb
(ns script
(:require
[babashka.pods :as pods]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :as t :refer [deftest is testing]]))
(defmethod clojure.test/report :begin-test-var [m]
(println "===" (-> m :var meta :name))
(println))
(if (= "executable" (System/getProperty "org.graalvm.nativeimage.kind"))
(do
(println "Running native tests")
(pods/load-pod "./pod-babashka-aws"))
(do
(println "Running JVM tests")
(pods/load-pod ["clojure" "-M" "-m" "pod.babashka.aws"])))
(require '[pod.babashka.aws :as aws])
;; TODO maybe less implicit config for this
(defn localstack? []
(= "test" (System/getenv "AWS_ACCESS_KEY_ID") (System/getenv "AWS_SECRET_ACCESS_KEY")))
(def localstack-endpoint {:protocol :http :hostname "localhost" :port 4566})
(def region (System/getenv "AWS_REGION"))
(def s3 (aws/client (merge
{:api :s3 :region (or region "eu-central-1") }
(when (localstack?)
{:endpoint-override localstack-endpoint}))))
(def lambda (aws/client (merge
{:api :lambda :region (or region "eu-central-1") }
(when (localstack?)
{:endpoint-override localstack-endpoint}))))
(deftest aws-ops-test
(is (contains? (aws/ops s3) :ListBuckets)))
(deftest aws-doc-test
(is (str/includes?
(with-out-str (aws/doc s3 :ListBuckets))
"Returns a list of all buckets")))
(deftest aws-invoke-test
;; tests cannot be conditionally defined in bb currently, see #705, so moved
;; the conditions inside the test
(if (and (System/getenv "AWS_ACCESS_KEY_ID")
(System/getenv "AWS_SECRET_ACCESS_KEY")
region)
(do (is (= (keys (aws/invoke s3 {:op :ListBuckets})) [:Buckets :Owner]))
(let [bucket-name (str "pod-bb-aws-test-" (java.util.UUID/randomUUID))
png (java.nio.file.Files/readAllBytes
(.toPath (io/file "resources" "babashka.png")))
;; ensure bucket, ignore error if it already exists
_bucket-resp (aws/invoke
s3
{:op :CreateBucket
:request {:Bucket bucket-name
:CreateBucketConfiguration {:LocationConstraint region}}})
put1 (aws/invoke s3 {:op :PutObject
:request {:Bucket bucket-name
:Key "logo.png"
:Body png}})
_ (is (not (:Error put1)))
get1 (aws/invoke s3 {:op :GetObject
:request {:Bucket bucket-name
:Key "logo.png"}})
read-bytes (fn [is]
(let [baos (java.io.ByteArrayOutputStream.)]
(io/copy is baos)
(.toByteArray baos)))
bytes (read-bytes (:Body get1))
_ (is (= (count png) (count bytes)))
put2 (testing "inputstream arg"
(aws/invoke s3 {:op :PutObject
:request {:Bucket bucket-name
:Key "logo.png"
:Body (io/input-stream
(io/file "resources" "babashka.png"))}}))
_ (is (not (:Error put2)))
get2 (aws/invoke s3 {:op :GetObject
:request {:Bucket bucket-name
:Key "logo.png"}})
bytes (read-bytes (:Body get2))
_ (is (= (count png) (count bytes)))
put3 (testing "file arg"
(aws/invoke s3 {:op :PutObject
:request {:Bucket bucket-name
:Key "logo.png"
:Body (io/file "resources" "babashka.png")}}))
_ (is (not (:Error put3)))
get3 (aws/invoke s3 {:op :GetObject
:request {:Bucket bucket-name
:Key "logo.png"}})
bytes (read-bytes (:Body get3))
_ (is (= (count png) (count bytes)))
lambda-resp (aws/invoke lambda {:op :ListFunctions})
_ (is (:Functions lambda-resp))
_ (is (not (:Error (aws/invoke s3 {:op :DeleteObject
:request {:Bucket bucket-name
:Key "logo.png"}}))))
_ (is (not (:Error (aws/invoke s3 {:op :DeleteBucket
:request {:Bucket bucket-name}}))))]
:the-end))
(println "Skipping credential test")))
(deftest no-such-service-test
(is (thrown-with-msg?
Exception #"api :some-typo not available"
(aws/client {:api :some-typo}))))
(def services (edn/read-string (slurp "resources/aws-services.edn")))
(deftest all-services-test
(testing "all clients of all available services"
(doseq [service services]
(is (= service (do (aws/client {:api service})
service))))))
(require '[pod.babashka.aws.logging :as logging])
(deftest logging-test
(logging/set-level! :info)
(let [s3 (aws/client (merge
{:api :s3 :region (or region "eu-central-1") }
(when (localstack?)
{:endpoint-override localstack-endpoint})))]
(aws/invoke s3 {:op :ListBuckets}))
(logging/set-level! :warn))
(deftest throwable-test
(let [s3 (aws/client (merge
{:api :s3}
(when (localstack?)
{:endpoint-override localstack-endpoint})))]
(is (aws/invoke s3 {:op :GetObject
:request {:Bucket "pod-babashka-aws"
:Key "logo.png"}}))))
(when-not (= "executable" (System/getProperty "org.graalvm.nativeimage.kind"))
(shutdown-agents))
(let [{:keys [:fail :error]} (t/run-tests)]
(System/exit (+ fail error)))