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

     1  // Package ais provides core functionality for the AIStore object storage.
     2  /*
     3   * Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package ais
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/NVIDIA/aistore/api/apc"
    13  	"github.com/NVIDIA/aistore/cmn"
    14  	"github.com/NVIDIA/aistore/cmn/cos"
    15  	"github.com/NVIDIA/aistore/cmn/jsp"
    16  	"github.com/NVIDIA/aistore/ext/etl"
    17  	. "github.com/onsi/ginkgo/v2"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  func TestEtlMDDeepCopy(t *testing.T) {
    22  	etlMD := newEtlMD()
    23  	etlMD.Add(&etl.InitCodeMsg{
    24  		InitMsgBase: etl.InitMsgBase{
    25  			IDX:       "init-code",
    26  			CommTypeX: etl.Hpush,
    27  		},
    28  		Code: []byte("print('hello')"),
    29  	})
    30  	clone := etlMD.clone()
    31  	s1 := string(cos.MustMarshal(etlMD))
    32  	s2 := string(cos.MustMarshal(clone))
    33  	if s1 == "" || s2 == "" || s1 != s2 {
    34  		t.Log(s1)
    35  		t.Log(s2)
    36  		t.Fatal("marshal(etlmd) != marshal(clone(etlmd))")
    37  	}
    38  }
    39  
    40  var _ = Describe("EtlMD marshal and unmarshal", func() {
    41  	const (
    42  		mpath    = "/tmp"
    43  		testpath = "/tmp/.ais.test.etlMD"
    44  	)
    45  
    46  	var (
    47  		etlMD *etlMD
    48  		cfg   *cmn.Config
    49  	)
    50  
    51  	BeforeEach(func() {
    52  		// Set path for proxy (it uses ConfigDir)
    53  		config := cmn.GCO.BeginUpdate()
    54  		config.ConfigDir = mpath
    55  		config.Cksum.Type = cos.ChecksumXXHash
    56  		config.Space = cmn.SpaceConf{
    57  			LowWM: 75, HighWM: 90, OOS: 95,
    58  		}
    59  		config.LRU = cmn.LRUConf{
    60  			DontEvictTime: cos.Duration(time.Second), CapacityUpdTime: cos.Duration(time.Minute), Enabled: true,
    61  		}
    62  		cmn.GCO.CommitUpdate(config)
    63  		cfg = cmn.GCO.Get()
    64  
    65  		etlMD = newEtlMD()
    66  		for _, initType := range []string{etl.Code, etl.Spec} {
    67  			for i := range 5 {
    68  				var msg etl.InitMsg
    69  				if initType == etl.Code {
    70  					msg = &etl.InitCodeMsg{
    71  						InitMsgBase: etl.InitMsgBase{
    72  							IDX:       fmt.Sprintf("init-code-%d", i),
    73  							CommTypeX: etl.Hpush,
    74  						},
    75  						Code: []byte(fmt.Sprintf("print('hello-%d')", i)),
    76  					}
    77  				} else {
    78  					msg = &etl.InitSpecMsg{
    79  						InitMsgBase: etl.InitMsgBase{
    80  							IDX:       fmt.Sprintf("init-spec-%d", i),
    81  							CommTypeX: etl.Hpush,
    82  						},
    83  						Spec: []byte(fmt.Sprintf("test spec - %d", i)),
    84  					}
    85  				}
    86  				etlMD.Add(msg)
    87  			}
    88  		}
    89  	})
    90  
    91  	for _, node := range []string{apc.Target, apc.Proxy} {
    92  		makeEtlMDOwner := func() etlOwner {
    93  			var eowner etlOwner
    94  			switch node {
    95  			case apc.Target:
    96  				eowner = newEtlMDOwnerTgt()
    97  			case apc.Proxy:
    98  				eowner = newEtlMDOwnerPrx(cfg)
    99  			}
   100  			return eowner
   101  		}
   102  
   103  		Describe(node, func() {
   104  			var eowner etlOwner
   105  
   106  			BeforeEach(func() {
   107  				eowner = makeEtlMDOwner()
   108  				eowner.putPersist(etlMD, nil)
   109  			})
   110  
   111  			It("should correctly load etlMD for "+node, func() {
   112  				eowner.init()
   113  				Expect(eowner.Get()).To(Equal(&etlMD.MD))
   114  			})
   115  
   116  			It("should save and load etlMD using jsp methods for "+node, func() {
   117  				eowner.init()
   118  				etlMD := eowner.get()
   119  				for _, signature := range []bool{false, true} {
   120  					for _, compress := range []bool{false, true} {
   121  						for _, checksum := range []bool{false, true} {
   122  							opts := jsp.Options{
   123  								Compress:  compress,
   124  								Checksum:  checksum,
   125  								Signature: signature,
   126  							}
   127  							clone := etlMD.clone()
   128  							msg := &etl.InitCodeMsg{
   129  								InitMsgBase: etl.InitMsgBase{
   130  									IDX:       "init-code-" + cos.GenTie(),
   131  									CommTypeX: etl.Hpush,
   132  								},
   133  								Code: []byte("print('hello')"),
   134  							}
   135  
   136  							// Add bucket and save.
   137  							clone.Add(msg)
   138  							err := jsp.Save(testpath, clone, opts, nil)
   139  							Expect(err).NotTo(HaveOccurred())
   140  
   141  							// Load elsewhere and check.
   142  							loaded := newEtlMD()
   143  							_, err = jsp.Load(testpath, loaded, opts)
   144  							Expect(err).NotTo(HaveOccurred())
   145  							Expect(loaded.Version).To(BeEquivalentTo(clone.Version))
   146  							_, present := loaded.Get(msg.Name())
   147  							Expect(present).To(BeTrue())
   148  						}
   149  					}
   150  				}
   151  			})
   152  		})
   153  	}
   154  })