| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- Copyright 2020 Docker Compose CLI authors
- 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.
- */
- package compose
- import (
- "errors"
- "os"
- "slices"
- "testing"
- "github.com/compose-spec/compose-go/v2/loader"
- "github.com/compose-spec/compose-go/v2/types"
- "github.com/google/go-cmp/cmp"
- v1 "github.com/opencontainers/image-spec/specs-go/v1"
- "gotest.tools/v3/assert"
- "github.com/docker/compose/v5/internal"
- "github.com/docker/compose/v5/pkg/api"
- )
- func Test_createLayers(t *testing.T) {
- project, err := loader.LoadWithContext(t.Context(), types.ConfigDetails{
- WorkingDir: "testdata/publish/",
- Environment: types.Mapping{},
- ConfigFiles: []types.ConfigFile{
- {
- Filename: "testdata/publish/compose.yaml",
- },
- },
- })
- assert.NilError(t, err)
- project.ComposeFiles = []string{"testdata/publish/compose.yaml"}
- service := &composeService{}
- layers, err := service.createLayers(t.Context(), project, api.PublishOptions{
- WithEnvironment: true,
- })
- assert.NilError(t, err)
- published := string(layers[0].Data)
- assert.Equal(t, published, `name: test
- services:
- test:
- extends:
- file: f8f9ede3d201ec37d5a5e3a77bbadab79af26035e53135e19571f50d541d390c.yaml
- service: foo
- string:
- image: test
- env_file: 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
- list:
- image: test
- env_file:
- - 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
- mapping:
- image: test
- env_file:
- - path: 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
- `)
- expectedLayers := []v1.Descriptor{
- {
- MediaType: "application/vnd.docker.compose.file+yaml",
- Annotations: map[string]string{
- "com.docker.compose.file": "compose.yaml",
- "com.docker.compose.version": internal.Version,
- },
- },
- {
- MediaType: "application/vnd.docker.compose.file+yaml",
- Annotations: map[string]string{
- "com.docker.compose.extends": "true",
- "com.docker.compose.file": "f8f9ede3d201ec37d5a5e3a77bbadab79af26035e53135e19571f50d541d390c",
- "com.docker.compose.version": internal.Version,
- },
- },
- {
- MediaType: "application/vnd.docker.compose.envfile",
- Annotations: map[string]string{
- "com.docker.compose.envfile": "5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3",
- "com.docker.compose.version": internal.Version,
- },
- },
- }
- assert.DeepEqual(t, expectedLayers, layers, cmp.FilterPath(func(path cmp.Path) bool {
- return !slices.Contains([]string{".Data", ".Digest", ".Size"}, path.String())
- }, cmp.Ignore()))
- }
- func Test_preChecks_sensitive_data_detected_decline(t *testing.T) {
- dir := t.TempDir()
- envPath := dir + "/secrets.env"
- secretData := `AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"`
- err := os.WriteFile(envPath, []byte(secretData), 0o600)
- assert.NilError(t, err)
- project := &types.Project{
- Services: types.Services{
- "web": {
- Name: "web",
- Image: "nginx",
- EnvFiles: []types.EnvFile{
- {Path: envPath, Required: true},
- },
- },
- },
- }
- declined := func(message string, defaultValue bool) (bool, error) {
- return false, nil
- }
- svc := &composeService{
- prompt: declined,
- }
- accept, err := svc.preChecks(t.Context(), project, api.PublishOptions{})
- assert.NilError(t, err)
- assert.Equal(t, accept, false)
- }
- func Test_publish_decline_returns_ErrCanceled(t *testing.T) {
- project := &types.Project{
- Services: types.Services{
- "web": {
- Name: "web",
- Image: "nginx",
- Volumes: []types.ServiceVolumeConfig{
- {
- Type: types.VolumeTypeBind,
- Source: "/host/path",
- Target: "/container/path",
- },
- },
- },
- },
- }
- declined := func(message string, defaultValue bool) (bool, error) {
- return false, nil
- }
- svc := &composeService{
- prompt: declined,
- events: &ignore{},
- }
- err := svc.publish(t.Context(), project, "docker.io/myorg/myapp:latest", api.PublishOptions{})
- assert.Assert(t, errors.Is(err, api.ErrCanceled),
- "expected api.ErrCanceled when user declines, got: %v", err)
- }
|