go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/cli/config/config_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package config
     5  
     6  import (
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	homedir "github.com/mitchellh/go-homedir"
    12  	"github.com/spf13/afero"
    13  	"github.com/spf13/viper"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  var (
    19  	home          = getHomeDir()
    20  	homeConfigDir = filepath.Join(home, ".config", "mondoo")
    21  	homeConfig    = filepath.Join(homeConfigDir, DefaultConfigFile)
    22  
    23  	systemConfigDir = filepath.Join("/etc", "opt", "mondoo")
    24  	systemConfig    = filepath.Join(systemConfigDir, DefaultConfigFile)
    25  	systemInventory = filepath.Join(systemConfigDir, "inventory.yml")
    26  
    27  	oldConfig = filepath.Join(home, "."+DefaultConfigFile)
    28  
    29  	configBody = []byte("theconfig")
    30  )
    31  
    32  func getHomeDir() string {
    33  	home, _ := homedir.Dir()
    34  	return home
    35  }
    36  
    37  func resetAppFsToMemFs() {
    38  	AppFs = afero.NewMemMapFs()
    39  	AppFs.MkdirAll(homeConfigDir, 0o755)
    40  	AppFs.MkdirAll(systemConfigDir, 0o755)
    41  }
    42  
    43  func Test_autodetectConfig(t *testing.T) {
    44  	defer func() {
    45  		AppFs = afero.NewOsFs()
    46  	}()
    47  
    48  	t.Run("test homeConfig returned if exists", func(t *testing.T) {
    49  		resetAppFsToMemFs()
    50  		afero.WriteFile(AppFs, homeConfig, configBody, 0o644)
    51  
    52  		config := autodetectConfig()
    53  		assert.Equal(t, homeConfig, config)
    54  	})
    55  
    56  	t.Run("test homeConfig returned even if systemConfig exists", func(t *testing.T) {
    57  		resetAppFsToMemFs()
    58  		afero.WriteFile(AppFs, homeConfig, configBody, 0o644)
    59  		afero.WriteFile(AppFs, oldConfig, configBody, 0o644)
    60  		afero.WriteFile(AppFs, systemConfig, configBody, 0o644)
    61  
    62  		config := autodetectConfig()
    63  		assert.Equal(t, homeConfig, config)
    64  	})
    65  
    66  	t.Run("test systemConfig returned", func(t *testing.T) {
    67  		resetAppFsToMemFs()
    68  		afero.WriteFile(AppFs, systemConfig, configBody, 0o644)
    69  
    70  		config := autodetectConfig()
    71  		assert.Equal(t, systemConfig, config)
    72  	})
    73  }
    74  
    75  func Test_probeConfigMemFs(t *testing.T) {
    76  	defer func() {
    77  		AppFs = afero.NewOsFs()
    78  	}()
    79  
    80  	resetAppFsToMemFs()
    81  	afero.WriteFile(AppFs, homeConfig, configBody, 0o644)
    82  
    83  	assert.False(t, ProbeFile(homeConfigDir))
    84  	assert.True(t, ProbeDir(homeConfigDir))
    85  	assert.True(t, ProbeFile(homeConfig))
    86  	assert.False(t, ProbeFile(homeConfig+".nothere"))
    87  }
    88  
    89  func Test_probeConfigOsFs(t *testing.T) {
    90  	dir := t.TempDir()
    91  	tmpConfig := filepath.Join(dir, DefaultConfigFile)
    92  	afero.WriteFile(AppFs, tmpConfig, configBody, 0o000)
    93  
    94  	assert.Equal(t, false, ProbeFile(tmpConfig))
    95  }
    96  
    97  func Test_inventoryPath(t *testing.T) {
    98  	resetAppFsToMemFs()
    99  	afero.WriteFile(AppFs, systemConfig, configBody, 0o644)
   100  	afero.WriteFile(AppFs, systemInventory, []byte("---"), 0o644)
   101  
   102  	path, ok := InventoryPath(systemConfig)
   103  	assert.Equal(t, systemInventory, path)
   104  	assert.True(t, ok)
   105  }
   106  
   107  func TestConfigParsing(t *testing.T) {
   108  	data := `
   109  agent_mrn: //agents.api.mondoo.app/spaces/musing-saha-952142/agents/1zDY7auR20SgrFfiGUT5qZWx6mE
   110  api_endpoint: https://us.api.mondoo.com
   111  api_proxy: http://192.168.4.4:3128
   112  certificate: |
   113    -----BEGIN CERTIFICATE-----
   114    MIICV .. fis=
   115    -----END CERTIFICATE-----
   116  
   117  mrn: //agents.api.mondoo.app/spaces/musing-saha-952142/serviceaccounts/1zDY7cJ7bA84JxxNBWDxBdui2xE
   118  private_key: |
   119    -----BEGIN PRIVATE KEY-----
   120    MIG2AgE....C0Dvs=
   121    -----END PRIVATE KEY-----
   122  space_mrn: //captain.api.mondoo.app/spaces/musing-saha-952142
   123  `
   124  
   125  	viper.SetConfigType("yaml")
   126  	viper.ReadConfig(strings.NewReader(data))
   127  
   128  	cfg, err := Read()
   129  	require.NoError(t, err)
   130  	assert.Equal(t, "//agents.api.mondoo.app/spaces/musing-saha-952142/agents/1zDY7auR20SgrFfiGUT5qZWx6mE", cfg.AgentMrn)
   131  	assert.Equal(t, "//agents.api.mondoo.app/spaces/musing-saha-952142/serviceaccounts/1zDY7cJ7bA84JxxNBWDxBdui2xE", cfg.ServiceAccountMrn)
   132  	assert.Equal(t, "-----BEGIN PRIVATE KEY-----\nMIG2AgE....C0Dvs=\n-----END PRIVATE KEY-----\n", cfg.PrivateKey)
   133  	assert.Equal(t, "-----BEGIN CERTIFICATE-----\nMIICV .. fis=\n-----END CERTIFICATE-----\n", cfg.Certificate)
   134  	assert.Equal(t, "http://192.168.4.4:3128", cfg.APIProxy)
   135  }