github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/url/environment_test.go (about) 1 package url 2 3 import ( 4 "testing" 5 ) 6 7 const ( 8 // defaultDockerHost is the non-endpoint-specific value for the DOCKER_HOST 9 // environment variable. 10 defaultDockerHost = "unix:///default/docker.sock" 11 // defaultDockerTLSVerify is the non-endpoint-specific value for the 12 // DOCKER_TLS_VERIFY environment variable. 13 defaultDockerTLSVerify = "sure!" 14 // alphaSpecificDockerHost is the alpha-specific value for the DOCKER_HOST 15 // environment variable. 16 alphaSpecificDockerHost = "unix:///alpha/docker.sock" 17 // betaSpecificDockerTLSVerify is the beta-specific value for the 18 // DOCKER_TLS_VERIFY environment variable. 19 betaSpecificDockerTLSVerify = "true" 20 // sourceSpecificDockerContext is the source-specific value for the 21 // DOCKER_CONTEXT environment variable. 22 sourceSpecificDockerContext = "some-context" 23 // destinationSpecificDockerTLSVerify is the destination-specific value for 24 // the DOCKER_TLS_VERIFY environment variable. 25 destinationSpecificDockerTLSVerify = "false" 26 ) 27 28 // mockEnvironment is a mock environment setup for use in testing. 29 var mockEnvironment = map[string]string{ 30 "DOCKER_HOST": defaultDockerHost, 31 "DOCKER_TLS_VERIFY": defaultDockerTLSVerify, 32 "MUTAGEN_ALPHA_DOCKER_HOST": alphaSpecificDockerHost, 33 "MUTAGEN_BETA_DOCKER_TLS_VERIFY": betaSpecificDockerTLSVerify, 34 "MUTAGEN_SOURCE_DOCKER_CONTEXT": sourceSpecificDockerContext, 35 "MUTAGEN_DESTINATION_DOCKER_TLS_VERIFY": destinationSpecificDockerTLSVerify, 36 } 37 38 // mockLookupEnv is a mock implementation of the os.LookupEnv function. 39 func mockLookupEnv(name string) (string, bool) { 40 value, ok := mockEnvironment[name] 41 return value, ok 42 } 43 44 func init() { 45 // Replace the lookupEnv function with one that uses a mock environment. 46 lookupEnv = mockLookupEnv 47 } 48 49 func TestAlphaLookupAlphaSpecificExists(t *testing.T) { 50 if value, ok := getEnvironmentVariable("DOCKER_HOST", Kind_Synchronization, true); !ok { 51 t.Fatal("unable to find alpha-specific value") 52 } else if value != alphaSpecificDockerHost { 53 t.Fatal("alpha-specific value does not match expected") 54 } 55 } 56 57 func TestAlphaLookupOnlyDefaultExists(t *testing.T) { 58 if value, ok := getEnvironmentVariable("DOCKER_TLS_VERIFY", Kind_Synchronization, true); !ok { 59 t.Fatal("unable to find non-endpoint-specific value for alpha") 60 } else if value != defaultDockerTLSVerify { 61 t.Fatal("non-endpoint-specific value does not match expected") 62 } 63 } 64 65 func TestAlphaLookupNeitherExists(t *testing.T) { 66 if _, ok := getEnvironmentVariable("DOCKER_CERT_PATH", Kind_Synchronization, true); ok { 67 t.Fatal("able to find unset environment variable") 68 } 69 } 70 71 func TestBetaLookupBetaSpecificExists(t *testing.T) { 72 if value, ok := getEnvironmentVariable("DOCKER_TLS_VERIFY", Kind_Synchronization, false); !ok { 73 t.Fatal("unable to find beta-specific value") 74 } else if value != betaSpecificDockerTLSVerify { 75 t.Fatal("beta-specific value does not match expected") 76 } 77 } 78 79 func TestBetaLookupOnlyDefaultExists(t *testing.T) { 80 if value, ok := getEnvironmentVariable("DOCKER_HOST", Kind_Synchronization, false); !ok { 81 t.Fatal("unable to find non-endpoint-specific value for alpha") 82 } else if value != defaultDockerHost { 83 t.Fatal("non-endpoint-specific value does not match expected") 84 } 85 } 86 87 func TestBetaLookupNeitherExists(t *testing.T) { 88 if _, ok := getEnvironmentVariable("DOCKER_CERT_PATH", Kind_Synchronization, true); ok { 89 t.Fatal("able to find unset environment variable") 90 } 91 } 92 93 func TestSourceLookupSourceSpecificExists(t *testing.T) { 94 if value, ok := getEnvironmentVariable("DOCKER_CONTEXT", Kind_Forwarding, true); !ok { 95 t.Fatal("unable to find source-specific value") 96 } else if value != sourceSpecificDockerContext { 97 t.Fatal("source-specific value does not match expected") 98 } 99 } 100 101 func TestSourceLookupOnlyDefaultExists(t *testing.T) { 102 if value, ok := getEnvironmentVariable("DOCKER_TLS_VERIFY", Kind_Forwarding, true); !ok { 103 t.Fatal("unable to find non-endpoint-specific value for source") 104 } else if value != defaultDockerTLSVerify { 105 t.Fatal("non-endpoint-specific value does not match expected") 106 } 107 } 108 109 func TestSourceLookupNeitherExists(t *testing.T) { 110 if _, ok := getEnvironmentVariable("DOCKER_CERT_PATH", Kind_Forwarding, true); ok { 111 t.Fatal("able to find unset environment variable") 112 } 113 } 114 115 func TestDestinationLookupDestinationSpecificExists(t *testing.T) { 116 if value, ok := getEnvironmentVariable("DOCKER_TLS_VERIFY", Kind_Forwarding, false); !ok { 117 t.Fatal("unable to find destination-specific value") 118 } else if value != destinationSpecificDockerTLSVerify { 119 t.Fatal("destination-specific value does not match expected") 120 } 121 } 122 123 func TestDestinationLookupOnlyDefaultExists(t *testing.T) { 124 if value, ok := getEnvironmentVariable("DOCKER_HOST", Kind_Forwarding, false); !ok { 125 t.Fatal("unable to find non-endpoint-specific value for source") 126 } else if value != defaultDockerHost { 127 t.Fatal("non-endpoint-specific value does not match expected") 128 } 129 } 130 131 func TestDestinationLookupNeitherExists(t *testing.T) { 132 if _, ok := getEnvironmentVariable("DOCKER_CERT_PATH", Kind_Forwarding, true); ok { 133 t.Fatal("able to find unset environment variable") 134 } 135 }