2021-05-19 12:44:58 +00:00
|
|
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build !oss
|
|
|
|
|
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2021-06-01 09:29:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-05-19 12:44:58 +00:00
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/drone/store/shared/db/dbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var noContext = context.TODO()
|
|
|
|
|
|
|
|
func TestTemplate(t *testing.T) {
|
|
|
|
conn, err := dbtest.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
dbtest.Reset(conn)
|
|
|
|
dbtest.Disconnect(conn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
store := New(conn).(*templateStore)
|
2021-05-19 15:07:32 +00:00
|
|
|
t.Run("Create", testTemplateCreate(store))
|
2021-05-19 12:44:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:07:32 +00:00
|
|
|
func testTemplateCreate(store *templateStore) func(t *testing.T) {
|
2021-05-19 12:44:58 +00:00
|
|
|
return func(t *testing.T) {
|
|
|
|
item := &core.Template{
|
2021-05-28 15:59:00 +00:00
|
|
|
Id: 1,
|
|
|
|
Name: "my_template",
|
|
|
|
Namespace: "my_org",
|
|
|
|
Data: "some_template_data",
|
|
|
|
Created: 1,
|
|
|
|
Updated: 2,
|
2021-05-19 12:44:58 +00:00
|
|
|
}
|
|
|
|
err := store.Create(noContext, item)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if item.Id == 0 {
|
|
|
|
t.Errorf("Want template Id assigned, got %d", item.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Find", testTemplateFind(store, item))
|
2021-05-19 15:07:32 +00:00
|
|
|
t.Run("FindName", testTemplateFindName(store))
|
|
|
|
t.Run("ListAll", testTemplateListAll(store))
|
2021-05-28 15:59:00 +00:00
|
|
|
t.Run("List", testTemplateList(store))
|
2021-05-19 15:07:32 +00:00
|
|
|
t.Run("Update", testTemplateUpdate(store))
|
|
|
|
t.Run("Delete", testTemplateDelete(store))
|
2021-05-19 12:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testTemplateFind(store *templateStore, template *core.Template) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
item, err := store.Find(noContext, template.Id)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Run("Fields", testTemplate(item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 15:07:32 +00:00
|
|
|
func testTemplateFindName(store *templateStore) func(t *testing.T) {
|
2021-05-19 12:44:58 +00:00
|
|
|
return func(t *testing.T) {
|
2021-05-28 15:59:00 +00:00
|
|
|
item, err := store.FindName(noContext, "my_template", "my_org")
|
2021-05-19 12:44:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Run("Fields", testTemplate(item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testTemplate(item *core.Template) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
if got, want := item.Name, "my_template"; got != want {
|
|
|
|
t.Errorf("Want template name %q, got %q", want, got)
|
|
|
|
}
|
2021-05-28 15:59:00 +00:00
|
|
|
if got, want := item.Data, "some_template_data"; got != want {
|
2021-05-19 12:44:58 +00:00
|
|
|
t.Errorf("Want template data %q, got %q", want, got)
|
|
|
|
}
|
2021-05-28 15:59:00 +00:00
|
|
|
if got, want := item.Namespace, "my_org"; got != want {
|
|
|
|
t.Errorf("Want template org %q, got %q", want, got)
|
|
|
|
}
|
2021-05-19 12:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 15:07:32 +00:00
|
|
|
func testTemplateListAll(store *templateStore) func(t *testing.T) {
|
2021-05-19 12:44:58 +00:00
|
|
|
return func(t *testing.T) {
|
2021-05-19 15:07:32 +00:00
|
|
|
list, err := store.ListAll(noContext)
|
2021-05-19 12:44:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := len(list), 1; got != want {
|
|
|
|
t.Errorf("Want count %d, got %d", want, got)
|
|
|
|
} else {
|
|
|
|
t.Run("Fields", testTemplate(list[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:59:00 +00:00
|
|
|
func testTemplateList(store *templateStore) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
list, err := store.List(noContext, "my_org")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := len(list), 1; got != want {
|
|
|
|
t.Errorf("Want count %d, got %d", want, got)
|
|
|
|
} else {
|
|
|
|
t.Run("Fields", testTemplate(list[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 15:07:32 +00:00
|
|
|
func testTemplateUpdate(store *templateStore) func(t *testing.T) {
|
2021-05-19 12:44:58 +00:00
|
|
|
return func(t *testing.T) {
|
2021-05-28 15:59:00 +00:00
|
|
|
before, err := store.FindName(noContext, "my_template", "my_org")
|
2021-05-19 12:44:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = store.Update(noContext, before)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
after, err := store.Find(noContext, before.Id)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if after == nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 15:07:32 +00:00
|
|
|
func testTemplateDelete(store *templateStore) func(t *testing.T) {
|
2021-05-19 12:44:58 +00:00
|
|
|
return func(t *testing.T) {
|
2021-05-28 15:59:00 +00:00
|
|
|
secret, err := store.FindName(noContext, "my_template", "my_org")
|
2021-05-19 12:44:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = store.Delete(noContext, secret)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = store.Find(noContext, secret.Id)
|
|
|
|
if got, want := sql.ErrNoRows, err; got != want {
|
|
|
|
t.Errorf("Want sql.ErrNoRows, got %v", got)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|