github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/trust_test.go (about)

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