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

Bump Go runtime to 1.23.0, improved api, comparator, util tests #28

Merged
merged 2 commits into from
Sep 4, 2024
Merged
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
5 changes: 2 additions & 3 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ PORT=8000
MONGO_URI=mongodb://localhost:27017
MONGO_DB=switcher-gitops-test

SWITCHER_API_URL=https://switcherapi.com/api/gitops-graphql
SWITCHER_API_JWT_SECRET=[YOUR_JWT_SECRET]
SWITCHER_API_URL=https://switcherapi.com/api
SWITCHER_API_JWT_SECRET=

# Only for testing purposes. Values are loaded from accounts
API_DOMAIN_ID=
GIT_USER=
GIT_TOKEN=
GIT_TOKEN_READ_ONLY=
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
with:
fetch-depth: 0

- name: Set up Go 1.22.4
- name: Set up Go 1.23.0
uses: actions/setup-go@v5
with:
go-version: '1.22.4'
go-version: '1.23.0'

- name: Start MongoDB
uses: supercharge/[email protected]
Expand All @@ -38,7 +38,6 @@ jobs:
MONGO_DB: switcher-gitops-test
SWITCHER_API_URL: ${{ secrets.SWITCHER_API_URL }}
SWITCHER_API_JWT_SECRET: ${{ secrets.SWITCHER_API_JWT_SECRET }}
API_DOMAIN_ID: ${{ secrets.API_DOMAIN_ID }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
GIT_USER: ${{ secrets.GIT_USER }}
GIT_TOKEN_READ_ONLY: ${{ secrets.GIT_TOKEN_READ_ONLY }}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/switcherapi/switcher-gitops

go 1.22
go 1.23

toolchain go1.22.4
toolchain go1.23.0

require (
github.com/go-git/go-billy/v5 v5.5.0
Expand Down
35 changes: 35 additions & 0 deletions resources/fixtures/api/default_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"data": {
"domain": {
"name": "GitOps",
"version": 1718772781488,
"group": [
{
"name": "Group 1",
"description": "",
"activated": true,
"config": [
{
"key": "FEATURE_1",
"description": "",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"test2"
]
}
],
"components": [
"app1"
]
}
]
}
]
}
}
}
19 changes: 19 additions & 0 deletions resources/fixtures/api/error_invalid_domain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"errors": [
{
"message": "Cast to ObjectId failed for value \"INVALID_DOMAIN\" (type string) at path \"_id\" for model \"Domain\"",
"locations": [
{
"line": 3,
"column": 9
}
],
"path": [
"domain"
]
}
],
"data": {
"domain": null
}
}
49 changes: 49 additions & 0 deletions resources/fixtures/util/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"domain": {
"group": [
{
"name": "Release 1",
"description": "Showcase configuration",
"activated": true,
"config": [
{
"key": "MY_SWITCHER_1",
"description": "My first switcher",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"user_1"
]
}
],
"components": [
"switcher-playground"
]
},
{
"key": "MY_SWITCHER_2",
"description": "",
"activated": false,
"strategies": [],
"components": [
"switcher-playground"
]
},
{
"key": "MY_SWITCHER_3",
"description": "",
"activated": true,
"strategies": [],
"components": [
"benchmark"
]
}
]
}
]
}
}
42 changes: 28 additions & 14 deletions src/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import (
"github.com/stretchr/testify/assert"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/model"
"github.com/switcherapi/switcher-gitops/src/utils"
)

func TestFetchSnapshot(t *testing.T) {
if !canRunIntegratedTests() {
t.Skip(SkipMessage)
}
const SWITCHER_API_JWT_SECRET = "SWITCHER_API_JWT_SECRET"

func TestFetchSnapshot(t *testing.T) {
t.Run("Should return snapshot", func(t *testing.T) {
apiService := NewApiService(config.GetEnv("SWITCHER_API_JWT_SECRET"), config.GetEnv("SWITCHER_API_URL"))
snapshot, _ := apiService.FetchSnapshot(config.GetEnv("API_DOMAIN_ID"), "default")
responsePayload := utils.ReadJsonFromFile("../../resources/fixtures/api/default_snapshot.json")
fakeApiServer := givenApiResponse(http.StatusOK, responsePayload)
defer fakeApiServer.Close()

apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)
snapshot, _ := apiService.FetchSnapshot("domainId", "default")

assert.Contains(t, snapshot, "domain", "Missing domain in snapshot")
assert.Contains(t, snapshot, "version", "Missing version in snapshot")
Expand All @@ -26,8 +29,12 @@ func TestFetchSnapshot(t *testing.T) {
})

t.Run("Should return data from snapshot", func(t *testing.T) {
apiService := NewApiService(config.GetEnv("SWITCHER_API_JWT_SECRET"), config.GetEnv("SWITCHER_API_URL"))
snapshot, _ := apiService.FetchSnapshot(config.GetEnv("API_DOMAIN_ID"), "default")
responsePayload := utils.ReadJsonFromFile("../../resources/fixtures/api/default_snapshot.json")
fakeApiServer := givenApiResponse(http.StatusOK, responsePayload)
defer fakeApiServer.Close()

apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)
snapshot, _ := apiService.FetchSnapshot("domainId", "default")
data := apiService.NewDataFromJson([]byte(snapshot))

assert.NotNil(t, data.Snapshot.Domain, "domain", "Missing domain in data")
Expand All @@ -36,22 +43,29 @@ func TestFetchSnapshot(t *testing.T) {
})

t.Run("Should return error - invalid API key", func(t *testing.T) {
apiService := NewApiService("INVALID_KEY", config.GetEnv("SWITCHER_API_URL"))
snapshot, _ := apiService.FetchSnapshot(config.GetEnv("API_DOMAIN_ID"), "default")
fakeApiServer := givenApiResponse(http.StatusUnauthorized, `{ "error": "Invalid API token" }`)
defer fakeApiServer.Close()

apiService := NewApiService("INVALID_KEY", fakeApiServer.URL)
snapshot, _ := apiService.FetchSnapshot("domainId", "default")

assert.Contains(t, snapshot, "Invalid API token")
})

t.Run("Should return error - invalid domain", func(t *testing.T) {
apiService := NewApiService(config.GetEnv("SWITCHER_API_JWT_SECRET"), config.GetEnv("SWITCHER_API_URL"))
responsePayload := utils.ReadJsonFromFile("../../resources/fixtures/api/error_invalid_domain.json")
fakeApiServer := givenApiResponse(http.StatusUnauthorized, responsePayload)
defer fakeApiServer.Close()

apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)
snapshot, _ := apiService.FetchSnapshot("INVALID_DOMAIN", "default")

assert.Contains(t, snapshot, "errors")
})

t.Run("Should return error - invalid API URL", func(t *testing.T) {
apiService := NewApiService(config.GetEnv("SWITCHER_API_JWT_SECRET"), "http://localhost:8080")
_, err := apiService.FetchSnapshot(config.GetEnv("API_DOMAIN_ID"), "default")
apiService := NewApiService(config.GetEnv(SWITCHER_API_JWT_SECRET), "http://localhost:8080")
_, err := apiService.FetchSnapshot("domainId", "default")

AssertNotNil(t, err)
})
Expand All @@ -67,7 +81,7 @@ func TestApplyChangesToAPI(t *testing.T) {
}`)
defer fakeApiServer.Close()

apiService := NewApiService("[SWITCHER_API_JWT_SECRET]", fakeApiServer.URL)
apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)

// Test
response, _ := apiService.ApplyChangesToAPI("domainId", "default", diff)
Expand Down
32 changes: 16 additions & 16 deletions src/core/comparator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"github.com/switcherapi/switcher-gitops/src/utils"
)

const DEFAULT_JSON = "../../resources/fixtures/default.json"
const DEFAULT_JSON = "../../resources/fixtures/comparator/default.json"

func TestCheckGroupSnapshot(t *testing.T) {
c := NewComparatorService()

t.Run("Should return changes in group", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/changed_group.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/changed_group.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -46,7 +46,7 @@ func TestCheckGroupSnapshot(t *testing.T) {
t.Run("Should return new group", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_group.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_group.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -84,7 +84,7 @@ func TestCheckGroupSnapshot(t *testing.T) {
t.Run("Should return deleted group", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_group.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/deleted_group.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand All @@ -110,7 +110,7 @@ func TestCheckGroupSnapshot(t *testing.T) {

t.Run("Should return new group from empty group", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile("../../resources/fixtures/default_empty.json")
jsonLeft := utils.ReadJsonFromFile("../../resources/fixtures/comparator/default_empty.json")
jsonRight := utils.ReadJsonFromFile(DEFAULT_JSON)
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))
Expand All @@ -128,7 +128,7 @@ func TestCheckGroupSnapshot(t *testing.T) {

t.Run("Should return new group from empty config", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile("../../resources/fixtures/default_empty_config.json")
jsonLeft := utils.ReadJsonFromFile("../../resources/fixtures/comparator/default_empty_config.json")
jsonRight := utils.ReadJsonFromFile(DEFAULT_JSON)
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))
Expand All @@ -151,7 +151,7 @@ func TestCheckConfigSnapshot(t *testing.T) {
t.Run("Should return changes in config", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/changed_config.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/changed_config.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -182,7 +182,7 @@ func TestCheckConfigSnapshot(t *testing.T) {
t.Run("Should return new config", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_config.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_config.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -215,7 +215,7 @@ func TestCheckConfigSnapshot(t *testing.T) {
t.Run("Should return deleted config", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_config.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/deleted_config.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -247,7 +247,7 @@ func TestCheckStrategySnapshot(t *testing.T) {
t.Run("Should return changes in strategy", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/changed_strategy.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/changed_strategy.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -278,7 +278,7 @@ func TestCheckStrategySnapshot(t *testing.T) {
t.Run("Should return new strategy", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_strategy.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_strategy.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -313,7 +313,7 @@ func TestCheckStrategySnapshot(t *testing.T) {
t.Run("Should return deleted strategy", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_strategy.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/deleted_strategy.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -342,7 +342,7 @@ func TestCheckStrategySnapshot(t *testing.T) {
t.Run("Should return new strategy value", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_strategy_value.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_strategy_value.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -373,7 +373,7 @@ func TestCheckStrategySnapshot(t *testing.T) {
t.Run("Should return deleted strategy value", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_strategy_value.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/deleted_strategy_value.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -408,7 +408,7 @@ func TestCheckComponentSnapshot(t *testing.T) {
t.Run("Should return new component", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_component.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_component.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down Expand Up @@ -438,7 +438,7 @@ func TestCheckComponentSnapshot(t *testing.T) {
t.Run("Should return deleted component", func(t *testing.T) {
// Given
jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_component.json")
jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/comparator/deleted_component.json")
snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft))
snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight))

Expand Down
3 changes: 1 addition & 2 deletions src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func canRunIntegratedTests() bool {
return config.GetEnv("GIT_REPO_URL") != "" &&
config.GetEnv("GIT_TOKEN") != "" &&
config.GetEnv("GIT_TOKEN_READ_ONLY") != "" &&
config.GetEnv("GIT_BRANCH") != "" &&
config.GetEnv("API_DOMAIN_ID") != ""
config.GetEnv("GIT_BRANCH") != ""
}

func AssertNotNil(t *testing.T, object interface{}) {
Expand Down
Loading