github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/ais/aismsg_internal_test.go (about) 1 // Package ais provides core functionality for the AIStore object storage. 2 /* 3 * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 6 package ais 7 8 import ( 9 "fmt" 10 "reflect" 11 "testing" 12 13 "github.com/NVIDIA/aistore/api/apc" 14 "github.com/NVIDIA/aistore/cmn" 15 "github.com/NVIDIA/aistore/cmn/cos" 16 jsoniter "github.com/json-iterator/go" 17 ) 18 19 type aismsgTestConf struct { 20 actionMsgPresent bool 21 restPropsPresent bool 22 } 23 24 func (atc aismsgTestConf) Name() string { 25 return fmt.Sprintf("actionMsgPresent:%v/restPropsPresent:%v", 26 atc.actionMsgPresent, atc.restPropsPresent) 27 } 28 29 func testAisMsgMarshal(t *testing.T, tc aismsgTestConf) { 30 t.Run(tc.Name(), func(t *testing.T) { 31 beforeMsg := &aisMsg{} 32 if tc.actionMsgPresent { 33 actionMsg := apc.ActMsg{ 34 Action: "test-action", 35 Name: "test-name", 36 Value: &cmn.Bck{ 37 Name: "test-bck", 38 Provider: "test-provider", 39 Ns: cmn.Ns{ 40 UUID: "test-uuid", 41 Name: "test-name", 42 }, 43 }, 44 } 45 beforeMsg.ActMsg = actionMsg 46 } 47 if tc.restPropsPresent { 48 beforeMsg.BMDVersion = 12 49 beforeMsg.RMDVersion = 34 50 beforeMsg.UUID = "outer-uuid" 51 } 52 b, err := jsoniter.Marshal(beforeMsg) 53 if err != nil { 54 t.Errorf("Failed to marshal beforeMsg: %v", err) 55 } 56 afterAisMsg := &aisMsg{} 57 58 err = jsoniter.Unmarshal(b, afterAisMsg) 59 if err != nil { 60 t.Errorf("Unmarshal failed for aisMsg, err: %v", err) 61 } 62 63 if afterAisMsg.Value != nil { 64 bck := &cmn.Bck{} 65 err = cos.MorphMarshal(afterAisMsg.Value, bck) 66 if err != nil { 67 t.Errorf("Morph marshal failed for aisMsg.Value: %v, err: %v", afterAisMsg.Value, err) 68 } 69 afterAisMsg.Value = bck 70 } 71 if !reflect.DeepEqual(*beforeMsg, *afterAisMsg) { 72 t.Errorf("Marshaled: %v and Unmarshalled: %v beforeMsg differ", beforeMsg, afterAisMsg) 73 } 74 }) 75 } 76 77 func TestAisMsgMarshal(t *testing.T) { 78 tests := []aismsgTestConf{ 79 {actionMsgPresent: true, restPropsPresent: true}, 80 {actionMsgPresent: true, restPropsPresent: false}, 81 {actionMsgPresent: false, restPropsPresent: true}, 82 {actionMsgPresent: false, restPropsPresent: false}, 83 } 84 for _, test := range tests { 85 testAisMsgMarshal(t, test) 86 } 87 }