diff --git a/README.md b/README.md index 3030e88b..42d8e738 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,11 @@ The intention of this repo is to centralize the core evaluation logic for Flipt' The evaluation logic is written in Rust and can be found in the `flipt-engine` directory. The language clients that wrap the engine can be found in the `flipt-client-{language}` directories. -The Rust core is compiled down into a dynamically-linked library and the language clients are able to access that logic through [FFI (foreign function interface)](https://levelup.gitconnected.com/what-is-ffi-foreign-function-interface-an-intuitive-explanation-7327444e347a). +## Architecture + +The `flipt-engine` is a Rust library responsible for evaluating context and returning the results of the evaluation. The client engine polls for evaluation state from the Flipt server and uses this state to determine the results of the evaluation. The client engine is designed to be embedded in the native language client SDKs. The native language client SDKs will send context to the client engine via [FFI](https://en.wikipedia.org/wiki/Foreign_function_interface) and receive the results of the evaluation from engine. + +This design allows for the client evaluation logic to be written once in a memory safe language and embedded in the native language client SDKs. This design also allows for the client engine to be updated independently of the native language client SDKs. You can refer to the architecture diagram below: diff --git a/build/README.md b/build/README.md index 12a09c3a..77b51c64 100644 --- a/build/README.md +++ b/build/README.md @@ -1,14 +1,16 @@ -## Integration Testing +# Integration Tests -The different languages clients should all have an integration test suite that is dependent on a dynamic library being present somewhere and a running instance of Flipt. In the `build/` directory we will use [Dagger](https://dagger.io/) to orchestrate setting up the dependencies for running the test suites for the different languages. +The different languages clients should all have an integration test suite that is dependent on the dynamic library being present somewhere and a running instance of Flipt. -### Requirements +In the `build/` directory we will use [Dagger](https://dagger.io/) to orchestrate setting up the dependencies for running the test suites for the different languages. + +## Requirements Make sure you have `dagger` installed. This module is pinned to `v0.9.3` currently. Here are the [installation instructions](https://docs.dagger.io/quickstart/729236/cli) for `dagger`. -### How to run tests? +## Running Tests From the root of this repository you can run: diff --git a/build/main.go b/build/main.go index 5b3f8cd9..89fd1a99 100644 --- a/build/main.go +++ b/build/main.go @@ -18,6 +18,7 @@ var ( "python": pythonTests, "go": goTests, "node": nodeTests, + "ruby": rubyTests, } sema = make(chan struct{}, len(languageToFn)) ) @@ -47,11 +48,11 @@ func run() error { l := strings.Split(languages, ",") tlm := make(map[string]integrationTestFn, len(l)) for _, language := range l { - if testFn, ok := languageToFn[language]; !ok { + testFn, ok := languageToFn[language] + if !ok { return fmt.Errorf("language %s is not supported", language) - } else { - tlm[language] = testFn } + tlm[language] = testFn } languagesTestsMap = tlm @@ -182,3 +183,19 @@ func nodeTests(ctx context.Context, client *dagger.Client, flipt *dagger.Contain return err } + +// rubyTests runs the ruby integration test suite against a container running Flipt. +func rubyTests(ctx context.Context, client *dagger.Client, flipt *dagger.Container, dynamicLibrary *dagger.File, _ *dagger.File, hostDirectory *dagger.Directory) error { + _, err := client.Container().From("ruby:3.1.0-bookworm"). + WithWorkdir("/app"). + WithDirectory("/app", hostDirectory.Directory("flipt-client-ruby")). + WithFile("/app/libfliptengine.so", dynamicLibrary). + WithServiceBinding("flipt", flipt.WithExec(nil).AsService()). + WithEnvVariable("FLIPT_URL", "http://flipt:8080"). + WithEnvVariable("FLIPT_ENGINE_LIB_PATH", "/app/libfliptengine.so"). + WithExec([]string{"bundle", "install"}). + WithExec([]string{"bundle", "exec", "rake", "test"}). + Sync(ctx) + + return err +} diff --git a/flipt-client-ruby/.rspec b/flipt-client-ruby/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/flipt-client-ruby/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/flipt-client-ruby/spec/evaluation_spec.rb b/flipt-client-ruby/spec/evaluation_spec.rb new file mode 100644 index 00000000..fce87112 --- /dev/null +++ b/flipt-client-ruby/spec/evaluation_spec.rb @@ -0,0 +1,45 @@ +require_relative '../lib/evaluation' + +RSpec.describe Flipt::EvaluationClient do + describe "#initialize" do + it "initializes the engine" do + url = ENV.fetch("FLIPT_URL", "http://localhost:8080") + client = Flipt::EvaluationClient.new("default", {url: url}) + expect(client).to be_a(Flipt::EvaluationClient) + end + end + + describe "#variant" do + it "returns a variant" do + url = ENV.fetch("FLIPT_URL", "http://localhost:8080") + client = Flipt::EvaluationClient.new("default", {url: url}) + + resp = client.variant({flag_key: "flag1", entity_id: "someentity", context: {"fizz":"buzz"}}) + + expect(resp).to_not be_nil + expect(resp["status"]).to eq("success") + expect(resp["error_message"]).to be_nil + expect(resp["result"]["flag_key"]).to eq("flag1") + expect(resp["result"]["match"]).to eq(true) + expect(resp["result"]["reason"]).to eq("MATCH_EVALUATION_REASON") + expect(resp["result"]["variant_key"]).to eq("variant1") + expect(resp["result"]["segment_keys"]).to eq(["segment1"]) + end + end + + describe "#boolean" do + it "returns a boolean" do + url = ENV.fetch("FLIPT_URL", "http://localhost:8080") + client = Flipt::EvaluationClient.new("default", {url: url}) + + resp = client.boolean({flag_key: "flag_boolean", entity_id: "someentity", context: {"fizz":"buzz"}}) + + expect(resp).to_not be_nil + expect(resp["status"]).to eq("success") + expect(resp["error_message"]).to be_nil + expect(resp["result"]["flag_key"]).to eq("flag_boolean") + expect(resp["result"]["enabled"]).to eq(true) + expect(resp["result"]["reason"]).to eq("MATCH_EVALUATION_REASON") + end + end +end \ No newline at end of file diff --git a/flipt-client-ruby/spec/spec_helper.rb b/flipt-client-ruby/spec/spec_helper.rb new file mode 100644 index 00000000..c80d44b9 --- /dev/null +++ b/flipt-client-ruby/spec/spec_helper.rb @@ -0,0 +1,98 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/ + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/flipt-engine/README.md b/flipt-engine/README.md index b5da4dad..b7982692 100644 --- a/flipt-engine/README.md +++ b/flipt-engine/README.md @@ -4,14 +4,6 @@ This is the client engine for Flipt. It is responsible for evaluating context provided by the native language client SDKs and returning the results of the evaluation. -## Architecture - -The client engine is a Rust library responsible for evaluating context and returning the results of the evaluation. The client engine polls for evaluation state from the Flipt server and uses this state to determine the results of the evaluation. The client engine is designed to be embedded in the native language client SDKs. The native language client SDKs will send context to the client engine via [FFI](https://en.wikipedia.org/wiki/Foreign_function_interface) and receive the results of the evaluation from engine. - -This design allows for the client evaluation logic to be written once in a memory safe language and embedded in the native language client SDKs. This design also allows for the client engine to be updated independently of the native language client SDKs. - -TODO: Diagram - ## Development TODO: Add more details