Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Add a test for json_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lutter committed Jun 15, 2017
1 parent d12f67f commit e4b899a
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
79 changes: 79 additions & 0 deletions lib/tests/data/providers/json.prov
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#! /usr/bin/env mruby
# -*- ruby -*-

# This test provider does not use our Ruby provider framework because it
# needs to generate errors that the framework would shield us from

def describe
puts <<EOS
---
provider:
type: json
desc: |
Test provider for the JSON provider harness
invoke: json
actions: [set, get]
suitable: true
attributes:
name:
ensure:
type: enum[absent, present]
message:
EOS
end

ALL = [ "error_no_message",
"error_no_kind", "error_message_is_array" ].map do |name|
r = { name: name, ensure: "present" }
if name != "error_no_message"
r["message"] = name
end
r
end

ERRORS = {
"error_no_message" => { error: { kind: "failed" } },
"error_no_kind" => { error: { message: "error_no_kind" } },
"error_message_is_array" => { error:
{ message: [ "error_message_is_array", "more stuff" ],
kind: "failed" } }
}

def get(names)
if names.size > 0
name = names[0]
if err = ERRORS[name]
return err
else
return { resources: [ { name: name, ensure: "present" } ] }
end
else
{ resources: ALL }
end
end

def set(upds)
upd=upds[0]
name = upd.name.sub("set_", "")
if err = ERRORS[name]
return err
end
end

# main
action=ARGV[0].split("=").last

if action == "describe"
describe
elsif action == "get"
inp = JSON.load($stdin)
puts JSON.dump(get(inp["names"]))
elsif action == "set"
inp = JSON.load($stdin)
upds = inp["updates"].map do |upd|
Ral::Update.new(upd["name"], upd["is"], upd["should"])
end
puts JSON.dump(set(upds))
else
puts JSON.dump({ error: { message: "Unknown action #{action}" } })
end
52 changes: 52 additions & 0 deletions lib/tests/json_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <catch.hpp>
#include <libral/ral.hpp>
#include <iostream>
#include <memory>

#include <boost/optional/optional_io.hpp>
#include "boost/filesystem.hpp"

#include "fixtures.hpp"

namespace libral {
SCENARIO("json_provider harness") {
auto aral = ral::create({ TEST_DATA_DIR });
auto json_type = *aral->find_type("json");

SECTION("behaves with invalid error messages") {
auto all = json_type->instances();
REQUIRE(all.is_ok());

for(const auto &r0 : all.ok()) {
auto res = json_type->find(r0.name());
REQUIRE(res.is_err());

// Every error message needs to contain the provider name
REQUIRE_THAT(res.err().detail, Catch::Contains("[json::json]"));
// The resources tell us in "message" if there are specific things
// we should look for in the error message
auto msg = r0.lookup<std::string>("message");
if (msg) {
REQUIRE_THAT(res.err().detail, Catch::Contains(*msg));
}
}

// Now tickle the same error via an update
for(const auto &r0 : all.ok()) {
// The resource names for set need to be prefixed with 'set_' so we
// do not trip the provider's get which happens before set is
// called
auto should = json_type->prov().create("set_" + r0.name());
should["ensure"] = "absent";
auto res = json_type->set(should);
REQUIRE(res.is_err());
REQUIRE_THAT(res.err().detail, Catch::Contains("[json::json]"));
auto msg = r0.lookup<std::string>("message");
if (msg) {
REQUIRE_THAT(res.err().detail, Catch::Contains(*msg));
}
}
}

}
}

0 comments on commit e4b899a

Please sign in to comment.