| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package framework
- import (
- "context"
- "io/ioutil"
- "os"
- "github.com/stretchr/testify/require"
- "github.com/stretchr/testify/suite"
- apicontext "github.com/docker/api/context"
- "github.com/docker/api/context/store"
- )
- // CliSuite is a helper struct that creates a configured context
- // and captures the output of a command. it should be used in the
- // same way as testify.suite.Suite
- type CliSuite struct {
- suite.Suite
- ctx context.Context
- writer *os.File
- reader *os.File
- OriginalStdout *os.File
- storeRoot string
- }
- // BeforeTest is called by testify.suite
- func (sut *CliSuite) BeforeTest(suiteName, testName string) {
- ctx := context.Background()
- ctx = apicontext.WithCurrentContext(ctx, "example")
- dir, err := ioutil.TempDir("", "store")
- require.Nil(sut.T(), err)
- s, err := store.New(
- store.WithRoot(dir),
- )
- require.Nil(sut.T(), err)
- err = s.Create("example", "example", "", store.ContextMetadata{})
- require.Nil(sut.T(), err)
- sut.storeRoot = dir
- ctx = store.WithContextStore(ctx, s)
- sut.ctx = ctx
- sut.OriginalStdout = os.Stdout
- r, w, err := os.Pipe()
- require.Nil(sut.T(), err)
- os.Stdout = w
- sut.writer = w
- sut.reader = r
- }
- // Context returns a configured context
- func (sut *CliSuite) Context() context.Context {
- return sut.ctx
- }
- // GetStdOut returns the output of the command
- func (sut *CliSuite) GetStdOut() string {
- err := sut.writer.Close()
- require.Nil(sut.T(), err)
- out, _ := ioutil.ReadAll(sut.reader)
- return string(out)
- }
- // AfterTest is called by testify.suite
- func (sut *CliSuite) AfterTest(suiteName, testName string) {
- os.Stdout = sut.OriginalStdout
- err := os.RemoveAll(sut.storeRoot)
- require.Nil(sut.T(), err)
- }
|