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