github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/image/trust_test.go (about)

     1  package image
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/cli/trust"
     9  	registrytypes "github.com/docker/docker/api/types/registry"
    10  	"github.com/theupdateframework/notary/client"
    11  	"github.com/theupdateframework/notary/passphrase"
    12  	"github.com/theupdateframework/notary/trustpinning"
    13  	"gotest.tools/v3/assert"
    14  	"gotest.tools/v3/env"
    15  )
    16  
    17  func TestENVTrustServer(t *testing.T) {
    18  	defer env.PatchAll(t, map[string]string{"DOCKER_CONTENT_TRUST_SERVER": "https://notary-test.com:5000"})()
    19  	indexInfo := &registrytypes.IndexInfo{Name: "testserver"}
    20  	output, err := trust.Server(indexInfo)
    21  	expectedStr := "https://notary-test.com:5000"
    22  	if err != nil || output != expectedStr {
    23  		t.Fatalf("Expected server to be %s, got %s", expectedStr, output)
    24  	}
    25  }
    26  
    27  func TestHTTPENVTrustServer(t *testing.T) {
    28  	defer env.PatchAll(t, map[string]string{"DOCKER_CONTENT_TRUST_SERVER": "http://notary-test.com:5000"})()
    29  	indexInfo := &registrytypes.IndexInfo{Name: "testserver"}
    30  	_, err := trust.Server(indexInfo)
    31  	if err == nil {
    32  		t.Fatal("Expected error with invalid scheme")
    33  	}
    34  }
    35  
    36  func TestOfficialTrustServer(t *testing.T) {
    37  	indexInfo := &registrytypes.IndexInfo{Name: "testserver", Official: true}
    38  	output, err := trust.Server(indexInfo)
    39  	if err != nil || output != trust.NotaryServer {
    40  		t.Fatalf("Expected server to be %s, got %s", trust.NotaryServer, output)
    41  	}
    42  }
    43  
    44  func TestNonOfficialTrustServer(t *testing.T) {
    45  	indexInfo := &registrytypes.IndexInfo{Name: "testserver", Official: false}
    46  	output, err := trust.Server(indexInfo)
    47  	expectedStr := "https://" + indexInfo.Name
    48  	if err != nil || output != expectedStr {
    49  		t.Fatalf("Expected server to be %s, got %s", expectedStr, output)
    50  	}
    51  }
    52  
    53  func TestAddTargetToAllSignableRolesError(t *testing.T) {
    54  	tmpDir, err := ioutil.TempDir("", "notary-test-")
    55  	assert.NilError(t, err)
    56  	defer os.RemoveAll(tmpDir)
    57  
    58  	notaryRepo, err := client.NewFileCachedRepository(tmpDir, "gun", "https://localhost", nil, passphrase.ConstantRetriever("password"), trustpinning.TrustPinConfig{})
    59  	assert.NilError(t, err)
    60  	target := client.Target{}
    61  	err = AddTargetToAllSignableRoles(notaryRepo, &target)
    62  	assert.Error(t, err, "client is offline")
    63  }