harness-drone/service/content/cache/contents_test.go

101 lines
2.6 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +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.
2019-02-28 07:07:13 +00:00
// +build !oss
2019-02-19 23:56:41 +00:00
package cache
import (
"context"
"fmt"
"testing"
"github.com/drone/drone/core"
2019-02-28 07:07:13 +00:00
"github.com/drone/drone/mock"
2019-02-19 23:56:41 +00:00
"github.com/drone/go-scm/scm"
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
)
var noContext = context.Background()
func TestFind(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUser := &core.User{}
mockFile := &core.File{
Data: []byte("hello world"),
Hash: []byte(""),
}
mockContents := mock.NewMockFileService(controller)
mockContents.EXPECT().Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml").Return(mockFile, nil)
2019-02-19 23:56:41 +00:00
service := Contents(mockContents).(*service)
want := &core.File{
Data: []byte("hello world"),
Hash: []byte(""),
}
got, err := service.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml")
2019-02-19 23:56:41 +00:00
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
if len(service.cache.Keys()) == 0 {
t.Errorf("Expect item added to cache")
}
}
func TestFindError(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUser := &core.User{}
mockContents := mock.NewMockFileService(controller)
mockContents.EXPECT().Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml").Return(nil, scm.ErrNotFound)
2019-02-19 23:56:41 +00:00
service := Contents(mockContents).(*service)
_, err := service.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml")
2019-02-19 23:56:41 +00:00
if err != scm.ErrNotFound {
t.Errorf("Expect not found error")
}
}
func TestFindCache(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUser := &core.User{}
mockFile := &core.File{
Data: []byte("hello world"),
Hash: []byte(""),
}
key := fmt.Sprintf(contentKey, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", ".drone.yml")
2019-02-19 23:56:41 +00:00
service := Contents(nil).(*service)
service.cache.Add(key, mockFile)
want := &core.File{
Data: []byte("hello world"),
Hash: []byte(""),
}
got, err := service.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml")
2019-02-19 23:56:41 +00:00
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}