storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/env/web_env_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package env
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"net/url"
    25  	"testing"
    26  
    27  	"github.com/gorilla/mux"
    28  )
    29  
    30  func GetenvHandler(w http.ResponseWriter, r *http.Request) {
    31  	vars := mux.Vars(r)
    32  	if vars["namespace"] != "default" {
    33  		http.Error(w, "namespace not found", http.StatusNotFound)
    34  		return
    35  	}
    36  	if vars["name"] != "minio" {
    37  		http.Error(w, "tenant not found", http.StatusNotFound)
    38  		return
    39  	}
    40  	if vars["key"] != "MINIO_ARGS" {
    41  		http.Error(w, "key not found", http.StatusNotFound)
    42  		return
    43  	}
    44  	w.Write([]byte("http://127.0.0.{1..4}:9000/data{1...4}"))
    45  	w.(http.Flusher).Flush()
    46  }
    47  
    48  func startTestServer(t *testing.T) *httptest.Server {
    49  	router := mux.NewRouter().SkipClean(true).UseEncodedPath()
    50  	router.Methods(http.MethodGet).
    51  		Path("/webhook/v1/getenv/{namespace}/{name}").
    52  		HandlerFunc(GetenvHandler).Queries("key", "{key:.*}")
    53  
    54  	ts := httptest.NewServer(router)
    55  	t.Cleanup(func() {
    56  		ts.Close()
    57  	})
    58  
    59  	return ts
    60  }
    61  
    62  func TestWebEnv(t *testing.T) {
    63  	ts := startTestServer(t)
    64  
    65  	u, err := url.Parse(ts.URL)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	v, user, pwd, err := getEnvValueFromHTTP(
    71  		fmt.Sprintf("env://minio:minio123@%s/webhook/v1/getenv/default/minio",
    72  			u.Host),
    73  		"MINIO_ARGS")
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if v != "http://127.0.0.{1..4}:9000/data{1...4}" {
    79  		t.Fatalf("Unexpected value %s", v)
    80  	}
    81  
    82  	if user != "minio" {
    83  		t.Fatalf("Unexpected value %s", v)
    84  	}
    85  
    86  	if pwd != "minio123" {
    87  		t.Fatalf("Unexpected value %s", v)
    88  	}
    89  }