Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade latest sdks #62

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 125 additions & 110 deletions src/from-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,204 +1,219 @@
import specs from '@unleash/client-specification/specifications/index.json'
import got from 'got'
import YAML from 'yaml'
import fs from 'fs'
import { GenericContainer, Network, StartedNetwork } from 'testcontainers'
import specs from "@unleash/client-specification/specifications/index.json";
import got from "got";
import YAML from "yaml";
import fs from "fs";
import { GenericContainer, Network, StartedNetwork } from "testcontainers";
import {
MockServerConfig,
OSSServerConfig,
parseResult,
TestConfiguration,
TestYamlConfig
} from './lib/Config'
import { ContainerInstance, UnleashServerInterface } from './lib/BaseContainers'
import { SDKOptions } from './lib/SDKContainers'
TestYamlConfig,
} from "./lib/Config";
import {
ContainerInstance,
UnleashServerInterface,
} from "./lib/BaseContainers";
import { SDKOptions } from "./lib/SDKContainers";

const path = require("path");
const rawConfig = fs.readFileSync(
`./src/${process.env.CONFIG || 'all-against-mock'}.yaml`,
'utf8'
)
const parsedConfig = YAML.parse(rawConfig) as TestYamlConfig
`./src/${process.env.CONFIG || "all-against-mock"}.yaml`,
"utf8"
);
const parsedConfig = YAML.parse(rawConfig) as TestYamlConfig;

const sdks = process.env.SDK
? [{ name: process.env.SDK, type: process.env.SDK }]
: parsedConfig.sdks.map(c => {
c.name = c.name || c.type
return c
})
: parsedConfig.sdks.map((c) => {
c.name = c.name || c.type;
return c;
});

const servers = process.env.SERVER
? [{ type: process.env.SERVER }]
: parsedConfig.servers
: parsedConfig.servers;

const testDetails = parsedConfig.tests.load
const testDetails = parsedConfig.tests.load;

const excludeTests = testDetails.excluding || []
const excludeTests = testDetails.excluding || [];

function parseConfig(
server: OSSServerConfig | MockServerConfig
): TestConfiguration {
let config: TestConfiguration
if (server.type === 'OSSServer') {
const ossServer = server as OSSServerConfig
let config: TestConfiguration;
if (server.type === "OSSServer") {
const ossServer = server as OSSServerConfig;
config = {
serverImpl: ossServer.type,
postgres: {
image: ossServer.postgresImage,
dbName: 'unleash',
user: 'unleash_user',
password: 'unleash.the.password'
dbName: "unleash",
user: "unleash_user",
password: "unleash.the.password",
},
unleash: {
image: ossServer.image,
clientToken: parsedConfig.clientToken,
adminToken: parsedConfig.adminToken
}
}
} else if (server.type === 'MockServer' || server.type === 'Edge') {
adminToken: parsedConfig.adminToken,
},
};
} else if (server.type === "MockServer" || server.type === "Edge") {
// This is pointless, we should remove TestConfiguration
config = {
serverImpl: server.type,
postgres: {
image: '',
dbName: '',
user: '',
password: ''
image: "",
dbName: "",
user: "",
password: "",
},
unleash: {
image: '',
image: "",
clientToken: parsedConfig.clientToken,
adminToken: parsedConfig.adminToken
}
}
adminToken: parsedConfig.adminToken,
},
};
} else {
throw new Error(`Invalid server config type ${server.type}`)
throw new Error(`Invalid server config type ${server.type}`);
}
return config
return config;
}

function partition<T>(arr: T[], check: (o: T) => boolean): T[][] {
let initialValue: T[][] = [[], []]
let initialValue: T[][] = [[], []];
return arr.reduce((result, element) => {
result[check(element) ? 0 : 1].push(element)
return result
}, initialValue)
result[check(element) ? 0 : 1].push(element);
return result;
}, initialValue);
}

const tests: string[] = specs.filter(
spec => !excludeTests.includes(spec.slice(0, 2))
)

describe.each(servers)(`$type`, server => {
let unleashServer: ContainerInstance & UnleashServerInterface
let network: StartedNetwork
let initialized = false
let sdkContainers = new Map<string, ContainerInstance>()
let config = parseConfig(server)
describe.each(tests)(`%s suite`, testName => {
(spec) => !excludeTests.includes(spec.slice(0, 2))
);

describe.each(servers)(`$type`, (server) => {
let unleashServer: ContainerInstance & UnleashServerInterface;
let network: StartedNetwork;
let initialized = false;
let sdkContainers = new Map<string, ContainerInstance>();
let config = parseConfig(server);
describe.each(tests)(`%s suite`, (testName) => {
// eslint-disable-next-line
const definition: ISpecDefinition = require(`@unleash/client-specification/specifications/${testName}`)
const definition: ISpecDefinition = require(`@unleash/client-specification/specifications/${testName}`);

beforeAll(async () => {
if (!initialized) {
console.log(
`===== Initializing Unleash ${server.type} ${definition.name} =====`
)
network = await new Network().start()
);
network = await new Network().start();

if (process.env.DEBUG) {
const ngrepContainer = await GenericContainer
.fromDockerfile(path.resolve('./src/tools/ngrep'), 'Dockerfile')
.build()
const ngrepContainer = await GenericContainer.fromDockerfile(
path.resolve("./src/tools/ngrep"),
"Dockerfile"
).build();
await ngrepContainer
.withNetworkMode("host") // bind to host network cause it will listen to docker bridged network
.withBindMount(path.resolve('.'), '/output')
.withBindMount(path.resolve("."), "/output")
.withCmd([
"-q", // Be quiet; don't output any information other than packet headers and their payloads (if relevant).
"-t", // Print a timestamp in the form of YYYY/MM/DD HH:MM:SS.UUUUUU everytime a packet is matched.
"-l", // Make stdout line buffered
"-Wbyline", // Specify alternate manner for displaying packets
`-dbr-${network.getId().slice(0, 12)}`, // listen to the bridge interface created for this network id
".*HTTP.*", // match only HTTP
"/output/ngrep-http.out" // this last parameter will be used as output file
"/output/ngrep-http.out", // this last parameter will be used as output file
])
.start()
.start();
}

unleashServer = require('./servers/index').create(config, network)
await unleashServer.initialize()
initialized = true
unleashServer = require("./servers/index").create(config, network);
await unleashServer.initialize();
initialized = true;
} else {
console.log(
`===== Reseting Unleash ${server.type} before ${definition.name} =====`
)
await unleashServer.reset()
);
await unleashServer.reset();
}

// ========= set unleash state (~50ms)
let succeed = await unleashServer.setState(definition.state)
expect(succeed).toBeTruthy()
})
let succeed = await unleashServer.setState(definition.state);
expect(succeed).toBeTruthy();
});

describe.each(sdks)(`$name SDK`, sdkTestConfig => {
const excludedForSDK = sdkTestConfig.excluding || []
let sdkUrl: string
describe.each(sdks)(`$name SDK`, (sdkTestConfig) => {
const excludedForSDK = sdkTestConfig.excluding || [];
let sdkUrl: string;

console.log(`${JSON.stringify(sdkTestConfig)}`);
beforeAll(async () => {
let sdkContainer = sdkContainers.get(sdkTestConfig.type)
let sdkContainer = sdkContainers.get(sdkTestConfig.type);
if (!sdkContainer) {
// console.log(`===== Initializing ${sdk} =====`)
let options: SDKOptions = {
unleashApiUrl: `http://${unleashServer.getInternalIpAddress()}:${unleashServer.getInternalPort()}/api`,
apiToken: config.unleash.adminToken,
network: network,
sdkImpl: sdkTestConfig.client
}
let { create } = require(`./sdks/${sdkTestConfig.type}/container`)
sdkContainer = create(options) as ContainerInstance
await sdkContainer.initialize()
sdkContainers.set(sdkTestConfig.name!, sdkContainer)
sdkImpl: sdkTestConfig.client,
};
let { create } = require(`./sdks/${sdkTestConfig.type}/container`);
sdkContainer = create(options) as ContainerInstance;
await sdkContainer.initialize();
sdkContainers.set(sdkTestConfig.name!, sdkContainer);
} else {
// cleanup cached SDK state
// console.log(`===== Reseting state of ${sdk} =====`)
await sdkContainer.reset()
await sdkContainer.reset();
}
sdkUrl = `http://localhost:${sdkContainer.getMappedPort()}`
})

const splitFn = (t: ITestDef) => excludedForSDK.filter(s => testName.startsWith(s) || t.description.startsWith(s)).length > 0
const [skip, evaluate] = partition(definition.tests || [], splitFn)
sdkUrl = `http://localhost:${sdkContainer.getMappedPort()}`;
});

const splitFn = (t: ITestDef) =>
excludedForSDK.filter(
(s) => testName.startsWith(s) || t.description.startsWith(s)
).length > 0;
const [skip, evaluate] = partition(definition.tests || [], splitFn);
console.log(`=== ${definition.name} TO SKIP: ${JSON.stringify(skip)}`);
if (skip.length > 0) test.skip.each(skip)(`$description`, () => {});
if (evaluate.length > 0) {
test.each(evaluate)(`$description`, async testCase => {
test.each(evaluate)(`$description`, async (testCase) => {
const { body, statusCode } = await got.post(`${sdkUrl}/is-enabled`, {
json: {
toggle: testCase.toggleName,
context: testCase.context
}
})
expect(statusCode).toBe(200)
const result: EnabledResult = JSON.parse(body)
expect(result.enabled).toBe(testCase.expectedResult)
})
context: testCase.context,
},
});
expect(statusCode).toBe(200);
const result: EnabledResult = JSON.parse(body);
expect(result.enabled).toBe(testCase.expectedResult);
});
}

const [skipVariants, evaluateVariants] = partition(definition.variantTests || [], splitFn)
if (skipVariants.length > 0) test.skip.each(skipVariants)(`$description`, () => {});
const [skipVariants, evaluateVariants] = partition(
definition.variantTests || [],
splitFn
);
if (skipVariants.length > 0)
test.skip.each(skipVariants)(`$description`, () => {});
if (evaluateVariants.length > 0) {
test.each(evaluateVariants)(`$description`, async testCase => {
test.each(evaluateVariants)(`$description`, async (testCase) => {
const { body, statusCode } = await got.post(`${sdkUrl}/variant`, {
json: {
toggle: testCase.toggleName,
context: testCase.context
}
})
expect(statusCode).toBe(200)
const variantResult: VariantResult = JSON.parse(body)
const result = parseResult(sdkTestConfig.name!, variantResult)
expect(result).toEqual(testCase.expectedResult)
})
context: testCase.context,
},
});
expect(statusCode).toBe(200);
const variantResult: VariantResult = JSON.parse(body);
const result = parseResult(sdkTestConfig.name!, variantResult);
expect(result).toEqual(
expect.objectContaining(testCase.expectedResult)
);
});
}
})
})
})
});
});
});
14 changes: 4 additions & 10 deletions src/only-valid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ servers:
sdks:
- type: node
- type: python
excluding:
- 'Feature.constraints.dual should be enabled in prod'
- 'Enabled in prod for userId=123'
- 'Feature.constraints.custom should be enabled in prod for norway'
- 'Feature.constraints.multi should be enabled in prod for user 2'
- 'Feature.constraints.list should be enabled in stage environment'
- 'Feature.constraints.simple should be enabled in dev environment'
- '12'
- '13'
- '15'
- type: java
client: io.getunleash:unleash-client-java:6.1.0
- type: java
client: io.getunleash:unleash-client-java:7.1.0
- type: java
client: io.getunleash:unleash-client-java:8.3.0
- type: go
excluding:
- '08'
Expand Down
2 changes: 1 addition & 1 deletion src/sdks/dotnet/dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Unleash.Client" Version="2.2.0" />
<PackageReference Include="Unleash.Client" Version="3.3.0" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion src/sdks/go/go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module github.com/Unleash/sdk-integration-tester

require (
github.com/Unleash/unleash-client-go/v3 v3.7.0
github.com/Unleash/unleash-client-go/v3 v3.8.0
github.com/gin-gonic/gin v1.9.0
)

require (
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
Expand All @@ -21,6 +22,9 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/twmb/murmur3 v1.1.5 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions src/sdks/go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Unleash/unleash-client-go/v3 v3.7.0 h1:N0NYPCXEYw04U84d9wUgP9vrYijLqpOYsa1XbD+9Vqk=
github.com/Unleash/unleash-client-go/v3 v3.7.0/go.mod h1:jAf7F2WWpfJbfn1n8bZ74p7hkAhijrqH4TpWoT7kWLc=
github.com/Unleash/unleash-client-go/v3 v3.8.0 h1:DS2yU0/ouy8lXGO3FzocUTZMyO7uhjHPEYmwqGEJopQ=
github.com/Unleash/unleash-client-go/v3 v3.8.0/go.mod h1:jAf7F2WWpfJbfn1n8bZ74p7hkAhijrqH4TpWoT7kWLc=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
Expand Down
Loading
Loading