github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/report/issue_test.go (about) 1 // Copyright (c) 2021, 2024, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 package report 4 5 import ( 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 utilfiles "github.com/verrazzano/verrazzano/tools/vz/pkg/internal/util/files" 10 ) 11 12 // TestHandlingUnknownIssues Tests unknown issue types are handled correctly when using the known issue helpers 13 // GIVEN a call to *KnownIssue* helper 14 // WHEN with an unknown issue type 15 // THEN the call panic's (this is catching a coding error) 16 func TestHandlingUnknownIssues(t *testing.T) { 17 var issueReporter = IssueReporter{ 18 PendingIssues: make(map[string]Issue), 19 } 20 rootDirectory := "test root directory" 21 messages := []string{"test message"} 22 files := []string{"test file name"} 23 matches := make([]utilfiles.TextMatch, 1) 24 matches[0] = utilfiles.TextMatch{ 25 FileName: "test file", 26 FileLine: 50, 27 MatchedText: "test matched text", 28 } 29 supportingData := make([]SupportData, 1) 30 supportingData[0] = SupportData{ 31 Messages: messages, 32 TextMatches: matches, 33 } 34 assert.Panics(t, func() { issueReporter.AddKnownIssueMessagesFiles("BADISSUETYPE", rootDirectory, messages, files) }) 35 assert.Panics(t, func() { issueReporter.AddKnownIssueSupportingData("BADISSUETYPE", rootDirectory, supportingData) }) 36 assert.Panics(t, func() { issueReporter.AddKnownIssueMessagesMatches("BADISSUETYPE", rootDirectory, messages, matches) }) 37 assert.Panics(t, func() { NewKnownIssueMessagesFiles("BADISSUETYPE", rootDirectory, messages, files) }) 38 assert.Panics(t, func() { NewKnownIssueSupportingData("BADISSUETYPE", rootDirectory, supportingData) }) 39 assert.Panics(t, func() { NewKnownIssueMessagesMatches("BADISSUETYPE", rootDirectory, messages, matches) }) 40 } 41 42 // TestHandlingKnownIssues Tests the known issue helpers 43 // GIVEN a call to *KnownIssue* helper 44 // WHEN with a known issue type 45 // THEN the issue is successfully added or created 46 func TestHandlingKnownIssues(t *testing.T) { 47 var issueReporter = IssueReporter{ 48 PendingIssues: make(map[string]Issue), 49 } 50 rootDirectory := "test root directory" 51 messages := []string{"test message"} 52 files := []string{"test file name"} 53 matches := make([]utilfiles.TextMatch, 1) 54 matches[0] = utilfiles.TextMatch{ 55 FileName: "test file", 56 FileLine: 50, 57 MatchedText: "test matched text", 58 } 59 supportingData := make([]SupportData, 1) 60 supportingData[0] = SupportData{ 61 Messages: messages, 62 TextMatches: matches, 63 } 64 issueA := NewKnownIssueMessagesFiles(ImagePullBackOff, rootDirectory, messages, files) 65 assert.NotNil(t, issueA) 66 issueB := NewKnownIssueSupportingData(InsufficientMemory, rootDirectory, supportingData) 67 assert.NotNil(t, issueB) 68 issueC := NewKnownIssueMessagesMatches(PodProblemsNotReported, rootDirectory, messages, matches) 69 assert.NotNil(t, issueC) 70 for i := 0; i < 2; i++ { 71 issueReporter.AddKnownIssueMessagesFiles(ImagePullBackOff, rootDirectory, messages, files) 72 issueReporter.AddKnownIssueSupportingData(InsufficientMemory, rootDirectory, supportingData) 73 issueReporter.AddKnownIssueMessagesMatches(PodProblemsNotReported, rootDirectory, messages, matches) 74 } 75 } 76 77 // TestMiscHelpers tests misc helpers 78 func TestMiscHelpers(t *testing.T) { 79 messages := SingleMessage("test message") 80 assert.NotNil(t, messages) 81 assert.True(t, len(messages) == 1) 82 } 83 84 func TestDeduplicateSupportingData(t *testing.T) { 85 var duplicateData = []SupportData{{Messages: []string{"data", "data"}}, {Messages: []string{"data", "data"}}} 86 assert.NotEmpty(t, DeduplicateSupportingDataList(duplicateData)) 87 } 88 89 func TestEventPodRelatedUtilities(t *testing.T) { 90 var tests = []struct { 91 pod string 92 service string 93 namespace string 94 podlog string 95 }{ 96 { 97 "fluentd", 98 "open-search", 99 "verrazzano-system", 100 "namespaces/verrazzano-system/fluentd/operator", 101 }, 102 } 103 104 for _, tt := range tests { 105 assert.NotEmpty(t, GetRelatedPodMessage(tt.pod, tt.namespace)) 106 assert.NotEmpty(t, GetRelatedServiceMessage(tt.service, tt.namespace)) 107 assert.NotEmpty(t, GetRelatedLogFromPodMessage(tt.podlog)) 108 assert.NotEmpty(t, GetRelatedEventMessage(tt.namespace)) 109 assert.NotEmpty(t, GetRelatedVZResourceMessage()) 110 } 111 }