github.com/anchore/syft@v1.38.2/syft/get_source_test.go (about)

     1  package syft
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/anchore/stereoscope/pkg/image"
     9  	"github.com/anchore/syft/internal/sourcemetadata"
    10  	"github.com/anchore/syft/syft/source"
    11  	"github.com/anchore/syft/syft/source/sourceproviders"
    12  )
    13  
    14  type mockSource struct {
    15  	source.Source
    16  	desc source.Description
    17  }
    18  
    19  func (s mockSource) Describe() source.Description {
    20  	return s.desc
    21  }
    22  
    23  func TestValidateSourcePlatform_NilSource(t *testing.T) {
    24  	cfg := &GetSourceConfig{
    25  		SourceProviderConfig: &sourceproviders.Config{
    26  			Platform: &image.Platform{
    27  				Architecture: "amd64",
    28  				OS:           "linux",
    29  			},
    30  		},
    31  	}
    32  
    33  	err := validateSourcePlatform(nil, cfg)
    34  	if err != nil {
    35  		t.Errorf("Expected no error for nil source, got: %v", err)
    36  	}
    37  }
    38  
    39  func TestValidateSourcePlatform_NilPlatformConfig(t *testing.T) {
    40  	tests := []struct {
    41  		name string
    42  		cfg  *GetSourceConfig
    43  	}{
    44  		{
    45  			name: "nil config",
    46  			cfg:  nil,
    47  		},
    48  		{
    49  			name: "nil SourceProviderConfig",
    50  			cfg: &GetSourceConfig{
    51  				SourceProviderConfig: nil,
    52  			},
    53  		},
    54  		{
    55  			name: "nil Platform",
    56  			cfg: &GetSourceConfig{
    57  				SourceProviderConfig: &sourceproviders.Config{
    58  					Platform: nil,
    59  				},
    60  			},
    61  		},
    62  	}
    63  
    64  	for _, tt := range tests {
    65  		t.Run(tt.name, func(t *testing.T) {
    66  			src := mockSource{
    67  				desc: source.Description{
    68  					Metadata: &source.ImageMetadata{},
    69  				},
    70  			}
    71  
    72  			err := validateSourcePlatform(src, tt.cfg)
    73  			if err != nil {
    74  				t.Errorf("Expected no error for nil platform, got: %v", err)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestValidateSourcePlatform_SupportedMetadataTypes(t *testing.T) {
    81  	tracker := sourcemetadata.NewCompletionTester(t)
    82  	cfg := &GetSourceConfig{
    83  		SourceProviderConfig: &sourceproviders.Config{
    84  			Platform: &image.Platform{
    85  				Architecture: "amd64",
    86  				OS:           "linux",
    87  			},
    88  		},
    89  	}
    90  
    91  	tests := []struct {
    92  		name     string
    93  		metadata any
    94  		wantErr  require.ErrorAssertionFunc
    95  	}{
    96  		{
    97  			name:     "image",
    98  			metadata: source.ImageMetadata{},
    99  		},
   100  		{
   101  			name:     "snap",
   102  			metadata: source.SnapMetadata{},
   103  		},
   104  		{
   105  			name:     "dir",
   106  			metadata: source.DirectoryMetadata{},
   107  			wantErr:  require.Error,
   108  		},
   109  		{
   110  			name:     "file",
   111  			metadata: source.FileMetadata{},
   112  			wantErr:  require.Error,
   113  		},
   114  	}
   115  
   116  	for _, tt := range tests {
   117  		t.Run(tt.name, func(t *testing.T) {
   118  			if tt.wantErr == nil {
   119  				tt.wantErr = require.NoError
   120  			}
   121  			tracker.Tested(t, tt.metadata)
   122  
   123  			src := mockSource{
   124  				desc: source.Description{
   125  					Metadata: tt.metadata,
   126  				},
   127  			}
   128  
   129  			err := validateSourcePlatform(src, cfg)
   130  			tt.wantErr(t, err, "Expected no error for %s, got: %v", tt.name, err)
   131  		})
   132  	}
   133  }
   134  
   135  func TestValidateSourcePlatform_UnsupportedMetadataTypes(t *testing.T) {
   136  	cfg := &GetSourceConfig{
   137  		SourceProviderConfig: &sourceproviders.Config{
   138  			Platform: &image.Platform{
   139  				Architecture: "amd64",
   140  				OS:           "linux",
   141  			},
   142  		},
   143  	}
   144  
   145  	tests := []struct {
   146  		name     string
   147  		metadata interface{}
   148  	}{
   149  		{
   150  			name:     "string metadata",
   151  			metadata: "unsupported",
   152  		},
   153  		{
   154  			name:     "int metadata",
   155  			metadata: 42,
   156  		},
   157  		{
   158  			name:     "nil metadata",
   159  			metadata: nil,
   160  		},
   161  		{
   162  			name:     "custom struct",
   163  			metadata: struct{ Name string }{Name: "test"},
   164  		},
   165  	}
   166  
   167  	for _, tt := range tests {
   168  		t.Run(tt.name, func(t *testing.T) {
   169  			src := mockSource{
   170  				desc: source.Description{
   171  					Metadata: tt.metadata,
   172  				},
   173  			}
   174  
   175  			err := validateSourcePlatform(src, cfg)
   176  			if err == nil {
   177  				t.Errorf("Expected error for %s, got nil", tt.name)
   178  			}
   179  
   180  			expectedMsg := "platform is not supported for this source type"
   181  			if err.Error() != expectedMsg {
   182  				t.Errorf("Expected error message %q, got %q", expectedMsg, err.Error())
   183  			}
   184  		})
   185  	}
   186  }
   187  
   188  func TestValidateSourcePlatform_ValidCombination(t *testing.T) {
   189  	cfg := &GetSourceConfig{
   190  		SourceProviderConfig: &sourceproviders.Config{
   191  			Platform: &image.Platform{
   192  				Architecture: "amd64",
   193  				OS:           "linux",
   194  			},
   195  		},
   196  	}
   197  
   198  	src := mockSource{
   199  		desc: source.Description{
   200  			Metadata: &source.ImageMetadata{},
   201  		},
   202  	}
   203  
   204  	err := validateSourcePlatform(src, cfg)
   205  	if err != nil {
   206  		t.Errorf("Expected no error for valid combination, got: %v", err)
   207  	}
   208  }