Skip to content

Commit

Permalink
provider/zencoder: use dbtest in tests
Browse files Browse the repository at this point in the history
Redis should not be used from packages. Fixes #240.
  • Loading branch information
fsouza committed Jul 26, 2019
1 parent 15af2ab commit b18e42e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 118 deletions.
2 changes: 1 addition & 1 deletion db/dbtest/fake_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (d *fakeRepository) CreateLocalPreset(preset *db.LocalPreset) error {
return errors.New("database error")
}
if preset.Name == "" {
return errors.New("invalid local preset name")
return errors.New("preset name missing")
}
if _, ok := d.localpresets[preset.Name]; ok {
return db.ErrLocalPresetAlreadyExists
Expand Down
144 changes: 27 additions & 117 deletions provider/zencoder/zencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (

"github.com/NYTimes/video-transcoding-api/config"
"github.com/NYTimes/video-transcoding-api/db"
"github.com/NYTimes/video-transcoding-api/db/redis"
"github.com/NYTimes/video-transcoding-api/db/redis/storage"
"github.com/NYTimes/video-transcoding-api/db/dbtest"
"github.com/NYTimes/video-transcoding-api/provider"
"github.com/flavioribeiro/zencoder"
redisDriver "github.com/go-redis/redis"
"github.com/kr/pretty"
)

Expand All @@ -24,11 +22,7 @@ func TestFactoryIsRegistered(t *testing.T) {
}

func TestZencoderFactory(t *testing.T) {
cfg := config.Config{
Zencoder: &config.Zencoder{
APIKey: "api-key-here",
},
}
cfg := config.Config{Zencoder: &config.Zencoder{APIKey: "api-key-here"}}
prov, err := zencoderFactory(&cfg)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -80,11 +74,6 @@ func TestZencoderCapabilities(t *testing.T) {
}

func TestZencoderCreatePreset(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
preset := db.Preset{
Audio: db.AudioPreset{
Bitrate: "128000",
Expand All @@ -104,16 +93,7 @@ func TestZencoderCreatePreset(t *testing.T) {
Height: "1080",
},
}
provider, err := zencoderFactory(&cfg)
if err != nil {
t.Fatal(err)
}

repo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}

provider, repo := testProvider(t)
presetName, err := provider.CreatePreset(preset)
if err != nil {
t.Fatal(err)
Expand All @@ -132,29 +112,16 @@ func TestZencoderCreatePreset(t *testing.T) {
}

func TestCreatePresetError(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
preset := db.Preset{}
provider, err := zencoderFactory(&cfg)
if err != nil {
t.Fatal(err)
}
provider, _ := testProvider(t)

_, err = provider.CreatePreset(preset)
_, err := provider.CreatePreset(preset)
if expectedMsg := "preset name missing"; err.Error() != expectedMsg {
t.Errorf("got wrong error message\nwant %q\ngot %q", expectedMsg, err.Error())
}
}

func TestGetPreset(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
preset := db.Preset{
Name: "get_preset",
Video: db.VideoPreset{
Expand All @@ -169,11 +136,7 @@ func TestGetPreset(t *testing.T) {
Codec: "aac",
},
}
provider, err := zencoderFactory(&cfg)
if err != nil {
t.Fatal(err)
}

provider, _ := testProvider(t)
presetName, err := provider.CreatePreset(preset)
if err != nil {
t.Fatal(err)
Expand All @@ -192,11 +155,6 @@ func TestGetPreset(t *testing.T) {
}

func TestZencoderDeletePreset(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
preset := db.Preset{
Name: "get_preset",
Video: db.VideoPreset{
Expand All @@ -211,10 +169,7 @@ func TestZencoderDeletePreset(t *testing.T) {
Codec: "aac",
},
}
prov, err := zencoderFactory(&cfg)
if err != nil {
t.Fatal(err)
}
prov, _ := testProvider(t)
presetName, err := prov.CreatePreset(preset)
if err != nil {
t.Fatal(err)
Expand All @@ -230,16 +185,11 @@ func TestZencoderDeletePreset(t *testing.T) {
}

func TestZencoderTranscode(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
Expand All @@ -265,7 +215,7 @@ func TestZencoderTranscode(t *testing.T) {
Width: "720",
},
}
_, err = prov.CreatePreset(preset)
_, err := prov.CreatePreset(preset)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -297,16 +247,11 @@ func TestZencoderTranscode(t *testing.T) {
}

func TestZencoderBuildOutputs(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here", Destination: "https://log:pass@s3.here.com"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
Expand Down Expand Up @@ -584,7 +529,6 @@ func TestZencoderBuildOutputs(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.Description, func(t *testing.T) {
cleanLocalPresets()
for _, preset := range test.Presets {
_, err := prov.CreatePreset(preset)
if err != nil {
Expand Down Expand Up @@ -808,20 +752,16 @@ func TestZencoderBuildOutput(t *testing.T) {
func TestZencoderHealthcheck(t *testing.T) {
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
db: dbRepo,
}

err = prov.Healthcheck()
err := prov.Healthcheck()
if err != nil {
t.Fatal(err)
}
Expand All @@ -830,20 +770,16 @@ func TestZencoderHealthcheck(t *testing.T) {
func TestZencoderCancelJob(t *testing.T) {
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
db: dbRepo,
}

err = prov.CancelJob("123")
err := prov.CancelJob("123")
if err != nil {
t.Fatal(err)
}
Expand All @@ -852,13 +788,9 @@ func TestZencoderCancelJob(t *testing.T) {
func TestZencoderJobStatus(t *testing.T) {
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
Expand Down Expand Up @@ -1022,13 +954,9 @@ func TestZencoderJobStatus(t *testing.T) {
func TestZencoderStatusMap(t *testing.T) {
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
Expand All @@ -1053,16 +981,11 @@ func TestZencoderStatusMap(t *testing.T) {
}
}
func TestZencoderGetResolution(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
Expand Down Expand Up @@ -1092,16 +1015,11 @@ func TestZencoderGetResolution(t *testing.T) {
}

func TestGetJobOutputs(t *testing.T) {
cleanLocalPresets()
cfg := config.Config{
Zencoder: &config.Zencoder{APIKey: "api-key-here", Destination: "s3://login:pass@aws.com"},
Redis: new(storage.Config),
}
fakeZencoder := &FakeZencoder{}
dbRepo, err := redis.NewRepository(&cfg)
if err != nil {
t.Fatal(err)
}
dbRepo := dbtest.NewFakeRepository(false)
prov := &zencoderProvider{
config: &cfg,
client: fakeZencoder,
Expand Down Expand Up @@ -1135,25 +1053,17 @@ func TestGetJobOutputs(t *testing.T) {
}
}

func cleanLocalPresets() error {
client := redisDriver.NewClient(&redisDriver.Options{Addr: "127.0.0.1:6379"})
defer client.Close()
err := deleteKeys("localpreset:*", client)
func testProvider(t *testing.T) (*zencoderProvider, db.Repository) {
t.Helper()
cfg := config.Config{Zencoder: &config.Zencoder{APIKey: "api-key-here"}}
p, err := zencoderFactory(&cfg)
if err != nil {
return err
t.Fatal(err)
}

err = deleteKeys("localpresets", client)
return err
}
repo := dbtest.NewFakeRepository(false)
provider := p.(*zencoderProvider)
provider.db = repo

func deleteKeys(pattern string, client *redisDriver.Client) error {
keys, err := client.Keys(pattern).Result()
if err != nil {
return err
}
if len(keys) > 0 {
_, err = client.Del(keys...).Result()
}
return err
return provider, repo
}

1 comment on commit b18e42e

@zsiec
Copy link
Member

@zsiec zsiec commented on b18e42e Jul 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I’ve seen this failure before and it was in my list to fix. :)

Please sign in to comment.