github.com/matrixorigin/matrixone@v1.2.0/pkg/util/uuidutil_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"net"
    21  	"testing"
    22  
    23  	"github.com/google/uuid"
    24  	"github.com/prashantv/gostub"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  var dummyRealHardwareAddr = func(ctx context.Context) (net.HardwareAddr, error) {
    29  	return net.ParseMAC("3e:bf:9f:39:60:c8")
    30  }
    31  var dummyDockerHardwareAdder = func(ctx context.Context) (net.HardwareAddr, error) {
    32  	return net.ParseMAC("02:42:ac:11:00:02")
    33  }
    34  
    35  func TestSetUUIDNodeID(t *testing.T) {
    36  	type args struct {
    37  		nodeUuid []byte
    38  	}
    39  	type fields struct {
    40  		prepare func() *gostub.Stubs
    41  	}
    42  	nodeUUID := uuid.Must(uuid.Parse("abddd266-238f-11ed-ba8c-d6aee46d73fa"))
    43  	tests := []struct {
    44  		name    string
    45  		fields  fields
    46  		args    args
    47  		wantErr bool
    48  	}{
    49  		{
    50  			name: "normal",
    51  			fields: fields{
    52  				prepare: func() *gostub.Stubs {
    53  					return gostub.Stub(&getDefaultHardwareAddr, dummyRealHardwareAddr)
    54  				},
    55  			},
    56  			args: args{
    57  				nodeUuid: nodeUUID[:],
    58  			},
    59  			wantErr: false,
    60  		},
    61  		{
    62  			name: "normal_use_nodeUUID",
    63  			fields: fields{
    64  				prepare: func() *gostub.Stubs {
    65  					return gostub.Stub(&getDefaultHardwareAddr, dummyDockerHardwareAdder)
    66  				},
    67  			},
    68  			args: args{
    69  				nodeUuid: nodeUUID[:],
    70  			},
    71  			wantErr: false,
    72  		},
    73  		{
    74  			name: "random",
    75  			fields: fields{
    76  				prepare: func() *gostub.Stubs {
    77  					return gostub.Stub(&getDefaultHardwareAddr, dummyDockerHardwareAdder)
    78  				},
    79  			},
    80  			args: args{
    81  				nodeUuid: nodeUUID[:3],
    82  			},
    83  			wantErr: false,
    84  		},
    85  		{
    86  			name:   "non-node_uuid",
    87  			fields: fields{},
    88  			args: args{
    89  				nodeUuid: []byte{},
    90  			},
    91  			wantErr: false,
    92  		},
    93  		{
    94  			name: "non-node_uuid-with_mock",
    95  			fields: fields{
    96  				prepare: func() *gostub.Stubs {
    97  					return gostub.Stub(&getDefaultHardwareAddr, dummyDockerHardwareAdder)
    98  				},
    99  			},
   100  			args: args{
   101  				nodeUuid: []byte{},
   102  			},
   103  			wantErr: false,
   104  		},
   105  	}
   106  	ctx := context.Background()
   107  	for _, tt := range tests {
   108  		t.Run(tt.name, func(t *testing.T) {
   109  			var stubs *gostub.Stubs
   110  			if tt.fields.prepare != nil {
   111  				stubs = tt.fields.prepare()
   112  			}
   113  			if err := SetUUIDNodeID(ctx, tt.args.nodeUuid); (err != nil) != tt.wantErr {
   114  				t.Errorf("SetUUIDNodeID() error = %v, wantErr %v", err, tt.wantErr)
   115  			}
   116  			localMac, _ := getDefaultHardwareAddr(ctx)
   117  			mockMac, _ := dummyDockerHardwareAdder(ctx)
   118  			t.Logf("local mac: %s", localMac)
   119  			t.Logf("mock  mac: %s", mockMac)
   120  			t.Logf("nodeUUID : %x", tt.args.nodeUuid)
   121  			id, _ := uuid.NewV7()
   122  			t.Logf("uuid 1: %s", id)
   123  			id, _ = uuid.NewV7()
   124  			t.Logf("uuid 2: %s", id)
   125  			require.Equal(t, false, bytes.Equal(id[10:13], dockerMacPrefix))
   126  			if stubs != nil {
   127  				stubs.Reset()
   128  			}
   129  		})
   130  	}
   131  }