2020-06-18 16:13:24 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-20 15:06:08 +02:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-08-04 14:03:24 +02:00
|
|
|
"testing"
|
2020-05-20 15:06:08 +02:00
|
|
|
|
2020-08-04 14:03:24 +02:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/assert/cmp"
|
2020-05-20 15:06:08 +02:00
|
|
|
|
2020-08-21 17:24:53 +02:00
|
|
|
apicontext "github.com/docker/compose-cli/context"
|
|
|
|
"github.com/docker/compose-cli/context/store"
|
2020-05-20 15:06:08 +02:00
|
|
|
)
|
|
|
|
|
2020-08-04 14:03:24 +02:00
|
|
|
// TestCLI is a helper struct for CLI tests.
|
|
|
|
type TestCLI struct {
|
|
|
|
ctx context.Context
|
|
|
|
writer *os.File
|
|
|
|
reader *os.File
|
2020-05-20 15:06:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 14:03:24 +02:00
|
|
|
// NewTestCLI returns a CLI testing helper.
|
|
|
|
func NewTestCLI(t *testing.T) *TestCLI {
|
2020-05-20 15:06:08 +02:00
|
|
|
dir, err := ioutil.TempDir("", "store")
|
2020-08-04 14:03:24 +02:00
|
|
|
assert.Check(t, cmp.Nil(err))
|
|
|
|
|
|
|
|
originalStdout := os.Stdout
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Stdout = originalStdout
|
|
|
|
_ = os.RemoveAll(dir)
|
|
|
|
})
|
|
|
|
|
2020-05-20 15:06:08 +02:00
|
|
|
s, err := store.New(
|
|
|
|
store.WithRoot(dir),
|
|
|
|
)
|
2020-08-04 14:03:24 +02:00
|
|
|
assert.Check(t, cmp.Nil(err))
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
err = s.Create("example", "example", "", store.ContextMetadata{})
|
2020-08-04 14:03:24 +02:00
|
|
|
assert.Check(t, cmp.Nil(err))
|
2020-05-20 15:06:08 +02:00
|
|
|
|
2020-08-04 14:03:24 +02:00
|
|
|
ctx := context.Background()
|
2020-05-20 15:06:08 +02:00
|
|
|
ctx = store.WithContextStore(ctx, s)
|
2020-08-04 14:03:24 +02:00
|
|
|
ctx = apicontext.WithCurrentContext(ctx, "example")
|
2020-05-20 15:06:08 +02:00
|
|
|
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
os.Stdout = w
|
2020-08-04 14:03:24 +02:00
|
|
|
assert.Check(t, cmp.Nil(err))
|
|
|
|
return &TestCLI{ctx, w, r}
|
2020-05-20 15:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns a configured context
|
2020-08-04 14:03:24 +02:00
|
|
|
func (c *TestCLI) Context() context.Context {
|
|
|
|
return c.ctx
|
2020-05-20 15:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStdOut returns the output of the command
|
2020-08-04 14:03:24 +02:00
|
|
|
func (c *TestCLI) GetStdOut() string {
|
|
|
|
_ = c.writer.Close()
|
|
|
|
out, _ := ioutil.ReadAll(c.reader)
|
2020-05-20 15:06:08 +02:00
|
|
|
return string(out)
|
|
|
|
}
|