github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/actionmsg_test.go (about)

     1  // Package cmn provides common constants, types, and utilities for AIS clients
     2  // and AIStore.
     3  /*
     4   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     5   */
     6  
     7  package cmn_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/NVIDIA/aistore/api/apc"
    14  	"github.com/NVIDIA/aistore/cmn/cos"
    15  	"github.com/NVIDIA/aistore/tools/tassert"
    16  	jsoniter "github.com/json-iterator/go"
    17  )
    18  
    19  type actmsgTestConf struct {
    20  	action       string
    21  	vals         []string
    22  	isSelectObjs bool
    23  }
    24  
    25  func testRawUnmarshal(t *testing.T, tc actmsgTestConf) {
    26  	t.Run(tc.action, func(t *testing.T) {
    27  		for _, val := range tc.vals {
    28  			msg := &apc.ActMsg{}
    29  			raw := fmt.Sprintf(val, tc.action)
    30  			err := jsoniter.Unmarshal([]byte(raw), &msg)
    31  			tassert.CheckError(t, err)
    32  			tassert.Errorf(t, tc.action == msg.Action, "actions do not match (%q vs %q)", tc.action, msg.Action)
    33  
    34  			// parse the template
    35  			if tc.isSelectObjs {
    36  				lrMsg := &apc.ListRange{}
    37  				err = cos.MorphMarshal(msg.Value, lrMsg)
    38  				tassert.CheckError(t, err)
    39  			}
    40  		}
    41  	})
    42  }
    43  
    44  func TestActMsgRawUnmarshal(t *testing.T) {
    45  	tests := []actmsgTestConf{
    46  		{
    47  			action: apc.ActEvictObjects,
    48  			vals: []string{
    49  				`{"action":"%s","value":{"template":"__tst/test-{1000..2000}"}}`,
    50  				`{"action":"%s","value":{"objnames":["o1","o2","o3"]}}`,
    51  			},
    52  			isSelectObjs: true,
    53  		},
    54  		{
    55  			action: apc.ActPrefetchObjects,
    56  			vals: []string{
    57  				`{"action":"%s","value":{"template":"__tst/test-{1000..2000}"}}`,
    58  				`{"action":"%s","value":{"objnames":["o1","o2","o3"]}}`,
    59  			},
    60  			isSelectObjs: true,
    61  		},
    62  		{
    63  			action: apc.ActDeleteObjects,
    64  			vals: []string{
    65  				`{"action":"%s","value":{"template":"__tst/test-{1000..2000}"}}`,
    66  				`{"action":"%s","value":{"objnames":["o1","o2","o3"]}}`,
    67  			},
    68  			isSelectObjs: true,
    69  		},
    70  		{
    71  			action: apc.ActSetBprops,
    72  			vals: []string{
    73  				`{"action":"%s","value":{"checksum": {"type": "sha256"}, "mirror": {"enable": true}}}`,
    74  			},
    75  		},
    76  		{
    77  			action: apc.ActCreateBck,
    78  			vals: []string{
    79  				`{"action":"%s","value":{"checksum": {"type": "sha256"}, "mirror": {"enable": true}}}`,
    80  			},
    81  		},
    82  		{
    83  			action: apc.ActXactStart,
    84  			vals: []string{
    85  				`{"action":"%s","value":{"kind": "rebalance"}}`,
    86  			},
    87  		},
    88  		{
    89  			action: apc.ActXactStop,
    90  			vals: []string{
    91  				`{"action":"%s","value":{"kind": "rebalance"}}`,
    92  			},
    93  		},
    94  		{
    95  			action: apc.ActList,
    96  			vals: []string{
    97  				`{"action":"%s","value":{"props": "size"}}`,
    98  			},
    99  		},
   100  		{
   101  			action: apc.ActPromote,
   102  			vals: []string{
   103  				`{"action":"%s","value":{"target": "234ed78", "recurs": true, "keep": false}}`,
   104  			},
   105  		},
   106  	}
   107  	for _, test := range tests {
   108  		testRawUnmarshal(t, test)
   109  	}
   110  }