github.com/google/osv-scalibr@v0.4.1/enricher/enricherlist/list_test.go (about)

     1  // Copyright 2025 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 enricherlist_test
    16  
    17  import (
    18  	"regexp"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  	cpb "github.com/google/osv-scalibr/binary/proto/config_go_proto"
    24  	"github.com/google/osv-scalibr/enricher/baseimage"
    25  	el "github.com/google/osv-scalibr/enricher/enricherlist"
    26  )
    27  
    28  var (
    29  	reValidName = regexp.MustCompile(`^[a-z0-9/-]+$`)
    30  )
    31  
    32  func TestPluginNamesValid(t *testing.T) {
    33  	for _, initers := range el.All {
    34  		for _, initer := range initers {
    35  			name := initer(&cpb.PluginConfig{}).Name()
    36  			if !reValidName.MatchString(name) {
    37  				t.Errorf("Invalid plugin name %q", name)
    38  			}
    39  		}
    40  	}
    41  }
    42  
    43  func TestEnrichersFromName(t *testing.T) {
    44  	testCases := []struct {
    45  		desc    string
    46  		name    string
    47  		want    []string
    48  		wantErr error
    49  	}{
    50  		{
    51  			desc: "Find_all_extractors_of_a_type",
    52  			name: "layerdetails",
    53  			want: []string{baseimage.Name},
    54  		},
    55  		{
    56  			desc:    "Nonexistent plugin",
    57  			name:    "nonexistent",
    58  			wantErr: cmpopts.AnyError,
    59  		},
    60  	}
    61  
    62  	for _, tc := range testCases {
    63  		t.Run(tc.desc, func(t *testing.T) {
    64  			got, err := el.EnrichersFromName(tc.name, &cpb.PluginConfig{})
    65  			if diff := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); diff != "" {
    66  				t.Errorf("el.EnrichersFromName(%v) error got diff (-want +got):\n%s", tc.name, diff)
    67  			}
    68  			var gotNames []string
    69  			for _, e := range got {
    70  				gotNames = append(gotNames, e.Name())
    71  			}
    72  			sort := func(e1, e2 string) bool { return e1 < e2 }
    73  			if diff := cmp.Diff(tc.want, gotNames, cmpopts.SortSlices(sort)); diff != "" {
    74  				t.Errorf("el.EnrichersFromName(%v): got diff (-want +got):\n%s", tc.name, diff)
    75  			}
    76  		})
    77  	}
    78  }