github.com/minio/madmin-go/v2@v2.2.1/parse-config_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  	"reflect"
    24  	"testing"
    25  )
    26  
    27  func TestParseServerConfigOutput(t *testing.T) {
    28  	tests := []struct {
    29  		Name        string
    30  		Config      string
    31  		Expected    []SubsysConfig
    32  		ExpectedErr error
    33  	}{
    34  		{
    35  			Name:   "single target config data only",
    36  			Config: "subnet license= api_key= proxy=",
    37  			Expected: []SubsysConfig{
    38  				{
    39  					SubSystem: SubnetSubSys,
    40  					Target:    "",
    41  					KV: []ConfigKV{
    42  						{
    43  							Key:         "license",
    44  							Value:       "",
    45  							EnvOverride: nil,
    46  						},
    47  						{
    48  							Key:         "api_key",
    49  							Value:       "",
    50  							EnvOverride: nil,
    51  						},
    52  						{
    53  							Key:         "proxy",
    54  							Value:       "",
    55  							EnvOverride: nil,
    56  						},
    57  					},
    58  					kvIndexMap: map[string]int{
    59  						"license": 0,
    60  						"api_key": 1,
    61  						"proxy":   2,
    62  					},
    63  				},
    64  			},
    65  		},
    66  		{
    67  			Name: "single target config + env",
    68  			Config: `# MINIO_SUBNET_API_KEY=xxx
    69  # MINIO_SUBNET_LICENSE=2
    70  subnet license=1 api_key= proxy=`,
    71  			Expected: []SubsysConfig{
    72  				{
    73  					SubSystem: SubnetSubSys,
    74  					Target:    "",
    75  					KV: []ConfigKV{
    76  						{
    77  							Key:   "api_key",
    78  							Value: "",
    79  							EnvOverride: &EnvOverride{
    80  								Name:  "MINIO_SUBNET_API_KEY",
    81  								Value: "xxx",
    82  							},
    83  						},
    84  						{
    85  							Key:   "license",
    86  							Value: "1",
    87  							EnvOverride: &EnvOverride{
    88  								Name:  "MINIO_SUBNET_LICENSE",
    89  								Value: "2",
    90  							},
    91  						},
    92  						{
    93  							Key:         "proxy",
    94  							Value:       "",
    95  							EnvOverride: nil,
    96  						},
    97  					},
    98  					kvIndexMap: map[string]int{
    99  						"license": 1,
   100  						"api_key": 0,
   101  						"proxy":   2,
   102  					},
   103  				},
   104  			},
   105  		},
   106  		{
   107  			Name: "multiple targets no env",
   108  			Config: `logger_webhook enable=off endpoint= auth_token= client_cert= client_key= queue_size=100000
   109  logger_webhook:1 endpoint=http://localhost:8080/ auth_token= client_cert= client_key= queue_size=100000
   110  `,
   111  			Expected: []SubsysConfig{
   112  				{
   113  					SubSystem: LoggerWebhookSubSys,
   114  					Target:    "",
   115  					KV: []ConfigKV{
   116  						{
   117  							Key:   "enable",
   118  							Value: "off",
   119  						},
   120  						{
   121  							Key:   "endpoint",
   122  							Value: "",
   123  						},
   124  						{
   125  							Key:   "auth_token",
   126  							Value: "",
   127  						},
   128  						{
   129  							Key:   "client_cert",
   130  							Value: "",
   131  						},
   132  						{
   133  							Key:   "client_key",
   134  							Value: "",
   135  						},
   136  						{
   137  							Key:   "queue_size",
   138  							Value: "100000",
   139  						},
   140  					},
   141  					kvIndexMap: map[string]int{
   142  						"enable":      0,
   143  						"endpoint":    1,
   144  						"auth_token":  2,
   145  						"client_cert": 3,
   146  						"client_key":  4,
   147  						"queue_size":  5,
   148  					},
   149  				},
   150  				{
   151  					SubSystem: LoggerWebhookSubSys,
   152  					Target:    "1",
   153  					KV: []ConfigKV{
   154  						{
   155  							Key:   "endpoint",
   156  							Value: "http://localhost:8080/",
   157  						},
   158  						{
   159  							Key:   "auth_token",
   160  							Value: "",
   161  						},
   162  						{
   163  							Key:   "client_cert",
   164  							Value: "",
   165  						},
   166  						{
   167  							Key:   "client_key",
   168  							Value: "",
   169  						},
   170  						{
   171  							Key:   "queue_size",
   172  							Value: "100000",
   173  						},
   174  					},
   175  					kvIndexMap: map[string]int{
   176  						"endpoint":    0,
   177  						"auth_token":  1,
   178  						"client_cert": 2,
   179  						"client_key":  3,
   180  						"queue_size":  4,
   181  					},
   182  				},
   183  			},
   184  		},
   185  	}
   186  
   187  	for i, test := range tests {
   188  		r, err := ParseServerConfigOutput(test.Config)
   189  		if err != nil {
   190  			if err.Error() != test.ExpectedErr.Error() {
   191  				t.Errorf("Test %d (%s) got unexpected error: %v", i, test.Name, err)
   192  			}
   193  			// got an expected error.
   194  			continue
   195  		}
   196  		if !reflect.DeepEqual(test.Expected, r) {
   197  			t.Errorf("Test %d (%s) expected:\n%#v\nbut got:\n%#v\n", i, test.Name, test.Expected, r)
   198  		}
   199  	}
   200  }