github.com/seilagamo/poc-lava-release@v0.3.3-rc3/internal/config/config_test.go (about)

     1  // Copyright 2023 Adevinta
     2  
     3  package config
     4  
     5  import (
     6  	"errors"
     7  	"io"
     8  	"log/slog"
     9  	"regexp"
    10  	"testing"
    11  
    12  	agentconfig "github.com/adevinta/vulcan-agent/config"
    13  	types "github.com/adevinta/vulcan-types"
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestParse(t *testing.T) {
    18  	tests := []struct {
    19  		name          string
    20  		file          string
    21  		want          Config
    22  		wantErr       error
    23  		wantErrRegexp *regexp.Regexp
    24  	}{
    25  		{
    26  			name: "valid",
    27  			file: "testdata/valid.yaml",
    28  			want: Config{
    29  				LavaVersion: "v1.0.0",
    30  				ChecktypeURLs: []string{
    31  					"checktypes.json",
    32  				},
    33  				Targets: []Target{
    34  					{
    35  						Identifier: "example.com",
    36  						AssetType:  types.DomainName,
    37  					},
    38  				},
    39  			},
    40  		},
    41  		{
    42  			name:    "empty",
    43  			file:    "testdata/empty.yaml",
    44  			want:    Config{},
    45  			wantErr: io.EOF,
    46  		},
    47  		{
    48  			name:    "invalid lava version",
    49  			file:    "testdata/invalid_lava_version.yaml",
    50  			want:    Config{},
    51  			wantErr: ErrInvalidLavaVersion,
    52  		},
    53  		{
    54  			name:    "no checktypes URLs",
    55  			file:    "testdata/no_checktypes_urls.yaml",
    56  			want:    Config{},
    57  			wantErr: ErrNoChecktypeURLs,
    58  		},
    59  		{
    60  			name:    "no targets",
    61  			file:    "testdata/no_targets.yaml",
    62  			want:    Config{},
    63  			wantErr: ErrNoTargets,
    64  		},
    65  		{
    66  			name:    "no target identifier",
    67  			file:    "testdata/no_target_identifier.yaml",
    68  			want:    Config{},
    69  			wantErr: ErrNoTargetIdentifier,
    70  		},
    71  		{
    72  			name:    "no target asset type",
    73  			file:    "testdata/no_target_asset_type.yaml",
    74  			want:    Config{},
    75  			wantErr: ErrNoTargetAssetType,
    76  		},
    77  		{
    78  			name: "critical severity",
    79  			file: "testdata/critical_severity.yaml",
    80  			want: Config{
    81  				LavaVersion: "v1.0.0",
    82  				ChecktypeURLs: []string{
    83  					"checktypes.json",
    84  				},
    85  				ReportConfig: ReportConfig{
    86  					Severity: SeverityCritical,
    87  				},
    88  				Targets: []Target{
    89  					{
    90  						Identifier: "example.com",
    91  						AssetType:  types.DomainName,
    92  					},
    93  				},
    94  			},
    95  		},
    96  		{
    97  			name:    "invalid severity",
    98  			file:    "testdata/invalid_severity.yaml",
    99  			want:    Config{},
   100  			wantErr: ErrInvalidSeverity,
   101  		},
   102  		{
   103  			name: "never pull policy",
   104  			file: "testdata/never_pull_policy.yaml",
   105  			want: Config{
   106  				LavaVersion: "v1.0.0",
   107  				ChecktypeURLs: []string{
   108  					"checktypes.json",
   109  				},
   110  				AgentConfig: AgentConfig{
   111  					PullPolicy: agentconfig.PullPolicyNever,
   112  				},
   113  				Targets: []Target{
   114  					{
   115  						Identifier: "example.com",
   116  						AssetType:  types.DomainName,
   117  					},
   118  				},
   119  			},
   120  		},
   121  		{
   122  			name:          "invalid pull policy",
   123  			file:          "testdata/invalid_pull_policy.yaml",
   124  			want:          Config{},
   125  			wantErrRegexp: regexp.MustCompile(`value .* is not a valid PullPolicy value`),
   126  		},
   127  		{
   128  			name:    "invalid target asset type",
   129  			file:    "testdata/invalid_target_asset_type.yaml",
   130  			want:    Config{},
   131  			wantErr: ErrInvalidAssetType,
   132  		},
   133  		{
   134  			name: "JSON output format",
   135  			file: "testdata/json_output_format.yaml",
   136  			want: Config{
   137  				LavaVersion: "v1.0.0",
   138  				ChecktypeURLs: []string{
   139  					"checktypes.json",
   140  				},
   141  				Targets: []Target{
   142  					{
   143  						Identifier: "example.com",
   144  						AssetType:  types.DomainName,
   145  					},
   146  				},
   147  				ReportConfig: ReportConfig{
   148  					Format: OutputFormatJSON,
   149  				},
   150  			},
   151  		},
   152  		{
   153  			name:    "invalid output format",
   154  			file:    "testdata/invalid_output_format.yaml",
   155  			want:    Config{},
   156  			wantErr: ErrInvalidOutputFormat,
   157  		},
   158  		{
   159  			name: "debug log level",
   160  			file: "testdata/debug_log_level.yaml",
   161  			want: Config{
   162  				LavaVersion: "v1.0.0",
   163  				ChecktypeURLs: []string{
   164  					"checktypes.json",
   165  				},
   166  				Targets: []Target{
   167  					{
   168  						Identifier: "example.com",
   169  						AssetType:  types.DomainName,
   170  					},
   171  				},
   172  				LogLevel: slog.LevelDebug,
   173  			},
   174  		},
   175  		{
   176  			name:          "invalid log level",
   177  			file:          "testdata/invalid_log_level.yaml",
   178  			want:          Config{},
   179  			wantErrRegexp: regexp.MustCompile(`level string ".*": unknown name`),
   180  		},
   181  	}
   182  
   183  	for _, tt := range tests {
   184  		t.Run(tt.name, func(t *testing.T) {
   185  			got, err := ParseFile(tt.file)
   186  
   187  			switch {
   188  			case tt.wantErr != nil:
   189  				if !errors.Is(err, tt.wantErr) {
   190  					t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErr)
   191  				}
   192  			case tt.wantErrRegexp != nil:
   193  				if err == nil {
   194  					t.Errorf("unexpected nil error: want: %v", tt.wantErrRegexp)
   195  				} else if !tt.wantErrRegexp.MatchString(err.Error()) {
   196  					t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErrRegexp)
   197  				}
   198  			default:
   199  				if err != nil {
   200  					t.Errorf("unexpected error: %v", err)
   201  				}
   202  			}
   203  
   204  			if diff := cmp.Diff(tt.want, got); diff != "" {
   205  				t.Errorf("configs mismatch (-want +got):\n%v", diff)
   206  			}
   207  		})
   208  	}
   209  }
   210  
   211  func TestSeverity_MarshalText(t *testing.T) {
   212  	tests := []struct {
   213  		name     string
   214  		severity Severity
   215  		want     string
   216  		wantErr  error
   217  	}{
   218  		{
   219  			name:     "critical",
   220  			severity: SeverityCritical,
   221  			want:     "critical",
   222  			wantErr:  nil,
   223  		},
   224  		{
   225  			name:     "high",
   226  			severity: SeverityHigh,
   227  			want:     "high",
   228  			wantErr:  nil,
   229  		},
   230  		{
   231  			name:     "medium",
   232  			severity: SeverityMedium,
   233  			want:     "medium",
   234  			wantErr:  nil,
   235  		},
   236  		{
   237  			name:     "low",
   238  			severity: SeverityLow,
   239  			want:     "low",
   240  			wantErr:  nil,
   241  		},
   242  		{
   243  			name:     "info",
   244  			severity: SeverityInfo,
   245  			want:     "info",
   246  			wantErr:  nil,
   247  		},
   248  		{
   249  			name:     "invalid",
   250  			severity: 7,
   251  			want:     "",
   252  			wantErr:  ErrInvalidSeverity,
   253  		},
   254  	}
   255  	for _, tt := range tests {
   256  		t.Run(tt.name, func(t *testing.T) {
   257  			got, err := tt.severity.MarshalText()
   258  			if !errors.Is(err, tt.wantErr) {
   259  				t.Errorf("unexpected error: want: %v, got: %v", tt.wantErr, err)
   260  			}
   261  			if string(got) != tt.want {
   262  				t.Errorf("unexpected severity string: want: %v, got: %v", tt.want, got)
   263  			}
   264  		})
   265  	}
   266  }