blob: 3ae9b4687ef99cc044ae6113da0924d65c4f6898 [file] [log] [blame]
Bob Badour9ee7d032021-10-25 16:51:48 -07001// 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"
Bob Badour103eb0f2022-01-10 13:50:57 -080020 "strings"
Bob Badour9ee7d032021-10-25 16:51:48 -070021 "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 {
Colin Cross35f79c32022-01-27 15:18:52 -080032 name: "singleton",
33 roots: []string{"apacheLib.meta_lic"},
34 edges: []annotated{},
Bob Badour9ee7d032021-10-25 16:51:48 -070035 expectedNodes: []string{"apacheLib.meta_lic"},
36 },
37 {
Colin Cross35f79c32022-01-27 15:18:52 -080038 name: "simplebinary",
39 roots: []string{"apacheBin.meta_lic"},
Bob Badour9ee7d032021-10-25 16:51:48 -070040 edges: []annotated{
41 {"apacheBin.meta_lic", "apacheLib.meta_lic", []string{"static"}},
42 },
43 expectedNodes: []string{"apacheBin.meta_lic", "apacheLib.meta_lic"},
44 },
45 {
Colin Cross35f79c32022-01-27 15:18:52 -080046 name: "simpledynamic",
47 roots: []string{"apacheBin.meta_lic"},
Bob Badour9ee7d032021-10-25 16:51:48 -070048 edges: []annotated{
49 {"apacheBin.meta_lic", "lgplLib.meta_lic", []string{"dynamic"}},
50 },
51 expectedNodes: []string{"apacheBin.meta_lic"},
52 },
53 {
Colin Cross35f79c32022-01-27 15:18:52 -080054 name: "container",
55 roots: []string{"apacheContainer.meta_lic"},
Bob Badour9ee7d032021-10-25 16:51:48 -070056 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 {
Colin Cross35f79c32022-01-27 15:18:52 -080067 name: "binary",
68 roots: []string{"apacheBin.meta_lic"},
Bob Badour9ee7d032021-10-25 16:51:48 -070069 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 {
Colin Cross35f79c32022-01-27 15:18:52 -080080 name: "binarydynamic",
81 roots: []string{"apacheBin.meta_lic"},
Bob Badour9ee7d032021-10-25 16:51:48 -070082 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 {
Colin Cross35f79c32022-01-27 15:18:52 -080092 name: "containerdeep",
93 roots: []string{"apacheContainer.meta_lic"},
Bob Badour9ee7d032021-10-25 16:51:48 -070094 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 {
Colin Cross179ec3e2022-01-27 15:47:09 -0800111 t.Errorf("unexpected test data error: got %s, want no error", err)
Bob Badour9ee7d032021-10-25 16:51:48 -0700112 return
113 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800114 t.Logf("graph:")
115 for _, edge := range lg.Edges() {
116 t.Logf(" %s", edge.String())
117 }
Bob Badour9ee7d032021-10-25 16:51:48 -0700118 expectedNodes := append([]string{}, tt.expectedNodes...)
Bob Badour103eb0f2022-01-10 13:50:57 -0800119 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
Bob Badour9ee7d032021-10-25 16:51:48 -0700125 sort.Strings(expectedNodes)
126 sort.Strings(actualNodes)
Bob Badour103eb0f2022-01-10 13:50:57 -0800127
128 t.Logf("sorted nodes: [%s]", strings.Join(actualNodes, ", "))
129 t.Logf("expected nodes: [%s]", strings.Join(expectedNodes, ", "))
Colin Cross35f79c32022-01-27 15:18:52 -0800130 if len(expectedNodes) != len(actualNodes) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800131 t.Errorf("unexpected number of shipped nodes: %d nodes, want %d nodes",
132 len(actualNodes), len(expectedNodes))
Bob Badour9ee7d032021-10-25 16:51:48 -0700133 return
134 }
135 for i := 0; i < len(actualNodes); i++ {
136 if expectedNodes[i] != actualNodes[i] {
Bob Badour103eb0f2022-01-10 13:50:57 -0800137 t.Errorf("unexpected node at index %d: got %q, want %q",
138 i, actualNodes[i], expectedNodes[i])
Bob Badour9ee7d032021-10-25 16:51:48 -0700139 }
140 }
141 })
142 }
143}