github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/e2e/internal/fixtures/fixtures.go (about)

     1  package fixtures
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"gotest.tools/fs"
     9  	"gotest.tools/icmd"
    10  )
    11  
    12  const (
    13  	//NotaryURL is the location of the notary server
    14  	NotaryURL = "https://notary-server:4443"
    15  	//AlpineImage is an image in the test registry
    16  	AlpineImage = "registry:5000/alpine:3.6"
    17  	//AlpineSha is the sha of the alpine image
    18  	AlpineSha = "641b95ddb2ea9dc2af1a0113b6b348ebc20872ba615204fbe12148e98fd6f23d"
    19  	//BusyboxImage is an image in the test registry
    20  	BusyboxImage = "registry:5000/busybox:1.27.2"
    21  	//BusyboxSha is the sha of the busybox image
    22  	BusyboxSha = "030fcb92e1487b18c974784dcc110a93147c9fc402188370fbfd17efabffc6af"
    23  )
    24  
    25  //SetupConfigFile creates a config.json file for testing
    26  func SetupConfigFile(t *testing.T) fs.Dir {
    27  	t.Helper()
    28  	dir := fs.NewDir(t, "trust_test", fs.WithMode(0700), fs.WithFile("config.json", `
    29  	{
    30  		"auths": {
    31  			"registry:5000": {
    32  				"auth": "ZWlhaXM6cGFzc3dvcmQK"
    33  			},
    34  			"https://notary-server:4443": {
    35  				"auth": "ZWlhaXM6cGFzc3dvcmQK"
    36  			}
    37  		},
    38  		"experimental": "enabled"
    39  	}
    40  	`), fs.WithDir("trust", fs.WithDir("private")))
    41  	return *dir
    42  }
    43  
    44  //WithConfig sets an environment variable for the docker config location
    45  func WithConfig(dir string) func(cmd *icmd.Cmd) {
    46  	return func(cmd *icmd.Cmd) {
    47  		env := append(os.Environ(),
    48  			"DOCKER_CONFIG="+dir,
    49  		)
    50  		cmd.Env = append(cmd.Env, env...)
    51  	}
    52  }
    53  
    54  //WithPassphrase sets environment variables for passphrases
    55  func WithPassphrase(rootPwd, repositoryPwd string) func(cmd *icmd.Cmd) {
    56  	return func(cmd *icmd.Cmd) {
    57  		env := append(os.Environ(),
    58  			"DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE="+rootPwd,
    59  			"DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE="+repositoryPwd,
    60  		)
    61  		cmd.Env = append(cmd.Env, env...)
    62  	}
    63  }
    64  
    65  //WithTrust sets DOCKER_CONTENT_TRUST to 1
    66  func WithTrust(cmd *icmd.Cmd) {
    67  	env := append(os.Environ(),
    68  		"DOCKER_CONTENT_TRUST=1",
    69  	)
    70  	cmd.Env = append(cmd.Env, env...)
    71  }
    72  
    73  //WithNotary sets the location of the notary server
    74  func WithNotary(cmd *icmd.Cmd) {
    75  	env := append(os.Environ(),
    76  		"DOCKER_CONTENT_TRUST_SERVER="+NotaryURL,
    77  	)
    78  	cmd.Env = append(cmd.Env, env...)
    79  }
    80  
    81  //WithHome sets the HOME environment variable
    82  func WithHome(path string) func(*icmd.Cmd) {
    83  	return func(cmd *icmd.Cmd) {
    84  		cmd.Env = append(cmd.Env, "HOME="+path)
    85  	}
    86  }
    87  
    88  //WithNotaryServer sets the location of the notary server
    89  func WithNotaryServer(notaryURL string) func(*icmd.Cmd) {
    90  	return func(cmd *icmd.Cmd) {
    91  		env := append(os.Environ(),
    92  			"DOCKER_CONTENT_TRUST_SERVER="+notaryURL,
    93  		)
    94  		cmd.Env = append(cmd.Env, env...)
    95  	}
    96  }
    97  
    98  // CreateMaskedTrustedRemoteImage creates a remote image that is signed with
    99  // content trust, then pushes a different untrusted image at the same tag.
   100  func CreateMaskedTrustedRemoteImage(t *testing.T, registryPrefix, repo, tag string) string {
   101  	t.Helper()
   102  	image := createTrustedRemoteImage(t, registryPrefix, repo, tag)
   103  	createNamedUnsignedImageFromBusyBox(t, image)
   104  	return image
   105  }
   106  
   107  func createTrustedRemoteImage(t *testing.T, registryPrefix, repo, tag string) string {
   108  	t.Helper()
   109  	image := fmt.Sprintf("%s/%s:%s", registryPrefix, repo, tag)
   110  	icmd.RunCommand("docker", "image", "pull", AlpineImage).Assert(t, icmd.Success)
   111  	icmd.RunCommand("docker", "image", "tag", AlpineImage, image).Assert(t, icmd.Success)
   112  	result := icmd.RunCmd(
   113  		icmd.Command("docker", "image", "push", image),
   114  		WithPassphrase("root_password", "repo_password"), WithTrust, WithNotary)
   115  	result.Assert(t, icmd.Success)
   116  	icmd.RunCommand("docker", "image", "rm", image).Assert(t, icmd.Success)
   117  	return image
   118  }
   119  
   120  func createNamedUnsignedImageFromBusyBox(t *testing.T, image string) {
   121  	t.Helper()
   122  	icmd.RunCommand("docker", "image", "pull", BusyboxImage).Assert(t, icmd.Success)
   123  	icmd.RunCommand("docker", "image", "tag", BusyboxImage, image).Assert(t, icmd.Success)
   124  	icmd.RunCommand("docker", "image", "push", image).Assert(t, icmd.Success)
   125  	icmd.RunCommand("docker", "image", "rm", image).Assert(t, icmd.Success)
   126  }