github.com/adevinta/lava@v0.7.2/internal/checktypes/checktypes_test.go (about)

     1  // Copyright 2023 Adevinta
     2  
     3  package checktypes
     4  
     5  import (
     6  	"errors"
     7  	"os"
     8  	"testing"
     9  
    10  	checkcatalog "github.com/adevinta/vulcan-check-catalog/pkg/model"
    11  	types "github.com/adevinta/vulcan-types"
    12  	"github.com/google/go-cmp/cmp"
    13  )
    14  
    15  func TestAccepts(t *testing.T) {
    16  	tests := []struct {
    17  		name      string
    18  		assetType types.AssetType
    19  		checktype checkcatalog.Checktype
    20  		want      bool
    21  	}{
    22  		{
    23  			name:      "accepted asset type",
    24  			assetType: types.Hostname,
    25  			checktype: checkcatalog.Checktype{
    26  				Name:        "vulcan-drupal",
    27  				Description: "Checks for some vulnerable versions of Drupal.",
    28  				Image:       "vulcansec/vulcan-drupal:edge",
    29  				Assets: []string{
    30  					"Hostname",
    31  				},
    32  			},
    33  			want: true,
    34  		},
    35  		{
    36  			name:      "not accepted asset type",
    37  			assetType: types.DomainName,
    38  			checktype: checkcatalog.Checktype{
    39  				Name:        "vulcan-drupal",
    40  				Description: "Checks for some vulnerable versions of Drupal.",
    41  				Image:       "vulcansec/vulcan-drupal:edge",
    42  				Assets: []string{
    43  					"Hostname",
    44  				},
    45  			},
    46  			want: false,
    47  		},
    48  	}
    49  
    50  	for _, tt := range tests {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			got := Accepts(tt.checktype, tt.assetType)
    53  			if got != tt.want {
    54  				t.Errorf("unexpected return value: want: %v, got: %v", got, tt.want)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestNewCatalog(t *testing.T) {
    61  	tests := []struct {
    62  		name    string
    63  		urls    []string
    64  		want    Catalog
    65  		wantErr error
    66  	}{
    67  		{
    68  			name: "valid file",
    69  			urls: []string{
    70  				"testdata/checktype_catalog.json",
    71  			},
    72  			want: Catalog{
    73  				"vulcan-drupal": {
    74  					Name:        "vulcan-drupal",
    75  					Description: "Checks for some vulnerable versions of Drupal.",
    76  					Image:       "vulcansec/vulcan-drupal:edge",
    77  					Assets: []string{
    78  						"Hostname",
    79  					},
    80  					RequiredVars: []any{
    81  						"REQUIRED_VAR_1",
    82  					},
    83  				},
    84  			},
    85  			wantErr: nil,
    86  		},
    87  		{
    88  			name: "checktype catalog override",
    89  			urls: []string{
    90  				"testdata/checktype_catalog.json",
    91  				"testdata/checktype_catalog_override.json",
    92  			},
    93  			want: Catalog{
    94  				"vulcan-drupal": {
    95  					Name:        "vulcan-drupal",
    96  					Description: "Checks for some vulnerable versions of Drupal (overridden).",
    97  					Image:       "vulcansec/vulcan-drupal:overridden",
    98  					Assets: []string{
    99  						"Hostname",
   100  					},
   101  				},
   102  			},
   103  			wantErr: nil,
   104  		},
   105  		{
   106  			name: "wrong file",
   107  			urls: []string{
   108  				"testdata/not_exists",
   109  			},
   110  			want:    nil,
   111  			wantErr: os.ErrNotExist,
   112  		},
   113  		{
   114  			name: "invalid file",
   115  			urls: []string{
   116  				"testdata/invalid_checktype_catalog.json",
   117  			},
   118  			want:    nil,
   119  			wantErr: ErrMalformedCatalog,
   120  		},
   121  	}
   122  
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			got, err := NewCatalog(tt.urls)
   126  
   127  			if !errors.Is(err, tt.wantErr) {
   128  				t.Fatalf("unexpected error: want: %v, got: %v", tt.wantErr, err)
   129  			}
   130  
   131  			if diff := cmp.Diff(tt.want, got); diff != "" {
   132  				t.Errorf("checktypes mismatch (-want +got):\n%v", diff)
   133  			}
   134  		})
   135  	}
   136  }