<commit_before>(ns puppetlabs.services.jruby.puppet-environments-int-test
  (:require [clojure.test :refer :all]
            [puppetlabs.puppetserver.bootstrap-testutils :as bootstrap]
            [puppetlabs.http.client.sync :as http-client]
            [puppetlabs.services.jruby.jruby-testutils :as jruby-testutils]
            [me.raynes.fs :as fs]
            [cheshire.core :as json]
            [puppetlabs.trapperkeeper.app :as tk-app]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.services.jruby.jruby-puppet-core :as jruby-core]
            [puppetlabs.services.protocols.jruby-puppet :as jruby-protocol]))

(def test-resources-dir
  "./dev-resources/puppetlabs/services/jruby/puppet_environments_int_test")

(use-fixtures :once
              (jruby-testutils/with-puppet-conf
                (fs/file test-resources-dir "puppet.conf")))

(def ca-cert
  (bootstrap/pem-file "certs" "ca.pem"))

(def localhost-cert
  (bootstrap/pem-file "certs" "localhost.pem"))

(def localhost-key
  (bootstrap/pem-file "private_keys" "localhost.pem"))

(def num-jrubies 2)

(defn write-site-pp-file
  [site-pp-contents]
  (let [site-pp-file (fs/file bootstrap/master-conf-dir "environments" "production" "manifests" "site.pp")]
    (fs/mkdirs (fs/parent site-pp-file))
    (spit site-pp-file site-pp-contents)))

(defn write-foo-pp-file
  [foo-pp-contents]
  (let [foo-pp-file (fs/file bootstrap/master-conf-dir
                                  "environments"
                                  "production"
                                  "modules"
                                  "foo"
                                  "manifests"
                                  "init.pp")]
    (fs/mkdirs (fs/parent foo-pp-file))
    (spit foo-pp-file foo-pp-contents)))

(def ssl-request-options
  {:ssl-cert    localhost-cert
   :ssl-key     localhost-key
   :ssl-ca-cert ca-cert})

(def catalog-request-options
  (merge
    ssl-request-options
    {:headers     {"Accept" "pson"}
     :as          :text}))

(defn get-catalog
  "Make an HTTP request to get a catalog."
  []
  (-> (http-client/get
        "https://localhost:8140/puppet/v3/catalog/localhost?environment=production"
        catalog-request-options)
      :body
      json/parse-string))

(defn get-catalog-and-borrow-jruby
  "Gets a catalog, and then borrows a JRuby instance from a pool to ensure that
  a subsequent catalog request will be directed to a different JRuby.  Returns a<commit_msg>(SERVER-876) Add post-catalog helper function<commit_after>(ns puppetlabs.services.jruby.puppet-environments-int-test
  (:require [clojure.test :refer :all]
            [puppetlabs.puppetserver.bootstrap-testutils :as bootstrap]
            [puppetlabs.http.client.sync :as http-client]
            [puppetlabs.services.jruby.jruby-testutils :as jruby-testutils]
            [me.raynes.fs :as fs]
            [cheshire.core :as json]
            [puppetlabs.trapperkeeper.app :as tk-app]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.services.jruby.jruby-puppet-core :as jruby-core]
            [puppetlabs.services.protocols.jruby-puppet :as jruby-protocol]))

(def test-resources-dir
  "./dev-resources/puppetlabs/services/jruby/puppet_environments_int_test")

(use-fixtures :once
              (jruby-testutils/with-puppet-conf
                (fs/file test-resources-dir "puppet.conf")))

(def ca-cert
  (bootstrap/pem-file "certs" "ca.pem"))

(def localhost-cert
  (bootstrap/pem-file "certs" "localhost.pem"))

(def localhost-key
  (bootstrap/pem-file "private_keys" "localhost.pem"))

(def num-jrubies 2)

(defn write-site-pp-file
  [site-pp-contents]
  (let [site-pp-file (fs/file bootstrap/master-conf-dir "environments" "production" "manifests" "site.pp")]
    (fs/mkdirs (fs/parent site-pp-file))
    (spit site-pp-file site-pp-contents)))

(defn write-foo-pp-file
  [foo-pp-contents]
  (let [foo-pp-file (fs/file bootstrap/master-conf-dir
                                  "environments"
                                  "production"
                                  "modules"
                                  "foo"
                                  "manifests"
                                  "init.pp")]
    (fs/mkdirs (fs/parent foo-pp-file))
    (spit foo-pp-file foo-pp-contents)))

(def ssl-request-options
  {:ssl-cert    localhost-cert
   :ssl-key     localhost-key
   :ssl-ca-cert ca-cert})

(def catalog-request-options
  (merge
    ssl-request-options
    {:headers     {"Accept" "pson"}
     :as          :text}))

(defn get-catalog
  "Make an HTTP request to get a catalog."
  []
  (-> (http-client/get
        "https://localhost:8140/puppet/v3/catalog/localhost?environment=production"
        catalog-request-options)
      :body
      json/parse-string))

(defn post-catalog
  "Make an HTTP post request for a catalog."
  []
  (-> (http-client/post
       "https://localhost:8140/puppet/v3/catalog/localhost"
       (assoc-in (assoc catalog-request-options
                   :body "environment=production")
                 [:headers "Content-Type"] "application/x-www-form-urlencoded"))
      :body
      json/parse-string))

(defn get-catalog-and-borrow-jruby
  "Gets a catalog, and then borrows a JRuby instance from a pool to ensure that
  a subsequent catalog request will be directed to a different JRuby.  Returns a
{'fraction_non_alphanumeric': 0.1279373368146214, 'fraction_numerical': 0.003729951510630362, 'mean_word_length': 2.2248947684906795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112145', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1780, 'n_words': 341}