github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/e2e/pushpull_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"gotest.tools/assert"
    10  	"gotest.tools/assert/cmp"
    11  	"gotest.tools/icmd"
    12  )
    13  
    14  func TestPushUnknown(t *testing.T) {
    15  	cmd, cleanup := dockerCli.createTestCmd()
    16  	defer cleanup()
    17  
    18  	t.Run("push unknown reference", func(t *testing.T) {
    19  		cmd.Command = dockerCli.Command("app", "push", "unknown")
    20  		icmd.RunCmd(cmd).Assert(t, icmd.Expected{
    21  			ExitCode: 1,
    22  			Err:      `could not push "unknown": unknown: reference not found`,
    23  		})
    24  	})
    25  
    26  	t.Run("push invalid reference", func(t *testing.T) {
    27  		cmd.Command = dockerCli.Command("app", "push", "@")
    28  		icmd.RunCmd(cmd).Assert(t, icmd.Expected{
    29  			ExitCode: 1,
    30  			Err:      `could not push "@": could not parse "@" as a valid reference: invalid reference format`,
    31  		})
    32  	})
    33  }
    34  
    35  func TestPushInsecureRegistry(t *testing.T) {
    36  	runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
    37  		path := filepath.Join("testdata", "local")
    38  		ref := info.registryAddress + "/test/push-insecure"
    39  
    40  		// create a command outside of the dind context so without the insecure registry configured
    41  		cmdNoInsecureRegistry, cleanupNoInsecureRegistryCommand := dockerCli.createTestCmd()
    42  		defer cleanupNoInsecureRegistryCommand()
    43  		build(t, cmdNoInsecureRegistry, dockerCli, ref, path)
    44  		cmdNoInsecureRegistry.Command = dockerCli.Command("app", "push", ref)
    45  		icmd.RunCmd(cmdNoInsecureRegistry).Assert(t, icmd.Expected{ExitCode: 1})
    46  
    47  		// run the push with the command inside dind context configured to allow access to the insecure registr
    48  		cmd := info.configuredCmd
    49  		build(t, cmd, dockerCli, ref, path)
    50  		cmd.Command = dockerCli.Command("app", "push", ref)
    51  		icmd.RunCmd(cmd).Assert(t, icmd.Success)
    52  	})
    53  }
    54  
    55  func TestPushInstall(t *testing.T) {
    56  	runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
    57  		cmd := info.configuredCmd
    58  		ref := info.registryAddress + "/test/push-pull"
    59  		build(t, cmd, dockerCli, ref, filepath.Join("testdata", "push-pull"))
    60  
    61  		cmd.Command = dockerCli.Command("app", "push", ref)
    62  		icmd.RunCmd(cmd).Assert(t, icmd.Success)
    63  
    64  		cmd.Command = dockerCli.Command("app", "run", ref, "--name", t.Name())
    65  		icmd.RunCmd(cmd).Assert(t, icmd.Success)
    66  		cmd.Command = dockerCli.Command("service", "ls")
    67  		assert.Check(t, cmp.Contains(icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), t.Name()))
    68  	})
    69  }
    70  
    71  func TestPushPullInstall(t *testing.T) {
    72  	runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
    73  		cmd := info.configuredCmd
    74  		ref := info.registryAddress + "/test/push-pull"
    75  		tag := ":v.0.0.1"
    76  		build(t, cmd, dockerCli, ref+tag, filepath.Join("testdata", "push-pull"))
    77  
    78  		cmd.Command = dockerCli.Command("app", "push", ref+tag)
    79  		icmd.RunCmd(cmd).Assert(t, icmd.Success)
    80  		cmd.Command = dockerCli.Command("app", "pull", ref+tag)
    81  		icmd.RunCmd(cmd).Assert(t, icmd.Success)
    82  
    83  		// stop the registry
    84  		info.stopRegistry()
    85  
    86  		// install from local store
    87  		cmd.Command = dockerCli.Command("app", "run", ref+tag, "--name", t.Name())
    88  		icmd.RunCmd(cmd).Assert(t, icmd.Success)
    89  
    90  		// listing the installed application shows the pulled application reference
    91  		cmd.Command = dockerCli.Command("app", "ls")
    92  		checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(),
    93  			[]string{
    94  				fmt.Sprintf(`%s\s+push-pull \(1.1.0-beta1\)\s+\d/1\s+install\s+success\s+.+second[s]?\sago\s+.+second[s]?\sago\s+%s`, t.Name(), ref+tag),
    95  			})
    96  
    97  		// install should fail (registry is stopped)
    98  		cmd.Command = dockerCli.Command("app", "run", "unknown")
    99  		//nolint: lll
   100  		expected := `Unable to find App image "unknown:latest" locally
   101  Unable to find App "unknown": failed to resolve bundle manifest "docker.io/library/unknown:latest": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed`
   102  		icmd.RunCmd(cmd).Assert(t, icmd.Expected{
   103  			ExitCode: 1,
   104  			Err:      expected,
   105  			Out:      "Pulling from registry...",
   106  		})
   107  	})
   108  }
   109  
   110  func TestPushInstallBundle(t *testing.T) {
   111  	runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
   112  		cmd := info.configuredCmd
   113  		ref := info.registryAddress + "/test/push-bundle"
   114  
   115  		// render the app to a bundle, we use the app from the push pull test above.
   116  		build(t, cmd, dockerCli, "a-simple-app:1.0.0", filepath.Join("testdata", "push-pull"))
   117  
   118  		// push it and install to check it is available
   119  		t.Run("push-bundle", func(t *testing.T) {
   120  			name := strings.Replace(t.Name(), "/", "_", 1)
   121  			cmd.Command = dockerCli.Command("app", "image", "tag", "a-simple-app:1.0.0", ref)
   122  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   123  			cmd.Command = dockerCli.Command("app", "push", ref)
   124  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   125  
   126  			cmd.Command = dockerCli.Command("app", "run", ref, "--name", name)
   127  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   128  			cmd.Command = dockerCli.Command("service", "ls")
   129  			assert.Check(t, cmp.Contains(icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), name))
   130  
   131  			// ensure it doesn't confuse the next test
   132  			cmd.Command = dockerCli.Command("app", "rm", name)
   133  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   134  
   135  			cmd.Command = dockerCli.Command("service", "ls")
   136  			assert.Check(t, !strings.Contains(icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), name))
   137  		})
   138  
   139  		// push it again using the first ref and install from the new ref to check it is also available
   140  		t.Run("push-ref", func(t *testing.T) {
   141  			name := strings.Replace(t.Name(), "/", "_", 1)
   142  			ref2 := info.registryAddress + "/test/push-ref"
   143  			cmd.Command = dockerCli.Command("app", "image", "tag", ref+":latest", ref2)
   144  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   145  			cmd.Command = dockerCli.Command("app", "push", ref2)
   146  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   147  
   148  			cmd.Command = dockerCli.Command("app", "run", ref2, "--name", name)
   149  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   150  			cmd.Command = dockerCli.Command("service", "ls")
   151  			assert.Check(t, cmp.Contains(icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), name))
   152  		})
   153  
   154  		// push it again using an app pre-bundled and tagged in the bundle store and install it to check it is also available
   155  		t.Run("push-bundleref", func(t *testing.T) {
   156  			name := strings.Replace(t.Name(), "/", "_", 1)
   157  			ref2 := ref + ":v0.42"
   158  			// Create a new command so the bundle store can be trashed before installing the app
   159  			cmdIsolatedStore, cleanupIsolatedStore := dockerCli.createTestCmd()
   160  
   161  			// Enter the same context as `cmd` to run commands within the same environment
   162  			cmdIsolatedStore.Command = dockerCli.Command("context", "create", "swarm-context", "--docker", fmt.Sprintf(`"host=tcp://%s"`, info.swarmAddress))
   163  			icmd.RunCmd(cmdIsolatedStore).Assert(t, icmd.Success)
   164  			cmdIsolatedStore.Env = append(cmdIsolatedStore.Env, "DOCKER_CONTEXT=swarm-context")
   165  
   166  			// bundle the app again but this time with a tag to store it into the bundle store
   167  			build(t, cmdIsolatedStore, dockerCli, ref2, filepath.Join("testdata", "push-pull"))
   168  			// Push the app without tagging it explicitly
   169  			cmdIsolatedStore.Command = dockerCli.Command("app", "push", ref2)
   170  			icmd.RunCmd(cmdIsolatedStore).Assert(t, icmd.Success)
   171  			// remove the bundle from the bundle store to be sure it won't be used instead of registry
   172  			cleanupIsolatedStore()
   173  			// install from the registry
   174  			cmd.Command = dockerCli.Command("app", "run", ref2, "--name", name)
   175  			icmd.RunCmd(cmd).Assert(t, icmd.Success)
   176  			cmd.Command = dockerCli.Command("service", "ls")
   177  			assert.Check(t, cmp.Contains(icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), name))
   178  		})
   179  	})
   180  }