diff --git a/.env.test b/.env.test index c4eb691..03a5490 100644 --- a/.env.test +++ b/.env.test @@ -6,6 +6,6 @@ SWITCHER_API_URL=https://switcherapi.com/api/gitops-graphql SWITCHER_API_JWT_SECRET=[YOUR_JWT_SECRET] # Only for testing purposes. Values are loaded from accounts -GIT_TOKEN=[GIT_TOKEN] -GIT_REPO_URL=[GIT_REPO_URL] -GIT_BRANCH=[GIT_BRANCH] \ No newline at end of file +GIT_TOKEN= +GIT_REPO_URL=https://github.com/switcherapi/switcher-gitops-fixture +GIT_BRANCH=main \ No newline at end of file diff --git a/README.md b/README.md index d636c4b..5e38262 100644 --- a/README.md +++ b/README.md @@ -24,4 +24,25 @@ GitOps Domain Snapshot Orchestrator for Switcher API # Features - Manage Switchers with GitOps driven environment - Multiple and independent Environments -- Auto Sync enables a fully integrated environment with Switcher API Management, Slack App and GitOps working simultaneously \ No newline at end of file +- Auto Sync enables a fully integrated environment with Switcher API Management, Slack App and GitOps working simultaneously + +# Integrated tests + +This project does not run integration test without a valid GitOps repository settings. + +In order to run integration tests locally, you need to create a valid GitOps repository with the following structure: + +```bash +├── resources +│ ├── default.json (*) +``` +(*) see a Switcher snapshot sample [here](https://github.com/switcherapi/switcher-gitops/blob/master/resources/default.json) + +The next step is to set up PAT (Personal Access Token) for Switcher GitOps to access the repository. You can either create a fine-grained token with only the necessary permissions such as Content (Read and Write) and Metadata (Read) or use a personal token with full access. + +Once you have the token, you can set it up in the `.env.test` environment file by including the following: +```bash +GIT_TOKEN=[YOUR_GIT_TOKEN] +GIT_REPO_URL=[YOUR_GIT_REPO_URL] +GIT_BRANCH=[YOUR_GIT_BRANCH] +``` \ No newline at end of file diff --git a/resources/default.json b/resources/default.json new file mode 100644 index 0000000..0769846 --- /dev/null +++ b/resources/default.json @@ -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" + ] + } + ] + } + ] + } + } \ No newline at end of file diff --git a/src/core/git.go b/src/core/git.go index 9833de2..f0e0d44 100644 --- a/src/core/git.go +++ b/src/core/git.go @@ -32,7 +32,6 @@ func NewGitService(repoURL string, token string, branchName string) *GitService } func (g *GitService) GetRepositoryData(environment string) (string, string, string, error) { - println("GetRepositoryData") commitHash, commitDate, content, err := g.getLastCommitData(model.FilePath + environment + ".json") if err != nil { diff --git a/src/core/git_test.go b/src/core/git_test.go index edd8d4f..5dd13e4 100644 --- a/src/core/git_test.go +++ b/src/core/git_test.go @@ -8,6 +8,10 @@ import ( "github.com/switcherapi/switcher-gitops/src/model" ) +const ( + SkipMessage = "Skipping tests because environment variables are not set" +) + func TestNewGitService(t *testing.T) { // Given repoURL := "repoURL" @@ -24,6 +28,10 @@ func TestNewGitService(t *testing.T) { } func TestGetRepositoryData(t *testing.T) { + if !canRunTests() { + t.Skip(SkipMessage) + } + // Given gitService := NewGitService( config.GetEnv("GIT_REPO_URL"), @@ -41,6 +49,10 @@ func TestGetRepositoryData(t *testing.T) { } func TestGetRepositoryDataErrorInvalidEnvironment(t *testing.T) { + if !canRunTests() { + t.Skip(SkipMessage) + } + // Given gitService := NewGitService( config.GetEnv("GIT_REPO_URL"), @@ -58,6 +70,10 @@ func TestGetRepositoryDataErrorInvalidEnvironment(t *testing.T) { } func TestGetRepositoryDataErrorInvalidToken(t *testing.T) { + if !canRunTests() { + t.Skip(SkipMessage) + } + // Given gitService := NewGitService( config.GetEnv("GIT_REPO_URL"), @@ -75,6 +91,10 @@ func TestGetRepositoryDataErrorInvalidToken(t *testing.T) { } func TestCheckForChanges(t *testing.T) { + if !canRunTests() { + t.Skip(SkipMessage) + } + // Given gitService := NewGitService( config.GetEnv("GIT_REPO_URL"), @@ -91,3 +111,9 @@ func TestCheckForChanges(t *testing.T) { assert.Equal(t, model.StatusSynced, status) assert.Equal(t, "Synced successfully", message) } + +// Helpers + +func canRunTests() bool { + return config.GetEnv("GIT_REPO_URL") != "" && config.GetEnv("GIT_TOKEN") != "" && config.GetEnv("GIT_BRANCH") != "" +}