 (ns puppetlabs.puppetdb.integration.exported-resources
   (:require [clojure.test :refer :all]
             [puppetlabs.puppetdb.integration.fixtures :as int]
-            [me.raynes.fs :as fs]))
-
-(defn manifest-for [certname]
-  (str "@@notify { 'Hello from " certname "': }\n"
-       "Notify <<| |>>"))
+            [slingshot.test]))
 
 (deftest ^:integration basic-collection
   (with-open [pg (int/setup-postgres)
               pdb (int/run-puppetdb pg {})
               ps (int/run-puppet-server [pdb] {})]
-    (let [agents ["agent-1" "agent-2"]]
+    (let [agents ["agent-1" "agent-2"]
+          manifest-for (fn manifest-for [certname]
+                         (str "@@notify { 'Hello from " certname "': }\n"
+                              "Notify <<| |>>"))]
       (testing "Run with an exported resource first"
         (doseq [agent agents]
           (int/run-puppet-as agent ps pdb (manifest-for agent))))

                           (re-find #"duplicate resource was found while collecting exported resources"
                                    (get-in % [:result :err])))
                      (int/run-puppet-as "collector" ps pdb "Notify <<| title == 'DUPE NOTIFY' |>>")))))))



(deftest ^:integration deactivated-nodes
  (with-open [pg (int/setup-postgres)

  (with-open [pg (int/setup-postgres)
              pdb (int/run-puppetdb pg {})
              ps (int/run-puppet-server [pdb] {})]
    (let [test-collection (fn test-collection [manifest regexes-to-check]
                            (let [{:keys [out]} (int/run-puppet-as "collector" ps pdb manifest)]
                              (doseq [r regexes-to-check]
                                (is (re-find r out)))))]

      (testing "Run puppet to create resources for collection"
        (int/run-puppet-as "exporter" ps pdb
                           (str "@@notify { 'message-a': name => 'a', tag => 'here' }"
                                "@@notify { 'message-b': name => 'b', tag => 'there' }"
                                "@@notify { 'message-c': name => 'c', tag => ['here', 'there'] }"
                                "@@notify { 'message-d': name => 'd' }")))

      (testing "= query"
        (test-collection "Notify <<| name == 'a' |>>"
                         [#"message-a"]))

      (testing "!= query"
        (test-collection "Notify <<| name != 'a' |>>"
                         [#"message-b" #"message-c" #"message-d"]))

      (testing "'or' query"
        (test-collection "Notify <<| name == 'a' or name == 'b' |>>"
                         [#"message-a" #"message-b"]))

      (testing "'and' query"
        (test-collection "Notify <<| title == 'message-a' and name == 'a' |>> "
                         [#"message-a"]))

      (testing "nested query"
        (test-collection (str "Notify <<| (title == 'message-a' or title == 'message-b') and "
                              "           (name == 'a' or name == 'b') |>>")
                         [#"message-a" #"message-b"]))

      (testing "title query"
        (test-collection "Notify <<| title == 'message-a' |>>"
                         [#"message-a"]))

      (testing "title query when nothing matches"
        (test-collection "Notify <<| title == 'message-q' |>>"
                         []))

      (testing "title query with uri-invalid characters"
        (test-collection "Notify <<| title != 'a string with spaces and & and ? in it' |>>"
                         [#"message-a" #"message-b" #"message-c" #"message-d"]))

      (testing "tag query"
        (test-collection "Notify <<| tag == 'here'|>> "
                         [#"message-a" #"message-c"]))

      (testing "inverse tag query"
        (test-collection "Notify <<| tag != 'here'|>> "
                         [#"message-b" #"message-d"]))

      (testing "tag queries should be case-insensitive"
        (test-collection "Notify <<| tag == 'HERE'|>> "
                         [#"message-a" #"message-c"])))))

