Skip to content

Coverage Increase: token/services/config Package#1529

Merged
adecaro merged 2 commits into
hyperledger-labs:mainfrom
Rozerxshashank:fix/issue-1484-coverage-config-v2
Apr 29, 2026
Merged

Coverage Increase: token/services/config Package#1529
adecaro merged 2 commits into
hyperledger-labs:mainfrom
Rozerxshashank:fix/issue-1484-coverage-config-v2

Conversation

@Rozerxshashank
Copy link
Copy Markdown
Contributor

Description

This PR significantly improves the unit test coverage for the token/services/config package, moving the baseline from 60% to 88.5%. To achieve this, I've introduced a robust mocking infrastructure using counterfeiter and refactored the configuration service to be more testable while maintaining a clean public API.

Key Changes

Mocking Infrastructure

  • Introduced counterfeiter to generate mocks for core interfaces: Provider, ValidateConfiguration, and Validator.
  • Centralized all generated mocks in a new token/services/config/mocks directory to align with project best practices.

Architectural Improvements

  • Decoupled Logic: Refactored AddConfiguration to use an internal addConfiguration(Provider, []byte) method. This allows us to inject mocked providers and verify the merge/validation logic without relying on concrete file-system or cryptographic implementations.
  • Testing Accessors: Added export_test.go to safely expose unexported service fields and methods to the config_test package, ensuring our unit tests stay isolated from internal implementation details while still being comprehensive.

Enhanced Test Suite

  • service_unit_test.go: Implemented exhaustive tests for:
    • LookupNamespace (covering success, not-found, and collision scenarios).
    • ConfigurationFor error handling.
    • AddConfiguration validation and existing TMS ID conflict detection.
  • configuration_unit_test.go: Added targeted tests for the Configuration struct, including its validation logic, serialization paths, and path translation wrappers.

Impact

  • Test Coverage: Increased from ~60% to 88.5% statement coverage.
  • Maintainability: The new mocking pattern makes it significantly easier to test future configuration-related features in isolation.

Verification

  • Ran the full test suite with coverage reporting: go test -cover -timeout 30s ./token/services/config/...
  • Verified code quality with golangci-lint: 0 issues found.

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1484-coverage-config-v2 branch 3 times, most recently from b528e0b to 9fbd0ff Compare April 11, 2026 13:24
Comment thread token/core/tms.go Outdated
if ppRaw, err := retriever(opts); err != nil {
logger.Warnf("failed to retrieve params for [%s]: [%s]", opts, err)
} else if len(ppRaw) != 0 {
for i, retriever := range []func(options *driver.ServiceOptions) ([]byte, error){m.ppFromOpts, m.ppFromStorage, m.ppFromConfig, m.ppFromFetcher} {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unrelated to the scope of the PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were originally added to troubleshoot and resolve specific integration test failures where TMS configurations weren't being correctly identified due to key formatting mismatches with the network orchestrator (NWO).

I have now reverted these changes to ensure this PR remains strictly focused on code coverage. 🙏


// Serialize serializes this configuration with the respect to the passed tms ID
func (m *Configuration) Serialize(tmsID token.TMSID) ([]byte, error) {
keyID := fmt.Sprintf("%s%s%s", tmsID.Network, tmsID.Channel, tmsID.Namespace)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally changed this to match the NWO's dash-delimited TmsID format, but I've reverted it now to keep the PR focused strictly on coverage. 🙏

Comment thread token/services/config/export_test.go Outdated
SPDX-License-Identifier: Apache-2.0
*/

package config
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found confusing to have these functions here. Better to move them where they belong (service.go and configuration.go). Even if they are just used by tests, they are exposed and part of the API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve deleted export_test.go and moved the test hooks directly into service.go and configuration.go as exported methods, as you suggested. Thanks 🙌

Comment thread token/services/config/service.go Outdated
"github.com/hyperledger-labs/fabric-token-sdk/token/services/logging"
)

var logger = logging.MustGetLogger("token.services.config")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to pass an argument here, the package path is inferred directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've simplified the logger initialization as requested since the package path is inferred automatically. ❤️

return errors.Wrapf(err, "failed validating configuration [%s]", config.ID())
}
}
// Updates to an existing TMS should be rejected.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really sorry for that, I originally removed this during troubleshooting to allow for dynamic parameter reloads, but I have now restored it to maintain the original behavior. Sorry 🙌

Copy link
Copy Markdown
Contributor

@adecaro adecaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Rozerxshashank , thanks for the effort.
I left comments to be addressed.

To be a PR whose scope is to increase the test coverage. It modifies a lot of existing behaviors. I would expect changes to simplify the testing strategy but no semantic changes.

Please, have a second look to the PR. Thanks 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1484-coverage-config-v2 branch 4 times, most recently from 997db28 to 99c664f Compare April 12, 2026 11:39
@adecaro adecaro self-assigned this Apr 12, 2026
}

cp.MergeConfigReturns(nil)
err = s.AddConfigurationInternal(vcp, raw)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to call AddConfigurationInternal. Just instantiate a new config service with the reprogrammed provider. This way we can remove the function AddConfigurationInternal and reduce the surface of the exposed functions

// Initial state has "exist"
err = s.ResetConfigurations()
require.NoError(t, err)
_, _ = s.ConfigurationsInternal()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for? Try to remove ConfigurationsInternal by using a different strategy.

@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 12, 2026

HI @Rozerxshashank , great effort. We are almost there. I left other two comments and then we should be good with this PR 😄

@adecaro adecaro added this to the Q2/26 milestone Apr 14, 2026
@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 14, 2026

Hi @Rozerxshashank , please, update the PR as soon as you have time 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1484-coverage-config-v2 branch 2 times, most recently from 0f8471f to 41a5025 Compare April 14, 2026 10:09
@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 15, 2026

Hi @Rozerxshashank , there are still comments that needs to be addressed. Please, have a look ASAP 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1484-coverage-config-v2 branch 3 times, most recently from 5f30877 to a2e667a Compare April 15, 2026 19:29
@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

Hey @adecaro, I've addressed all the comments. 🙌 But I've noticed the TestVaultKVS and TestIdentityDBWithHashicorpVault tests are failing in CI with CAP_SETFCAP capability errors. This seems to be an environment issue in GitHub Actions and it's blocking the PR. Since my changes are focused on the Configuration Service, could you please look into these flakes or let me know how you'd like to proceed?

@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 16, 2026

Hi @Rozerxshashank , yes, I'll submit a PR to fix this ASAP. Thanks 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1484-coverage-config-v2 branch from a2e667a to 40da1d8 Compare April 23, 2026 05:00
@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1484-coverage-config-v2 branch 2 times, most recently from 13afa9b to 030e86e Compare April 23, 2026 06:46
@adecaro adecaro force-pushed the fix/issue-1484-coverage-config-v2 branch from 030e86e to b254a09 Compare April 29, 2026 11:50
@adecaro adecaro self-requested a review April 29, 2026 11:50
Copy link
Copy Markdown
Contributor

@adecaro adecaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@adecaro adecaro self-requested a review April 29, 2026 12:24
@adecaro adecaro merged commit e1ebca5 into hyperledger-labs:main Apr 29, 2026
95 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants