Skip to content

Commit

Permalink
fix: add a fake deployments implementation
Browse files Browse the repository at this point in the history
so we can fake out testing deployments
  • Loading branch information
jstrachan committed Mar 17, 2021
1 parent 547c29e commit e4c925c
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 1 deletion.
4 changes: 4 additions & 0 deletions scm/driver/fake/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Data struct {
Users []*scm.User
Hooks map[string][]*scm.Hook
Releases map[string]map[int]*scm.Release
Deployments map[string][]*scm.Deployment
DeploymentStatus map[string][]*scm.DeploymentStatus

//All Labels That Exist In The Repo
RepoLabelsExisting []string
Expand Down Expand Up @@ -113,5 +115,7 @@ func NewData() *Data {
AssigneesAdded: []string{},
UserPermissions: map[string]map[string]string{},
Hooks: map[string][]*scm.Hook{},
Deployments: map[string][]*scm.Deployment{},
DeploymentStatus: map[string][]*scm.DeploymentStatus{},
}
}
109 changes: 109 additions & 0 deletions scm/driver/fake/deploy.go
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
}
75 changes: 75 additions & 0 deletions scm/driver/fake/deploy_test.go
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
}
3 changes: 2 additions & 1 deletion scm/driver/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func NewDefault() (*scm.Client, *Data) {
// initialize services
client.Driver = scm.DriverFake

client.Contents = &contentService{client: client, data: data}
client.Deployments = &deploymentService{client: client, data: data}
client.Git = &gitService{client: client, data: data}
client.Issues = &issueService{client: client, data: data}
client.Organizations = &organizationService{client: client, data: data}
Expand All @@ -31,7 +33,6 @@ func NewDefault() (*scm.Client, *Data) {
client.Releases = &releaseService{client: client, data: data}
client.Reviews = &reviewService{client: client, data: data}
client.Users = &userService{client: client, data: data}
client.Contents = &contentService{client: client, data: data}

// TODO
/*
Expand Down

0 comments on commit e4c925c

Please sign in to comment.