github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/image/trust_test.go (about)

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