github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/utils/logging/service/literal_loggable_test.go (about)

     1  //  Copyright 2020 Google Inc. All Rights Reserved.
     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 service
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestLiteralLoggable_GetValueAsInt64Slice(t *testing.T) {
    24  	loggable := literalLoggable{
    25  		int64s: map[string][]int64{
    26  			"gb": {1, 2, 3},
    27  		},
    28  	}
    29  
    30  	assert.Equal(t, []int64{1, 2, 3}, loggable.GetValueAsInt64Slice("gb"))
    31  	assert.Empty(t, loggable.GetValueAsInt64Slice("not-there"))
    32  }
    33  
    34  func TestLiteralLoggable_GetValue(t *testing.T) {
    35  	loggable := literalLoggable{
    36  		strings: map[string]string{"hello": "world"},
    37  	}
    38  
    39  	assert.Equal(t, "world", loggable.GetValue("hello"))
    40  	assert.Empty(t, loggable.GetValue("not-there"))
    41  }
    42  
    43  func TestLiteralLoggable_ReadSerialPortLogs(t *testing.T) {
    44  	loggable := literalLoggable{
    45  		traceLogs: []string{"log-a", "log-b"},
    46  	}
    47  
    48  	assert.Equal(t, []string{"log-a", "log-b"}, loggable.ReadSerialPortLogs())
    49  }
    50  
    51  func TestSingleImageImportLoggableBuilder(t *testing.T) {
    52  	format := "vmdk"
    53  	sourceGb := int64(12)
    54  	targetGb := int64(100)
    55  	traceLogs1 := []string{"log-a", "log-b"}
    56  	traceLogs2 := []string{"log-c", "log-d"}
    57  	inflationTypeValue := "qemu"
    58  	inflationTimeValue := int64(10000)
    59  	shadowInflationTimeValue := int64(5000)
    60  	matchResultValue := "true"
    61  	bootFSValue := "btrfs"
    62  	for _, isUEFICompatibleImageValue := range []bool{true, false} {
    63  		for _, isUEFIDetectedValue := range []bool{true, false} {
    64  			for _, biosBootableValue := range []bool{true, false} {
    65  				expected := literalLoggable{
    66  					strings: map[string]string{
    67  						importFileFormat:      format,
    68  						inflationType:         inflationTypeValue,
    69  						shadowDiskMatchResult: matchResultValue,
    70  						rootFS:                bootFSValue,
    71  					},
    72  					int64s: map[string][]int64{
    73  						sourceSizeGb:        {sourceGb},
    74  						targetSizeGb:        {targetGb},
    75  						inflationTime:       {inflationTimeValue},
    76  						shadowInflationTime: {shadowInflationTimeValue},
    77  					},
    78  					bools: map[string]bool{
    79  						isUEFICompatibleImage: isUEFICompatibleImageValue,
    80  						isUEFIDetected:        isUEFIDetectedValue,
    81  						uefiBootable:          isUEFIDetectedValue,
    82  						biosBootable:          biosBootableValue,
    83  					},
    84  					traceLogs: append(traceLogs1, traceLogs2...),
    85  				}
    86  				assert.Equal(t, expected, NewSingleImageImportLoggableBuilder().
    87  					SetDiskAttributes(format, sourceGb, targetGb).
    88  					SetUEFIMetrics(isUEFICompatibleImageValue, isUEFIDetectedValue, biosBootableValue, bootFSValue).
    89  					SetInflationAttributes(matchResultValue, inflationTypeValue, inflationTimeValue, shadowInflationTimeValue).
    90  					AppendTraceLogs(traceLogs1).
    91  					AppendTraceLogs(traceLogs2).
    92  					Build())
    93  			}
    94  		}
    95  	}
    96  }
    97  
    98  func TestOvfExportLoggableBuilder(t *testing.T) {
    99  	sourceGb := []int64{12, 25}
   100  	targetGb := []int64{100, 300}
   101  	traceLogs1 := []string{"log-a", "log-b"}
   102  	traceLogs2 := []string{"log-c", "log-d"}
   103  
   104  	expected := literalLoggable{
   105  		strings: map[string]string{},
   106  		int64s: map[string][]int64{
   107  			sourceSizeGb: {12, 25},
   108  			targetSizeGb: {100, 300},
   109  		},
   110  		bools:     map[string]bool{},
   111  		traceLogs: append(traceLogs1, traceLogs2...),
   112  	}
   113  	assert.Equal(t, expected, NewOvfExportLoggableBuilder().SetDiskSizes(sourceGb, targetGb).
   114  		AppendTraceLogs(traceLogs1).
   115  		AppendTraceLogs(traceLogs2).
   116  		Build())
   117  }