github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/e2e/trust/sign_test.go (about)

     1  package trust
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/e2e/internal/fixtures"
     8  	"github.com/docker/cli/internal/test/environment"
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  	"gotest.tools/v3/fs"
    12  	"gotest.tools/v3/icmd"
    13  	"gotest.tools/v3/skip"
    14  )
    15  
    16  const (
    17  	localImage = "registry:5000/signlocal:v1"
    18  	signImage  = "registry:5000/sign:v1"
    19  )
    20  
    21  func TestSignLocalImage(t *testing.T) {
    22  	skip.If(t, environment.RemoteDaemon())
    23  
    24  	dir := fixtures.SetupConfigFile(t)
    25  	defer dir.Remove()
    26  	icmd.RunCmd(icmd.Command("docker", "pull", fixtures.AlpineImage)).Assert(t, icmd.Success)
    27  	icmd.RunCommand("docker", "tag", fixtures.AlpineImage, signImage).Assert(t, icmd.Success)
    28  	result := icmd.RunCmd(
    29  		icmd.Command("docker", "trust", "sign", signImage),
    30  		fixtures.WithPassphrase("root_password", "repo_password"),
    31  		fixtures.WithConfig(dir.Path()), fixtures.WithNotary)
    32  	result.Assert(t, icmd.Success)
    33  	assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.AlpineSha)))
    34  }
    35  
    36  func TestSignWithLocalFlag(t *testing.T) {
    37  	skip.If(t, environment.RemoteDaemon())
    38  
    39  	dir := fixtures.SetupConfigFile(t)
    40  	defer dir.Remove()
    41  	setupTrustedImageForOverwrite(t, dir)
    42  	result := icmd.RunCmd(
    43  		icmd.Command("docker", "trust", "sign", "--local", localImage),
    44  		fixtures.WithPassphrase("root_password", "repo_password"),
    45  		fixtures.WithConfig(dir.Path()), fixtures.WithNotary)
    46  	result.Assert(t, icmd.Success)
    47  	assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.BusyboxSha)))
    48  }
    49  
    50  func setupTrustedImageForOverwrite(t *testing.T, dir fs.Dir) {
    51  	icmd.RunCmd(icmd.Command("docker", "pull", fixtures.AlpineImage)).Assert(t, icmd.Success)
    52  	icmd.RunCommand("docker", "tag", fixtures.AlpineImage, localImage).Assert(t, icmd.Success)
    53  	result := icmd.RunCmd(
    54  		icmd.Command("docker", "-D", "trust", "sign", localImage),
    55  		fixtures.WithPassphrase("root_password", "repo_password"),
    56  		fixtures.WithConfig(dir.Path()), fixtures.WithNotary)
    57  	result.Assert(t, icmd.Success)
    58  	assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.AlpineSha)))
    59  	icmd.RunCmd(icmd.Command("docker", "pull", fixtures.BusyboxImage)).Assert(t, icmd.Success)
    60  	icmd.RunCommand("docker", "tag", fixtures.BusyboxImage, localImage).Assert(t, icmd.Success)
    61  }