github.com/minio/console@v1.4.1/api/admin_site_replication_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // These tests are for AdminAPI Tag based on swagger-console.yml
    18  
    19  package api
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/minio/madmin-go/v3"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestGetSiteReplicationInfo(t *testing.T) {
    31  	assert := assert.New(t)
    32  	// mock minIO client
    33  	adminClient := AdminClientMock{}
    34  
    35  	function := "getSiteReplicationInfo()"
    36  	ctx, cancel := context.WithCancel(context.Background())
    37  	defer cancel()
    38  
    39  	retValueMock := madmin.SiteReplicationInfo{
    40  		Enabled: true,
    41  		Name:    "site1",
    42  		Sites: []madmin.PeerInfo{
    43  			{
    44  				Endpoint:     "http://localhost:9000",
    45  				Name:         "site1",
    46  				DeploymentID: "12345",
    47  			},
    48  			{
    49  				Endpoint:     "http://localhost:9001",
    50  				Name:         "site2",
    51  				DeploymentID: "123456",
    52  			},
    53  		},
    54  		ServiceAccountAccessKey: "test-key",
    55  	}
    56  
    57  	expValueMock := &madmin.SiteReplicationInfo{
    58  		Enabled: true,
    59  		Name:    "site1",
    60  		Sites: []madmin.PeerInfo{
    61  			{
    62  				Endpoint:     "http://localhost:9000",
    63  				Name:         "site1",
    64  				DeploymentID: "12345",
    65  			},
    66  			{
    67  				Endpoint:     "http://localhost:9001",
    68  				Name:         "site2",
    69  				DeploymentID: "123456",
    70  			},
    71  		},
    72  		ServiceAccountAccessKey: "test-key",
    73  	}
    74  
    75  	getSiteReplicationInfo = func(_ context.Context) (info *madmin.SiteReplicationInfo, err error) {
    76  		return &retValueMock, nil
    77  	}
    78  
    79  	srInfo, err := adminClient.getSiteReplicationInfo(ctx)
    80  	assert.Nil(err)
    81  	assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function))
    82  }
    83  
    84  func TestAddSiteReplicationInfo(t *testing.T) {
    85  	assert := assert.New(t)
    86  	// mock minIO client
    87  	adminClient := AdminClientMock{}
    88  
    89  	function := "addSiteReplicationInfo()"
    90  	ctx, cancel := context.WithCancel(context.Background())
    91  	defer cancel()
    92  
    93  	retValueMock := &madmin.ReplicateAddStatus{
    94  		Success:                 true,
    95  		Status:                  "success",
    96  		ErrDetail:               "",
    97  		InitialSyncErrorMessage: "",
    98  	}
    99  
   100  	expValueMock := &madmin.ReplicateAddStatus{
   101  		Success:                 true,
   102  		Status:                  "success",
   103  		ErrDetail:               "",
   104  		InitialSyncErrorMessage: "",
   105  	}
   106  
   107  	addSiteReplicationInfo = func(_ context.Context, _ []madmin.PeerSite) (res *madmin.ReplicateAddStatus, err error) {
   108  		return retValueMock, nil
   109  	}
   110  
   111  	sites := []madmin.PeerSite{
   112  		{
   113  			Name:      "site1",
   114  			Endpoint:  "http://localhost:9000",
   115  			AccessKey: "test",
   116  			SecretKey: "test",
   117  		},
   118  		{
   119  			Name:      "site2",
   120  			Endpoint:  "http://localhost:9001",
   121  			AccessKey: "test",
   122  			SecretKey: "test",
   123  		},
   124  	}
   125  
   126  	srInfo, err := adminClient.addSiteReplicationInfo(ctx, sites, madmin.SRAddOptions{})
   127  	assert.Nil(err)
   128  	assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function))
   129  }
   130  
   131  func TestEditSiteReplicationInfo(t *testing.T) {
   132  	assert := assert.New(t)
   133  	// mock minIO client
   134  	adminClient := AdminClientMock{}
   135  
   136  	function := "editSiteReplicationInfo()"
   137  	ctx, cancel := context.WithCancel(context.Background())
   138  	defer cancel()
   139  
   140  	retValueMock := &madmin.ReplicateEditStatus{
   141  		Success:   true,
   142  		Status:    "success",
   143  		ErrDetail: "",
   144  	}
   145  
   146  	expValueMock := &madmin.ReplicateEditStatus{
   147  		Success:   true,
   148  		Status:    "success",
   149  		ErrDetail: "",
   150  	}
   151  
   152  	editSiteReplicationInfo = func(_ context.Context, _ madmin.PeerInfo) (res *madmin.ReplicateEditStatus, err error) {
   153  		return retValueMock, nil
   154  	}
   155  
   156  	site := madmin.PeerInfo{
   157  		Name:         "",
   158  		Endpoint:     "",
   159  		DeploymentID: "12345",
   160  	}
   161  
   162  	srInfo, err := adminClient.editSiteReplicationInfo(ctx, site, madmin.SREditOptions{})
   163  	assert.Nil(err)
   164  	assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function))
   165  }
   166  
   167  func TestDeleteSiteReplicationInfo(t *testing.T) {
   168  	assert := assert.New(t)
   169  	// mock minIO client
   170  	adminClient := AdminClientMock{}
   171  
   172  	function := "deleteSiteReplicationInfo()"
   173  	ctx, cancel := context.WithCancel(context.Background())
   174  	defer cancel()
   175  
   176  	retValueMock := &madmin.ReplicateRemoveStatus{
   177  		Status:    "success",
   178  		ErrDetail: "",
   179  	}
   180  
   181  	expValueMock := &madmin.ReplicateRemoveStatus{
   182  		Status:    "success",
   183  		ErrDetail: "",
   184  	}
   185  
   186  	deleteSiteReplicationInfoMock = func(_ context.Context, _ madmin.SRRemoveReq) (res *madmin.ReplicateRemoveStatus, err error) {
   187  		return retValueMock, nil
   188  	}
   189  
   190  	remReq := madmin.SRRemoveReq{
   191  		SiteNames: []string{
   192  			"test1",
   193  		},
   194  		RemoveAll: false,
   195  	}
   196  
   197  	srInfo, err := adminClient.deleteSiteReplicationInfo(ctx, remReq)
   198  	assert.Nil(err)
   199  	assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function))
   200  }
   201  
   202  func TestSiteReplicationStatus(t *testing.T) {
   203  	assert := assert.New(t)
   204  	// mock minIO client
   205  	adminClient := AdminClientMock{}
   206  
   207  	function := "getSiteReplicationStatus()"
   208  	ctx, cancel := context.WithCancel(context.Background())
   209  	defer cancel()
   210  
   211  	retValueMock := madmin.SRStatusInfo{
   212  		Enabled:      true,
   213  		MaxBuckets:   0,
   214  		MaxUsers:     0,
   215  		MaxGroups:    0,
   216  		MaxPolicies:  0,
   217  		Sites:        nil,
   218  		StatsSummary: nil,
   219  		BucketStats:  nil,
   220  		PolicyStats:  nil,
   221  		UserStats:    nil,
   222  		GroupStats:   nil,
   223  	}
   224  
   225  	expValueMock := &madmin.SRStatusInfo{
   226  		Enabled:      true,
   227  		MaxBuckets:   0,
   228  		MaxUsers:     0,
   229  		MaxGroups:    0,
   230  		MaxPolicies:  0,
   231  		Sites:        nil,
   232  		StatsSummary: nil,
   233  		BucketStats:  nil,
   234  		PolicyStats:  nil,
   235  		UserStats:    nil,
   236  		GroupStats:   nil,
   237  	}
   238  
   239  	getSiteReplicationStatus = func(_ context.Context, _ madmin.SRStatusOptions) (info *madmin.SRStatusInfo, err error) {
   240  		return &retValueMock, nil
   241  	}
   242  
   243  	reqValues := madmin.SRStatusOptions{
   244  		Buckets:  true,
   245  		Policies: true,
   246  		Users:    true,
   247  		Groups:   true,
   248  	}
   249  	srInfo, err := adminClient.getSiteReplicationStatus(ctx, reqValues)
   250  	if err != nil {
   251  		assert.Error(err)
   252  	}
   253  
   254  	assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: expected result is not same", function))
   255  }