aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/dependency/deployment.lux
blob: ef6d7a4e7466f781a29c211ae3b34df98cab99bf (plain)
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
(.using
  [library
   [lux "*"
    ["_" test {"+" Test}]
    [abstract
     [monad {"+" do}]
     ["[0]" hash {"+" Hash}]]
    [control
     ["[0]" io {"+" IO}]
     ["[0]" maybe ("[1]#[0]" functor)]
     ["[0]" try ("[1]#[0]" functor)]
     [concurrency
      ["[0]" atom {"+" Atom}]
      ["[0]" async]]]
    [data
     ["[0]" product]
     ["[0]" binary {"+" Binary} ("[1]#[0]" equivalence)]
     ["[0]" text
      ["%" format {"+" format}]]
     [collection
      ["[0]" dictionary {"+" Dictionary}]
      ["[0]" set]
      ["[0]" list ("[1]#[0]" mix)]]]
    [math
     ["[0]" random {"+" Random}]
     [number
      ["n" nat]]]
    [world
     [net {"+" URL}
      ["[0]" uri {"+" URI}]
      ["[0]" http "_"
       ["[1]" client]
       ["[1]/[0]" status]
       ["@[1]" /]]]]]]
  ["$[0]" //
   ["[1]/" // "_"
    ["[1][0]" package]]]
  [\\program
   ["[0]" /
    [// {"+" Dependency}
     ["[0]" resolution]
     [//
      ["[0]" profile]
      ["[0]" metadata]
      ["[0]" package {"+" Package}]
      ["[0]" artifact {"+" Artifact} ("[1]#[0]" equivalence)
       ["[1]/[0]" type]
       ["[1]/[0]" extension]]
      ["[0]" repository
       ["[0]" remote]]]]]])

(def: good_upload
  (@http.Response IO)
  [http/status.created
   [#@http.headers (http.headers (list))
    #@http.body (function (_ _)
                  (|> [0 (binary.empty 0)]
                      {try.#Success}
                      io.io))]])

(type: Cache
  (Atom (Dictionary URL Binary)))

(def: (http cache)
  (-> Cache (http.Client IO))
  (implementation
   (def: (request method url headers input)
     (do io.monad
       [_ (: (IO Any)
             (case [method input]
               [{#@http.Put} {.#Some input}]
               (atom.update! (dictionary.has url input) cache)
               
               _
               (in [])))]
       (in {try.#Success ..good_upload})))))

(def: (verify_one expected_deployments address package cache expected_artifact actual_artifact)
  (-> Nat URL Package (Dictionary URL Binary) Artifact Artifact Bit)
  (let [url (: (-> URI URL)
               (|>> (format address)))
        library_url (url (format (artifact.uri (value@ artifact.#version expected_artifact)
                                               expected_artifact)
                                 artifact/extension.lux_library))
        pom_url (url (format (artifact.uri (value@ artifact.#version expected_artifact)
                                           expected_artifact)
                             artifact/extension.pom))
        artifact_metadata_url (url (metadata.remote_artifact_uri expected_artifact))
        project_metadata_url (url (metadata.remote_project_uri expected_artifact))

        expected_library (|> package
                             (value@ package.#library)
                             product.left)
        expected_pom (|> package
                         (value@ package.#pom)
                         product.right
                         product.left)

        correct_artifact!
        (artifact#= expected_artifact actual_artifact)

        expected_number_of_uploads!
        (n.= (n.* expected_deployments 8)
             (dictionary.size cache))

        correct_library_upload!
        (and (|> cache
                 (dictionary.value library_url)
                 (maybe#each (binary#= expected_library))
                 (maybe.else false))
             (dictionary.key? cache (format library_url artifact/extension.sha-1))
             (dictionary.key? cache (format library_url artifact/extension.md5)))

        correct_pom_upload!
        (and (|> cache
                 (dictionary.value pom_url)
                 (maybe#each (binary#= expected_pom))
                 (maybe.else false))
             (dictionary.key? cache (format pom_url artifact/extension.sha-1))
             (dictionary.key? cache (format pom_url artifact/extension.md5)))

        artifact_metadata_upload!
        (dictionary.key? cache artifact_metadata_url)

        project_metadata_upload!
        (dictionary.key? cache project_metadata_url)]
    (and correct_artifact!
         expected_number_of_uploads!
         correct_library_upload!
         correct_pom_upload!
         artifact_metadata_upload!
         project_metadata_upload!)))

(def: bundle
  (Random [Dependency Artifact Package])
  (do random.monad
    [[profile package] $///package.random
     .let [artifact (|> profile
                        (value@ profile.#identity)
                        maybe.trusted)
           dependency (: Dependency
                         [artifact
                          artifact/type.lux_library])]]
    (in [dependency artifact package])))

(def: .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [address (# ! each (text.suffix uri.separator)
                    (random.ascii/upper 10))]
        ($_ _.and
            (do [! random.monad]
              [[dependency expected_artifact package] ..bundle
               .let [cache (: Cache
                              (atom.atom (dictionary.empty text.hash)))
                     http (..http cache)
                     repository (repository.async (remote.repository http {.#None} address))]]
              (in (do async.monad
                    [?outcome (/.one repository dependency package)
                     cache (async.future (atom.read! cache))]
                    (_.cover' [/.one]
                              (|> ?outcome
                                  (try#each (verify_one 1 address package cache expected_artifact))
                                  (try.else false))))))
            (do [! random.monad]
              [.let [hash (: (Hash [Dependency Artifact Package])
                             (# hash.functor each (|>> product.right product.left product.left)
                                text.hash))]
               num_bundles (# ! each (n.% 10) random.nat)
               bundles (|> ..bundle
                           (random.set hash num_bundles)
                           (# ! each set.list))
               .let [resolution (list#mix (function (_ [dependency expected_artifact package] resolution)
                                            (dictionary.has dependency package resolution))
                                          resolution.empty
                                          bundles)
                     cache (: Cache
                              (atom.atom (dictionary.empty text.hash)))
                     http (..http cache)
                     repository (repository.async (remote.repository http {.#None} address))]]
              (in (do async.monad
                    [?outcome (/.all repository resolution)
                     cache (async.future (atom.read! cache))]
                    (_.cover' [/.all]
                              (|> ?outcome
                                  (try#each (function (_ actual_artifacts)
                                              (let [expected_deployments!
                                                    (n.= num_bundles (set.size actual_artifacts))

                                                    every_deployment_was_correct!
                                                    (list.every? (function (_ [dependency expected_artifact package])
                                                                   (let [deployed!
                                                                         (set.member? actual_artifacts expected_artifact)

                                                                         deployed_correctly!
                                                                         (verify_one num_bundles address package cache expected_artifact expected_artifact)]
                                                                     (and deployed!
                                                                          deployed_correctly!)))
                                                                 bundles)]
                                                (and expected_deployments!
                                                     every_deployment_was_correct!))))
                                  (try.else false))))))
            ))))