github.com/minio/console@v1.4.1/api/admin_objects_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  package api
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	mc "github.com/minio/mc/cmd"
    25  	"github.com/minio/minio-go/v7"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestWSRewindObjects(t *testing.T) {
    30  	assert := assert.New(t)
    31  	client := s3ClientMock{}
    32  
    33  	tests := []struct {
    34  		name         string
    35  		testOptions  objectsListOpts
    36  		testMessages []*mc.ClientContent
    37  	}{
    38  		{
    39  			name: "Get list with multiple elements",
    40  			testOptions: objectsListOpts{
    41  				BucketName: "buckettest",
    42  				Prefix:     "/",
    43  				Date:       time.Now(),
    44  			},
    45  			testMessages: []*mc.ClientContent{
    46  				{
    47  					BucketName: "buckettest",
    48  					URL:        mc.ClientURL{Path: "/file1.txt"},
    49  				},
    50  				{
    51  					BucketName: "buckettest",
    52  					URL:        mc.ClientURL{Path: "/file2.txt"},
    53  				},
    54  				{
    55  					BucketName: "buckettest",
    56  					URL:        mc.ClientURL{Path: "/path1"},
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "Empty list of elements",
    62  			testOptions: objectsListOpts{
    63  				BucketName: "emptybucket",
    64  				Prefix:     "/",
    65  				Date:       time.Now(),
    66  			},
    67  			testMessages: []*mc.ClientContent{},
    68  		},
    69  		{
    70  			name: "Get list with one element",
    71  			testOptions: objectsListOpts{
    72  				BucketName: "buckettest",
    73  				Prefix:     "/",
    74  				Date:       time.Now(),
    75  			},
    76  			testMessages: []*mc.ClientContent{
    77  				{
    78  					BucketName: "buckettestsingle",
    79  					URL:        mc.ClientURL{Path: "/file12.txt"},
    80  				},
    81  			},
    82  		},
    83  		{
    84  			name: "Get data from subpaths",
    85  			testOptions: objectsListOpts{
    86  				BucketName: "buckettest",
    87  				Prefix:     "/path1/path2",
    88  				Date:       time.Now(),
    89  			},
    90  			testMessages: []*mc.ClientContent{
    91  				{
    92  					BucketName: "buckettestsingle",
    93  					URL:        mc.ClientURL{Path: "/path1/path2/file12.txt"},
    94  				},
    95  			},
    96  		},
    97  	}
    98  
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(_ *testing.T) {
   101  			ctx, cancel := context.WithCancel(context.Background())
   102  			defer cancel()
   103  
   104  			mcListMock = func(_ context.Context, _ mc.ListOptions) <-chan *mc.ClientContent {
   105  				ch := make(chan *mc.ClientContent)
   106  				go func() {
   107  					defer close(ch)
   108  					for _, m := range tt.testMessages {
   109  						ch <- m
   110  					}
   111  				}()
   112  				return ch
   113  			}
   114  
   115  			rewindList := startRewindListing(ctx, client, &tt.testOptions)
   116  
   117  			// check that the rewindList got the same number of data from Console.
   118  
   119  			totalItems := 0
   120  			for data := range rewindList {
   121  				// Compare elements as we are defining the channel responses
   122  				assert.Equal(tt.testMessages[totalItems].URL.Path, data.URL.Path)
   123  				totalItems++
   124  			}
   125  			assert.Equal(len(tt.testMessages), totalItems)
   126  		})
   127  	}
   128  }
   129  
   130  func TestWSListObjects(t *testing.T) {
   131  	assert := assert.New(t)
   132  	client := minioClientMock{}
   133  
   134  	tests := []struct {
   135  		name         string
   136  		wantErr      bool
   137  		testOptions  objectsListOpts
   138  		testMessages []minio.ObjectInfo
   139  	}{
   140  		{
   141  			name:    "Get list with multiple elements",
   142  			wantErr: false,
   143  			testOptions: objectsListOpts{
   144  				BucketName: "buckettest",
   145  				Prefix:     "/",
   146  			},
   147  			testMessages: []minio.ObjectInfo{
   148  				{
   149  					Key:          "/file1.txt",
   150  					Size:         500,
   151  					IsLatest:     true,
   152  					LastModified: time.Now(),
   153  				},
   154  				{
   155  					Key:          "/file2.txt",
   156  					Size:         500,
   157  					IsLatest:     true,
   158  					LastModified: time.Now(),
   159  				},
   160  				{
   161  					Key: "/path1",
   162  				},
   163  			},
   164  		},
   165  		{
   166  			name:    "Empty list of elements",
   167  			wantErr: false,
   168  			testOptions: objectsListOpts{
   169  				BucketName: "emptybucket",
   170  				Prefix:     "/",
   171  			},
   172  			testMessages: []minio.ObjectInfo{},
   173  		},
   174  		{
   175  			name:    "Get list with one element",
   176  			wantErr: false,
   177  			testOptions: objectsListOpts{
   178  				BucketName: "buckettest",
   179  				Prefix:     "/",
   180  			},
   181  			testMessages: []minio.ObjectInfo{
   182  				{
   183  					Key:          "/file2.txt",
   184  					Size:         500,
   185  					IsLatest:     true,
   186  					LastModified: time.Now(),
   187  				},
   188  			},
   189  		},
   190  		{
   191  			name:    "Get data from subpaths",
   192  			wantErr: false,
   193  			testOptions: objectsListOpts{
   194  				BucketName: "buckettest",
   195  				Prefix:     "/path1/path2",
   196  			},
   197  			testMessages: []minio.ObjectInfo{
   198  				{
   199  					Key:          "/path1/path2/file1.txt",
   200  					Size:         500,
   201  					IsLatest:     true,
   202  					LastModified: time.Now(),
   203  				},
   204  			},
   205  		},
   206  	}
   207  
   208  	for _, tt := range tests {
   209  		t.Run(tt.name, func(_ *testing.T) {
   210  			ctx, cancel := context.WithCancel(context.Background())
   211  			defer cancel()
   212  
   213  			minioListObjectsMock = func(_ context.Context, _ string, _ minio.ListObjectsOptions) <-chan minio.ObjectInfo {
   214  				ch := make(chan minio.ObjectInfo)
   215  				go func() {
   216  					defer close(ch)
   217  					for _, m := range tt.testMessages {
   218  						ch <- m
   219  					}
   220  				}()
   221  				return ch
   222  			}
   223  
   224  			objectsListing := startObjectsListing(ctx, client, &tt.testOptions)
   225  
   226  			// check that the TestReceiver got the same number of data from Console
   227  			totalItems := 0
   228  			for data := range objectsListing {
   229  				// Compare elements as we are defining the channel responses
   230  				assert.Equal(tt.testMessages[totalItems].Key, data.Key)
   231  				totalItems++
   232  			}
   233  			assert.Equal(len(tt.testMessages), totalItems)
   234  		})
   235  	}
   236  }