1// Copyright 2021 Google LLC 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 15package compliance 16 17import ( 18 "bytes" 19 "sort" 20 "strings" 21 "testing" 22) 23 24func TestShippedNodes(t *testing.T) { 25 tests := []struct { 26 name string 27 roots []string 28 edges []annotated 29 expectedNodes []string 30 }{ 31 { 32 name: "singleton", 33 roots: []string{"apacheLib.meta_lic"}, 34 edges: []annotated{}, 35 expectedNodes: []string{"apacheLib.meta_lic"}, 36 }, 37 { 38 name: "simplebinary", 39 roots: []string{"apacheBin.meta_lic"}, 40 edges: []annotated{ 41 {"apacheBin.meta_lic", "apacheLib.meta_lic", []string{"static"}}, 42 }, 43 expectedNodes: []string{"apacheBin.meta_lic", "apacheLib.meta_lic"}, 44 }, 45 { 46 name: "simpledynamic", 47 roots: []string{"apacheBin.meta_lic"}, 48 edges: []annotated{ 49 {"apacheBin.meta_lic", "lgplLib.meta_lic", []string{"dynamic"}}, 50 }, 51 expectedNodes: []string{"apacheBin.meta_lic"}, 52 }, 53 { 54 name: "container", 55 roots: []string{"apacheContainer.meta_lic"}, 56 edges: []annotated{ 57 {"apacheContainer.meta_lic", "apacheLib.meta_lic", []string{"static"}}, 58 {"apacheContainer.meta_lic", "gplLib.meta_lic", []string{"static"}}, 59 }, 60 expectedNodes: []string{ 61 "apacheContainer.meta_lic", 62 "apacheLib.meta_lic", 63 "gplLib.meta_lic", 64 }, 65 }, 66 { 67 name: "binary", 68 roots: []string{"apacheBin.meta_lic"}, 69 edges: []annotated{ 70 {"apacheBin.meta_lic", "apacheLib.meta_lic", []string{"static"}}, 71 {"apacheBin.meta_lic", "gplLib.meta_lic", []string{"static"}}, 72 }, 73 expectedNodes: []string{ 74 "apacheBin.meta_lic", 75 "apacheLib.meta_lic", 76 "gplLib.meta_lic", 77 }, 78 }, 79 { 80 name: "binarydynamic", 81 roots: []string{"apacheBin.meta_lic"}, 82 edges: []annotated{ 83 {"apacheBin.meta_lic", "apacheLib.meta_lic", []string{"static"}}, 84 {"apacheBin.meta_lic", "gplLib.meta_lic", []string{"dynamic"}}, 85 }, 86 expectedNodes: []string{ 87 "apacheBin.meta_lic", 88 "apacheLib.meta_lic", 89 }, 90 }, 91 { 92 name: "containerdeep", 93 roots: []string{"apacheContainer.meta_lic"}, 94 edges: []annotated{ 95 {"apacheContainer.meta_lic", "apacheBin.meta_lic", []string{"static"}}, 96 {"apacheBin.meta_lic", "apacheLib.meta_lic", []string{"static"}}, 97 {"apacheLib.meta_lic", "gplLib.meta_lic", []string{"dynamic"}}, 98 }, 99 expectedNodes: []string{ 100 "apacheContainer.meta_lic", 101 "apacheBin.meta_lic", 102 "apacheLib.meta_lic", 103 }, 104 }, 105 } 106 for _, tt := range tests { 107 t.Run(tt.name, func(t *testing.T) { 108 stderr := &bytes.Buffer{} 109 lg, err := toGraph(stderr, tt.roots, tt.edges) 110 if err != nil { 111 t.Errorf("unexpected test data error: got %s, want no error", err) 112 return 113 } 114 t.Logf("graph:") 115 for _, edge := range lg.Edges() { 116 t.Logf(" %s", edge.String()) 117 } 118 expectedNodes := append([]string{}, tt.expectedNodes...) 119 nodeset := ShippedNodes(lg) 120 t.Logf("shipped node set: %s", nodeset.String()) 121 122 actualNodes := nodeset.Names() 123 t.Logf("shipped nodes: [%s]", strings.Join(actualNodes, ", ")) 124 125 sort.Strings(expectedNodes) 126 sort.Strings(actualNodes) 127 128 t.Logf("sorted nodes: [%s]", strings.Join(actualNodes, ", ")) 129 t.Logf("expected nodes: [%s]", strings.Join(expectedNodes, ", ")) 130 if len(expectedNodes) != len(actualNodes) { 131 t.Errorf("unexpected number of shipped nodes: %d nodes, want %d nodes", 132 len(actualNodes), len(expectedNodes)) 133 return 134 } 135 for i := 0; i < len(actualNodes); i++ { 136 if expectedNodes[i] != actualNodes[i] { 137 t.Errorf("unexpected node at index %d: got %q, want %q", 138 i, actualNodes[i], expectedNodes[i]) 139 } 140 } 141 }) 142 } 143} 144