github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/utils/logging/service/outputinfo_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 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 23 "github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/utils/path" 24 "github.com/GoogleCloudPlatform/compute-image-tools/proto/go/pb" 25 "github.com/GoogleCloudPlatform/compute-image-tools/proto/go/pbtesting" 26 ) 27 28 func Test_OutputInfoLoggable_GetInspectionResults(t *testing.T) { 29 outputInfo := &pb.OutputInfo{ 30 InspectionResults: &pb.InspectionResults{ 31 OsRelease: &pb.OsRelease{ 32 DistroId: pb.Distro_WINDOWS, 33 }, 34 RootFs: path.RandString(5), 35 OsCount: 1, 36 }, 37 } 38 pbtesting.AssertEqual(t, outputInfo.InspectionResults, NewOutputInfoLoggable(outputInfo).GetInspectionResults()) 39 } 40 41 func Test_OutputInfoLoggable_GetValue(t *testing.T) { 42 outputInfo := &pb.OutputInfo{ 43 ImportFileFormat: "vmdk", 44 InflationType: "api", 45 ShadowDiskMatchResult: "matched", 46 } 47 48 loggable := NewOutputInfoLoggable(outputInfo) 49 assert.Equal(t, "vmdk", loggable.GetValue(importFileFormat)) 50 assert.Equal(t, "api", loggable.GetValue(inflationType)) 51 assert.Equal(t, "matched", loggable.GetValue(shadowDiskMatchResult)) 52 assert.Equal(t, "", loggable.GetValue("not-a-key")) 53 } 54 55 func Test_OutputInfoLoggable_GetValueAsBool(t *testing.T) { 56 for i, tt := range []struct { 57 input *pb.OutputInfo 58 expectedUefiCompatible bool 59 expectedUefiDetected bool 60 }{ 61 { 62 input: &pb.OutputInfo{}, 63 expectedUefiCompatible: false, 64 expectedUefiDetected: false, 65 }, 66 { 67 input: &pb.OutputInfo{IsUefiCompatibleImage: true}, 68 expectedUefiCompatible: true, 69 expectedUefiDetected: false, 70 }, 71 { 72 input: &pb.OutputInfo{IsUefiDetected: true}, 73 expectedUefiCompatible: false, 74 expectedUefiDetected: true, 75 }, 76 } { 77 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 78 loggable := NewOutputInfoLoggable(tt.input) 79 assert.Equal(t, tt.expectedUefiCompatible, loggable.GetValueAsBool(isUEFICompatibleImage)) 80 assert.Equal(t, tt.expectedUefiDetected, loggable.GetValueAsBool(isUEFIDetected)) 81 assert.False(t, loggable.GetValueAsBool("random-key")) 82 }) 83 } 84 } 85 86 func Test_OutputInfoLoggable_GetValueAsInt64Slice(t *testing.T) { 87 outputInfo := &pb.OutputInfo{ 88 TargetsSizeGb: []int64{1, 2}, 89 SourcesSizeGb: []int64{3, 4}, 90 InflationTimeMs: []int64{5, 6}, 91 ShadowInflationTimeMs: []int64{7, 8}, 92 } 93 94 loggable := NewOutputInfoLoggable(outputInfo) 95 assert.Equal(t, []int64{1, 2}, loggable.GetValueAsInt64Slice(targetSizeGb)) 96 assert.Equal(t, []int64{3, 4}, loggable.GetValueAsInt64Slice(sourceSizeGb)) 97 assert.Equal(t, []int64{5, 6}, loggable.GetValueAsInt64Slice(inflationTime)) 98 assert.Equal(t, []int64{7, 8}, loggable.GetValueAsInt64Slice(shadowInflationTime)) 99 assert.Empty(t, loggable.GetValueAsInt64Slice("not-a-key")) 100 } 101 102 func Test_OutputInfoLoggable_ReadSerialPortLogs(t *testing.T) { 103 outputInfo := &pb.OutputInfo{ 104 ImportFileFormat: "vmdk", 105 InflationType: "api", 106 ShadowDiskMatchResult: "matched", 107 } 108 109 loggable := NewOutputInfoLoggable(outputInfo) 110 assert.Equal(t, "vmdk", loggable.GetValue(importFileFormat)) 111 assert.Equal(t, "api", loggable.GetValue(inflationType)) 112 assert.Equal(t, "matched", loggable.GetValue(shadowDiskMatchResult)) 113 assert.Equal(t, "", loggable.GetValue("not-a-key")) 114 }