github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/load_config_test.go (about)

     1  package steampipeconfig
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  
    11  	utils "github.com/turbot/steampipe/pkg/utils"
    12  	"golang.org/x/exp/maps"
    13  
    14  	"github.com/turbot/steampipe/pkg/filepaths"
    15  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
    16  	"github.com/turbot/steampipe/pkg/steampipeconfig/options"
    17  )
    18  
    19  // TODO KAI add plugin block tests
    20  
    21  type loadConfigTest struct {
    22  	steampipeDir string
    23  	workspaceDir string
    24  	expected     interface{}
    25  }
    26  
    27  var trueVal = true
    28  var ttlVal = 300
    29  
    30  var databasePort = 9193
    31  var databaseListen = "local"
    32  var databaseSearchPath = "aws,gcp,foo"
    33  var databaseQueryTimeout int64 = 240
    34  
    35  var terminalMulti = false
    36  var terminalOutput = "table"
    37  var terminalHeader = true
    38  var terminalSeparator = ","
    39  var terminalTiming = false
    40  var terminalSearchPath = "aws,gcp"
    41  var generalUpdateCheck = "true"
    42  var terminalAutoComplete = true
    43  
    44  var workspaceMulti = true
    45  var workspaceAutoComplete = true
    46  var workspaceOutput = "json"
    47  var workspaceSearchPath = "bar,aws,gcp"
    48  var workspaceSearchPathPrefix = "foobar"
    49  
    50  var testCasesLoadConfig = map[string]loadConfigTest{
    51  	"multiple_connections": {
    52  		steampipeDir: "testdata/connection_config/multiple_connections",
    53  		expected: &SteampipeConfig{
    54  			Connections: map[string]*modconfig.Connection{
    55  				"aws_dmi_001": {
    56  					Name:           "aws_dmi_001",
    57  					PluginAlias:    "aws",
    58  					Plugin:         "hub.steampipe.io/plugins/turbot/aws@latest",
    59  					PluginInstance: utils.ToStringPointer("hub.steampipe.io/plugins/turbot/aws@latest"),
    60  					Type:           "",
    61  					ImportSchema:   "enabled",
    62  					Config:         "access_key = \"aws_dmi_001_access_key\"\nregions    = \"- us-east-1\\n-us-west-\"\nsecret_key = \"aws_dmi_001_secret_key\"\n",
    63  					DeclRange: modconfig.Range{
    64  						Filename: "$$test_pwd$$/testdata/connection_config/multiple_connections/config/connection1.spc",
    65  						Start: modconfig.Pos{
    66  							Line:   1,
    67  							Column: 1,
    68  							Byte:   0,
    69  						},
    70  						End: modconfig.Pos{
    71  							Line:   1,
    72  							Column: 11,
    73  							Byte:   10,
    74  						},
    75  					},
    76  				},
    77  				"aws_dmi_002": {
    78  					Name:           "aws_dmi_002",
    79  					PluginAlias:    "aws",
    80  					Plugin:         "hub.steampipe.io/plugins/turbot/aws@latest",
    81  					PluginInstance: utils.ToStringPointer("hub.steampipe.io/plugins/turbot/aws@latest"),
    82  					Type:           "",
    83  					ImportSchema:   "enabled",
    84  					Config:         "access_key = \"aws_dmi_002_access_key\"\nregions    = \"- us-east-1\\n-us-west-\"\nsecret_key = \"aws_dmi_002_secret_key\"\n",
    85  					DeclRange: modconfig.Range{
    86  						Filename: "$$test_pwd$$/testdata/connection_config/multiple_connections/config/connection2.spc",
    87  						Start: modconfig.Pos{
    88  							Line:   1,
    89  							Column: 1,
    90  							Byte:   0,
    91  						},
    92  						End: modconfig.Pos{
    93  							Line:   1,
    94  							Column: 11,
    95  							Byte:   10,
    96  						},
    97  					},
    98  				},
    99  			},
   100  			DefaultConnectionOptions: &options.Connection{
   101  				Cache:    &trueVal,
   102  				CacheTTL: &ttlVal,
   103  			},
   104  		},
   105  	},
   106  	//"single_connection": {
   107  	//	steampipeDir: "testdata/connection_config/single_connection",
   108  	//	expected: &SteampipeConfig{
   109  	//		Connections: map[string]*modconfig.Connection{
   110  	//			"a": {
   111  	//				Name:           "a",
   112  	//				PluginAlias:    "test_data/connection-test-1",
   113  	//				Plugin:         "hub.steampipe.io/plugins/test_data/connection-test-1@latest",
   114  	//				PluginInstance: utils.ToStringPointer("hub.steampipe.io/plugins/test_data/connection-test-1@latest"),
   115  	//				Type:           "",
   116  	//				ImportSchema:   "enabled",
   117  	//				Config:         "",
   118  	//				DeclRange: modconfig.Range{
   119  	//					Filename: "$$test_pwd$$/testdata/connection_config/single_connection/config/connection1.spc",
   120  	//					Start: modconfig.Pos{
   121  	//						Line:   1,
   122  	//						Column: 1,
   123  	//						Byte:   0,
   124  	//					},
   125  	//					End: modconfig.Pos{
   126  	//						Line:   1,
   127  	//						Column: 11,
   128  	//						Byte:   10,
   129  	//					},
   130  	//				},
   131  	//			},
   132  	//		},
   133  	//		DefaultConnectionOptions: &options.Connection{
   134  	//			Cache:    &trueVal,
   135  	//			CacheTTL: &ttlVal,
   136  	//		},
   137  	//	},
   138  	//},
   139  	//"single_connection_with_default_options": { // fixed
   140  	//	steampipeDir: "testdata/connection_config/single_connection_with_default_options",
   141  	//	expected: &SteampipeConfig{
   142  	//		Connections: map[string]*modconfig.Connection{
   143  	//			"a": {
   144  	//				Name:           "a",
   145  	//				PluginAlias:    "test_data/connection-test-1",
   146  	//				Plugin:         "hub.steampipe.io/plugins/test_data/connection-test-1@latest",
   147  	//				PluginInstance: utils.ToStringPointer("hub.steampipe.io/plugins/test_data/connection-test-1@latest"),
   148  	//				Type:           "",
   149  	//				ImportSchema:   "enabled",
   150  	//				Config:         "",
   151  	//				DeclRange: modconfig.Range{
   152  	//					Filename: "$$test_pwd$$/testdata/connection_config/single_connection_with_default_options/config/connection1.spc",
   153  	//					Start: modconfig.Pos{
   154  	//						Line:   1,
   155  	//						Column: 1,
   156  	//						Byte:   0,
   157  	//					},
   158  	//					End: modconfig.Pos{
   159  	//						Line:   1,
   160  	//						Column: 11,
   161  	//						Byte:   10,
   162  	//					},
   163  	//				},
   164  	//			},
   165  	//		},
   166  	//		DefaultConnectionOptions: &options.Connection{
   167  	//			Cache:    &trueVal,
   168  	//			CacheTTL: &ttlVal,
   169  	//		},
   170  	//		DatabaseOptions: &options.Database{
   171  	//			Port:       &databasePort,
   172  	//			Listen:     &databaseListen,
   173  	//			SearchPath: &databaseSearchPath,
   174  	//		},
   175  	//		GeneralOptions: &options.General{
   176  	//			UpdateCheck: &generalUpdateCheck,
   177  	//		},
   178  	//	},
   179  	//},
   180  	//"single_connection_with_default_options_and_workspace_invalid_options_block": { // fixed
   181  	//	steampipeDir: "testdata/connection_config/single_connection_with_default_options",
   182  	//	workspaceDir: "testdata/load_config_test/invalid_options_block",
   183  	//	expected:     "ERROR",
   184  	//},
   185  	//"single_connection_with_default_options_and_workspace_search_path_prefix": { // fixed
   186  	//	steampipeDir: "testdata/connection_config/single_connection_with_default_options",
   187  	//	workspaceDir: "testdata/load_config_test/search_path_prefix",
   188  	//	expected: &SteampipeConfig{
   189  	//		Connections: map[string]*modconfig.Connection{
   190  	//			"a": {
   191  	//				Name:           "a",
   192  	//				PluginAlias:    "test_data/connection-test-1",
   193  	//				Plugin:         "hub.steampipe.io/plugins/test_data/connection-test-1@latest",
   194  	//				PluginInstance: utils.ToStringPointer("hub.steampipe.io/plugins/test_data/connection-test-1@latest"),
   195  	//				Type:           "",
   196  	//				ImportSchema:   "enabled",
   197  	//				Config:         "",
   198  	//				DeclRange: modconfig.Range{
   199  	//					Filename: "$$test_pwd$$/testdata/connection_config/single_connection_with_default_options/config/connection1.spc",
   200  	//					Start: modconfig.Pos{
   201  	//						Line:   1,
   202  	//						Column: 1,
   203  	//						Byte:   0,
   204  	//					},
   205  	//					End: modconfig.Pos{
   206  	//						Line:   1,
   207  	//						Column: 11,
   208  	//						Byte:   10,
   209  	//					},
   210  	//				},
   211  	//			},
   212  	//		},
   213  	//		DefaultConnectionOptions: &options.Connection{
   214  	//			Cache:    &trueVal,
   215  	//			CacheTTL: &ttlVal,
   216  	//		},
   217  	//		DatabaseOptions: &options.Database{
   218  	//			Port:       &databasePort,
   219  	//			Listen:     &databaseListen,
   220  	//			SearchPath: &databaseSearchPath,
   221  	//		},
   222  	//		GeneralOptions: &options.General{
   223  	//			UpdateCheck: &generalUpdateCheck,
   224  	//		},
   225  	//	},
   226  	//},
   227  	//"single_connection_with_default_options_and_workspace_override_terminal_config": { // fixed
   228  	//	steampipeDir: "testdata/connection_config/single_connection_with_default_options",
   229  	//	workspaceDir: "testdata/load_config_test/override_terminal_config",
   230  	//	expected: &SteampipeConfig{
   231  	//		Connections: map[string]*modconfig.Connection{
   232  	//			"a": {
   233  	//				Name:           "a",
   234  	//				PluginAlias:    "test_data/connection-test-1",
   235  	//				Plugin:         "hub.steampipe.io/plugins/test_data/connection-test-1@latest",
   236  	//				PluginInstance: utils.ToStringPointer("hub.steampipe.io/plugins/test_data/connection-test-1@latest"),
   237  	//				Type:           "",
   238  	//				ImportSchema:   "enabled",
   239  	//				Config:         "",
   240  	//				DeclRange: modconfig.Range{
   241  	//					Filename: "$$test_pwd$$/testdata/connection_config/single_connection_with_default_options/config/connection1.spc",
   242  	//					Start: modconfig.Pos{
   243  	//						Line:   1,
   244  	//						Column: 1,
   245  	//						Byte:   0,
   246  	//					},
   247  	//					End: modconfig.Pos{
   248  	//						Line:   1,
   249  	//						Column: 11,
   250  	//						Byte:   10,
   251  	//					},
   252  	//				},
   253  	//			},
   254  	//		},
   255  	//		DefaultConnectionOptions: &options.Connection{
   256  	//			Cache:    &trueVal,
   257  	//			CacheTTL: &ttlVal,
   258  	//		},
   259  	//		DatabaseOptions: &options.Database{
   260  	//			Port:       &databasePort,
   261  	//			Listen:     &databaseListen,
   262  	//			SearchPath: &databaseSearchPath,
   263  	//		},
   264  	//		GeneralOptions: &options.General{
   265  	//			UpdateCheck: &generalUpdateCheck,
   266  	//		},
   267  	//	},
   268  	//},
   269  	//"single_connection_with_default_and_connection_options": {
   270  	//	steampipeDir: "testdata/connection_config/single_connection_with_default_and_connection_options",
   271  	//	expected: &SteampipeConfig{
   272  	//		Connections: map[string]*modconfig.Connection{
   273  	//			"a": {
   274  	//				Name:           "a",
   275  	//				ImportSchema:   "enabled",
   276  	//				PluginAlias:    "test_data/connection-test-1",
   277  	//				Plugin:         "hub.steampipe.io/plugins/test_data/connection-test-1@latest",
   278  	//				PluginInstance: utils.ToStringPointer("hub.steampipe.io/plugins/test_data/connection-test-1@latest"),
   279  	//				Config:         "",
   280  	//				Options: &options.Connection{
   281  	//					Cache:    &trueVal,
   282  	//					CacheTTL: &ttlVal,
   283  	//				},
   284  	//				DeclRange: modconfig.Range{
   285  	//					Filename: "$$test_pwd$$/testdata/connection_config/single_connection_with_default_and_connection_options/config/connection1.spc",
   286  	//					Start: modconfig.Pos{
   287  	//						Line:   1,
   288  	//						Column: 1,
   289  	//						Byte:   0,
   290  	//					},
   291  	//					End: modconfig.Pos{
   292  	//						Line:   1,
   293  	//						Column: 11,
   294  	//						Byte:   10,
   295  	//					},
   296  	//				},
   297  	//			},
   298  	//		},
   299  	//		DefaultConnectionOptions: &options.Connection{
   300  	//			Cache:    &trueVal,
   301  	//			CacheTTL: &ttlVal,
   302  	//		},
   303  	//		DatabaseOptions: &options.Database{
   304  	//			Port:       &databasePort,
   305  	//			Listen:     &databaseListen,
   306  	//			SearchPath: &databaseSearchPath,
   307  	//		},
   308  	//		GeneralOptions: &options.General{
   309  	//			UpdateCheck: &generalUpdateCheck,
   310  	//		},
   311  	//	},
   312  	//},
   313  	//"options_only": { // fixed
   314  	//	steampipeDir: "testdata/connection_config/options_only",
   315  	//	expected: &SteampipeConfig{
   316  	//		Connections: map[string]*modconfig.Connection{},
   317  	//		DefaultConnectionOptions: &options.Connection{
   318  	//			Cache:    &trueVal,
   319  	//			CacheTTL: &ttlVal,
   320  	//		},
   321  	//		DatabaseOptions: &options.Database{
   322  	//			Port:       &databasePort,
   323  	//			Listen:     &databaseListen,
   324  	//			SearchPath: &databaseSearchPath,
   325  	//		},
   326  	//		GeneralOptions: &options.General{
   327  	//			UpdateCheck: &generalUpdateCheck,
   328  	//		},
   329  	//	},
   330  	//},
   331  	//"options_duplicate_block": {
   332  	//	steampipeDir: "testdata/connection_config/options_duplicate_block",
   333  	//	expected:     "ERROR",
   334  	//},
   335  }
   336  
   337  func TestLoadConfig(t *testing.T) {
   338  	// TODO KAI update these
   339  	t.Skip("needs updating")
   340  	// get the current working directory of the test(used to build the DeclRange.Filename property)
   341  	pwd, err := os.Getwd()
   342  	if err != nil {
   343  		t.Errorf("failed to get current working directory")
   344  	}
   345  
   346  	for name, test := range testCasesLoadConfig {
   347  		// default workspoace to empty dir
   348  		workspaceDir := test.workspaceDir
   349  		if workspaceDir == "" {
   350  			workspaceDir = "testdata/load_config_test/empty"
   351  		}
   352  		steampipeDir, err := filepath.Abs(test.steampipeDir)
   353  		if err != nil {
   354  			t.Errorf("failed to build absolute config filepath from %s", test.steampipeDir)
   355  		}
   356  
   357  		workspaceDir, err = filepath.Abs(workspaceDir)
   358  		if err != nil {
   359  			t.Errorf("failed to build absolute config filepath from %s", workspaceDir)
   360  		}
   361  
   362  		// set SteampipeDir
   363  		filepaths.SteampipeDir = steampipeDir
   364  
   365  		// now load config
   366  		config, errorsAndWarnings := loadSteampipeConfig(context.TODO(), workspaceDir, "")
   367  		if errorsAndWarnings.GetError() != nil {
   368  			if test.expected != "ERROR" {
   369  				t.Errorf("Test: '%s'' FAILED with unexpected error: %v", name, errorsAndWarnings.GetError())
   370  			}
   371  			continue
   372  		}
   373  
   374  		if test.expected == "ERROR" {
   375  			t.Errorf("Test: '%s'' FAILED - expected error", name)
   376  			continue
   377  		}
   378  
   379  		expectedConfig := test.expected.(*SteampipeConfig)
   380  		for _, c := range expectedConfig.Connections {
   381  			c.DeclRange.Filename = strings.Replace(c.DeclRange.Filename, "$$test_pwd$$", pwd, 1)
   382  		}
   383  		if !SteampipeConfigEquals(config, expectedConfig) {
   384  			t.Errorf("Test: '%s'' FAILED : expected:\n%s\n\ngot:\n%s", name, expectedConfig, config)
   385  		}
   386  	}
   387  }
   388  
   389  // helpers
   390  func SteampipeConfigEquals(left, right *SteampipeConfig) bool {
   391  	if left == nil || right == nil {
   392  		return left == nil && right == nil
   393  	}
   394  
   395  	if !maps.EqualFunc(left.Connections, right.Connections,
   396  		func(c1, c2 *modconfig.Connection) bool { return c1.Equals(c2) }) {
   397  		return false
   398  	}
   399  	if !reflect.DeepEqual(left.DefaultConnectionOptions, right.DefaultConnectionOptions) {
   400  		return false
   401  	}
   402  	if !reflect.DeepEqual(left.DatabaseOptions, right.DatabaseOptions) {
   403  		return false
   404  	}
   405  	if !reflect.DeepEqual(left.TerminalOptions, right.TerminalOptions) {
   406  		return false
   407  	}
   408  	if !reflect.DeepEqual(left.GeneralOptions, right.GeneralOptions) {
   409  		return false
   410  	}
   411  	return true
   412  }