From 2c69e4fad2349bea2106c383c9a513189c9024e4 Mon Sep 17 00:00:00 2001 From: tyiuhc Date: Thu, 25 Jul 2024 11:52:24 -0700 Subject: [PATCH] add CI tests and update build.yml --- .github/workflows/build.yml | 6 +++ .gitignore | 1 + go.mod | 1 + go.sum | 2 + pkg/experiment/local/client_eu_test.go | 46 ++++++++++++++++++++ pkg/experiment/local/client_test.go | 56 +++++++++++++++++++++++-- pkg/experiment/local/flag_config_api.go | 2 +- 7 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 pkg/experiment/local/client_eu_test.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a1fcf5e..2c361ef 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,6 +25,7 @@ jobs: with: version: latest test: + environment: Unit Test runs-on: 'ubuntu-latest' steps: - name: Checkout @@ -35,5 +36,10 @@ jobs: go-version: '1.17' check-latest: true - name: Test + env: + API_KEY: ${{ secrets.API_KEY }} + SECRET_KEY: ${{ secrets.SECRET_KEY }} + EU_API_KEY: ${{ secrets.EU_API_KEY }} + EU_SECRET_KEY: ${{ secrets.EU_SECRET_KEY }} run: | go test ./... diff --git a/.gitignore b/.gitignore index ff53c67..9e9437e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ xpmt .DS_Store cmd/xpmt/bin/ +pkg/experiment/local/.env diff --git a/go.mod b/go.mod index 81525f6..bad5092 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,6 @@ require github.com/spaolacci/murmur3 v1.1.0 require ( github.com/amplitude/analytics-go v1.0.1 github.com/jarcoal/httpmock v1.3.1 + github.com/joho/godotenv v1.5.1 github.com/stretchr/testify v1.9.0 ) diff --git a/go.sum b/go.sum index 46aa182..ec3c7e2 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/pkg/experiment/local/client_eu_test.go b/pkg/experiment/local/client_eu_test.go new file mode 100644 index 0000000..b4a01ff --- /dev/null +++ b/pkg/experiment/local/client_eu_test.go @@ -0,0 +1,46 @@ +package local + +import ( + "github.com/amplitude/experiment-go-server/pkg/experiment" + "github.com/joho/godotenv" + "log" + "os" + "testing" +) + +var clientEU *Client + +func init() { + err := godotenv.Load() + if err != nil { + log.Fatalf("Error loading .env file: %v", err) + } + projectApiKey := os.Getenv("EU_API_KEY") + secretKey := os.Getenv("EU_SECRET_KEY") + cohortSyncConfig := CohortSyncConfig{ + ApiKey: projectApiKey, + SecretKey: secretKey, + } + clientEU = Initialize("server-Qlp7XiSu6JtP2S3JzA95PnP27duZgQCF", + &Config{CohortSyncConfig: &cohortSyncConfig, ServerZone: "eu"}) + err = clientEU.Start() + if err != nil { + panic(err) + } +} + +func TestEvaluateV2CohortEU(t *testing.T) { + user := &experiment.User{UserId: "1", DeviceId: "0"} + flagKeys := []string{"sdk-local-evaluation-user-cohort"} + result, err := clientEU.EvaluateV2(user, flagKeys) + if err != nil { + t.Fatalf("Unexpected error %v", err) + } + variant := result["sdk-local-evaluation-user-cohort"] + if variant.Key != "on" { + t.Fatalf("Unexpected variant %v", variant) + } + if variant.Value != "on" { + t.Fatalf("Unexpected variant %v", variant) + } +} diff --git a/pkg/experiment/local/client_test.go b/pkg/experiment/local/client_test.go index be7cc67..2cb346b 100644 --- a/pkg/experiment/local/client_test.go +++ b/pkg/experiment/local/client_test.go @@ -2,14 +2,28 @@ package local import ( "github.com/amplitude/experiment-go-server/pkg/experiment" + "github.com/joho/godotenv" + "log" + "os" "testing" ) var client *Client func init() { - client = Initialize("server-qz35UwzJ5akieoAdIgzM4m9MIiOLXLoz", nil) - err := client.Start() + err := godotenv.Load() + if err != nil { + log.Fatalf("Error loading .env file: %v", err) + } + projectApiKey := os.Getenv("API_KEY") + secretKey := os.Getenv("SECRET_KEY") + cohortSyncConfig := CohortSyncConfig{ + ApiKey: projectApiKey, + SecretKey: secretKey, + } + client = Initialize("server-qz35UwzJ5akieoAdIgzM4m9MIiOLXLoz", + &Config{CohortSyncConfig: &cohortSyncConfig}) + err = client.Start() if err != nil { panic(err) } @@ -52,7 +66,6 @@ func TestEvaluate(t *testing.T) { } } - func TestEvaluateV2AllFlags(t *testing.T) { user := &experiment.User{UserId: "test_user"} result, err := client.EvaluateV2(user, nil) @@ -157,3 +170,40 @@ func TestFlagMetadataLocalFlagKey(t *testing.T) { t.Fatalf("Unexpected metadata %v", md) } } + +func TestEvaluateV2Cohort(t *testing.T) { + user := &experiment.User{UserId: "12345"} + flagKeys := []string{"sdk-local-evaluation-user-cohort-ci-test"} + result, err := client.EvaluateV2(user, flagKeys) + if err != nil { + t.Fatalf("Unexpected error %v", err) + } + variant := result["sdk-local-evaluation-user-cohort-ci-test"] + if variant.Key != "on" { + t.Fatalf("Unexpected variant %v", variant) + } + if variant.Value != "on" { + t.Fatalf("Unexpected variant %v", variant) + } +} + +func TestEvaluateV2GroupCohort(t *testing.T) { + user := &experiment.User{ + UserId: "12345", + DeviceId: "device_id", + Groups: map[string][]string{ + "org id": {"1"}, + }} + flagKeys := []string{"sdk-local-evaluation-group-cohort-ci-test"} + result, err := client.EvaluateV2(user, flagKeys) + if err != nil { + t.Fatalf("Unexpected error %v", err) + } + variant := result["sdk-local-evaluation-group-cohort-ci-test"] + if variant.Key != "on" { + t.Fatalf("Unexpected variant %v", variant) + } + if variant.Value != "on" { + t.Fatalf("Unexpected variant %v", variant) + } +} diff --git a/pkg/experiment/local/flag_config_api.go b/pkg/experiment/local/flag_config_api.go index 2964891..b8dc324 100644 --- a/pkg/experiment/local/flag_config_api.go +++ b/pkg/experiment/local/flag_config_api.go @@ -32,7 +32,7 @@ func newFlagConfigApiV2(deploymentKey, serverURL string, flagConfigPollerRequest func (a *flagConfigApiV2) getFlagConfigs() (map[string]*evaluation.Flag, error) { client := &http.Client{} - endpoint, err := url.Parse("https://api.lab.amplitude.com/") + endpoint, err := url.Parse(a.ServerURL) if err != nil { return nil, err }