forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add a fake deployments implementation
so we can fake out testing deployments
- Loading branch information
Showing
4 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package fake | ||
|
||
import ( | ||
"context" | ||
"github.com/jenkins-x/go-scm/scm" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type deploymentService struct { | ||
client *wrapper | ||
data *Data | ||
} | ||
|
||
func (s *deploymentService) Find(ctx context.Context, repoFullName string, deploymentID string) (*scm.Deployment, *scm.Response, error) { | ||
for _, d := range s.data.Deployments[repoFullName] { | ||
if d.ID == deploymentID { | ||
return d, nil, nil | ||
} | ||
} | ||
return nil, nil, scm.ErrNotFound | ||
} | ||
|
||
func (s *deploymentService) List(ctx context.Context, repoFullName string, opts scm.ListOptions) ([]*scm.Deployment, *scm.Response, error) { | ||
return s.data.Deployments[repoFullName], nil, nil | ||
} | ||
|
||
func (s *deploymentService) Create(ctx context.Context, repoFullName string, input *scm.DeploymentInput) (*scm.Deployment, *scm.Response, error) { | ||
deployments := s.data.Deployments[repoFullName] | ||
owner, name := scm.Split(repoFullName) | ||
|
||
d := &scm.Deployment{ | ||
ID: "deployment-" + strconv.Itoa(len(deployments)+1), | ||
Namespace: owner, | ||
Name: name, | ||
Link: "", | ||
Sha: "", | ||
Ref: input.Ref, | ||
Task: input.Task, | ||
FullName: "", | ||
Description: input.Description, | ||
OriginalEnvironment: input.Environment, | ||
Environment: input.Environment, | ||
RepositoryLink: "", | ||
StatusLink: "", | ||
Author: nil, | ||
Created: time.Time{}, | ||
Updated: time.Time{}, | ||
TransientEnvironment: input.TransientEnvironment, | ||
ProductionEnvironment: input.ProductionEnvironment, | ||
Payload: input.Payload, | ||
} | ||
|
||
s.data.Deployments[repoFullName] = append(deployments, d) | ||
return d, nil, nil | ||
} | ||
|
||
func (s *deploymentService) Delete(ctx context.Context, repoFullName string, deploymentID string) (*scm.Response, error) { | ||
deployments := s.data.Deployments[repoFullName] | ||
for i, d := range deployments { | ||
if d.ID == deploymentID { | ||
result := deployments[0:i] | ||
if i+1 < len(deployments) { | ||
result = append(result, deployments[i+1:]...) | ||
} | ||
s.data.Deployments[repoFullName] = result | ||
return nil, nil | ||
} | ||
} | ||
return nil, scm.ErrNotFound | ||
} | ||
|
||
func (s *deploymentService) FindStatus(ctx context.Context, repoFullName string, deploymentID string, statusID string) (*scm.DeploymentStatus, *scm.Response, error) { | ||
key := scm.Join(repoFullName, deploymentID) | ||
for _, d := range s.data.DeploymentStatus[key] { | ||
if d.ID == statusID { | ||
return d, nil, nil | ||
} | ||
} | ||
return nil, nil, scm.ErrNotFound | ||
} | ||
|
||
func (s *deploymentService) ListStatus(ctx context.Context, repoFullName string, deploymentID string, opts scm.ListOptions) ([]*scm.DeploymentStatus, *scm.Response, error) { | ||
key := scm.Join(repoFullName, deploymentID) | ||
return s.data.DeploymentStatus[key], nil, nil | ||
} | ||
|
||
func (s *deploymentService) CreateStatus(ctx context.Context, repoFullName string, deploymentID string, input *scm.DeploymentStatusInput) (*scm.DeploymentStatus, *scm.Response, error) { | ||
key := scm.Join(repoFullName, deploymentID) | ||
statuses := s.data.DeploymentStatus[key] | ||
|
||
status := &scm.DeploymentStatus{ | ||
ID: "status-" + strconv.Itoa(len(statuses)+1), | ||
State: input.State, | ||
Author: nil, | ||
Description: input.Description, | ||
Environment: input.Environment, | ||
DeploymentLink: "", | ||
EnvironmentLink: input.EnvironmentLink, | ||
LogLink: input.LogLink, | ||
RepositoryLink: "", | ||
TargetLink: input.LogLink, | ||
Created: time.Time{}, | ||
Updated: time.Time{}, | ||
} | ||
|
||
s.data.DeploymentStatus[key] = append(statuses, status) | ||
return status, nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package fake_test | ||
|
||
import ( | ||
"context" | ||
"github.com/jenkins-x/go-scm/scm" | ||
"testing" | ||
|
||
"github.com/jenkins-x/go-scm/scm/driver/fake" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeploy(t *testing.T) { | ||
client, _ := fake.NewDefault() | ||
|
||
ctx := context.Background() | ||
|
||
repo := "myorg/myrepo" | ||
|
||
AssertDeploymentSize(t, ctx, client, 0, repo) | ||
|
||
// lets create a deployment | ||
input := &scm.DeploymentInput{ | ||
Ref: "12345", | ||
Payload: "deplyment", | ||
Environment: "production", | ||
Description: "my funky deployment thingy", | ||
} | ||
deploy, _, err := client.Deployments.Create(ctx, repo, input) | ||
require.NoError(t, err, "failed to create deploy in repo %s", repo) | ||
require.NotNil(t, deploy, "should have created a deployment") | ||
|
||
AssertDeploymentSize(t, ctx, client, 1, repo) | ||
AssertDeploymentStatusSize(t, ctx, client, 0, repo, deploy.ID) | ||
|
||
deploy2, _, err := client.Deployments.Find(ctx, repo, deploy.ID) | ||
require.NoError(t, err, "failed to find deploy in repo %s for deployment %s ", repo, deploy.ID) | ||
require.NotNil(t, deploy2, "should have found a deployment in repo %s for deployment %s ", repo, deploy.ID) | ||
|
||
// lets create a status | ||
statusInput := &scm.DeploymentStatusInput{ | ||
State: "success", | ||
TargetLink: "https://acme.com/my/app/thing", | ||
Description: "my update", | ||
Environment: "production", | ||
} | ||
status, _, err := client.Deployments.CreateStatus(ctx, repo, deploy.ID, statusInput) | ||
require.NoError(t, err, "failed to create status in repo %s for status %s", repo, deploy.ID) | ||
require.NotNil(t, status, "should have created a status") | ||
|
||
AssertDeploymentStatusSize(t, ctx, client, 1, repo, deploy.ID) | ||
|
||
status2, _, err := client.Deployments.FindStatus(ctx, repo, deploy.ID, status.ID) | ||
require.NoError(t, err, "failed to find status in repo %s for deployment %s status %s", repo, deploy.ID, status.ID) | ||
require.NotNil(t, status2, "should have found a status in repo %s for deployment %s status %s", repo, deploy.ID, status.ID) | ||
|
||
// lets delete the deployment | ||
_, err = client.Deployments.Delete(ctx, repo, deploy.ID) | ||
require.NoError(t, err, "failed to delete deploy in repo %s", repo) | ||
|
||
AssertDeploymentSize(t, ctx, client, 0, repo) | ||
} | ||
|
||
func AssertDeploymentSize(t *testing.T, ctx context.Context, client *scm.Client, size int, repo string) []*scm.Deployment { | ||
deploys, _, err := client.Deployments.List(ctx, repo, scm.ListOptions{}) | ||
require.NoError(t, err, "could not list deploys in repo %s", repo) | ||
require.Len(t, deploys, size, "deploy size") | ||
return deploys | ||
} | ||
|
||
func AssertDeploymentStatusSize(t *testing.T, ctx context.Context, client *scm.Client, size int, repo, deploymentID string) []*scm.DeploymentStatus { | ||
statuses, _, err := client.Deployments.ListStatus(ctx, repo, deploymentID, scm.ListOptions{}) | ||
require.NoError(t, err, "could not list statuses in repo %s deploymentID %s", repo, deploymentID) | ||
require.Len(t, statuses, size, "status size") | ||
return statuses | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters