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