github.com/minio/console@v1.3.0/api/admin_console_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 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  	"encoding/json"
    22  	"fmt"
    23  	"testing"
    24  
    25  	"github.com/minio/madmin-go/v3"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestAdminConsoleLog(t *testing.T) {
    30  	assert := assert.New(t)
    31  	adminClient := AdminClientMock{}
    32  	mockWSConn := mockConn{}
    33  	function := "startConsoleLog(ctx, )"
    34  	ctx, cancel := context.WithCancel(context.Background())
    35  	defer cancel()
    36  	testReceiver := make(chan madmin.LogInfo, 5)
    37  	textToReceive := "test message"
    38  	testStreamSize := 5
    39  	isClosed := false // testReceiver is closed?
    40  
    41  	// Test-1: Serve Console with no errors until Console finishes sending
    42  	// define mock function behavior for minio server Console
    43  	minioGetLogsMock = func(_ context.Context, _ string, _ int, _ string) <-chan madmin.LogInfo {
    44  		ch := make(chan madmin.LogInfo)
    45  		// Only success, start a routine to start reading line by line.
    46  		go func(ch chan<- madmin.LogInfo) {
    47  			defer close(ch)
    48  			lines := make([]int, testStreamSize)
    49  			// mocking sending 5 lines of info
    50  			for range lines {
    51  				info := madmin.LogInfo{
    52  					ConsoleMsg: textToReceive,
    53  				}
    54  				ch <- info
    55  			}
    56  		}(ch)
    57  		return ch
    58  	}
    59  	writesCount := 1
    60  	// mock connection WriteMessage() no error
    61  	connWriteMessageMock = func(_ int, data []byte) error {
    62  		// emulate that receiver gets the message written
    63  		var t madmin.LogInfo
    64  		_ = json.Unmarshal(data, &t)
    65  		if writesCount == testStreamSize {
    66  			if !isClosed {
    67  				close(testReceiver)
    68  				isClosed = true
    69  			}
    70  			return nil
    71  		}
    72  		testReceiver <- t
    73  		writesCount++
    74  		return nil
    75  	}
    76  	if err := startConsoleLog(ctx, mockWSConn, adminClient, LogRequest{node: "", logType: "all"}); err != nil {
    77  		t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
    78  	}
    79  	// check that the TestReceiver got the same number of data from Console.
    80  	for i := range testReceiver {
    81  		assert.Equal(textToReceive, i.ConsoleMsg)
    82  	}
    83  
    84  	// Test-2: if error happens while writing, return error
    85  	connWriteMessageMock = func(_ int, _ []byte) error {
    86  		return fmt.Errorf("error on write")
    87  	}
    88  	if err := startConsoleLog(ctx, mockWSConn, adminClient, LogRequest{node: "", logType: "all"}); assert.Error(err) {
    89  		assert.Equal("error on write", err.Error())
    90  	}
    91  
    92  	// Test-3: error happens on GetLogs Minio, Console should stop
    93  	// and error shall be returned.
    94  	minioGetLogsMock = func(_ context.Context, _ string, _ int, _ string) <-chan madmin.LogInfo {
    95  		ch := make(chan madmin.LogInfo)
    96  		// Only success, start a routine to start reading line by line.
    97  		go func(ch chan<- madmin.LogInfo) {
    98  			defer close(ch)
    99  			lines := make([]int, 2)
   100  			// mocking sending 5 lines of info
   101  			for range lines {
   102  				info := madmin.LogInfo{
   103  					ConsoleMsg: textToReceive,
   104  				}
   105  				ch <- info
   106  			}
   107  			ch <- madmin.LogInfo{Err: fmt.Errorf("error on Console")}
   108  		}(ch)
   109  		return ch
   110  	}
   111  	connWriteMessageMock = func(_ int, _ []byte) error {
   112  		return nil
   113  	}
   114  	if err := startConsoleLog(ctx, mockWSConn, adminClient, LogRequest{node: "", logType: "all"}); assert.Error(err) {
   115  		assert.Equal("error on Console", err.Error())
   116  	}
   117  }