File: core_test.clj

package info (click to toggle)
trapperkeeper-clojure 4.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: sh: 189; xml: 73; makefile: 25; java: 5
file content (130 lines) | stat: -rw-r--r-- 6,101 bytes parent folder | download
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
(ns puppetlabs.trapperkeeper.core-test
  (:require [clojure.test :refer :all]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.trapperkeeper.app :refer [get-service]]
            [puppetlabs.trapperkeeper.config :as config]
            [puppetlabs.trapperkeeper.internal :refer [parse-cli-args!]]
            [puppetlabs.trapperkeeper.services :refer [service]]
            [puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
            [puppetlabs.trapperkeeper.testutils.logging :as logging]
            [schema.test :as schema-test]
            [slingshot.slingshot :refer [try+]]))

(use-fixtures :each schema-test/validate-schemas logging/reset-logging-config-after-test)

(defprotocol FooService
  (foo [this]))

(deftest dependency-error-handling
  (testing "missing service dependency throws meaningful message and logs error"
    (let [broken-service (service
                           [[:MissingService f]]
                           (init [this context] (f) context))]
      (logging/with-test-logging
        (is (thrown-with-msg?
             RuntimeException #"Service ':MissingService' not found"
             (testutils/bootstrap-services-with-empty-config [broken-service])))
        (is (logged? #"Error during app buildup!" :error)
            "App buildup error message not logged"))))

  (testing "missing service function throws meaningful message and logs error"
    (let [test-service    (service FooService
                            []
                            (foo [this] "foo"))
          broken-service  (service
                            [[:FooService bar]]
                            (init [this context] (bar) context))]
      (logging/with-test-logging
        (is (thrown-with-msg?
             RuntimeException
             #"Service function 'bar' not found in service 'FooService"
             (testutils/bootstrap-services-with-empty-config
              [test-service
               broken-service])))
        (is (logged? #"Error during app buildup!" :error)
            "App buildup error message not logged")))
    (try (macroexpand '(puppetlabs.trapperkeeper.services/service
                       puppetlabs.trapperkeeper.core-test/FooService
                       []
                       (init [this context] context)))
      (catch RuntimeException e
        (let [cause (-> e Throwable->map :cause)]
          (is (re-matches #"Service does not define function 'foo'.*" cause)))))))

(deftest test-main
  (testing "Parsed CLI data"
    (let [bootstrap-file "/fake/path/bootstrap.cfg"
          config-dir     "/fake/config/dir"
          restart-file   "/fake/restart/file"
          cli-data         (parse-cli-args!
                            ["--debug"
                             "--bootstrap-config" bootstrap-file
                             "--config" config-dir
                             "--restart-file" restart-file])]
      (is (= bootstrap-file (cli-data :bootstrap-config)))
      (is (= config-dir (cli-data :config)))
      (is (= restart-file (cli-data :restart-file)))
      (is (cli-data :debug))))

  (testing "Invalid CLI data"
    (let [got-expected-exception (atom false)]
      (try+
        (parse-cli-args! ["--invalid-argument"])
        (catch map? m
          (is (contains? m :kind))
          (is (= :cli-error (ks/without-ns (:kind m))))
          (is (= :puppetlabs.kitchensink.core/cli-error (:kind m)))
          (is (contains? m :msg))
          (is (re-find
               #"Unknown option.*--invalid-argument"
               (m :msg)))
          (reset! got-expected-exception true)))
      (is (true? @got-expected-exception))))

  (testing "TK should allow the user to omit the --config arg"
    ;; Make sure args will be parsed if no --config arg is provided; will throw an exception if not
    (parse-cli-args! [])
    (is (true? true)))

  (testing "TK should use an empty config if none is specified"
    ;; Make sure data will be parsed if no path is provided; will throw an exception if not.
    (config/parse-config-data {})
    (is (true? true))))

(deftest test-cli-args
  (testing "debug mode is off by default"
    (testutils/with-app-with-empty-config app []
      (let [config-service (get-service app :ConfigService)]
        (is (false? (config/get-in-config config-service [:debug]))))))

  (testing "--debug puts TK in debug mode"
    (testutils/with-app-with-cli-args app [] ["--config" testutils/empty-config "--debug"]
      (let [config-service (get-service app :ConfigService)]
        (is (true? (config/get-in-config config-service [:debug]))))))

  (testing "TK should accept --plugins arg"
    ;; Make sure --plugins is allowed; will throw an exception if not.
    (parse-cli-args! ["--config" "yo mama"
                      "--plugins" "some/plugin/directory"])))

(deftest restart-file-config
  (let [tk-config-file-with-restart (ks/temp-file "restart-global" ".conf")
        tk-restart-file "/my/tk-restart-file"
        cli-restart-file "/my/cli-restart-file"]
    (spit tk-config-file-with-restart
          (format "global: {\nrestart-file: %s\n}" tk-restart-file))
    (testing "restart-file setting comes from TK config when CLI arg absent"
      (let [config (config/parse-config-data
                    {:config (str tk-config-file-with-restart)})]
        (is (= tk-restart-file (get-in config [:global :restart-file])))))
    (testing "restart-file setting comes from CLI arg when no TK config setting"
      (let [empty-tk-config-file (ks/temp-file "empty" ".conf")
            config (config/parse-config-data
                    {:config (str empty-tk-config-file)
                     :restart-file cli-restart-file})]
        (is (= cli-restart-file (get-in config [:global :restart-file])))))
    (testing "restart-file setting comes from CLI arg even when set in TK config"
      (let [config (config/parse-config-data
                    {:config (str tk-config-file-with-restart)
                     :restart-file cli-restart-file})]
        (is (= cli-restart-file (get-in config [:global :restart-file])))))))