1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package env
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/minio/mux"
)
func GetenvHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if vars["namespace"] != "default" {
http.Error(w, "namespace not found", http.StatusNotFound)
return
}
if vars["name"] != "minio" {
http.Error(w, "tenant not found", http.StatusNotFound)
return
}
if vars["key"] != "MINIO_ARGS" {
http.Error(w, "key not found", http.StatusNotFound)
return
}
w.Write([]byte("http://127.0.0.{1..4}:9000/data{1...4}"))
w.(http.Flusher).Flush()
}
func startTestServer(t *testing.T) *httptest.Server {
router := mux.NewRouter().SkipClean(true).UseEncodedPath()
router.Methods(http.MethodGet).
Path("/webhook/v1/getenv/{namespace}/{name}").
HandlerFunc(GetenvHandler).Queries("key", "{key:.*}")
ts := httptest.NewServer(router)
t.Cleanup(func() {
ts.Close()
})
return ts
}
func TestWebEnv(t *testing.T) {
ts := startTestServer(t)
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
v, user, pwd, err := getEnvValueFromHTTP(
fmt.Sprintf("env://minio:minio123@%s/webhook/v1/getenv/default/minio",
u.Host),
"MINIO_ARGS")
if err != nil {
t.Fatal(err)
}
if v != "http://127.0.0.{1..4}:9000/data{1...4}" {
t.Fatalf("Unexpected value %s", v)
}
if user != "minio" {
t.Fatalf("Unexpected value %s", v)
}
if pwd != "minio123" {
t.Fatalf("Unexpected value %s", v)
}
}
func TestIsSetType(t *testing.T) {
t.Setenv("_TEST_ENV", "")
v, err := GetInt("_TEST_ENV", 0)
if err != nil {
t.Fatal(err)
}
if v != 0 {
t.Fatal("unexpected")
}
t.Setenv("_TEST_ENV", "1")
v, err = GetInt("_TEST_ENV", 0)
if err != nil {
t.Fatal(err)
}
if v != 1 {
t.Fatal("unexpected")
}
t.Setenv("_TEST_ENV", "10s")
d, err := GetDuration("_TEST_ENV", time.Second)
if err != nil {
t.Fatal(err)
}
if d != 10*time.Second {
t.Fatal("unexpected")
}
t.Setenv("_TEST_ENV", "")
d, err = GetDuration("_TEST_ENV", time.Second)
if err != nil {
t.Fatal(err)
}
if d != time.Second {
t.Fatal("unexpected")
}
}
func TestIsSet(t *testing.T) {
t.Setenv("_TEST_ENV", "")
if IsSet("_TEST_ENV") {
t.Fatal("Expected IsSet(false) but found IsSet(true)")
}
t.Setenv("_TEST_ENV", "v")
if !IsSet("_TEST_ENV") {
t.Fatal("Expected IsSet(true) but found IsSet(false)")
}
}
func TestGetEnv(t *testing.T) {
// Set empty env-value, this test covers situation
// where env is set but with empty value, choose
// to fallback to default value at this point.
t.Setenv("_TEST_ENV", "")
if v := Get("_TEST_ENV", "value"); v != "value" {
t.Fatalf("Expected 'value', but got %s", v)
}
t.Setenv("_TEST_ENV", "")
if v := Get("_TEST_ENV", "value"); v != "value" {
t.Fatalf("Expected 'value', but got %s", v)
}
t.Setenv("_TEST_ENV", "value-new")
if v := Get("_TEST_ENV", "value"); v != "value-new" {
t.Fatalf("Expected 'value-new', but got %s", v)
}
}
|