github.com/minio/madmin-go/v2@v2.2.1/remote-target-commands_test.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"net/url"
    24  	"testing"
    25  )
    26  
    27  func isOpsEqual(op1 []TargetUpdateType, op2 []TargetUpdateType) bool {
    28  	if len(op1) != len(op2) {
    29  		return false
    30  	}
    31  	for _, o1 := range op1 {
    32  		found := false
    33  		for _, o2 := range op2 {
    34  			if o2 == o1 {
    35  				found = true
    36  				break
    37  			}
    38  		}
    39  		if !found {
    40  			return false
    41  		}
    42  	}
    43  	return true
    44  }
    45  
    46  // TestGetTargetUpdateOps tests GetTargetUpdateOps
    47  func TestGetTargetUpdateOps(t *testing.T) {
    48  	testCases := []struct {
    49  		values      url.Values
    50  		expectedOps []TargetUpdateType
    51  	}{
    52  		{
    53  			values: url.Values{
    54  				"update": []string{"true"},
    55  			},
    56  			expectedOps: []TargetUpdateType{},
    57  		},
    58  		{
    59  			values: url.Values{
    60  				"update": []string{"false"},
    61  				"path":   []string{"true"},
    62  			},
    63  			expectedOps: []TargetUpdateType{},
    64  		},
    65  		{
    66  			values: url.Values{
    67  				"update": []string{"true"},
    68  				"path":   []string{""},
    69  			},
    70  			expectedOps: []TargetUpdateType{},
    71  		},
    72  		{
    73  			values: url.Values{
    74  				"update": []string{"true"},
    75  				"path":   []string{"true"},
    76  				"bzzzz":  []string{"true"},
    77  			},
    78  			expectedOps: []TargetUpdateType{PathUpdateType},
    79  		},
    80  
    81  		{
    82  			values: url.Values{
    83  				"update":      []string{"true"},
    84  				"path":        []string{"true"},
    85  				"creds":       []string{"true"},
    86  				"sync":        []string{"true"},
    87  				"proxy":       []string{"true"},
    88  				"bandwidth":   []string{"true"},
    89  				"healthcheck": []string{"true"},
    90  			},
    91  			expectedOps: []TargetUpdateType{
    92  				PathUpdateType, CredentialsUpdateType, SyncUpdateType, ProxyUpdateType, BandwidthLimitUpdateType, HealthCheckDurationUpdateType,
    93  			},
    94  		},
    95  	}
    96  	for i, test := range testCases {
    97  		gotOps := GetTargetUpdateOps(test.values)
    98  		if !isOpsEqual(gotOps, test.expectedOps) {
    99  			t.Fatalf("test %d: expected %v got %v", i+1, test.expectedOps, gotOps)
   100  		}
   101  	}
   102  }