Skip to content

Commit

Permalink
fix: TestToTLS to use unreferenced config attributes (#1952)
Browse files Browse the repository at this point in the history
* Fix test to use unreferenced config attributes

* Adjust caCertPool solution
  • Loading branch information
mrsillydog authored Nov 8, 2024
1 parent 9ac8eff commit 5ba3fa4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
6 changes: 4 additions & 2 deletions opamp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ type TLSConfig struct {
}

// ToTLS converts the config to a tls.Config
func (c Config) ToTLS() (*tls.Config, error) {
func (c Config) ToTLS(caCertPool *x509.CertPool) (*tls.Config, error) {
if c.TLS == nil {
return nil, nil
}
Expand All @@ -232,7 +232,9 @@ func (c Config) ToTLS() (*tls.Config, error) {
if err != nil {
return nil, fmt.Errorf("failed to read CA file: %w", err)
}
caCertPool := x509.NewCertPool()
if caCertPool == nil {
caCertPool = x509.NewCertPool()
}
caCertPool.AppendCertsFromPEM(caCert)

tlsConfig.RootCAs = caCertPool
Expand Down
19 changes: 10 additions & 9 deletions opamp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestToTLS(t *testing.T) {
TLS: nil,
}

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(nil)
assert.NoError(t, err)
assert.Nil(t, actual)
},
Expand All @@ -77,7 +77,7 @@ func TestToTLS(t *testing.T) {
MinVersion: tls.VersionTLS12,
}

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(nil)
assert.NoError(t, err)
assert.Equal(t, &expectedConfig, actual)
},
Expand All @@ -95,7 +95,7 @@ func TestToTLS(t *testing.T) {
MinVersion: tls.VersionTLS12,
}

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(nil)
assert.NoError(t, err)
assert.Equal(t, &expectedConfig, actual)
},
Expand All @@ -110,7 +110,7 @@ func TestToTLS(t *testing.T) {
},
}

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(nil)
assert.ErrorContains(t, err, "failed to read CA file")
assert.Nil(t, actual)
},
Expand All @@ -125,7 +125,7 @@ func TestToTLS(t *testing.T) {
},
}

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(nil)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.False(t, actual.InsecureSkipVerify)
Expand All @@ -147,7 +147,7 @@ func TestToTLS(t *testing.T) {
_, err := tls.LoadX509KeyPair(invalidCertFile, invalidKeyFile)
errinvalidKeyorCertFile := fmt.Sprintf("failed to read Key and Cert file: %s", err)

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(nil)
assert.ErrorContains(t, err, errinvalidKeyorCertFile)
assert.Nil(t, actual)
},
Expand All @@ -171,7 +171,7 @@ func TestToTLS(t *testing.T) {
require.NoError(t, err)
expectedConfig.Certificates = []tls.Certificate{cert}

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(nil)
assert.NoError(t, err)
assert.Equal(t, &expectedConfig, actual)
},
Expand Down Expand Up @@ -202,9 +202,10 @@ func TestToTLS(t *testing.T) {
require.NoError(t, err)
expectedConfig.Certificates = []tls.Certificate{cert}

actual, err := cfg.ToTLS()
actual, err := cfg.ToTLS(caCertPool)
assert.NoError(t, err)
assert.Equal(t, expectedConfig.Certificates, actual.Certificates)

assert.Equal(t, &expectedConfig, actual)
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions opamp/observiq/observiq_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func NewClient(args *NewClientArgs) (opamp.Client, error) {
}

// Propagate TLS config to reportManager agent
tlsCfg, err := args.Config.ToTLS()
tlsCfg, err := args.Config.ToTLS(nil)
if err != nil {
return nil, fmt.Errorf("failed creating TLS config: %w", err)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func (c *Client) Connect(ctx context.Context) error {
return err
}

tlsCfg, err := c.currentConfig.ToTLS()
tlsCfg, err := c.currentConfig.ToTLS(nil)
if err != nil {
// Set package status file for error (for Updater to pick up), but do not force send to Server
c.tryToFailPackageInstall(fmt.Sprintf("Failed creating TLS config: %s", err.Error()), false)
Expand Down

0 comments on commit 5ba3fa4

Please sign in to comment.