storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/gateway-main_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 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  package cmd
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/minio/cli"
    25  )
    26  
    27  // Test RegisterGatewayCommand
    28  func TestRegisterGatewayCommand(t *testing.T) {
    29  	var err error
    30  
    31  	cmd := cli.Command{Name: "test"}
    32  	err = RegisterGatewayCommand(cmd)
    33  	if err != nil {
    34  		t.Errorf("RegisterGatewayCommand got unexpected error: %s", err)
    35  	}
    36  }
    37  
    38  // Test running a registered gateway command with a flag
    39  func TestRunRegisteredGatewayCommand(t *testing.T) {
    40  	var err error
    41  
    42  	flagName := "test-flag"
    43  	flagValue := "foo"
    44  
    45  	cmd := cli.Command{
    46  		Name: "test-run-with-flag",
    47  		Flags: []cli.Flag{
    48  			cli.StringFlag{Name: flagName},
    49  		},
    50  		Action: func(ctx *cli.Context) {
    51  			if actual := ctx.String(flagName); actual != flagValue {
    52  				t.Errorf("value of %s expects %s, but got %s", flagName, flagValue, actual)
    53  			}
    54  		},
    55  	}
    56  
    57  	err = RegisterGatewayCommand(cmd)
    58  	if err != nil {
    59  		t.Errorf("RegisterGatewayCommand got unexpected error: %s", err)
    60  	}
    61  
    62  	if err = newApp("minio").Run(
    63  		[]string{"minio", "gateway", cmd.Name, fmt.Sprintf("--%s", flagName), flagValue}); err != nil {
    64  		t.Errorf("running registered gateway command got unexpected error: %s", err)
    65  	}
    66  }
    67  
    68  // Test parseGatewayEndpoint
    69  func TestParseGatewayEndpoint(t *testing.T) {
    70  	testCases := []struct {
    71  		arg         string
    72  		endPoint    string
    73  		secure      bool
    74  		errReturned bool
    75  	}{
    76  		{"http://127.0.0.1:9000", "127.0.0.1:9000", false, false},
    77  		{"https://127.0.0.1:9000", "127.0.0.1:9000", true, false},
    78  		{"http://play.min.io:9000", "play.min.io:9000", false, false},
    79  		{"https://play.min.io:9000", "play.min.io:9000", true, false},
    80  		{"ftp://127.0.0.1:9000", "", false, true},
    81  		{"ftp://play.min.io:9000", "", false, true},
    82  		{"play.min.io:9000", "play.min.io:9000", true, false},
    83  	}
    84  
    85  	for i, test := range testCases {
    86  		endPoint, secure, err := ParseGatewayEndpoint(test.arg)
    87  		errReturned := err != nil
    88  
    89  		if endPoint != test.endPoint ||
    90  			secure != test.secure ||
    91  			errReturned != test.errReturned {
    92  			t.Errorf("Test %d: expected %s,%t,%t got %s,%t,%t",
    93  				i+1, test.endPoint, test.secure, test.errReturned,
    94  				endPoint, secure, errReturned)
    95  		}
    96  	}
    97  }
    98  
    99  // Test validateGatewayArguments
   100  func TestValidateGatewayArguments(t *testing.T) {
   101  	nonLoopBackIPs := localIP4.FuncMatch(func(ip string, matchString string) bool {
   102  		return !strings.HasPrefix(ip, "127.")
   103  	}, "")
   104  	if len(nonLoopBackIPs) == 0 {
   105  		t.Fatalf("No non-loop back IP address found for this host")
   106  	}
   107  	nonLoopBackIP := nonLoopBackIPs.ToSlice()[0]
   108  
   109  	testCases := []struct {
   110  		serverAddr   string
   111  		endpointAddr string
   112  		valid        bool
   113  	}{
   114  		{":9000", "http://localhost:9001", true},
   115  		{":9000", "http://google.com", true},
   116  		{"123.123.123.123:9000", "http://localhost:9000", false},
   117  		{":9000", "http://localhost:9000", false},
   118  		{":9000", nonLoopBackIP + ":9000", false},
   119  	}
   120  	for i, test := range testCases {
   121  		err := ValidateGatewayArguments(test.serverAddr, test.endpointAddr)
   122  		if test.valid && err != nil {
   123  			t.Errorf("Test %d expected not to return error but got %s", i+1, err)
   124  		}
   125  		if !test.valid && err == nil {
   126  			t.Errorf("Test %d expected to fail but it did not", i+1)
   127  		}
   128  	}
   129  }