github.com/goern/docker@v1.9.0-rc1/api/client/trust_test.go (about)

     1  package client
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/registry"
     8  )
     9  
    10  func unsetENV() {
    11  	os.Unsetenv("DOCKER_CONTENT_TRUST")
    12  	os.Unsetenv("DOCKER_CONTENT_TRUST_SERVER")
    13  }
    14  
    15  func TestENVTrustServer(t *testing.T) {
    16  	defer unsetENV()
    17  	indexInfo := &registry.IndexInfo{Name: "testserver"}
    18  	if err := os.Setenv("DOCKER_CONTENT_TRUST_SERVER", "https://notary-test.com:5000"); err != nil {
    19  		t.Fatal("Failed to set ENV variable")
    20  	}
    21  	output, err := trustServer(indexInfo)
    22  	expectedStr := "https://notary-test.com:5000"
    23  	if err != nil || output != expectedStr {
    24  		t.Fatalf("Expected server to be %s, got %s", expectedStr, output)
    25  	}
    26  }
    27  
    28  func TestHTTPENVTrustServer(t *testing.T) {
    29  	defer unsetENV()
    30  	indexInfo := &registry.IndexInfo{Name: "testserver"}
    31  	if err := os.Setenv("DOCKER_CONTENT_TRUST_SERVER", "http://notary-test.com:5000"); err != nil {
    32  		t.Fatal("Failed to set ENV variable")
    33  	}
    34  	_, err := trustServer(indexInfo)
    35  	if err == nil {
    36  		t.Fatal("Expected error with invalid scheme")
    37  	}
    38  }
    39  
    40  func TestOfficialTrustServer(t *testing.T) {
    41  	indexInfo := &registry.IndexInfo{Name: "testserver", Official: true}
    42  	output, err := trustServer(indexInfo)
    43  	if err != nil || output != registry.NotaryServer {
    44  		t.Fatalf("Expected server to be %s, got %s", registry.NotaryServer, output)
    45  	}
    46  }
    47  
    48  func TestNonOfficialTrustServer(t *testing.T) {
    49  	indexInfo := &registry.IndexInfo{Name: "testserver", Official: false}
    50  	output, err := trustServer(indexInfo)
    51  	expectedStr := "https://" + indexInfo.Name
    52  	if err != nil || output != expectedStr {
    53  		t.Fatalf("Expected server to be %s, got %s", expectedStr, output)
    54  	}
    55  }