Skip to content

Commit

Permalink
Added unit test for checking the duplicate name for template
Browse files Browse the repository at this point in the history
Signed-off-by: parauliya <[email protected]>
  • Loading branch information
parauliya committed Aug 28, 2020
1 parent acbf4df commit dd74eb6
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
36 changes: 36 additions & 0 deletions db/mock/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ package mock

import (
"context"
"errors"

"github.com/golang/protobuf/ptypes/timestamp"
uuid "github.com/satori/go.uuid"
)

type template struct {
id uuid.UUID
data string
}

var templateDB = map[string]interface{}{}

// CreateTemplate creates a new workflow template
func (d DB) CreateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error {
if len(templateDB) > 0 {
if _, ok := templateDB[name]; ok {
return errors.New("Template name already exist in the database")
}
templateDB[name] = template{
id: id,
data: data,
}

} else {
templateDB[name] = template{
id: id,
data: data,
}
}
return nil
}

Expand All @@ -19,6 +42,12 @@ func (d DB) GetTemplate(ctx context.Context, id string) (string, string, error)

// DeleteTemplate deletes a workflow template
func (d DB) DeleteTemplate(ctx context.Context, name string) error {
if len(templateDB) > 0 {
if _, ok := templateDB[name]; !ok {
return errors.New("Template name does not exist")
}
delete(templateDB, name)
}
return nil
}

Expand All @@ -31,3 +60,10 @@ func (d DB) ListTemplates(fn func(id, n string, in, del *timestamp.Timestamp) er
func (d DB) UpdateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error {
return nil
}

// ClearTemplateDB clear all the templates
func (d DB) ClearTemplateDB() {
for name := range templateDB {
delete(templateDB, name)
}
}
2 changes: 1 addition & 1 deletion grpc-server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (s *server) CreateTemplate(ctx context.Context, in *template.WorkflowTempla

logger.Info(msg)
err := fn()
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
l := logger
Expand All @@ -41,6 +40,7 @@ func (s *server) CreateTemplate(ctx context.Context, in *template.WorkflowTempla
l.Error(err)
return &template.CreateResponse{}, err
}
logger.Info("done " + msg)
return &template.CreateResponse{Id: id.String()}, err
}

Expand Down
75 changes: 75 additions & 0 deletions grpc-server/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package grpcserver

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/tinkerbell/tink/db/mock"
pb "github.com/tinkerbell/tink/protos/template"
)

const (
template1 = `version: "0.1"
name: hello_world_workflow
global_timeout: 600
tasks:
- name: "hello world"
worker: "{{.device_1}}"
actions:
- name: "hello_world"
image: hello-world
timeout: 60`

template2 = `version: "0.1"
name: hello_world_workflow
global_timeout: 600
tasks:
- name: "hello world again"
worker: "{{.device_1}}"
actions:
- name: "hello_world_again"
image: hello-world
timeout: 60`
)

func TestDuplicateTemplateName(t *testing.T) {
type (
args struct {
db mock.DB
name string
}
want struct {
expectedError bool
}
)
testCases := map[string]struct {
args args
want want
}{
"test_1": {
args: args{
db: mock.DB{},
name: "template_1",
},
want: want{
expectedError: true,
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
s := testServer(tc.args.db)
res, err := s.CreateTemplate(context.TODO(), &pb.WorkflowTemplate{Name: tc.args.name, Data: template1})
assert.Nil(t, err)
assert.NotNil(t, res)
if err == nil {
res, err = s.CreateTemplate(context.TODO(), &pb.WorkflowTemplate{Name: tc.args.name, Data: template2})
}
if err != nil {
assert.Error(t, err)
assert.True(t, tc.want.expectedError)
}
})
}
}
3 changes: 2 additions & 1 deletion grpc-server/tinkerbell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/tinkerbell/tink/db"
"github.com/tinkerbell/tink/db/mock"
"github.com/tinkerbell/tink/metrics"
pb "github.com/tinkerbell/tink/protos/workflow"
)

Expand All @@ -39,7 +40,7 @@ func TestMain(m *testing.M) {

l, _, _ := log.Init("github.com/tinkerbell/tink")
logger = l.Package("grpcserver")

metrics.SetupMetrics("onprem", logger)
os.Exit(m.Run())
}

Expand Down

0 comments on commit dd74eb6

Please sign in to comment.