Skip to content
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
2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ HANDLER_WAITING_TIME=1m

SWITCHER_API_URL=https://switcherapi.com/api
SWITCHER_API_JWT_SECRET=SecretSecretSecretSecretSecretSe
SWITCHER_PATH_GRAPHQL=/gitops-graphql
SWITCHER_PATH_PUSH=/gitops/v1/push

# Only for testing purposes. Values are loaded from accounts
GIT_USER=
Expand Down
67 changes: 67 additions & 0 deletions resources/fixtures/comparator/new_config_strategy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"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"
]
},
{
"key": "MY_SWITCHER_4",
"description": "",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"user_1"
]
}
],
"components": [
"benchmark"
]
}
]
}
]
}
}
9 changes: 5 additions & 4 deletions src/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/golang-jwt/jwt"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/model"
)

Expand All @@ -24,7 +25,7 @@ type PushChangeResponse struct {
type IAPIService interface {
FetchSnapshotVersion(domainId string, environment string) (string, error)
FetchSnapshot(domainId string, environment string) (string, error)
PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error)
PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error)
NewDataFromJson(jsonData []byte) model.Data
}

Expand Down Expand Up @@ -68,9 +69,9 @@ func (a *ApiService) FetchSnapshot(domainId string, environment string) (string,
return responseBody, nil
}

func (a *ApiService) PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error) {
func (a *ApiService) PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error) {
reqBody, _ := json.Marshal(diff)
responseBody, err := a.doPostRequest(a.apiUrl+"/gitops/push", domainId, reqBody)
responseBody, err := a.doPostRequest(a.apiUrl+config.GetEnv("SWITCHER_PATH_PUSH"), domainId, reqBody)

if err != nil {
return PushChangeResponse{}, err
Expand All @@ -87,7 +88,7 @@ func (a *ApiService) doGraphQLRequest(domainId string, query string) (string, er

// Create a new request
reqBody, _ := json.Marshal(GraphQLRequest{Query: query})
req, _ := http.NewRequest("POST", a.apiUrl+"/gitops-graphql", bytes.NewBuffer(reqBody))
req, _ := http.NewRequest("POST", a.apiUrl+config.GetEnv("SWITCHER_PATH_GRAPHQL"), bytes.NewBuffer(reqBody))

// Set the request headers
setHeaders(req, token)
Expand Down
15 changes: 8 additions & 7 deletions src/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestFetchSnapshot(t *testing.T) {
func TestPushChangesToAPI(t *testing.T) {
t.Run("Should push changes to API", func(t *testing.T) {
// Given
diff := givenDiffResult()
diff := givenDiffResult("default")
fakeApiServer := givenApiResponse(http.StatusOK, `{
"version": 2,
"message": "Changes applied successfully"
Expand All @@ -126,7 +126,7 @@ func TestPushChangesToAPI(t *testing.T) {
apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)

// Test
response, _ := apiService.PushChanges("domainId", "default", diff)
response, _ := apiService.PushChanges("domainId", diff)

// Assert
assert.NotNil(t, response)
Expand All @@ -136,14 +136,14 @@ func TestPushChangesToAPI(t *testing.T) {

t.Run("Should return error - invalid API key", func(t *testing.T) {
// Given
diff := givenDiffResult()
diff := givenDiffResult("default")
fakeApiServer := givenApiResponse(http.StatusUnauthorized, `{ "message": "Invalid API token" }`)
defer fakeApiServer.Close()

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

// Test
response, _ := apiService.PushChanges("domainId", "default", diff)
response, _ := apiService.PushChanges("domainId", diff)

// Assert
assert.NotNil(t, response)
Expand All @@ -152,11 +152,11 @@ func TestPushChangesToAPI(t *testing.T) {

t.Run("Should return error - API not accessible", func(t *testing.T) {
// Given
diff := givenDiffResult()
diff := givenDiffResult("default")
apiService := NewApiService("[SWITCHER_API_JWT_SECRET]", "http://localhost:8080")

// Test
_, err := apiService.PushChanges("domainId", "default", diff)
_, err := apiService.PushChanges("domainId", diff)

// Assert
assert.NotNil(t, err)
Expand All @@ -165,8 +165,9 @@ func TestPushChangesToAPI(t *testing.T) {

// Helpers

func givenDiffResult() model.DiffResult {
func givenDiffResult(environment string) model.DiffResult {
diffResult := model.DiffResult{}
diffResult.Environment = environment
diffResult.Changes = append(diffResult.Changes, model.DiffDetails{
Action: string(NEW),
Diff: string(CONFIG),
Expand Down
41 changes: 41 additions & 0 deletions src/core/comparator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,47 @@ func TestCheckConfigSnapshot(t *testing.T) {
]}`, utils.ToJsonFromObject(actual))
})

t.Run("Should return new config with strategy", func(t *testing.T) {
// Given
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)
jsonRepo := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_config_strategy.json")
fromApi := c.NewSnapshotFromJson([]byte(jsonApi))
fromRepo := c.NewSnapshotFromJson([]byte(jsonRepo))

// Test Check/Merge changes
diffNew := c.CheckSnapshotDiff(fromRepo, fromApi, NEW)
actual := c.MergeResults([]model.DiffResult{diffNew})

assert.NotNil(t, actual)
assert.JSONEq(t, `{
"changes": [
{
"action": "NEW",
"diff": "CONFIG",
"path": [
"Release 1"
],
"content": {
"key": "MY_SWITCHER_4",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"user_1"
]
}
],
"components": [
"benchmark"
]
}
}
]}`, utils.ToJsonFromObject(actual))
})

t.Run("Should return deleted config", func(t *testing.T) {
// Given
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)
Expand Down
3 changes: 2 additions & 1 deletion src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ func (c *CoreHandler) pushChangesToAPI(account model.Account,
}

// Push changes to API
apiResponse, err := c.apiService.PushChanges(account.Domain.ID, account.Environment, diff)
diff.Environment = account.Environment
apiResponse, err := c.apiService.PushChanges(account.Domain.ID, diff)

if err != nil {
return account, err
Expand Down
2 changes: 1 addition & 1 deletion src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ func (f *FakeApiService) FetchSnapshot(domainId string, environment string) (str
return f.responseSnapshot, nil
}

func (f *FakeApiService) PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error) {
func (f *FakeApiService) PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error) {
if f.throwErrorPush {
return PushChangeResponse{}, errors.New("something went wrong")
}
Expand Down
3 changes: 2 additions & 1 deletion src/model/diff.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package model

type DiffResult struct {
Changes []DiffDetails `json:"changes"`
Environment string `json:"environment,omitempty"`
Changes []DiffDetails `json:"changes"`
}

type DiffDetails struct {
Expand Down