Skip to content

Commit

Permalink
Merge pull request #40 from whywaita/fix/38
Browse files Browse the repository at this point in the history
can not delete target if runner is exist. so switch logical delete
  • Loading branch information
whywaita authored Mar 29, 2021
2 parents 9a44155 + 2bf6973 commit 87bfd27
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/datastore/mysql/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (m *MySQL) ListTargets(ctx context.Context) ([]datastore.Target, error) {

// DeleteTarget delete a target
func (m *MySQL) DeleteTarget(ctx context.Context, id uuid.UUID) error {
query := `DELETE FROM targets WHERE uuid = ?`
query := `UPDATE targets SET status = "deleted" WHERE uuid = ?`
if _, err := m.Conn.ExecContext(ctx, query, id.String()); err != nil {
return fmt.Errorf("failed to execute DELETE query: %w", err)
}
Expand Down
30 changes: 22 additions & 8 deletions pkg/datastore/mysql/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,22 @@ func TestMySQL_DeleteTarget(t *testing.T) {
}{
{
input: testTargetID,
want: nil,
err: false,
want: &datastore.Target{
UUID: testTargetID,
Scope: testScopeRepo,
GitHubPersonalToken: testGitHubPersonalToken,
ResourceType: datastore.ResourceTypeNano,
RunnerVersion: sql.NullString{
String: testRunnerVersion,
Valid: true,
},
ProviderURL: sql.NullString{
String: testProviderURL,
Valid: true,
},
Status: datastore.TargetStatusDeleted,
},
err: false,
},
}

Expand Down Expand Up @@ -362,7 +376,6 @@ func TestMySQL_UpdateStatus(t *testing.T) {
description: "",
},
want: &datastore.Target{
UUID: testTargetID,
Scope: testScopeRepo,
GitHubPersonalToken: testGitHubPersonalToken,
ResourceType: datastore.ResourceTypeNano,
Expand All @@ -388,7 +401,6 @@ func TestMySQL_UpdateStatus(t *testing.T) {
description: "job-id",
},
want: &datastore.Target{
UUID: testTargetID,
Scope: testScopeRepo,
GitHubPersonalToken: testGitHubPersonalToken,
ResourceType: datastore.ResourceTypeNano,
Expand All @@ -411,8 +423,9 @@ func TestMySQL_UpdateStatus(t *testing.T) {
}

for _, test := range tests {
tID := uuid.NewV4()
if err := testDatastore.CreateTarget(context.Background(), datastore.Target{
UUID: testTargetID,
UUID: tID,
Scope: testScopeRepo,
GitHubPersonalToken: testGitHubPersonalToken,
ResourceType: datastore.ResourceTypeNano,
Expand All @@ -428,15 +441,16 @@ func TestMySQL_UpdateStatus(t *testing.T) {
t.Fatalf("failed to create target: %+v", err)
}

err := testDatastore.UpdateTargetStatus(context.Background(), testTargetID, test.input.status, test.input.description)
err := testDatastore.UpdateTargetStatus(context.Background(), tID, test.input.status, test.input.description)
if !test.err && err != nil {
t.Fatalf("failed to update status: %+v", err)
}
got, err := getTargetFromSQL(testDB, testTargetID)
got, err := getTargetFromSQL(testDB, tID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
t.Fatalf("failed to get target from SQL: %+v", err)
}
if got != nil {
got.UUID = uuid.UUID{}
got.CreatedAt = time.Time{}
got.UpdatedAt = time.Time{}
}
Expand All @@ -445,7 +459,7 @@ func TestMySQL_UpdateStatus(t *testing.T) {
t.Errorf("mismatch (-want +got):\n%s", diff)
}

if err := testDatastore.DeleteTarget(context.Background(), testTargetID); err != nil {
if err := testDatastore.DeleteTarget(context.Background(), tID); err != nil {
t.Fatalf("failed to delete target: %+v", err)
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/web/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ func sanitizeTarget(t *datastore.Target) *datastore.Target {
return t
}

func handleTargetUpdate(w http.ResponseWriter, r *http.Request, ds datastore.Datastore) {}
func handleTargetUpdate(w http.ResponseWriter, r *http.Request, ds datastore.Datastore) {
outputErrorMsg(w, http.StatusMethodNotAllowed, "not implement")
return
}

func handleTargetDelete(w http.ResponseWriter, r *http.Request, ds datastore.Datastore) {
ctx := r.Context()
Expand Down
81 changes: 78 additions & 3 deletions pkg/web/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ func Test_handleTargetRead(t *testing.T) {
targetUUID := respTarget.UUID

tests := []struct {
input interface{}
input uuid.UUID
want *datastore.Target
err bool
}{
{
input: nil,
input: targetUUID,
want: &datastore.Target{
UUID: targetUUID,
Scope: "repo",
Expand All @@ -243,7 +243,7 @@ func Test_handleTargetRead(t *testing.T) {
}

for _, test := range tests {
resp, err := http.Get(fmt.Sprintf("%s/target/%s", testURL, targetUUID))
resp, err := http.Get(fmt.Sprintf("%s/target/%s", testURL, test.input))
if !test.err && err != nil {
t.Fatalf("failed to POST request: %+v", err)
}
Expand All @@ -265,3 +265,78 @@ func Test_handleTargetRead(t *testing.T) {
}
}
}

func Test_handleTargetDelete(t *testing.T) {
testURL := testutils.GetTestURL()
testDatastore, teardown := testutils.GetTestDatastore()
defer teardown()

setStubFunctions()

target := `{"scope": "repo", "github_personal_token": "secret", "resource_type": "micro", "runner_user": "ubuntu"}`

resp, err := http.Post(testURL+"/target", "application/json", bytes.NewBufferString(target))
if err != nil {
t.Fatalf("failed to POST request: %+v", err)
}
content, statusCode := parseResponse(resp)
if statusCode != http.StatusCreated {
t.Fatalf("must be response statuscode is 201, but got %d", resp.StatusCode)
}
var respTarget datastore.Target
if err := json.Unmarshal(content, &respTarget); err != nil {
t.Fatalf("failed to unmarshal response JSON: %+v", err)
}
targetUUID := respTarget.UUID

tests := []struct {
input uuid.UUID
want *datastore.Target
err bool
}{
{
input: targetUUID,
want: &datastore.Target{
UUID: targetUUID,
Scope: "repo",
GitHubPersonalToken: "secret",
ResourceType: datastore.ResourceTypeMicro,
RunnerUser: sql.NullString{
Valid: true,
String: "ubuntu",
},
Status: datastore.TargetStatusDeleted,
},
},
}

for _, test := range tests {
client := &http.Client{}

req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/target/%s", testURL, test.input), nil)
if err != nil {
t.Fatalf("failed to create request: %+v", err)
}

resp, err := client.Do(req)
if !test.err && err != nil {
t.Fatalf("failed to POST request: %+v", err)
}
_, code := parseResponse(resp)
if code != http.StatusNoContent {
t.Fatalf("must be response statuscode is 204, but got %d", code)
}

got, err := testDatastore.GetTarget(context.Background(), test.input)
if err != nil {
t.Fatalf("failed to get target from datastore: %+v", err)
}

got.CreatedAt = time.Time{}
got.UpdatedAt = time.Time{}

if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
}

0 comments on commit 87bfd27

Please sign in to comment.