github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/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/assert" 14 ) 15 16 func unsetENV() { 17 os.Unsetenv("DOCKER_CONTENT_TRUST") 18 os.Unsetenv("DOCKER_CONTENT_TRUST_SERVER") 19 } 20 21 func TestENVTrustServer(t *testing.T) { 22 defer unsetENV() 23 indexInfo := ®istrytypes.IndexInfo{Name: "testserver"} 24 if err := os.Setenv("DOCKER_CONTENT_TRUST_SERVER", "https://notary-test.com:5000"); err != nil { 25 t.Fatal("Failed to set ENV variable") 26 } 27 output, err := trust.Server(indexInfo) 28 expectedStr := "https://notary-test.com:5000" 29 if err != nil || output != expectedStr { 30 t.Fatalf("Expected server to be %s, got %s", expectedStr, output) 31 } 32 } 33 34 func TestHTTPENVTrustServer(t *testing.T) { 35 defer unsetENV() 36 indexInfo := ®istrytypes.IndexInfo{Name: "testserver"} 37 if err := os.Setenv("DOCKER_CONTENT_TRUST_SERVER", "http://notary-test.com:5000"); err != nil { 38 t.Fatal("Failed to set ENV variable") 39 } 40 _, err := trust.Server(indexInfo) 41 if err == nil { 42 t.Fatal("Expected error with invalid scheme") 43 } 44 } 45 46 func TestOfficialTrustServer(t *testing.T) { 47 indexInfo := ®istrytypes.IndexInfo{Name: "testserver", Official: true} 48 output, err := trust.Server(indexInfo) 49 if err != nil || output != trust.NotaryServer { 50 t.Fatalf("Expected server to be %s, got %s", trust.NotaryServer, output) 51 } 52 } 53 54 func TestNonOfficialTrustServer(t *testing.T) { 55 indexInfo := ®istrytypes.IndexInfo{Name: "testserver", Official: false} 56 output, err := trust.Server(indexInfo) 57 expectedStr := "https://" + indexInfo.Name 58 if err != nil || output != expectedStr { 59 t.Fatalf("Expected server to be %s, got %s", expectedStr, output) 60 } 61 } 62 63 func TestAddTargetToAllSignableRolesError(t *testing.T) { 64 tmpDir, err := ioutil.TempDir("", "notary-test-") 65 assert.NilError(t, err) 66 defer os.RemoveAll(tmpDir) 67 68 notaryRepo, err := client.NewFileCachedRepository(tmpDir, "gun", "https://localhost", nil, passphrase.ConstantRetriever("password"), trustpinning.TrustPinConfig{}) 69 assert.NilError(t, err) 70 target := client.Target{} 71 err = AddTargetToAllSignableRoles(notaryRepo, &target) 72 assert.Error(t, err, "client is offline") 73 }