Skip to content

Commit

Permalink
chore: add ruby ITs (#19)
Browse files Browse the repository at this point in the history
* chore(wip): add ruby ITs

* chore: run rspec

* chore: rename some things

* chore: setup rubocop

* chore: move some things around

* chore: more docs cleanup

* chore: update go runner

* chore: try to fix ruby docker version

* chore: oops forgot rspec

* chore: try to fix ruby IT
  • Loading branch information
markphelps authored Dec 4, 2023
1 parent 1fd66d0 commit 3e27b11
Show file tree
Hide file tree
Showing 24 changed files with 357 additions and 125 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/lint.yml → .github/workflows/lint-sdks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: SDKs
name: Lint SDKs
on:
push:
branches:
Expand All @@ -12,7 +12,7 @@ jobs:
steps:
- name: Checkout Sources
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
Expand All @@ -22,20 +22,20 @@ jobs:
uses: snok/install-poetry@v1
with:
version: 1.7.0

- name: Lint Python source
working-directory: flipt-client-python
run: |
poetry install
poetry run black --check .
lint-typescript:
name: Lint Typescript
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v4

- name: Install Node
uses: actions/setup-node@v4
with:
Expand All @@ -45,4 +45,4 @@ jobs:
working-directory: flipt-client-node
run: |
npm ci
npm run prettier-check
npm run lint
10 changes: 5 additions & 5 deletions .github/workflows/test.yml → .github/workflows/test-sdks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Test SDKs
on:
push:
branches:
Expand All @@ -7,7 +7,7 @@ on:

jobs:
test:
name: Dagger ITs
name: Integration Tests
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
Expand All @@ -18,12 +18,12 @@ jobs:
go-version: "1.21.3"
check-latest: true
cache: true

- name: Install Dagger
run: |
cd /usr/local
curl -L https://dl.dagger.io/dagger/install.sh | DAGGER_VERSION=0.9.3 sh
- name: Run ITs
- name: Run Integration Tests
run: |
dagger run go run ./build
11 changes: 11 additions & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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:

<p align="center">
<img src="./diagrams/architecture.png" alt="Client SDKs Architecture" width="500px" />
</p>
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Client SDKs

The intention of this repo is to centralize the core evaluation logic for Flipt's feature flags, and have thin multi-language wrappers around that logic.
![Status: Experimental](https://img.shields.io/badge/status-experimental-yellow)

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.
This repository centralizes the client-side SDKs for [Flipt](https://github.com/flipt-io/flipt).

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).
These client-side SDKs are responsible for evaluating context and returning the results of the evaluation. They enable developers to easily integrate Flipt into their applications without relying on server-side SDKs.

You can refer to the architecture diagram below:
## Architecture

<img src="./diagrams/architecture.png" alt="Client SDKs Architecture" width="500px" />
The client SDKs are designed to be embedded in end-user applications.

The evaluation logic is written in Rust and can be found in the `flipt-engine` directory. The language clients that are used in end-user applications wrap the engine can be found in the `flipt-client-{language}` directories.

See [ARCHITECTURE.md](./ARCHITECTURE.md).

## Language Support

Expand Down
10 changes: 6 additions & 4 deletions build/README.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
37 changes: 26 additions & 11 deletions build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"log"
"maps"
"os"
"strings"

Expand All @@ -18,6 +19,7 @@ var (
"python": pythonTests,
"go": goTests,
"node": nodeTests,
"ruby": rubyTests,
}
sema = make(chan struct{}, len(languageToFn))
)
Expand All @@ -37,24 +39,22 @@ func main() {
}

func run() error {
var languagesTestsMap = map[string]integrationTestFn{
"python": pythonTests,
"go": goTests,
"node": nodeTests,
}
var tests = make(map[string]integrationTestFn, len(languageToFn))

maps.Copy(tests, languageToFn)

if languages != "" {
l := strings.Split(languages, ",")
tlm := make(map[string]integrationTestFn, len(l))
subset := 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
}
subset[language] = testFn
}

languagesTestsMap = tlm
tests = subset
}

ctx := context.Background()
Expand All @@ -73,7 +73,7 @@ func run() error {

var g errgroup.Group

for _, testFn := range languagesTestsMap {
for _, testFn := range tests {
err := testFn(ctx, client, flipt, dynamicLibrary, headerFile, dir)
if err != nil {
return err
Expand Down Expand Up @@ -182,3 +182,18 @@ 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-bookworm").
WithWorkdir("/app").
WithDirectory("/app", hostDirectory.Directory("flipt-client-ruby")).
WithFile("/app/lib/ext/libfliptengine.so", dynamicLibrary).
WithServiceBinding("flipt", flipt.WithExec(nil).AsService()).
WithEnvVariable("FLIPT_URL", "http://flipt:8080").
WithExec([]string{"bundle", "install"}).
WithExec([]string{"bundle", "exec", "rspec"}).
Sync(ctx)

return err
}
54 changes: 26 additions & 28 deletions flipt-client-go/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flipt Client Go

The `flipt-client-go` directory contains the Golang source code for a Flipt evaluation client using FFI to make calls to a core built in Rust.
The `flipt-client-go` directory contains the Go source code for a Flipt evaluation client.

## Instructions

Expand All @@ -10,7 +10,7 @@ To use this client, you can run the following command from the root of the repos
cargo build --release
```

This should generate a `target/` directory in the root of this repository, which contains the dynamic linking library built for your platform. This dynamic library will contain the functionality necessary for the Golang client to make FFI calls.
This should generate a `target/` directory in the root of this repository, which contains the dynamic linking library built for your platform. This dynamic library will contain the functionality necessary for the Go client to make FFI calls.

You can import the module that contains the evaluation client: `go.flipt.io/flipt/flipt-client-go` and build your Go project with the `CGO_LDFLAGS` environment variable set:

Expand All @@ -26,44 +26,42 @@ The `path/to/lib` will be the path to the dynamic library which will have the fo

You can then use the client like so:

```golang
```go
package main

import (
"context"
"fmt"
"log"
"context"
"fmt"
"log"

evaluation "go.flipt.io/flipt/flipt-client-go"
evaluation "go.flipt.io/flipt/flipt-client-go"
)

func main() {
// The NewClient() accepts options which are the following:
// evaluation.WithNamespace(string): configures which namespace you will be making evaluations on
// evaluation.WithURL(string): configures which upstream Flipt data should be fetched from
// evaluation.WithUpdateInterval(int): configures how often data should be fetched from the upstream
evaluationClient, err := evaluation.NewClient()
if err != nil {
log.Fatal(err)
}
defer evaluationClient.Close()

variantResult, err := evaluationClient.Variant(context.Background(), "flag1", "someentity", map[string]string{
"fizz": "buzz",
})
if err != nil {
log.Fatal(err)
}

fmt.Println(*variantResult.Result)
// The NewClient() accepts options which are the following:
// evaluation.WithNamespace(string): configures which namespace you will be making evaluations on
// evaluation.WithURL(string): configures which upstream Flipt data should be fetched from
// evaluation.WithUpdateInterval(int): configures how often data should be fetched from the upstream
evaluationClient, err := evaluation.NewClient()
if err != nil {
log.Fatal(err)
}
defer evaluationClient.Close()

variantResult, err := evaluationClient.Variant(context.Background(), "flag1", "someentity", map[string]string{
"fizz": "buzz",
})
if err != nil {
log.Fatal(err)
}

fmt.Println(*variantResult.Result)
}
```

## Memory Management

The engine that is allocated on the Rust side to compute evaluations for flag state will not be properly deallocated unless you call the `Close()` method on a `Client` instance, please be sure to do that to avoid leaking memory.

ie:
The engine that is allocated on the Rust side to compute evaluations for flag state will not be properly deallocated unless you call the `Close()` method on a `Client` instance. Please be sure to do this to avoid leaking memory.

```go
defer evaluationClient.Close()
Expand Down
10 changes: 8 additions & 2 deletions flipt-client-node/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flipt Client Node

The `flipt-client-node` directory contains the TypeScript source code for a Flipt evaluation client using FFI to make calls to a core built in Rust.
The `flipt-client-node` directory contains the TypeScript source code for a Flipt evaluation client.

## Instructions

Expand Down Expand Up @@ -41,4 +41,10 @@ console.log(variant);

## Memory Management

Since TypeScript/JavaScript is a garbage collected language there is no concept of "freeing" memory. We had to allocated memory for the engine through the `initialize_engine` FFI call, and with that being said please make sure to call the `freeEngine` method on the `FliptEvaluationClient` class once you are done using it.
Since TypeScript/JavaScript is a garbage collected language there is no concept of "freeing" memory. We have to allocate memory for the engine through the `initialize_engine` FFI call.

Make sure to call the `freeEngine` method on the `FliptEvaluationClient` class once you are done using it.

```typescript
fliptEvaluationClient.freeEngine();
```
8 changes: 4 additions & 4 deletions flipt-client-node/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "flipt-client-node",
"version": "0.0.1",
"description": "Flipt Node Client for evaluations through FFI",
"description": "Flipt Node Client for client-side evaluation",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "",
"license": "ISC",
"license": "MIT",
"scripts": {
"prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write",
"prettier-check": "prettier --check src/**/*.ts",
"fmt": "prettier --config .prettierrc 'src/**/*.ts' --write",
"lint": "prettier --check src/**/*.ts",
"test": "jest"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions flipt-client-python/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flipt Client Python

The `flipt-client-python` directory contains the Python source code for a Flipt evaluation client using FFI to make calls to a core built in Rust.
The `flipt-client-python` directory contains the Python source code for a Flipt evaluation client.

## Instructions

Expand All @@ -10,7 +10,7 @@ To use this client, you can run the following command from the root of the repos
cargo build --release
```

This should generate a `target/` directory in the root of this repository, which contains the dynamic linking library built for your platform. This dynamic library will contain the functionality necessary for the Python client to make FFI calls. You'll need to set the `FLIPT_ENGINE_LIB_PATH` environment variable depending on your platform:
This should generate a `target/` directory in the root of this repository which contains the dynamic linking library built for your platform. This dynamic library will contain the functionality necessary for the Python client to make FFI calls. You'll need to set the `FLIPT_ENGINE_LIB_PATH` environment variable depending on your platform:

- **Linux**: `{REPO_ROOT}/target/release/libfliptengine.so`
- **Windows**: `{REPO_ROOT}/target/release/libfliptengine.dll`
Expand Down
1 change: 1 addition & 0 deletions flipt-client-ruby/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
1 change: 1 addition & 0 deletions flipt-client-ruby/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
33 changes: 33 additions & 0 deletions flipt-client-ruby/.rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-12-03 00:21:30 UTC using RuboCop version 1.58.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: Severity, Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'flipt-client-ruby.gemspec'

# Offense count: 1
Lint/ShadowingOuterLocalVariable:
Exclude:
- 'lib/evaluation.rb'

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 35

# Offense count: 1
# Configuration parameters: AllowedConstants.
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/evaluation.rb'
Loading

0 comments on commit 3e27b11

Please sign in to comment.