Files
fn-serverless/api/agent/drivers/mock/mocker.go
Tolga Ceylan fe2b9fb53d fn: cookie and driver api changes (#1312)
Now obsoleted driver.PrepareCookie() call handled image and
container creation. In agent, going forward we will need finer
grained control over the timeouts implied by the contexts.
For this reason, with this change, we split PrepareCookie()
into Validate/Pull/Create calls under Cookie interface.
2018-11-14 16:51:05 -08:00

89 lines
1.6 KiB
Go

// Package mock provides a fake Driver implementation that is only used for testing.
package mock
import (
"context"
"fmt"
"time"
"github.com/fnproject/fn/api/agent/drivers"
)
func New() drivers.Driver {
return &Mocker{}
}
type Mocker struct {
count int
}
func (m *Mocker) CreateCookie(context.Context, drivers.ContainerTask) (drivers.Cookie, error) {
return &cookie{m}, nil
}
func (m *Mocker) PrepareCookie(context.Context, drivers.Cookie) error {
return nil
}
func (m *Mocker) Close() error {
return nil
}
var _ drivers.Driver = &Mocker{}
type cookie struct {
m *Mocker
}
func (c *cookie) Freeze(context.Context) error {
return nil
}
func (c *cookie) Unfreeze(context.Context) error {
return nil
}
func (c *cookie) ValidateImage(context.Context) (bool, error) {
return false, nil
}
func (c *cookie) PullImage(context.Context) error {
return nil
}
func (c *cookie) CreateContainer(context.Context) error {
return nil
}
func (c *cookie) Close(context.Context) error {
return nil
}
func (c *cookie) ContainerOptions() interface{} {
return nil
}
func (c *cookie) Run(ctx context.Context) (drivers.WaitResult, error) {
c.m.count++
if c.m.count%100 == 0 {
return nil, fmt.Errorf("Mocker error! Bad.")
}
return &runResult{
err: nil,
status: "success",
start: time.Now(),
}, nil
}
var _ drivers.Cookie = &cookie{}
type runResult struct {
err error
status string
start time.Time
}
func (r *runResult) Wait(context.Context) drivers.RunResult { return r }
func (r *runResult) Status() string { return r.status }
func (r *runResult) Error() error { return r.err }