github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/cfghandler/pqsupdate_test.go (about)

     1  package cfghandler
     2  
     3  import (
     4  	"testing"
     5  
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	"github.com/siglens/siglens/pkg/config"
    11  	commonconfig "github.com/siglens/siglens/pkg/config/common"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestSavePQSConfigToRunMod(t *testing.T) {
    16  	testCases := []struct {
    17  		pqsEnabled       bool
    18  		expectConfigFile bool
    19  	}{
    20  		{true, true},
    21  		{false, true},
    22  	}
    23  
    24  	for _, tc := range testCases {
    25  		t.Run(fmt.Sprintf("pqsEnabled_%t", tc.pqsEnabled), func(t *testing.T) {
    26  			tempFile, err := ioutil.TempFile("", "runmodcfg_*.json")
    27  			if err != nil {
    28  				t.Fatalf("Failed to create temp file: %v", err)
    29  			}
    30  			tempFilePath := tempFile.Name()
    31  			defer os.Remove(tempFilePath)
    32  
    33  			err = SavePQSConfigToRunMod(tempFilePath, tc.pqsEnabled)
    34  			assert.NoError(t, err, "Error in SavePQSConfigToRunMod")
    35  		})
    36  	}
    37  }
    38  
    39  func TestExtractReadRunModConfig(t *testing.T) {
    40  	cases := []struct {
    41  		name     string
    42  		input    []byte
    43  		expected commonconfig.RunModConfig
    44  		wantErr  bool
    45  	}{
    46  		{
    47  			name:  "Valid Enabled Config",
    48  			input: []byte(`{"pqsEnabled": true}`),
    49  			expected: commonconfig.RunModConfig{
    50  				PQSEnabled: true,
    51  			},
    52  			wantErr: false,
    53  		},
    54  		{
    55  			name:  "Valid Disabled Config",
    56  			input: []byte(`{"pqsEnabled": false}`),
    57  			expected: commonconfig.RunModConfig{
    58  				PQSEnabled: false,
    59  			},
    60  			wantErr: false,
    61  		},
    62  	}
    63  
    64  	for _, tc := range cases {
    65  		t.Run(tc.name, func(t *testing.T) {
    66  			actualConfig, err := config.ExtractReadRunModConfig(tc.input)
    67  
    68  			if tc.wantErr {
    69  				assert.Error(t, err, "Expected an error in %s", tc.name)
    70  			} else {
    71  				assert.NoError(t, err, "Unexpected error in %s: %v", tc.name, err)
    72  				assert.Equal(t, tc.expected, actualConfig, "Mismatch in config for %s", tc.name)
    73  			}
    74  		})
    75  	}
    76  }